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('GNUSOCIAL')) { exit(1); }
34 * Throttle registration by IP address
36 * We a) record IP address of registrants and b) throttle registrations.
40 * @author Evan Prodromou <evan@status.net>
41 * @copyright 2010 StatusNet, Inc.
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
43 * @link http://status.net/
46 class RegisterThrottlePlugin extends Plugin
49 * Array of time spans in seconds to limits.
51 * Default is 3 registrations per hour, 5 per day, 10 per week.
53 public $regLimits = array(604800 => 10, // per week
54 86400 => 5, // per day
55 3600 => 3); // per hour
58 * Disallow registration if a silenced user has registered from
61 public $silenced = true;
64 * Auto-silence all other users from the same registration_ip
65 * as the one being silenced. Caution: Many users may come from
66 * the same IP (even entire countries) without having any sort
67 * of relevant connection for moderation.
69 public $auto_silence_by_ip = false;
72 * Whether we're enabled; prevents recursion.
74 static private $enabled = true;
77 * Database schema setup
79 * We store user registrations in a table registration_ip.
81 * @return boolean hook value; true means continue processing, false means stop.
83 public function onCheckSchema()
85 $schema = Schema::get();
87 // For storing user-submitted flags on profiles
88 $schema->ensureTable('registration_ip', Registration_ip::schemaDef());
92 public function onRouterInitialized(URLMapper $m)
94 $m->connect('main/ipregistrations/:ipaddress',
95 array('action' => 'ipregistrations'),
96 array('ipaddress' => '[0-9a-f\.\:]+'));
100 * Called when someone tries to register.
102 * We check the IP here to determine if it goes over any of our
105 * @param Action $action Action that is being executed
107 * @return boolean hook value
109 public function onStartRegistrationTry($action)
111 $ipaddress = $this->_getIpAddress();
113 if (empty($ipaddress)) {
114 // TRANS: Server exception thrown when no IP address can be found for a registation attempt.
115 throw new ServerException(_m('Cannot find IP address.'));
118 foreach ($this->regLimits as $seconds => $limit) {
120 $this->debug("Checking $seconds ($limit)");
122 $reg = $this->_getNthReg($ipaddress, $limit);
125 $this->debug("Got a {$limit}th registration.");
126 $regtime = strtotime($reg->created);
128 $this->debug("Comparing {$regtime} to {$now}");
129 if ($now - $regtime < $seconds) {
130 // TRANS: Exception thrown when too many user have registered from one IP address within a given time frame.
131 throw new Exception(_m('Too many registrations. Take a break and try again later.'));
136 // Check for silenced users
138 if ($this->silenced) {
139 $ids = Registration_ip::usersByIP($ipaddress);
140 foreach ($ids as $id) {
141 $profile = Profile::getKV('id', $id);
142 if ($profile && $profile->isSilenced()) {
143 // TRANS: Exception thrown when attempting to register from an IP address from which silenced users have registered.
144 throw new Exception(_m('A banned user has registered from this address.'));
152 function onEndShowSections(Action $action)
154 if (!$action instanceof ShowstreamAction) {
155 // early return for actions we're not interested in
159 $target = $action->getTarget();
160 if (!$target->isSilenced()) {
161 // Only show the IP of users who are not silenced.
165 $scoped = $action->getScoped();
166 if (!$scoped->hasRight(Right::SILENCEUSER)) {
167 // only show registration IP if we have the right to silence users
171 $ri = Registration_ip::getKV('user_id', $target->getID());
173 if ($ri instanceof Registration_ip) {
174 $ipaddress = $ri->ipaddress;
178 $action->elementStart('div', array('id' => 'entity_mod_log',
179 'class' => 'section'));
181 $action->element('h2', null, _('Registration IP'));
183 // TRANS: Label for the information about which IP a users registered from.
184 $action->element('strong', null, _('Registered from:'));
186 $attrs = ['class'=>'ipaddress'];
187 if (!is_null($ipaddress)) {
189 $attrs['href'] = common_local_url('ipregistrations', array('ipaddress'=>$ipaddress));
191 $action->element($el, $attrs,
192 // TRANS: Unknown IP address.
193 $ipaddress ?: _('unknown'));
195 $action->elementEnd('div');
199 * Called after someone registers, by any means.
201 * We record the successful registration and IP address.
203 * @param Profile $profile new user's profile
205 * @return boolean hook value
207 public function onEndUserRegister(Profile $profile)
209 $ipaddress = $this->_getIpAddress();
211 if (empty($ipaddress)) {
212 // User registration can happen from command-line scripts etc.
216 $reg = new Registration_ip();
218 $reg->user_id = $profile->getID();
219 $reg->ipaddress = mb_strtolower($ipaddress);
220 $reg->created = common_sql_now();
222 $result = $reg->insert();
224 if ($result === false) {
225 common_log_db_error($reg, 'INSERT', __FILE__);
226 // @todo throw an exception?
233 * Check the version of the plugin.
235 * @param array &$versions Version array.
237 * @return boolean hook value
239 public function onPluginVersion(array &$versions)
241 $versions[] = array('name' => 'RegisterThrottle',
242 'version' => GNUSOCIAL_VERSION,
243 'author' => 'Evan Prodromou',
244 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/RegisterThrottle',
246 // TRANS: Plugin description.
247 _m('Throttles excessive registration from a single IP address.'));
252 * Gets the current IP address.
254 * @return string IP address or null if not found.
256 private function _getIpAddress()
258 $keys = array('HTTP_X_FORWARDED_FOR',
263 foreach ($keys as $k) {
264 if (!empty($_SERVER[$k])) {
273 * Gets the Nth registration with the given IP address.
275 * @param string $ipaddress Address to key on
276 * @param integer $n Nth address
278 * @return Registration_ip nth registration or null if not found.
280 private function _getNthReg($ipaddress, $n)
282 $reg = new Registration_ip();
284 $reg->ipaddress = $ipaddress;
286 $reg->orderBy('created DESC');
287 $reg->limit($n - 1, 1);
289 if ($reg->find(true)) {
297 * When silencing a user, silence all other users registered from that IP
300 * @param Profile $profile Person getting a new role
301 * @param string $role Role being assigned like 'moderator' or 'silenced'
303 * @return boolean hook value
305 public function onEndGrantRole($profile, $role)
307 if (!self::$enabled) {
311 if ($role !== Profile_role::SILENCED) {
315 if (!$this->auto_silence_by_ip) {
319 $ri = Registration_ip::getKV('user_id', $profile->getID());
325 $ids = Registration_ip::usersByIP($ri->ipaddress);
327 foreach ($ids as $id) {
328 if ($id == $profile->getID()) {
333 $other = Profile::getByID($id);
334 } catch (NoResultException $e) {
338 if ($other->isSilenced()) {
342 // 'enabled' here is used to prevent recursion, since
343 // we'll end up in this function again on ->silence()
344 // though I actually think it doesn't matter since we
345 // do this in onEndGrantRole and that means the above
346 // $other->isSilenced() test should've 'continue'd...
347 $old = self::$enabled;
348 self::$enabled = false;
350 self::$enabled = $old;