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 * Database schema setup
63 * We store user registrations in a table registration_ip.
65 * @return boolean hook value; true means continue processing, false means stop.
68 function onCheckSchema()
70 $schema = Schema::get();
72 // For storing user-submitted flags on profiles
74 $schema->ensureTable('registration_ip',
75 array(new ColumnDef('user_id', 'integer', null,
77 new ColumnDef('ipaddress', 'varchar', 15, false, 'MUL'),
78 new ColumnDef('created', 'timestamp', null, false, 'MUL')));
84 * Load related modules when needed
86 * @param string $cls Name of the class to be loaded
88 * @return boolean hook value; true means continue processing, false means stop.
91 function onAutoload($cls)
93 $dir = dirname(__FILE__);
97 case 'Registration_ip':
98 include_once $dir . '/'.$cls.'.php';
106 * Called when someone tries to register.
108 * We check the IP here to determine if it goes over any of our
111 * @param Action $action Action that is being executed
113 * @return boolean hook value
116 function onStartRegistrationTry($action)
118 $ipaddress = $this->_getIpAddress();
120 if (empty($ipaddress)) {
121 throw new ServerException(_m('Cannot find IP address.'));
124 foreach ($this->regLimits as $seconds => $limit) {
126 $this->debug("Checking $seconds ($limit)");
128 $reg = $this->_getNthReg($ipaddress, $limit);
131 $this->debug("Got a {$limit}th registration.");
132 $regtime = strtotime($reg->created);
134 $this->debug("Comparing {$regtime} to {$now}");
135 if ($now - $regtime < $seconds) {
136 throw new Exception(_m("Too many registrations. Take a break and try again later."));
145 * Called after someone registers.
147 * We record the successful registration and IP address.
149 * @param Action $action Action that is being executed
151 * @return boolean hook value
155 function onEndRegistrationTry($action)
157 $ipaddress = $this->_getIpAddress();
159 if (empty($ipaddress)) {
160 throw new ServerException(_m('Cannot find IP address.'));
163 $user = common_current_user();
166 throw new ServerException(_m('Cannot find user after successful registration.'));
169 $reg = new Registration_ip();
171 $reg->user_id = $user->id;
172 $reg->ipaddress = $ipaddress;
174 $result = $reg->insert();
177 common_log_db_error($reg, 'INSERT', __FILE__);
178 // @todo throw an exception?
185 * Check the version of the plugin.
187 * @param array &$versions Version array.
189 * @return boolean hook value
192 function onPluginVersion(&$versions)
194 $versions[] = array('name' => 'RegisterThrottle',
195 'version' => STATUSNET_VERSION,
196 'author' => 'Evan Prodromou',
197 'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
199 _m('Throttles excessive registration from a single IP address.'));
204 * Gets the current IP address.
206 * @return string IP address or null if not found.
209 private function _getIpAddress()
211 $keys = array('HTTP_X_FORWARDED_FOR',
215 foreach ($keys as $k) {
216 if (!empty($_SERVER[$k])) {
225 * Gets the Nth registration with the given IP address.
227 * @param string $ipaddress Address to key on
228 * @param integer $n Nth address
230 * @return Registration_ip nth registration or null if not found.
233 private function _getNthReg($ipaddress, $n)
235 $reg = new Registration_ip();
237 $reg->ipaddress = $ipaddress;
239 $reg->orderBy('created DESC');
240 $reg->limit($n - 1, 1);
242 if ($reg->find(true)) {