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