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