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