3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Throttle registration by IP address
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
36 * Throttle registration by IP address
38 * We a) record IP address of registrants and b) throttle registrations.
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/
48 class RegisterThrottlePlugin extends Plugin
51 * Array of time spans in seconds to limits.
53 * Default is 3 registrations per hour, 5 per day, 10 per week.
56 public $regLimits = array(604800 => 10, // per week
57 86400 => 5, // per day
58 3600 => 3); // per hour
61 * Disallow registration if a silenced user has registered from
65 public $silenced = true;
68 * Whether we're enabled; prevents recursion.
71 static private $enabled = true;
74 * Database schema setup
76 * We store user registrations in a table registration_ip.
78 * @return boolean hook value; true means continue processing, false means stop.
81 function onCheckSchema()
83 $schema = Schema::get();
85 // For storing user-submitted flags on profiles
87 $schema->ensureTable('registration_ip',
88 array(new ColumnDef('user_id', 'integer', null,
90 new ColumnDef('ipaddress', 'varchar', 15, false, 'MUL'),
91 new ColumnDef('created', 'timestamp', null, false, 'MUL')));
97 * Load related modules when needed
99 * @param string $cls Name of the class to be loaded
101 * @return boolean hook value; true means continue processing, false means stop.
104 function onAutoload($cls)
106 $dir = dirname(__FILE__);
110 case 'Registration_ip':
111 include_once $dir . '/'.$cls.'.php';
119 * Called when someone tries to register.
121 * We check the IP here to determine if it goes over any of our
124 * @param Action $action Action that is being executed
126 * @return boolean hook value
129 function onStartRegistrationTry($action)
131 $ipaddress = $this->_getIpAddress();
133 if (empty($ipaddress)) {
134 throw new ServerException(_m('Cannot find IP address.'));
137 foreach ($this->regLimits as $seconds => $limit) {
139 $this->debug("Checking $seconds ($limit)");
141 $reg = $this->_getNthReg($ipaddress, $limit);
144 $this->debug("Got a {$limit}th registration.");
145 $regtime = strtotime($reg->created);
147 $this->debug("Comparing {$regtime} to {$now}");
148 if ($now - $regtime < $seconds) {
149 throw new Exception(_m("Too many registrations. Take a break and try again later."));
154 // Check for silenced users
156 if ($this->silenced) {
157 $ids = Registration_ip::usersByIP($ipaddress);
158 foreach ($ids as $id) {
159 $profile = Profile::staticGet('id', $id);
160 if ($profile && $profile->isSilenced()) {
161 throw new Exception(_m("A banned user has registered from this address."));
170 * Called after someone registers, by any means.
172 * We record the successful registration and IP address.
174 * @param Profile $profile new user's profile
175 * @param User $user new user
177 * @return boolean hook value
181 function onEndUserRegister($profile, $user)
183 $ipaddress = $this->_getIpAddress();
185 if (empty($ipaddress)) {
186 // User registration can happen from command-line scripts etc.
190 $reg = new Registration_ip();
192 $reg->user_id = $user->id;
193 $reg->ipaddress = $ipaddress;
195 $result = $reg->insert();
198 common_log_db_error($reg, 'INSERT', __FILE__);
199 // @todo throw an exception?
206 * Check the version of the plugin.
208 * @param array &$versions Version array.
210 * @return boolean hook value
213 function onPluginVersion(&$versions)
215 $versions[] = array('name' => 'RegisterThrottle',
216 'version' => STATUSNET_VERSION,
217 'author' => 'Evan Prodromou',
218 'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
220 _m('Throttles excessive registration from a single IP address.'));
225 * Gets the current IP address.
227 * @return string IP address or null if not found.
230 private function _getIpAddress()
232 $keys = array('HTTP_X_FORWARDED_FOR',
236 foreach ($keys as $k) {
237 if (!empty($_SERVER[$k])) {
246 * Gets the Nth registration with the given IP address.
248 * @param string $ipaddress Address to key on
249 * @param integer $n Nth address
251 * @return Registration_ip nth registration or null if not found.
254 private function _getNthReg($ipaddress, $n)
256 $reg = new Registration_ip();
258 $reg->ipaddress = $ipaddress;
260 $reg->orderBy('created DESC');
261 $reg->limit($n - 1, 1);
263 if ($reg->find(true)) {
271 * When silencing a user, silence all other users registered from that IP
274 * @param Profile $profile Person getting a new role
275 * @param string $role Role being assigned like 'moderator' or 'silenced'
277 * @return boolean hook value
280 function onEndGrantRole($profile, $role)
282 if (!self::$enabled) {
286 if ($role != Profile_role::SILENCED) {
290 if (!$this->silenced) {
294 $ri = Registration_ip::staticGet('user_id', $profile->id);
300 $ids = Registration_ip::usersByIP($ri->ipaddress);
302 foreach ($ids as $id) {
304 if ($id == $profile->id) {
308 $other = Profile::staticGet('id', $id);
314 if ($other->isSilenced()) {
318 $old = self::$enabled;
319 self::$enabled = false;
321 self::$enabled = $old;