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.
55 public $regLimits = array(604800 => 10, // per week
56 86400 => 5, // per day
57 3600 => 3); // per hour
60 * Disallow registration if a silenced user has registered from
63 public $silenced = true;
66 * Whether we're enabled; prevents recursion.
68 static private $enabled = true;
71 * Database schema setup
73 * We store user registrations in a table registration_ip.
75 * @return boolean hook value; true means continue processing, false means stop.
77 function onCheckSchema()
79 $schema = Schema::get();
81 // For storing user-submitted flags on profiles
82 $schema->ensureTable('registration_ip', Registration_ip::schemaDef());
87 * Load related modules when needed
89 * @param string $cls Name of the class to be loaded
91 * @return boolean hook value; true means continue processing, false means stop.
93 function onAutoload($cls)
95 $dir = dirname(__FILE__);
99 case 'Registration_ip':
100 include_once $dir . '/'.$cls.'.php';
108 * Called when someone tries to register.
110 * We check the IP here to determine if it goes over any of our
113 * @param Action $action Action that is being executed
115 * @return boolean hook value
117 function onStartRegistrationTry($action)
119 $ipaddress = $this->_getIpAddress();
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.'));
126 foreach ($this->regLimits as $seconds => $limit) {
128 $this->debug("Checking $seconds ($limit)");
130 $reg = $this->_getNthReg($ipaddress, $limit);
133 $this->debug("Got a {$limit}th registration.");
134 $regtime = strtotime($reg->created);
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.'));
144 // Check for silenced users
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.'));
161 * Called after someone registers, by any means.
163 * We record the successful registration and IP address.
165 * @param Profile $profile new user's profile
166 * @param User $user new user
168 * @return boolean hook value
170 function onEndUserRegister($profile, $user)
172 $ipaddress = $this->_getIpAddress();
174 if (empty($ipaddress)) {
175 // User registration can happen from command-line scripts etc.
179 $reg = new Registration_ip();
181 $reg->user_id = $user->id;
182 $reg->ipaddress = $ipaddress;
184 $result = $reg->insert();
187 common_log_db_error($reg, 'INSERT', __FILE__);
188 // @todo throw an exception?
195 * Check the version of the plugin.
197 * @param array &$versions Version array.
199 * @return boolean hook value
201 function onPluginVersion(&$versions)
203 $versions[] = array('name' => 'RegisterThrottle',
204 'version' => STATUSNET_VERSION,
205 'author' => 'Evan Prodromou',
206 'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
208 // TRANS: Plugin description.
209 _m('Throttles excessive registration from a single IP address.'));
214 * Gets the current IP address.
216 * @return string IP address or null if not found.
218 private function _getIpAddress()
220 $keys = array('HTTP_X_FORWARDED_FOR',
225 foreach ($keys as $k) {
226 if (!empty($_SERVER[$k])) {
235 * Gets the Nth registration with the given IP address.
237 * @param string $ipaddress Address to key on
238 * @param integer $n Nth address
240 * @return Registration_ip nth registration or null if not found.
242 private function _getNthReg($ipaddress, $n)
244 $reg = new Registration_ip();
246 $reg->ipaddress = $ipaddress;
248 $reg->orderBy('created DESC');
249 $reg->limit($n - 1, 1);
251 if ($reg->find(true)) {
259 * When silencing a user, silence all other users registered from that IP
262 * @param Profile $profile Person getting a new role
263 * @param string $role Role being assigned like 'moderator' or 'silenced'
265 * @return boolean hook value
267 function onEndGrantRole($profile, $role)
269 if (!self::$enabled) {
273 if ($role != Profile_role::SILENCED) {
277 if (!$this->silenced) {
281 $ri = Registration_ip::getKV('user_id', $profile->id);
287 $ids = Registration_ip::usersByIP($ri->ipaddress);
289 foreach ($ids as $id) {
290 if ($id == $profile->id) {
294 $other = Profile::getKV('id', $id);
300 if ($other->isSilenced()) {
304 $old = self::$enabled;
305 self::$enabled = false;
307 self::$enabled = $old;