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
117 function onStartRegistrationTry($action)
119 $ipaddress = $this->_getIpAddress();
121 if (empty($ipaddress)) {
122 throw new ServerException(_m('Cannot find IP address.'));
125 foreach ($this->regLimits as $seconds => $limit) {
127 $this->debug("Checking $seconds ($limit)");
129 $reg = $this->_getNthReg($ipaddress, $limit);
132 $this->debug("Got a {$limit}th registration.");
133 $regtime = strtotime($reg->created);
135 $this->debug("Comparing {$regtime} to {$now}");
136 if ($now - $regtime < $seconds) {
137 throw new Exception(_("Too many registrations. Take a break and try again later."));
146 * Called after someone registers.
148 * We record the successful registration and IP address.
150 * @param Action $action Action that is being executed
152 * @return boolean hook value
156 function onEndRegistrationTry($action)
158 $ipaddress = $this->_getIpAddress();
160 if (empty($ipaddress)) {
161 throw new ServerException(_m('Cannot find IP address.'));
164 $user = common_current_user();
167 throw new ServerException(_m('Cannot find user after successful registration.'));
170 $reg = new Registration_ip();
172 $reg->user_id = $user->id;
173 $reg->ipaddress = $ipaddress;
175 $result = $reg->insert();
178 common_log_db_error($reg, 'INSERT', __FILE__);
179 // @todo throw an exception?
186 * Check the version of the plugin.
188 * @param array &$versions Version array.
190 * @return boolean hook value
193 function onPluginVersion(&$versions)
195 $versions[] = array('name' => 'RegisterThrottle',
196 'version' => STATUSNET_VERSION,
197 'author' => 'Evan Prodromou',
198 'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
200 _m('Throttles excessive registration from a single IP.'));
205 * Gets the current IP address.
207 * @return string IP address or null if not found.
210 private function _getIpAddress()
212 $keys = array('HTTP_X_FORWARDED_FOR',
216 foreach ($keys as $k) {
217 if (!empty($_SERVER[$k])) {
226 * Gets the Nth registration with the given IP address.
228 * @param string $ipaddress Address to key on
229 * @param integer $n Nth address
231 * @return Registration_ip nth registration or null if not found.
234 private function _getNthReg($ipaddress, $n)
236 $reg = new Registration_ip();
238 $reg->ipaddress = $ipaddress;
240 $reg->orderBy('created DESC');
241 $reg->limit($n - 1, 1);
243 if ($reg->find(true)) {