]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
First pass at complete global API
[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     static function nicknameForDomain($domain)
161     {
162         $registered = self::registeredDomain($domain);
163
164         $parts = explode('.', $registered);
165
166         $base = $parts[0];
167
168         if (self::nicknameAvailable($base)) {
169             return $base;
170         }
171
172         $domainish = str_replace('.', '-', $registered);
173
174         if (self::nicknameAvailable($domainish)) {
175             return $domainish;
176         }
177
178         $i = 1;
179
180         // We don't need to keep doing this forever
181
182         while ($i < 1024) {
183             $candidate = $domainish.'-'.$i;
184             if (self::nicknameAvailable($candidate)) {
185                 return $candidate;
186             }
187             $i++;
188         }
189
190         return null;
191     }
192
193     static function siteForDomain($domain)
194     {
195         $snt = Status_network_tag::withTag('domain='.$domain);
196
197         while ($snt->fetch()) {
198             $sn = Status_network::staticGet('site_id', $snt->site_id);
199             if (!empty($sn)) {
200                 return $sn;
201             }
202         }
203         return null;
204     }
205
206     function onPluginVersion(&$versions)
207     {
208         $versions[] = array('name' => 'DomainStatusNetwork',
209                             'version' => STATUSNET_VERSION,
210                             'author' => 'Evan Prodromou',
211                             'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
212                             'rawdescription' =>
213                             // TRANS: Plugin description.
214                             _m('A plugin that maps a single status_network to an email domain.'));
215         return true;
216     }
217
218     static function userExists($email)
219     {
220         $domain = self::toDomain($email);
221
222         $sn = self::siteForDomain($domain);
223
224         if (empty($sn)) {
225             return false;
226         }
227
228         StatusNet::switchSite($sn->nickname);
229
230         $user = User::staticGet('email', $email);
231
232         return !empty($user);
233     }
234
235     static function registerEmail($email, $sendWelcome, $template)
236     {
237         $domain = self::toDomain($email);
238
239         $sn = self::siteForDomain($domain);
240
241         if (empty($sn)) {
242             $installer = new DomainStatusNetworkInstaller($domain);
243
244             // Do the thing
245             $installer->main();
246
247             $sn = $installer->getStatusNetwork();
248
249             $config = $installer->getConfig();
250
251             Status_network::$wildcard = $config['WILDCARD'];
252         }
253
254         StatusNet::switchSite($sn->nickname);
255
256         $confirm = EmailRegistrationPlugin::registerEmail($email);
257
258         return $confirm;
259     }
260
261     static function login($email, $password)
262     {
263         $domain = self::toDomain($email);
264
265         $sn = self::siteForDomain($domain);
266
267         if (empty($sn)) {
268             throw new ClientException(_("No such site."));
269         }
270
271         StatusNet::switchSite($sn->nickname);
272
273         $user = common_check_user($email, $password);
274
275         if (empty($user)) {
276             // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
277             throw new ClientException(_('Incorrect username or password.'));
278         }
279
280         $loginToken = Login_token::makeNew($user);
281
282         if (empty($loginToken)) {
283             throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
284         }
285
286         $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
287                                              'token' => $loginToken->token));
288
289         if (empty($url)) {
290             throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
291         }
292
293         return $url;
294     }
295
296     static function recoverPassword($email)
297     {
298         $domain = self::toDomain($email);
299
300         $sn = self::siteForDomain($domain);
301
302         if (empty($sn)) {
303             throw new NoSuchUserException(array('email' => $email));
304         }
305
306         StatusNet::switchSite($sn->nickname);
307
308         $user = User::staticGet('email', $email);
309         
310         if (empty($user)) {
311             throw new ClientException(_('No such user.'));
312         }
313     }
314 }
315
316 // The way addPlugin() works, this global variable gets disappeared.
317 // So, we re-appear it.
318
319 DomainStatusNetworkPlugin::$_thetree = $tldTree;