]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
Merge branch '1.0.x' into testing
[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     static $_thetree = null;
56
57     function initialize()
58     {
59         // For various reasons this gets squished
60
61         global $tldTree;
62
63         if (empty($tldTree)) {
64             if (!empty(self::$_thetree)) {
65                 $tldTree = self::$_thetree;
66             }
67         }
68
69         $nickname = StatusNet::currentSite();
70
71         if (empty($nickname)) {
72             $this->log(LOG_WARNING, "No current site");
73             return;
74         }
75
76         try {
77             $sn = Status_network::staticGet('nickname', $nickname);
78         } catch (Exception $e) {
79             $this->log(LOG_ERR, $e->getMessage());
80             return;
81         }
82
83         $tags = $sn->getTags();
84
85         foreach ($tags as $tag) {
86             if (strncmp($tag, 'domain=', 7) == 0) {
87                 $domain = substr($tag, 7);
88                 $this->log(LOG_INFO, "Setting email domain to {$domain}");
89                 common_config_append('email', 'whitelist', $domain);
90             }
91         }
92     }
93
94     function onAutoload($cls)
95     {
96         $dir = dirname(__FILE__);
97
98         switch ($cls)
99         {
100         case 'GlobalregisterAction':
101         case 'GloballoginAction':
102         case 'GlobalrecoverAction':
103             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
104             return false;
105         case 'DomainStatusNetworkInstaller':
106             include_once $dir . '/lib/' . strtolower($cls) . '.php';
107             return false;
108         case 'GlobalApiAction':
109             include_once $dir . '/lib/' . strtolower($cls) . '.php';
110             return false;
111         default:
112             return true;
113         }
114     }
115
116     static function toDomain($raw)
117     {
118         $parts = explode('@', $raw);
119
120         if (count($parts) == 1) {
121             $domain = $parts[0];
122         } else {
123             $domain = $parts[1];
124         }
125
126         $domain = strtolower(trim($domain));
127
128         return $domain;
129     }
130
131     static function registeredDomain($domain)
132     {
133         return getRegisteredDomain($domain);
134     }
135
136     static function nicknameAvailable($nickname)
137     {
138         $sn = Status_network::staticGet('nickname', $nickname);
139         if (!empty($sn)) {
140             return false;
141         }
142         $usn = Unavailable_status_network::staticGet('nickname', $nickname);
143         if (!empty($usn)) {
144             return false;
145         }
146         return true;
147     }
148
149     function onRouterInitialized($m)
150     {
151         if (common_config('globalapi', 'enabled')) {
152             foreach (array('register', 'login', 'recover') as $method) {
153                 $m->connect('api/statusnet/global/'.$method,
154                             array('action' => 'global'.$method));
155             }
156         }
157         return true;
158     }
159
160     function onLoginAction($action, &$login) {
161         $this->debug($action);
162         if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) {
163             $login = true;
164             return false;
165         }
166         return true;
167     }
168
169     static function nicknameForDomain($domain)
170     {
171         $registered = self::registeredDomain($domain);
172
173         $parts = explode('.', $registered);
174
175         $base = $parts[0];
176
177         if (self::nicknameAvailable($base)) {
178             return $base;
179         }
180
181         $domainish = str_replace('.', '-', $registered);
182
183         if (self::nicknameAvailable($domainish)) {
184             return $domainish;
185         }
186
187         $i = 1;
188
189         // We don't need to keep doing this forever
190
191         while ($i < 1024) {
192             $candidate = $domainish.'-'.$i;
193             if (self::nicknameAvailable($candidate)) {
194                 return $candidate;
195             }
196             $i++;
197         }
198
199         return null;
200     }
201
202     static function siteForDomain($domain)
203     {
204         $snt = Status_network_tag::withTag('domain='.$domain);
205
206         while ($snt->fetch()) {
207             $sn = Status_network::staticGet('site_id', $snt->site_id);
208             if (!empty($sn)) {
209                 return $sn;
210             }
211         }
212         return null;
213     }
214
215     function onPluginVersion(&$versions)
216     {
217         $versions[] = array('name' => 'DomainStatusNetwork',
218                             'version' => STATUSNET_VERSION,
219                             'author' => 'Evan Prodromou',
220                             'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
221                             'rawdescription' =>
222                             // TRANS: Plugin description.
223                             _m('A plugin that maps a single status_network to an email domain.'));
224         return true;
225     }
226
227     static function userExists($email)
228     {
229         $domain = self::toDomain($email);
230
231         $sn = self::siteForDomain($domain);
232
233         if (empty($sn)) {
234             return false;
235         }
236
237         StatusNet::switchSite($sn->nickname);
238
239         $user = User::staticGet('email', $email);
240
241         return !empty($user);
242     }
243
244     static function registerEmail($email, $sendWelcome, $template)
245     {
246         $domain = self::toDomain($email);
247
248         $sn = self::siteForDomain($domain);
249
250         if (empty($sn)) {
251             $installer = new DomainStatusNetworkInstaller($domain);
252
253             // Do the thing
254             $installer->main();
255
256             $sn = $installer->getStatusNetwork();
257
258             $config = $installer->getConfig();
259
260             Status_network::$wildcard = $config['WILDCARD'];
261         }
262
263         StatusNet::switchSite($sn->nickname);
264
265         $confirm = EmailRegistrationPlugin::registerEmail($email);
266
267         return $confirm;
268     }
269
270     static function login($email, $password)
271     {
272         $domain = self::toDomain($email);
273
274         $sn = self::siteForDomain($domain);
275
276         if (empty($sn)) {
277             throw new ClientException(_("No such site."));
278         }
279
280         StatusNet::switchSite($sn->nickname);
281
282         $user = common_check_user($email, $password);
283
284         if (empty($user)) {
285             // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
286             throw new ClientException(_('Incorrect username or password.'));
287         }
288
289         $loginToken = Login_token::makeNew($user);
290
291         if (empty($loginToken)) {
292             throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
293         }
294
295         $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
296                                              'token' => $loginToken->token));
297
298         if (empty($url)) {
299             throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
300         }
301
302         return $url;
303     }
304
305     static function recoverPassword($email)
306     {
307         $domain = self::toDomain($email);
308
309         $sn = self::siteForDomain($domain);
310
311         if (empty($sn)) {
312             throw new NoSuchUserException(array('email' => $email));
313         }
314
315         StatusNet::switchSite($sn->nickname);
316
317         $user = User::staticGet('email', $email);
318         
319         if (empty($user)) {
320             throw new ClientException(_('No such user.'));
321         }
322     }
323 }
324
325 // The way addPlugin() works, this global variable gets disappeared.
326 // So, we re-appear it.
327
328 DomainStatusNetworkPlugin::$_thetree = $tldTree;