]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
28b29d1f9ee2563c779148d8f6fa779cf032915d
[quix0rs-gnu-social.git] / plugins / DomainStatusNetwork / DomainStatusNetworkPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * One status_network per email domain
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  DomainStatusNetwork
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 $_dir = dirname(__FILE__);
38
39 require_once $_dir . '/extlib/effectiveTLDs.inc.php';
40 require_once $_dir . '/extlib/regDomain.inc.php';
41
42 /**
43  * Tools to map one status_network to one email domain in a multi-site
44  * installation.
45  *
46  * @category  DomainStatusNetwork
47  * @package   StatusNet
48  * @author    Evan Prodromou <evan@status.net>
49  * @copyright 2011 StatusNet, Inc.
50  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
51  * @link      http://status.net/
52  */
53 class DomainStatusNetworkPlugin extends Plugin
54 {
55     const PLUGIN_VERSION = '2.0.0';
56
57     static $_thetree = null;
58
59     function initialize()
60     {
61         // For various reasons this gets squished
62
63         global $tldTree;
64
65         if (empty($tldTree)) {
66             if (!empty(self::$_thetree)) {
67                 $tldTree = self::$_thetree;
68             }
69         }
70
71         $nickname = GNUsocial::currentSite();
72
73         if (empty($nickname)) {
74             $this->log(LOG_WARNING, "No current site");
75             return;
76         }
77
78         try {
79             $sn = Status_network::getKV('nickname', $nickname);
80         } catch (Exception $e) {
81             $this->log(LOG_ERR, $e->getMessage());
82             return;
83         }
84
85         $tags = $sn->getTags();
86
87         foreach ($tags as $tag) {
88             if (strncmp($tag, 'domain=', 7) == 0) {
89                 $domain = substr($tag, 7);
90                 $this->log(LOG_INFO, "Setting email domain to {$domain}");
91                 common_config_append('email', 'whitelist', $domain);
92             }
93         }
94     }
95
96     static function toDomain($raw)
97     {
98         $parts = explode('@', $raw);
99
100         if (count($parts) == 1) {
101             $domain = $parts[0];
102         } else {
103             $domain = $parts[1];
104         }
105
106         $domain = strtolower(trim($domain));
107
108         return $domain;
109     }
110
111     static function registeredDomain($domain)
112     {
113         return getRegisteredDomain($domain);
114     }
115
116     static function nicknameAvailable($nickname)
117     {
118         $sn = Status_network::getKV('nickname', $nickname);
119         if (!empty($sn)) {
120             return false;
121         }
122         $usn = Unavailable_status_network::getKV('nickname', $nickname);
123         if (!empty($usn)) {
124             return false;
125         }
126         return true;
127     }
128
129     function onRouterInitialized($m)
130     {
131         if (common_config('globalapi', 'enabled')) {
132             foreach (array('register', 'login', 'recover') as $method) {
133                 $m->connect('api/statusnet/global/'.$method,
134                             array('action' => 'global'.$method));
135             }
136         }
137         return true;
138     }
139
140     function onLoginAction($action, &$login) {
141         $this->debug($action);
142         if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) {
143             $login = true;
144             return false;
145         }
146         return true;
147     }
148
149     static function nicknameForDomain($domain)
150     {
151         $registered = self::registeredDomain($domain);
152
153         $parts = explode('.', $registered);
154
155         $base = $parts[0];
156
157         if (self::nicknameAvailable($base)) {
158             return $base;
159         }
160
161         $domainish = str_replace('.', '-', $registered);
162
163         if (self::nicknameAvailable($domainish)) {
164             return $domainish;
165         }
166
167         $i = 1;
168
169         // We don't need to keep doing this forever
170
171         while ($i < 1024) {
172             $candidate = $domainish.'-'.$i;
173             if (self::nicknameAvailable($candidate)) {
174                 return $candidate;
175             }
176             $i++;
177         }
178
179         return null;
180     }
181
182     static function siteForDomain($domain)
183     {
184         $snt = Status_network_tag::withTag('domain='.$domain);
185
186         while ($snt->fetch()) {
187             $sn = Status_network::getKV('site_id', $snt->site_id);
188             if (!empty($sn)) {
189                 return $sn;
190             }
191         }
192         return null;
193     }
194
195     function onPluginVersion(array &$versions)
196     {
197         $versions[] = array('name' => 'DomainStatusNetwork',
198                             'version' => self::PLUGIN_VERSION,
199                             'author' => 'Evan Prodromou',
200                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DomainStatusNetwork',
201                             'rawdescription' =>
202                             // TRANS: Plugin description.
203                             _m('A plugin that maps a single status_network to an email domain.'));
204         return true;
205     }
206
207     static function userExists($email)
208     {
209         $domain = self::toDomain($email);
210
211         $sn = self::siteForDomain($domain);
212
213         if (empty($sn)) {
214             return false;
215         }
216
217         GNUsocial::switchSite($sn->nickname);
218
219         $user = User::getKV('email', $email);
220
221         return !empty($user);
222     }
223
224     static function registerEmail($email)
225     {
226         $domain = self::toDomain($email);
227
228         if (FreeEmail::isFree($domain)) {
229             throw new ClientException(_("Use your work email."));
230         }
231
232         $sn = self::siteForDomain($domain);
233
234         if (empty($sn)) {
235             $installer = new DomainStatusNetworkInstaller($domain);
236
237             // Do the thing
238             $installer->main();
239
240             $sn = $installer->getStatusNetwork();
241
242             $config = $installer->getConfig();
243
244             Status_network::$wildcard = $config['WILDCARD'];
245         }
246
247         GNUsocial::switchSite($sn->nickname);
248
249         $confirm = EmailRegistrationPlugin::registerEmail($email);
250
251         return $confirm;
252     }
253
254     static function login($email, $password)
255     {
256         $domain = self::toDomain($email);
257
258         $sn = self::siteForDomain($domain);
259
260         if (empty($sn)) {
261             throw new ClientException(_("No such site."));
262         }
263
264         GNUsocial::switchSite($sn->nickname);
265
266         $user = common_check_user($email, $password);
267
268         if (empty($user)) {
269             // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
270             throw new ClientException(_('Incorrect username or password.'));
271         }
272
273         $loginToken = Login_token::makeNew($user);
274
275         if (empty($loginToken)) {
276             throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
277         }
278
279         $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
280                                              'token' => $loginToken->token));
281
282         if (empty($url)) {
283             throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
284         }
285
286         return $url;
287     }
288
289     static function recoverPassword($email)
290     {
291         $domain = self::toDomain($email);
292
293         $sn = self::siteForDomain($domain);
294
295         if (empty($sn)) {
296             throw new NoSuchUserException(array('email' => $email));
297         }
298
299         GNUsocial::switchSite($sn->nickname);
300
301         $user = User::getKV('email', $email);
302         
303         if (empty($user)) {
304             throw new ClientException(_('No such user.'));
305         }
306     }
307 }
308
309 // The way addPlugin() works, this global variable gets disappeared.
310 // So, we re-appear it.
311
312 DomainStatusNetworkPlugin::$_thetree = $tldTree;