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 static $_thetree = null;
59 // For various reasons this gets squished
63 if (empty($tldTree)) {
64 if (!empty(self::$_thetree)) {
65 $tldTree = self::$_thetree;
69 $nickname = StatusNet::currentSite();
71 if (empty($nickname)) {
72 $this->log(LOG_WARNING, "No current site");
77 $sn = Status_network::getKV('nickname', $nickname);
78 } catch (Exception $e) {
79 $this->log(LOG_ERR, $e->getMessage());
83 $tags = $sn->getTags();
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);
94 static function toDomain($raw)
96 $parts = explode('@', $raw);
98 if (count($parts) == 1) {
104 $domain = strtolower(trim($domain));
109 static function registeredDomain($domain)
111 return getRegisteredDomain($domain);
114 static function nicknameAvailable($nickname)
116 $sn = Status_network::getKV('nickname', $nickname);
120 $usn = Unavailable_status_network::getKV('nickname', $nickname);
127 function onRouterInitialized($m)
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));
138 function onLoginAction($action, &$login) {
139 $this->debug($action);
140 if (in_array($action, array('globalregister', 'globallogin', 'globalrecover'))) {
147 static function nicknameForDomain($domain)
149 $registered = self::registeredDomain($domain);
151 $parts = explode('.', $registered);
155 if (self::nicknameAvailable($base)) {
159 $domainish = str_replace('.', '-', $registered);
161 if (self::nicknameAvailable($domainish)) {
167 // We don't need to keep doing this forever
170 $candidate = $domainish.'-'.$i;
171 if (self::nicknameAvailable($candidate)) {
180 static function siteForDomain($domain)
182 $snt = Status_network_tag::withTag('domain='.$domain);
184 while ($snt->fetch()) {
185 $sn = Status_network::getKV('site_id', $snt->site_id);
193 function onPluginVersion(&$versions)
195 $versions[] = array('name' => 'DomainStatusNetwork',
196 'version' => GNUSOCIAL_VERSION,
197 'author' => 'Evan Prodromou',
198 'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
200 // TRANS: Plugin description.
201 _m('A plugin that maps a single status_network to an email domain.'));
205 static function userExists($email)
207 $domain = self::toDomain($email);
209 $sn = self::siteForDomain($domain);
215 StatusNet::switchSite($sn->nickname);
217 $user = User::getKV('email', $email);
219 return !empty($user);
222 static function registerEmail($email)
224 $domain = self::toDomain($email);
226 if (FreeEmail::isFree($domain)) {
227 throw new ClientException(_("Use your work email."));
230 $sn = self::siteForDomain($domain);
233 $installer = new DomainStatusNetworkInstaller($domain);
238 $sn = $installer->getStatusNetwork();
240 $config = $installer->getConfig();
242 Status_network::$wildcard = $config['WILDCARD'];
245 StatusNet::switchSite($sn->nickname);
247 $confirm = EmailRegistrationPlugin::registerEmail($email);
252 static function login($email, $password)
254 $domain = self::toDomain($email);
256 $sn = self::siteForDomain($domain);
259 throw new ClientException(_("No such site."));
262 StatusNet::switchSite($sn->nickname);
264 $user = common_check_user($email, $password);
267 // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
268 throw new ClientException(_('Incorrect username or password.'));
271 $loginToken = Login_token::makeNew($user);
273 if (empty($loginToken)) {
274 throw new ServerException(sprintf(_('Could not create new login token for user %s'), $user->nickname));
277 $url = common_local_url('otp', array('user_id' => $loginToken->user_id,
278 'token' => $loginToken->token));
281 throw new ServerException(sprintf(_('Could not create new OTP URL for user %s'), $user->nickname));
287 static function recoverPassword($email)
289 $domain = self::toDomain($email);
291 $sn = self::siteForDomain($domain);
294 throw new NoSuchUserException(array('email' => $email));
297 StatusNet::switchSite($sn->nickname);
299 $user = User::getKV('email', $email);
302 throw new ClientException(_('No such user.'));
307 // The way addPlugin() works, this global variable gets disappeared.
308 // So, we re-appear it.
310 DomainStatusNetworkPlugin::$_thetree = $tldTree;