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/
54 class DomainStatusNetworkPlugin extends Plugin
56 static $_thetree = null;
60 // For various reasons this gets squished
64 if (empty($tldTree)) {
65 if (!empty(self::$_thetree)) {
66 $tldTree = self::$_thetree;
70 $nickname = StatusNet::currentSite();
72 if (empty($nickname)) {
73 $this->log(LOG_WARNING, "No current site");
77 $sn = Status_network::staticGet('nickname', $nickname);
80 $this->log(LOG_ERR, "No site for nickname $nickname");
84 $tags = $sn->getTags();
86 foreach ($tags as $tag) {
87 if (strncmp($tag, 'domain=', 7) == 0) {
88 common_config_append('email', 'whitelist', substr($tag, 7));
93 static function toDomain($raw)
95 $parts = explode('@', $raw);
97 if (count($parts) == 1) {
103 $domain = strtolower(trim($domain));
108 static function registeredDomain($domain)
110 return getRegisteredDomain($domain);
113 static function nicknameAvailable($nickname)
115 $sn = Status_network::staticGet('nickname', $nickname);
119 static function nicknameForDomain($domain)
121 $registered = self::registeredDomain($domain);
123 $parts = explode('.', $registered);
127 if (self::nicknameAvailable($base)) {
131 $domainish = str_replace('.', '-', $registered);
133 if (self::nicknameAvailable($domainish)) {
139 // We don't need to keep doing this forever
142 $candidate = $domainish.'-'.$i;
143 if (self::nicknameAvailable($candidate)) {
152 static function siteForDomain($domain)
154 $snt = Status_network_tag::withTag('domain='.$domain);
156 while ($snt->fetch()) {
157 $sn = Status_network::staticGet('site_id', $snt->site_id);
165 function onPluginVersion(&$versions)
167 $versions[] = array('name' => 'DomainStatusNetwork',
168 'version' => STATUSNET_VERSION,
169 'author' => 'Evan Prodromou',
170 'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
172 _m('A plugin that maps a single status_network to an email domain.'));
177 // The way addPlugin() works, this global variable gets disappeared.
178 // So, we re-appear it.
180 DomainStatusNetworkPlugin::$_thetree = $tldTree;