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 * Whether we're enabled; prevents recursion.
66 static private $enabled = true;
69 * Database schema setup
71 * We store user registrations in a table registration_ip.
73 * @return boolean hook value; true means continue processing, false means stop.
75 public function onCheckSchema()
77 $schema = Schema::get();
79 // For storing user-submitted flags on profiles
80 $schema->ensureTable('registration_ip', Registration_ip::schemaDef());
84 public function onRouterInitialized(URLMapper $m)
86 $m->connect('main/ipregistrations/:ipaddress',
87 array('action' => 'ipregistrations'),
88 array('ipaddress' => '[0-9a-f\.\:]+'));
92 * Called when someone tries to register.
94 * We check the IP here to determine if it goes over any of our
97 * @param Action $action Action that is being executed
99 * @return boolean hook value
101 public function onStartRegistrationTry($action)
103 $ipaddress = $this->_getIpAddress();
105 if (empty($ipaddress)) {
106 // TRANS: Server exception thrown when no IP address can be found for a registation attempt.
107 throw new ServerException(_m('Cannot find IP address.'));
110 foreach ($this->regLimits as $seconds => $limit) {
112 $this->debug("Checking $seconds ($limit)");
114 $reg = $this->_getNthReg($ipaddress, $limit);
117 $this->debug("Got a {$limit}th registration.");
118 $regtime = strtotime($reg->created);
120 $this->debug("Comparing {$regtime} to {$now}");
121 if ($now - $regtime < $seconds) {
122 // TRANS: Exception thrown when too many user have registered from one IP address within a given time frame.
123 throw new Exception(_m('Too many registrations. Take a break and try again later.'));
128 // Check for silenced users
130 if ($this->silenced) {
131 $ids = Registration_ip::usersByIP($ipaddress);
132 foreach ($ids as $id) {
133 $profile = Profile::getKV('id', $id);
134 if ($profile && $profile->isSilenced()) {
135 // TRANS: Exception thrown when attempting to register from an IP address from which silenced users have registered.
136 throw new Exception(_m('A banned user has registered from this address.'));
144 function onEndShowSections(Action $action)
146 if (!$action instanceof ShowstreamAction) {
147 // early return for actions we're not interested in
151 $target = $action->getTarget();
152 if (!$target->isSilenced()) {
153 // Only show the IP of users who are not silenced.
157 $scoped = $action->getScoped();
158 if (!$scoped->hasRight(Right::SILENCEUSER)) {
159 // only show registration IP if we have the right to silence users
163 $ri = Registration_ip::getKV('user_id', $target->getID());
165 if ($ri instanceof Registration_ip) {
166 $ipaddress = $ri->ipaddress;
170 $action->elementStart('div', array('id' => 'entity_mod_log',
171 'class' => 'section'));
173 $action->element('h2', null, _('Registration IP'));
175 // TRANS: Label for the information about which IP a users registered from.
176 $action->element('strong', null, _('Registered from:'));
178 $attrs = ['class'=>'ipaddress'];
179 if (!is_null($ipaddress)) {
181 $attrs['href'] = common_local_url('ipregistrations', array('ipaddress'=>$ipaddress));
183 $action->element($el, $attrs,
184 // TRANS: Unknown IP address.
185 $ipaddress ?: _('unknown'));
187 $action->elementEnd('div');
191 * Called after someone registers, by any means.
193 * We record the successful registration and IP address.
195 * @param Profile $profile new user's profile
197 * @return boolean hook value
199 public function onEndUserRegister(Profile $profile)
201 $ipaddress = $this->_getIpAddress();
203 if (empty($ipaddress)) {
204 // User registration can happen from command-line scripts etc.
208 $reg = new Registration_ip();
210 $reg->user_id = $profile->getID();
211 $reg->ipaddress = mb_strtolower($ipaddress);
212 $reg->created = common_sql_now();
214 $result = $reg->insert();
216 if ($result === false) {
217 common_log_db_error($reg, 'INSERT', __FILE__);
218 // @todo throw an exception?
225 * Check the version of the plugin.
227 * @param array &$versions Version array.
229 * @return boolean hook value
231 public function onPluginVersion(array &$versions)
233 $versions[] = array('name' => 'RegisterThrottle',
234 'version' => GNUSOCIAL_VERSION,
235 'author' => 'Evan Prodromou',
236 'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
238 // TRANS: Plugin description.
239 _m('Throttles excessive registration from a single IP address.'));
244 * Gets the current IP address.
246 * @return string IP address or null if not found.
248 private function _getIpAddress()
250 $keys = array('HTTP_X_FORWARDED_FOR',
255 foreach ($keys as $k) {
256 if (!empty($_SERVER[$k])) {
265 * Gets the Nth registration with the given IP address.
267 * @param string $ipaddress Address to key on
268 * @param integer $n Nth address
270 * @return Registration_ip nth registration or null if not found.
272 private function _getNthReg($ipaddress, $n)
274 $reg = new Registration_ip();
276 $reg->ipaddress = $ipaddress;
278 $reg->orderBy('created DESC');
279 $reg->limit($n - 1, 1);
281 if ($reg->find(true)) {
289 * When silencing a user, silence all other users registered from that IP
292 * @param Profile $profile Person getting a new role
293 * @param string $role Role being assigned like 'moderator' or 'silenced'
295 * @return boolean hook value
297 public function onEndGrantRole($profile, $role)
299 if (!self::$enabled) {
303 if ($role != Profile_role::SILENCED) {
307 if (!$this->silenced) {
311 $ri = Registration_ip::getKV('user_id', $profile->id);
317 $ids = Registration_ip::usersByIP($ri->ipaddress);
319 foreach ($ids as $id) {
320 if ($id == $profile->id) {
324 $other = Profile::getKV('id', $id);
330 if ($other->isSilenced()) {
334 $old = self::$enabled;
335 self::$enabled = false;
337 self::$enabled = $old;