]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainWhitelist/DomainWhitelistPlugin.php
4bd9d599834c7c7ce90ceee5d577bc1a39ee16d0
[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(_('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(_('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         $parts = explode('@', $email);
159
160         $userDomain = strtolower(trim($parts[1]));
161
162         return in_array($userDomain, $whitelist);
163     }
164
165     function getWhitelist()
166     {
167         $whitelist = common_config('email', 'whitelist');
168
169         if (is_array($whitelist)) {
170             return $whitelist;
171         } else {
172             return explode('|', $whitelist);
173         }
174     }
175
176     /**
177      * Show a fancier invite form when domains are restricted to the
178      * whitelist.
179      *
180      * @param action $action the invite action
181      * @return boolean hook value
182      */
183     function onStartShowInviteForm($action)
184     {
185         $form = new WhitelistInviteForm($action, $this->getWhitelist());
186         $form->show();
187         return false;
188     }
189
190     /**
191      * This is a bit of a hack. We take the values from the custom
192      * whitelist invite form and reformat them so they look like
193      * their coming from the the normal invite form.
194      *
195      * @param action &$action the invite action
196      * @return boolean hook value
197      */
198     function onStartSendInvitations(&$action)
199     {
200        $emails    = array();
201        $usernames = $action->arg('username');
202        $domains   = $action->arg('domain');
203
204        for($i = 0; $i < count($usernames); $i++) {
205            if (!empty($usernames[$i])) {
206                $emails[] = $usernames[$i] . '@' . $domains[$i] . "\n";
207            }
208        }
209
210        $action->args['addresses'] = implode($emails);
211
212        return true;
213     }
214
215     function onPluginVersion(&$versions)
216     {
217         $versions[] = array('name' => 'DomainWhitelist',
218                             'version' => STATUSNET_VERSION,
219                             'author' => 'Evan Prodromou, Zach Copley',
220                             'homepage' => 'http://status.net/wiki/Plugin:DomainWhitelist',
221                             'rawdescription' =>
222                             // TRANS: Plugin description.
223                             _m('Restrict domains for email users.'));
224         return true;
225     }
226 }