3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * One status_network per email domain
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.
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.
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/>.
23 * @category DomainStatusNetwork
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
37 $_dir = dirname(__FILE__);
39 require_once $_dir . '/extlib/effectiveTLDs.inc.php';
40 require_once $_dir . '/extlib/regDomain.inc.php';
43 * Tools to map one status_network to one email domain in a multi-site
46 * @category DomainStatusNetwork
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/
53 class DomainStatusNetworkPlugin extends Plugin
55 const PLUGIN_VERSION = '2.0.0';
57 static $_thetree = null;
61 // For various reasons this gets squished
65 if (empty($tldTree)) {
66 if (!empty(self::$_thetree)) {
67 $tldTree = self::$_thetree;
71 $nickname = GNUsocial::currentSite();
73 if (empty($nickname)) {
74 $this->log(LOG_WARNING, "No current site");
79 $sn = Status_network::getKV('nickname', $nickname);
80 } catch (Exception $e) {
81 $this->log(LOG_ERR, $e->getMessage());
85 $tags = $sn->getTags();
87 foreach ($tags as $tag) {
88 if (strncmp($tag, 'domain=', 7) == 0) {
89 $domain = substr($tag, 7);
90 $this->log(LOG_INFO, "Setting email domain to {$domain}");
91 common_config_append('email', 'whitelist', $domain);
96 static function toDomain($raw)
98 $parts = explode('@', $raw);
100 if (count($parts) == 1) {
106 $domain = strtolower(trim($domain));
111 static function registeredDomain($domain)
113 return getRegisteredDomain($domain);
116 static function nicknameAvailable($nickname)
118 $sn = Status_network::getKV('nickname', $nickname);
122 $usn = Unavailable_status_network::getKV('nickname', $nickname);
129 function onRouterInitialized($m)
131 if (common_config('globalapi', 'enabled')) {
132 foreach (array('register', 'login', 'recover') as $method) {
133 $m->connect('api/statusnet/global/'.$method,
134 array('action' => 'global'.$method));
140 function onLoginAction($action, &$login) {
141 $this->debug($action);
142 if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) {
149 static function nicknameForDomain($domain)
151 $registered = self::registeredDomain($domain);
153 $parts = explode('.', $registered);
157 if (self::nicknameAvailable($base)) {
161 $domainish = str_replace('.', '-', $registered);
163 if (self::nicknameAvailable($domainish)) {
169 // We don't need to keep doing this forever
172 $candidate = $domainish.'-'.$i;
173 if (self::nicknameAvailable($candidate)) {
182 static function siteForDomain($domain)
184 $snt = Status_network_tag::withTag('domain='.$domain);
186 while ($snt->fetch()) {
187 $sn = Status_network::getKV('site_id', $snt->site_id);
195 function onPluginVersion(array &$versions)
197 $versions[] = array('name' => 'DomainStatusNetwork',
198 'version' => self::PLUGIN_VERSION,
199 'author' => 'Evan Prodromou',
200 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DomainStatusNetwork',
202 // TRANS: Plugin description.
203 _m('A plugin that maps a single status_network to an email domain.'));
207 static function userExists($email)
209 $domain = self::toDomain($email);
211 $sn = self::siteForDomain($domain);
217 GNUsocial::switchSite($sn->nickname);
219 $user = User::getKV('email', $email);
221 return !empty($user);
224 static function registerEmail($email)
226 $domain = self::toDomain($email);
228 if (FreeEmail::isFree($domain)) {
229 throw new ClientException(_("Use your work email."));
232 $sn = self::siteForDomain($domain);
235 $installer = new DomainStatusNetworkInstaller($domain);
240 $sn = $installer->getStatusNetwork();
242 $config = $installer->getConfig();
244 Status_network::$wildcard = $config['WILDCARD'];
247 GNUsocial::switchSite($sn->nickname);
249 $confirm = EmailRegistrationPlugin::registerEmail($email);
254 static function login($email, $password)
256 $domain = self::toDomain($email);
258 $sn = self::siteForDomain($domain);
261 throw new ClientException(_("No such site."));
264 GNUsocial::switchSite($sn->nickname);
266 $user = common_check_user($email, $password);
269 // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
270 throw new ClientException(_('Incorrect username or password.'));
273 $loginToken = Login_token::makeNew($user);
275 if (empty($loginToken)) {
276 throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
279 $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
280 'token' => $loginToken->token));
283 throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
289 static function recoverPassword($email)
291 $domain = self::toDomain($email);
293 $sn = self::siteForDomain($domain);
296 throw new NoSuchUserException(array('email' => $email));
299 GNUsocial::switchSite($sn->nickname);
301 $user = User::getKV('email', $email);
304 throw new ClientException(_('No such user.'));
309 // The way addPlugin() works, this global variable gets disappeared.
310 // So, we re-appear it.
312 DomainStatusNetworkPlugin::$_thetree = $tldTree;