]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
First version of installer for domain-based status networks
[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
54 class DomainStatusNetworkPlugin extends Plugin
55 {
56     static $_thetree = null;
57
58     function initialize()
59     {
60         // For various reasons this gets squished
61
62         global $tldTree;
63         
64         if (empty($tldTree)) {
65             if (!empty(self::$_thetree)) {
66                 $tldTree = self::$_thetree;
67             }
68         }
69
70         $nickname = StatusNet::currentSite();
71
72         if (empty($nickname)) {
73             $this->log(LOG_WARNING, "No current site");
74             return;
75         }
76
77         try {
78             $sn = Status_network::staticGet('nickname', $nickname);
79         } catch (Exception $e) {
80             $this->log(LOG_ERR, $e->getMessage());
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("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         return empty($sn);
132     }
133
134     static function nicknameForDomain($domain)
135     {
136         $registered = self::registeredDomain($domain);
137
138         $parts = explode('.', $registered);
139
140         $base = $parts[0];
141
142         if (self::nicknameAvailable($base)) {
143             return $base;
144         }
145
146         $domainish = str_replace('.', '-', $registered);
147
148         if (self::nicknameAvailable($domainish)) {
149             return $domainish;
150         }
151
152         $i = 1;
153
154         // We don't need to keep doing this forever
155
156         while ($i < 1024) {
157             $candidate = $domainish.'-'.$i;
158             if (self::nicknameAvailable($candidate)) {
159                 return $candidate;
160             }
161             $i++;
162         }
163
164         return null;
165     }
166
167     static function siteForDomain($domain)
168     {
169         $snt = Status_network_tag::withTag('domain='.$domain);
170
171         while ($snt->fetch()) {
172             $sn = Status_network::staticGet('site_id', $snt->site_id);
173             if (!empty($sn)) {
174                 return $sn;
175             }
176         }
177         return null;
178     }
179
180     function onPluginVersion(&$versions)
181     {
182         $versions[] = array('name' => 'DomainStatusNetwork',
183                             'version' => STATUSNET_VERSION,
184                             'author' => 'Evan Prodromou',
185                             'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
186                             'rawdescription' =>
187                             _m('A plugin that maps a single status_network to an email domain.'));
188         return true;
189     }
190 }
191
192 // The way addPlugin() works, this global variable gets disappeared.
193 // So, we re-appear it.
194
195 DomainStatusNetworkPlugin::$_thetree = $tldTree;