]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainWhitelist/DomainWhitelistPlugin.php
Update translator documentation, i18n and L10n.
[quix0rs-gnu-social.git] / plugins / DomainWhitelist / DomainWhitelistPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Restrict the email addresses in a domain to a select whitelist
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  Cache
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Zach Copley <zach@status.net>
27  * @copyright 2011 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     // This check helps protect against security problems;
34     // your code file can't be executed directly from the web.
35     exit(1);
36 }
37
38 /**
39  * Restrict the email addresses to a domain whitelist
40  *
41  * @category  General
42  * @package   StatusNet
43  * @author    Evan Prodromou <evan@status.net>
44  * @author    Zach Copley <zach@status.net>
45  * @copyright 2011 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49 class DomainWhitelistPlugin extends Plugin
50 {
51     /**
52      * Load related modules when needed
53      *
54      * @param string $cls Name of the class to be loaded
55      *
56      * @return boolean hook value; true means continue processing, false
57      *         means stop.
58      */
59     function onAutoload($cls) {
60         $base = dirname(__FILE__);
61         $lower = strtolower($cls);
62
63         $files = array("$base/classes/$cls.php",
64             "$base/lib/$lower.php");
65         if (substr($lower, -6) == 'action') {
66             $files[] = "$base/actions/" . substr($lower, 0, -6) . ".php";
67         }
68         foreach ($files as $file) {
69             if (file_exists($file)) {
70                 include_once $file;
71                 return false;
72             }
73         }
74         return true;
75     }
76
77     /**
78      * Get the path to the plugin's installation directory. Used
79      * to link in js files and whatnot.
80      *
81      * @return String the absolute path
82      */
83     protected function getPath() {
84         return preg_replace('/^' . preg_quote(INSTALLDIR, '/') . '\//', '', dirname(__FILE__));
85     }
86
87     /**
88      * Link in a JavaScript script for the whitelist invite form
89      *
90      * @param Action $action Action being shown
91      *
92      * @return boolean hook flag
93      */
94     function onEndShowStatusNetScripts($action) {
95         $name = $action->arg('action');
96         if ($name == 'invite') {
97             $action->script($this->getPath() . '/js/whitelistinvite.js');
98         }
99         return true;
100     }
101
102     function onRequireValidatedEmailPlugin_Override($user, &$knownGood)
103     {
104         $knownGood = (!empty($user->email) && $this->matchesWhitelist($user->email));
105         return true;
106     }
107
108     function onEndValidateUserEmail($user, $email, &$valid)
109     {
110         if ($valid) { // it's otherwise valid
111             if (!$this->matchesWhitelist($email)) {
112                 $whitelist = $this->getWhitelist();
113                 if (count($whitelist) == 1) {
114                     // TRANS: Client exception thrown when a given e-mailaddress is not in the domain whitelist.
115                     // TRANS: %s is a whitelisted e-mail domain.
116                     $message = sprintf(_m('Email address must be in this domain: %s.'),
117                                        $whitelist[0]);
118                 } else {
119                     // TRANS: Client exception thrown when a given e-mailaddress is not in the domain whitelist.
120                     // TRANS: %s are whitelisted e-mail domains separated by comma's (localisable).
121                     $message = sprintf(_m('Email address must be in one of these domains: %s.'),
122                                        // TRANS: Separator for whitelisted domains.
123                                        implode(_m('SEPARATOR',', '), $whitelist));
124                 }
125                 throw new ClientException($message);
126             }
127         }
128         return true;
129     }
130
131     function onStartAddEmailAddress($user, $email)
132     {
133         if (!$this->matchesWhitelist($email)) {
134             // TRANS: Exception thrown when an e-mail address does not match the site's domain whitelist.
135             throw new Exception(_m('That email address is not allowed on this site.'));
136         }
137
138         return true;
139     }
140
141     function onEndValidateEmailInvite($user, $email, &$valid)
142     {
143         if ($valid) {
144             $valid = $this->matchesWhitelist($email);
145         }
146
147         return true;
148     }
149
150     function matchesWhitelist($email)
151     {
152         $whitelist = $this->getWhitelist();
153
154         if (empty($whitelist) || empty($whitelist[0])) {
155             return true;
156         }
157
158         $userDomain = $this->domainFromEmail($email);
159
160         return in_array($userDomain, $whitelist);
161     }
162
163     /**
164      * Helper function to pull out a domain from
165      * an email address
166      *
167      * @param string $email and email address
168      * @return string the domain
169      */
170     function domainFromEmail($email)
171     {
172         $parts = explode('@', $email);
173         return strtolower(trim($parts[1]));
174     }
175
176     function getWhitelist()
177     {
178         $whitelist = common_config('email', 'whitelist');
179
180         if (is_array($whitelist)) {
181             return $this->sortWhiteList($whitelist);
182         } else {
183             return explode('|', $whitelist);
184         }
185     }
186
187     /**
188      * This is a filter function passed in to array_filter()
189      * in order to strip out the user's domain, which will
190      * be re-inserted as the first element (see sortWhitelist()
191      * below).
192      *
193      * @param string $domain domain to check
194      * @return boolean whether to include the domain
195      */
196     function userDomainFilter($domain)
197     {
198         $user       = common_current_user();
199         $userDomain = $this->domainFromEmail($user->email);
200         if ($userDomain == $domain) {
201             return false;
202         }
203         return true;
204     }
205
206     /**
207      * This function sorts the whitelist alphabetically, and sets the
208      * current user's domain as the first element in the array of
209      * allowed domains. Mostly, this is for the JavaScript on the invite
210      * page--in the case of multiple allowed domains, it's nicer if the
211      * user's own domain is the first option, and this seemed like a good
212      * way to do it.
213      *
214      * @param array $whitelist whitelist of allowed email domains
215      * @return array an ordered or sorted version of the whitelist
216      */
217     function sortWhitelist($whitelist)
218     {
219         $whitelist = array_unique($whitelist);
220         natcasesort($whitelist);
221
222         $user = common_current_user();
223
224         if (!empty($user) && !empty($user->email)) {
225             $userDomain = $this->domainFromEmail($user->email);
226
227             $orderedWhitelist = array_values(
228                 array_filter(
229                     $whitelist,
230                     array($this, "userDomainFilter")
231                 )
232             );
233
234             if (in_array($userDomain, $whitelist)) {
235                 array_unshift($orderedWhitelist, $userDomain);
236             }
237             return $orderedWhitelist;
238         }
239
240         return $whitelist;
241     }
242
243     /**
244      * Show a fancier invite form when domains are restricted to the
245      * whitelist.
246      *
247      * @param action $action the invite action
248      * @return boolean hook value
249      */
250     function onStartShowInviteForm($action)
251     {
252         $this->showConfirmDialog($action);
253         $form = new WhitelistInviteForm($action, $this->getWhitelist());
254         $form->show();
255         return false;
256     }
257
258     function showConfirmDialog($action)
259     {
260         // For JQuery UI modal dialog
261         $action->elementStart(
262             'div',
263             // TRANS: Title for invitiation deletion dialog.
264             array('id' => 'confirm-dialog', 'title' => _m('Confirmation Required'))
265         );
266         // TRANS: Confirmation text for invitation deletion dialog.
267         $action->text(_m('Really delete this invitation?'));
268         $action->elementEnd('div');
269     }
270
271     /**
272      * This is a bit of a hack. We take the values from the custom
273      * whitelist invite form and reformat them so they look like
274      * their coming from the the normal invite form.
275      *
276      * @param action &$action the invite action
277      * @return boolean hook value
278      */
279     function onStartSendInvitations(&$action)
280     {
281        $emails    = array();
282        $usernames = $action->arg('username');
283        $domains   = $action->arg('domain');
284
285        for($i = 0; $i < count($usernames); $i++) {
286            if (!empty($usernames[$i])) {
287                $emails[] = $usernames[$i] . '@' . $domains[$i] . "\n";
288            }
289        }
290
291        $action->args['addresses'] = implode($emails);
292
293        return true;
294     }
295
296     function onPluginVersion(&$versions)
297     {
298         $versions[] = array('name' => 'DomainWhitelist',
299                             'version' => STATUSNET_VERSION,
300                             'author' => 'Evan Prodromou, Zach Copley',
301                             'homepage' => 'http://status.net/wiki/Plugin:DomainWhitelist',
302                             'rawdescription' =>
303                             // TRANS: Plugin description.
304                             _m('Restrict domains for email users.'));
305         return true;
306     }
307 }