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