]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RegisterThrottle/RegisterThrottlePlugin.php
Plugin to restrict too many registrations from one IP
[quix0rs-gnu-social.git] / plugins / RegisterThrottle / RegisterThrottlePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Throttle registration by IP address
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Spam
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Throttle registration by IP address
37  *
38  * We a) record IP address of registrants and b) throttle registrations.
39  *
40  * @category  Spam
41  * @package   StatusNet
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/
46  */
47
48 class RegisterThrottlePlugin extends Plugin
49 {
50     /**
51      * Array of time spans in seconds to limits.
52      *
53      * Default is 3 registrations per hour, 5 per day, 10 per week.
54      */
55
56     public $regLimits = array(604800 => 10, // per week
57                               86400 => 5, // per day
58                               3600 => 3); // per hour
59
60     /**
61      * Database schema setup
62      *
63      * We store user registrations in a table registration_ip.
64      *
65      * @return boolean hook value; true means continue processing, false means stop.
66      */
67
68     function onCheckSchema()
69     {
70         $schema = Schema::get();
71
72         // For storing user-submitted flags on profiles
73
74         $schema->ensureTable('registration_ip',
75                              array(new ColumnDef('user_id', 'integer', null,
76                                                  false, 'PRI'),
77                                    new ColumnDef('ipaddress', 'varchar', 15, false, 'MUL'),
78                                    new ColumnDef('created', 'timestamp', null, false, 'MUL')));
79
80         return true;
81     }
82
83     /**
84      * Load related modules when needed
85      *
86      * @param string $cls Name of the class to be loaded
87      *
88      * @return boolean hook value; true means continue processing, false means stop.
89      */
90
91     function onAutoload($cls)
92     {
93         $dir = dirname(__FILE__);
94
95         switch ($cls)
96         {
97         case 'Registration_ip':
98             include_once $dir . '/'.$cls.'.php';
99             return false;
100         default:
101             return true;
102         }
103     }
104
105     /**
106      * Called when someone tries to register.
107      *
108      * We check the IP here to determine if it goes over any of our
109      * configured limits.
110      *
111      * @param Action $action Action that is being executed
112      *
113      * @return boolean hook value
114      *
115      */
116
117     function onStartRegistrationTry($action)
118     {
119         $ipaddress = $this->_getIpAddress();
120
121         if (empty($ipaddress)) {
122             throw new ServerException(_m('Cannot find IP address.'));
123         }
124
125         foreach ($this->regLimits as $seconds => $limit) {
126
127             $this->debug("Checking $seconds ($limit)");
128
129             $reg = $this->_getNthReg($ipaddress, $limit);
130
131             if (!empty($reg)) {
132                 $this->debug("Got a {$limit}th registration.");
133                 $regtime = strtotime($reg->created);
134                 $now     = time();
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."));
138                 }
139             }
140         }
141
142         return true;
143     }
144
145     /**
146      * Called after someone registers.
147      *
148      * We record the successful registration and IP address.
149      *
150      * @param Action $action Action that is being executed
151      *
152      * @return boolean hook value
153      *
154      */
155
156     function onEndRegistrationTry($action)
157     {
158         $ipaddress = $this->_getIpAddress();
159
160         if (empty($ipaddress)) {
161             throw new ServerException(_m('Cannot find IP address.'));
162         }
163
164         $user = common_current_user();
165
166         if (empty($user)) {
167             throw new ServerException(_m('Cannot find user after successful registration.'));
168         }
169
170         $reg = new Registration_ip();
171
172         $reg->user_id   = $user->id;
173         $reg->ipaddress = $ipaddress;
174
175         $result = $reg->insert();
176
177         if (!$result) {
178             common_log_db_error($reg, 'INSERT', __FILE__);
179             // @todo throw an exception?
180         }
181
182         return true;
183     }
184
185     /**
186      * Check the version of the plugin.
187      *
188      * @param array &$versions Version array.
189      *
190      * @return boolean hook value
191      */
192
193     function onPluginVersion(&$versions)
194     {
195         $versions[] = array('name' => 'RegisterThrottle',
196                             'version' => STATUSNET_VERSION,
197                             'author' => 'Evan Prodromou',
198                             'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
199                             'description' =>
200                             _m('Throttles excessive registration from a single IP.'));
201         return true;
202     }
203
204     /**
205      * Gets the current IP address.
206      *
207      * @return string IP address or null if not found.
208      */
209
210     private function _getIpAddress()
211     {
212         $keys = array('HTTP_X_FORWARDED_FOR',
213                       'CLIENT-IP',
214                       'REMOTE_ADDR');
215
216         foreach ($keys as $k) {
217             if (!empty($_SERVER[$k])) {
218                 return $_SERVER[$k];
219             }
220         }
221
222         return null;
223     }
224
225     /**
226      * Gets the Nth registration with the given IP address.
227      *
228      * @param string  $ipaddress Address to key on
229      * @param integer $n         Nth address
230      *
231      * @return Registration_ip nth registration or null if not found.
232      */
233
234     private function _getNthReg($ipaddress, $n)
235     {
236         $reg = new Registration_ip();
237
238         $reg->ipaddress = $ipaddress;
239
240         $reg->orderBy('created DESC');
241         $reg->limit($n - 1, 1);
242
243         if ($reg->find(true)) {
244             return $reg;
245         } else {
246             return null;
247         }
248     }
249 }