]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RegisterThrottle/RegisterThrottlePlugin.php
Merge remote branch 'gitorious/1.0.x' into 1.0.x
[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     function onStartRegistrationTry($action)
117     {
118         $ipaddress = $this->_getIpAddress();
119
120         if (empty($ipaddress)) {
121             throw new ServerException(_m('Cannot find IP address.'));
122         }
123
124         foreach ($this->regLimits as $seconds => $limit) {
125
126             $this->debug("Checking $seconds ($limit)");
127
128             $reg = $this->_getNthReg($ipaddress, $limit);
129
130             if (!empty($reg)) {
131                 $this->debug("Got a {$limit}th registration.");
132                 $regtime = strtotime($reg->created);
133                 $now     = time();
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."));
137                 }
138             }
139         }
140
141         return true;
142     }
143
144     /**
145      * Called after someone registers.
146      *
147      * We record the successful registration and IP address.
148      *
149      * @param Action $action Action that is being executed
150      *
151      * @return boolean hook value
152      *
153      */
154
155     function onEndRegistrationTry($action)
156     {
157         $ipaddress = $this->_getIpAddress();
158
159         if (empty($ipaddress)) {
160             throw new ServerException(_m('Cannot find IP address.'));
161         }
162
163         $user = common_current_user();
164
165         if (empty($user)) {
166             throw new ServerException(_m('Cannot find user after successful registration.'));
167         }
168
169         $reg = new Registration_ip();
170
171         $reg->user_id   = $user->id;
172         $reg->ipaddress = $ipaddress;
173
174         $result = $reg->insert();
175
176         if (!$result) {
177             common_log_db_error($reg, 'INSERT', __FILE__);
178             // @todo throw an exception?
179         }
180
181         return true;
182     }
183
184     /**
185      * Check the version of the plugin.
186      *
187      * @param array &$versions Version array.
188      *
189      * @return boolean hook value
190      */
191
192     function onPluginVersion(&$versions)
193     {
194         $versions[] = array('name' => 'RegisterThrottle',
195                             'version' => STATUSNET_VERSION,
196                             'author' => 'Evan Prodromou',
197                             'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
198                             'description' =>
199                             _m('Throttles excessive registration from a single IP address.'));
200         return true;
201     }
202
203     /**
204      * Gets the current IP address.
205      *
206      * @return string IP address or null if not found.
207      */
208
209     private function _getIpAddress()
210     {
211         $keys = array('HTTP_X_FORWARDED_FOR',
212                       'CLIENT-IP',
213                       'REMOTE_ADDR');
214
215         foreach ($keys as $k) {
216             if (!empty($_SERVER[$k])) {
217                 return $_SERVER[$k];
218             }
219         }
220
221         return null;
222     }
223
224     /**
225      * Gets the Nth registration with the given IP address.
226      *
227      * @param string  $ipaddress Address to key on
228      * @param integer $n         Nth address
229      *
230      * @return Registration_ip nth registration or null if not found.
231      */
232
233     private function _getNthReg($ipaddress, $n)
234     {
235         $reg = new Registration_ip();
236
237         $reg->ipaddress = $ipaddress;
238
239         $reg->orderBy('created DESC');
240         $reg->limit($n - 1, 1);
241
242         if ($reg->find(true)) {
243             return $reg;
244         } else {
245             return null;
246         }
247     }
248 }