]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RegisterThrottle/RegisterThrottlePlugin.php
More info for a proper, fancy-url lighttpd setup
[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      * Load related modules when needed
88      *
89      * @param string $cls Name of the class to be loaded
90      *
91      * @return boolean hook value; true means continue processing, false means stop.
92      */
93     function onAutoload($cls)
94     {
95         $dir = dirname(__FILE__);
96
97         switch ($cls)
98         {
99         case 'Registration_ip':
100             include_once $dir . '/'.$cls.'.php';
101             return false;
102         default:
103             return true;
104         }
105     }
106
107     /**
108      * Called when someone tries to register.
109      *
110      * We check the IP here to determine if it goes over any of our
111      * configured limits.
112      *
113      * @param Action $action Action that is being executed
114      *
115      * @return boolean hook value
116      */
117     function onStartRegistrationTry($action)
118     {
119         $ipaddress = $this->_getIpAddress();
120
121         if (empty($ipaddress)) {
122             // TRANS: Server exception thrown when no IP address can be found for a registation attempt.
123             throw new ServerException(_m('Cannot find IP address.'));
124         }
125
126         foreach ($this->regLimits as $seconds => $limit) {
127
128             $this->debug("Checking $seconds ($limit)");
129
130             $reg = $this->_getNthReg($ipaddress, $limit);
131
132             if (!empty($reg)) {
133                 $this->debug("Got a {$limit}th registration.");
134                 $regtime = strtotime($reg->created);
135                 $now     = time();
136                 $this->debug("Comparing {$regtime} to {$now}");
137                 if ($now - $regtime < $seconds) {
138                     // TRANS: Exception thrown when too many user have registered from one IP address within a given time frame.
139                     throw new Exception(_m('Too many registrations. Take a break and try again later.'));
140                 }
141             }
142         }
143
144         // Check for silenced users
145
146         if ($this->silenced) {
147             $ids = Registration_ip::usersByIP($ipaddress);
148             foreach ($ids as $id) {
149                 $profile = Profile::getKV('id', $id);
150                 if ($profile && $profile->isSilenced()) {
151                     // TRANS: Exception thrown when attempting to register from an IP address from which silenced users have registered.
152                     throw new Exception(_m('A banned user has registered from this address.'));
153                 }
154             }
155         }
156
157         return true;
158     }
159
160     /**
161      * Called after someone registers, by any means.
162      *
163      * We record the successful registration and IP address.
164      *
165      * @param Profile $profile new user's profile
166      * @param User $user new user
167      *
168      * @return boolean hook value
169      */
170     function onEndUserRegister($profile, $user)
171     {
172         $ipaddress = $this->_getIpAddress();
173
174         if (empty($ipaddress)) {
175             // User registration can happen from command-line scripts etc.
176             return true;
177         }
178
179         $reg = new Registration_ip();
180
181         $reg->user_id   = $user->id;
182         $reg->ipaddress = $ipaddress;
183
184         $result = $reg->insert();
185
186         if (!$result) {
187             common_log_db_error($reg, 'INSERT', __FILE__);
188             // @todo throw an exception?
189         }
190
191         return true;
192     }
193
194     /**
195      * Check the version of the plugin.
196      *
197      * @param array &$versions Version array.
198      *
199      * @return boolean hook value
200      */
201     function onPluginVersion(&$versions)
202     {
203         $versions[] = array('name' => 'RegisterThrottle',
204                             'version' => STATUSNET_VERSION,
205                             'author' => 'Evan Prodromou',
206                             'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
207                             'description' =>
208                             // TRANS: Plugin description.
209                             _m('Throttles excessive registration from a single IP address.'));
210         return true;
211     }
212
213     /**
214      * Gets the current IP address.
215      *
216      * @return string IP address or null if not found.
217      */
218     private function _getIpAddress()
219     {
220         $keys = array('HTTP_X_FORWARDED_FOR',
221                       'HTTP_X_CLIENT',
222                       'CLIENT-IP',
223                       'REMOTE_ADDR');
224
225         foreach ($keys as $k) {
226             if (!empty($_SERVER[$k])) {
227                 return $_SERVER[$k];
228             }
229         }
230
231         return null;
232     }
233
234     /**
235      * Gets the Nth registration with the given IP address.
236      *
237      * @param string  $ipaddress Address to key on
238      * @param integer $n         Nth address
239      *
240      * @return Registration_ip nth registration or null if not found.
241      */
242     private function _getNthReg($ipaddress, $n)
243     {
244         $reg = new Registration_ip();
245
246         $reg->ipaddress = $ipaddress;
247
248         $reg->orderBy('created DESC');
249         $reg->limit($n - 1, 1);
250
251         if ($reg->find(true)) {
252             return $reg;
253         } else {
254             return null;
255         }
256     }
257
258     /**
259      * When silencing a user, silence all other users registered from that IP
260      * address.
261      *
262      * @param Profile $profile Person getting a new role
263      * @param string  $role    Role being assigned like 'moderator' or 'silenced'
264      *
265      * @return boolean hook value
266      */
267     function onEndGrantRole($profile, $role)
268     {
269         if (!self::$enabled) {
270             return true;
271         }
272
273         if ($role != Profile_role::SILENCED) {
274             return true;
275         }
276
277         if (!$this->silenced) {
278             return true;
279         }
280
281         $ri = Registration_ip::getKV('user_id', $profile->id);
282
283         if (empty($ri)) {
284             return true;
285         }
286
287         $ids = Registration_ip::usersByIP($ri->ipaddress);
288
289         foreach ($ids as $id) {
290             if ($id == $profile->id) {
291                 continue;
292             }
293
294             $other = Profile::getKV('id', $id);
295
296             if (empty($other)) {
297                 continue;
298             }
299
300             if ($other->isSilenced()) {
301                 continue;
302             }
303
304             $old = self::$enabled;
305             self::$enabled = false;
306             $other->silence();
307             self::$enabled = $old;
308         }
309     }
310 }