]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/DomainStatusNetworkPlugin.php
Move common domain-to-network mapping to the plugin module
[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         $sn = Status_network::staticGet('nickname', $nickname);
78
79         if (empty($sn)) {
80             $this->log(LOG_ERR, "No site for nickname $nickname");
81             return;
82         }
83
84         $tags = $sn->getTags();
85
86         foreach ($tags as $tag) {
87             if (strncmp($tag, 'domain=', 7) == 0) {
88                 common_config_append('email', 'whitelist', substr($tag, 7));
89             }
90         }
91     }
92
93     static function toDomain($raw)
94     {
95         $parts = explode('@', $raw);
96
97         if (count($parts) == 1) {
98             $domain = $parts[0];
99         } else {
100             $domain = $parts[1];
101         }
102
103         $domain = strtolower(trim($domain));
104
105         return $domain;
106     }
107
108     static function registeredDomain($domain)
109     {
110         return getRegisteredDomain($domain);
111     }
112
113     static function nicknameAvailable($nickname)
114     {
115         $sn = Status_network::staticGet('nickname', $nickname);
116         return empty($sn);
117     }
118
119     static function nicknameForDomain($domain)
120     {
121         $registered = self::registeredDomain($domain);
122
123         $parts = explode('.', $registered);
124
125         $base = $parts[0];
126
127         if (self::nicknameAvailable($base)) {
128             return $base;
129         }
130
131         $domainish = str_replace('.', '-', $registered);
132
133         if (self::nicknameAvailable($domainish)) {
134             return $domainish;
135         }
136
137         $i = 1;
138
139         // We don't need to keep doing this forever
140
141         while ($i < 1024) {
142             $candidate = $domainish.'-'.$i;
143             if (self::nicknameAvailable($candidate)) {
144                 return $candidate;
145             }
146             $i++;
147         }
148
149         return null;
150     }
151
152     static function siteForDomain($domain)
153     {
154         $snt = Status_network_tag::withTag('domain='.$domain);
155
156         while ($snt->fetch()) {
157             $sn = Status_network::staticGet('site_id', $snt->site_id);
158             if (!empty($sn)) {
159                 return $sn;
160             }
161         }
162         return null;
163     }
164
165     function onPluginVersion(&$versions)
166     {
167         $versions[] = array('name' => 'DomainStatusNetwork',
168                             'version' => STATUSNET_VERSION,
169                             'author' => 'Evan Prodromou',
170                             'homepage' => 'http://status.net/wiki/Plugin:DomainStatusNetwork',
171                             'rawdescription' =>
172                             _m('A plugin that maps a single status_network to an email domain.'));
173         return true;
174     }
175 }
176
177 // The way addPlugin() works, this global variable gets disappeared.
178 // So, we re-appear it.
179
180 DomainStatusNetworkPlugin::$_thetree = $tldTree;