]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RegisterThrottle/RegisterThrottlePlugin.php
plugins onAutoload now only overloads if necessary (extlibs etc.)
[quix0rs-gnu-social.git] / plugins / RegisterThrottle / RegisterThrottlePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Throttle registration by IP address
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  Spam
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 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     exit(1);
33 }
34
35 /**
36  * Throttle registration by IP address
37  *
38  * We a) record IP address of registrants and b) throttle registrations.
39  *
40  * @category  Spam
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class RegisterThrottlePlugin extends Plugin
49 {
50     /**
51      * Array of time spans in seconds to limits.
52      *
53      * Default is 3 registrations per hour, 5 per day, 10 per week.
54      */
55     public $regLimits = array(604800 => 10, // per week
56                               86400 => 5, // per day
57                               3600 => 3); // per hour
58
59     /**
60      * Disallow registration if a silenced user has registered from
61      * this IP address.
62      */
63     public $silenced = true;
64
65     /**
66      * Whether we're enabled; prevents recursion.
67      */
68     static private $enabled = true;
69
70     /**
71      * Database schema setup
72      *
73      * We store user registrations in a table registration_ip.
74      *
75      * @return boolean hook value; true means continue processing, false means stop.
76      */
77     function onCheckSchema()
78     {
79         $schema = Schema::get();
80
81         // For storing user-submitted flags on profiles
82         $schema->ensureTable('registration_ip', Registration_ip::schemaDef());
83         return true;
84     }
85
86     /**
87      * Called when someone tries to register.
88      *
89      * We check the IP here to determine if it goes over any of our
90      * configured limits.
91      *
92      * @param Action $action Action that is being executed
93      *
94      * @return boolean hook value
95      */
96     function onStartRegistrationTry($action)
97     {
98         $ipaddress = $this->_getIpAddress();
99
100         if (empty($ipaddress)) {
101             // TRANS: Server exception thrown when no IP address can be found for a registation attempt.
102             throw new ServerException(_m('Cannot find IP address.'));
103         }
104
105         foreach ($this->regLimits as $seconds => $limit) {
106
107             $this->debug("Checking $seconds ($limit)");
108
109             $reg = $this->_getNthReg($ipaddress, $limit);
110
111             if (!empty($reg)) {
112                 $this->debug("Got a {$limit}th registration.");
113                 $regtime = strtotime($reg->created);
114                 $now     = time();
115                 $this->debug("Comparing {$regtime} to {$now}");
116                 if ($now - $regtime < $seconds) {
117                     // TRANS: Exception thrown when too many user have registered from one IP address within a given time frame.
118                     throw new Exception(_m('Too many registrations. Take a break and try again later.'));
119                 }
120             }
121         }
122
123         // Check for silenced users
124
125         if ($this->silenced) {
126             $ids = Registration_ip::usersByIP($ipaddress);
127             foreach ($ids as $id) {
128                 $profile = Profile::getKV('id', $id);
129                 if ($profile && $profile->isSilenced()) {
130                     // TRANS: Exception thrown when attempting to register from an IP address from which silenced users have registered.
131                     throw new Exception(_m('A banned user has registered from this address.'));
132                 }
133             }
134         }
135
136         return true;
137     }
138
139     /**
140      * Called after someone registers, by any means.
141      *
142      * We record the successful registration and IP address.
143      *
144      * @param Profile $profile new user's profile
145      * @param User $user new user
146      *
147      * @return boolean hook value
148      */
149     function onEndUserRegister($profile, $user)
150     {
151         $ipaddress = $this->_getIpAddress();
152
153         if (empty($ipaddress)) {
154             // User registration can happen from command-line scripts etc.
155             return true;
156         }
157
158         $reg = new Registration_ip();
159
160         $reg->user_id   = $user->id;
161         $reg->ipaddress = $ipaddress;
162
163         $result = $reg->insert();
164
165         if (!$result) {
166             common_log_db_error($reg, 'INSERT', __FILE__);
167             // @todo throw an exception?
168         }
169
170         return true;
171     }
172
173     /**
174      * Check the version of the plugin.
175      *
176      * @param array &$versions Version array.
177      *
178      * @return boolean hook value
179      */
180     function onPluginVersion(&$versions)
181     {
182         $versions[] = array('name' => 'RegisterThrottle',
183                             'version' => STATUSNET_VERSION,
184                             'author' => 'Evan Prodromou',
185                             'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
186                             'description' =>
187                             // TRANS: Plugin description.
188                             _m('Throttles excessive registration from a single IP address.'));
189         return true;
190     }
191
192     /**
193      * Gets the current IP address.
194      *
195      * @return string IP address or null if not found.
196      */
197     private function _getIpAddress()
198     {
199         $keys = array('HTTP_X_FORWARDED_FOR',
200                       'HTTP_X_CLIENT',
201                       'CLIENT-IP',
202                       'REMOTE_ADDR');
203
204         foreach ($keys as $k) {
205             if (!empty($_SERVER[$k])) {
206                 return $_SERVER[$k];
207             }
208         }
209
210         return null;
211     }
212
213     /**
214      * Gets the Nth registration with the given IP address.
215      *
216      * @param string  $ipaddress Address to key on
217      * @param integer $n         Nth address
218      *
219      * @return Registration_ip nth registration or null if not found.
220      */
221     private function _getNthReg($ipaddress, $n)
222     {
223         $reg = new Registration_ip();
224
225         $reg->ipaddress = $ipaddress;
226
227         $reg->orderBy('created DESC');
228         $reg->limit($n - 1, 1);
229
230         if ($reg->find(true)) {
231             return $reg;
232         } else {
233             return null;
234         }
235     }
236
237     /**
238      * When silencing a user, silence all other users registered from that IP
239      * address.
240      *
241      * @param Profile $profile Person getting a new role
242      * @param string  $role    Role being assigned like 'moderator' or 'silenced'
243      *
244      * @return boolean hook value
245      */
246     function onEndGrantRole($profile, $role)
247     {
248         if (!self::$enabled) {
249             return true;
250         }
251
252         if ($role != Profile_role::SILENCED) {
253             return true;
254         }
255
256         if (!$this->silenced) {
257             return true;
258         }
259
260         $ri = Registration_ip::getKV('user_id', $profile->id);
261
262         if (empty($ri)) {
263             return true;
264         }
265
266         $ids = Registration_ip::usersByIP($ri->ipaddress);
267
268         foreach ($ids as $id) {
269             if ($id == $profile->id) {
270                 continue;
271             }
272
273             $other = Profile::getKV('id', $id);
274
275             if (empty($other)) {
276                 continue;
277             }
278
279             if ($other->isSilenced()) {
280                 continue;
281             }
282
283             $old = self::$enabled;
284             self::$enabled = false;
285             $other->silence();
286             self::$enabled = $old;
287         }
288     }
289 }