]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
first pass at actions for initializing a network
[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 'DomainStatusNetworkInstaller':
101             include_once $dir . '/' . strtolower($cls) . '.php';
102             return false;
103         default:
104             return true;
105         }
106     }
107
108     static function toDomain($raw)
109     {
110         $parts = explode('@', $raw);
111
112         if (count($parts) == 1) {
113             $domain = $parts[0];
114         } else {
115             $domain = $parts[1];
116         }
117
118         $domain = strtolower(trim($domain));
119
120         return $domain;
121     }
122
123     static function registeredDomain($domain)
124     {
125         return getRegisteredDomain($domain);
126     }
127
128     static function nicknameAvailable($nickname)
129     {
130         $sn = Status_network::staticGet('nickname', $nickname);
131         if (!empty($sn)) {
132             return false;
133         }
134         $usn = Unavailable_status_network::staticGet('nickname', $nickname);
135         if (!empty($usn)) {
136             return false;
137         }
138         return true;
139     }
140
141     static function nicknameForDomain($domain)
142     {
143         $registered = self::registeredDomain($domain);
144
145         $parts = explode('.', $registered);
146
147         $base = $parts[0];
148
149         if (self::nicknameAvailable($base)) {
150             return $base;
151         }
152
153         $domainish = str_replace('.', '-', $registered);
154
155         if (self::nicknameAvailable($domainish)) {
156             return $domainish;
157         }
158
159         $i = 1;
160
161         // We don't need to keep doing this forever
162
163         while ($i < 1024) {
164             $candidate = $domainish.'-'.$i;
165             if (self::nicknameAvailable($candidate)) {
166                 return $candidate;
167             }
168             $i++;
169         }
170
171         return null;
172     }
173
174     static function siteForDomain($domain)
175     {
176         $snt = Status_network_tag::withTag('domain='.$domain);
177
178         while ($snt->fetch()) {
179             $sn = Status_network::staticGet('site_id', $snt->site_id);
180             if (!empty($sn)) {
181                 return $sn;
182             }
183         }
184         return null;
185     }
186
187     function onPluginVersion(&$versions)
188     {
189         $versions[] = array('name' => 'DomainStatusNetwork',
190                             'version' => STATUSNET_VERSION,
191                             'author' => 'Evan Prodromou',
192                             'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
193                             'rawdescription' =>
194                             // TRANS: Plugin description.
195                             _m('A plugin that maps a single status_network to an email domain.'));
196         return true;
197     }
198
199     static function userExists($email)
200     {
201         $domain = self::toDomain($email);
202
203         $sn = self::siteForDomain($domain);
204
205         if (empty($sn)) {
206             return false;
207         }
208
209         StatusNet::switchSite($sn->nickname);
210
211         $user = User::staticGet('email', $email);
212
213         return !empty($user);
214     }
215
216     static function registerEmail($email, $sendWelcome, $template)
217     {
218         $domain = self::toDomain($email);
219
220         $sn = self::siteForDomain($domain);
221
222         if (empty($sn)) {
223             $installer = new DomainStatusNetworkInstaller($domain);
224
225             // Do the thing
226             $installer->main();
227
228             $sn = $installer->getStatusNetwork();
229
230             $config = $installer->getConfig();
231
232             Status_network::$wildcard = $config['WILDCARD'];
233         }
234
235         StatusNet::switchSite($sn->nickname);
236
237         $confirm = EmailRegistrationPlugin::registerEmail($email);
238
239         return $confirm;
240     }
241
242     static function login($email, $password)
243     {
244         $domain = self::toDomain($email);
245
246         $sn = self::siteForDomain($domain);
247
248         if (empty($sn)) {
249             throw new ClientException(_("No such site."));
250         }
251
252         StatusNet::switchSite($sn->nickname);
253
254         $user = common_check_user($email, $password);
255
256         if (empty($user)) {
257             // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
258             throw new ClientException(_('Incorrect username or password.'));
259         }
260
261         $loginToken = Login_token::makeNew($user);
262
263         if (empty($loginToken)) {
264             throw new ServerException(_('Cannot log in.'));
265         }
266
267         $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
268                                              'token' => $loginToken->token));
269
270         return $url;
271     }
272
273     static function recoverPassword($email)
274     {
275         $domain = self::toDomain($email);
276
277         $sn = self::siteForDomain($domain);
278
279         if (empty($sn)) {
280             throw new NoSuchUserException(array('email' => $email));
281         }
282
283         StatusNet::switchSite($sn->nickname);
284
285 }
286
287 // The way addPlugin() works, this global variable gets disappeared.
288 // So, we re-appear it.
289
290 DomainStatusNetworkPlugin::$_thetree = $tldTree;