]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RegisterThrottle/RegisterThrottlePlugin.php
Show user registration IP to users who can see ModLog
[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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Throttle registration by IP address
35  *
36  * We a) record IP address of registrants and b) throttle registrations.
37  *
38  * @category  Spam
39  * @package   StatusNet
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/
44  */
45
46 class RegisterThrottlePlugin extends Plugin
47 {
48     /**
49      * Array of time spans in seconds to limits.
50      *
51      * Default is 3 registrations per hour, 5 per day, 10 per week.
52      */
53     public $regLimits = array(604800 => 10, // per week
54                               86400 => 5, // per day
55                               3600 => 3); // per hour
56
57     /**
58      * Disallow registration if a silenced user has registered from
59      * this IP address.
60      */
61     public $silenced = true;
62
63     /**
64      * Whether we're enabled; prevents recursion.
65      */
66     static private $enabled = true;
67
68     /**
69      * Database schema setup
70      *
71      * We store user registrations in a table registration_ip.
72      *
73      * @return boolean hook value; true means continue processing, false means stop.
74      */
75     public function onCheckSchema()
76     {
77         $schema = Schema::get();
78
79         // For storing user-submitted flags on profiles
80         $schema->ensureTable('registration_ip', Registration_ip::schemaDef());
81         return true;
82     }
83
84     /**
85      * Called when someone tries to register.
86      *
87      * We check the IP here to determine if it goes over any of our
88      * configured limits.
89      *
90      * @param Action $action Action that is being executed
91      *
92      * @return boolean hook value
93      */
94     public function onStartRegistrationTry($action)
95     {
96         $ipaddress = $this->_getIpAddress();
97
98         if (empty($ipaddress)) {
99             // TRANS: Server exception thrown when no IP address can be found for a registation attempt.
100             throw new ServerException(_m('Cannot find IP address.'));
101         }
102
103         foreach ($this->regLimits as $seconds => $limit) {
104
105             $this->debug("Checking $seconds ($limit)");
106
107             $reg = $this->_getNthReg($ipaddress, $limit);
108
109             if (!empty($reg)) {
110                 $this->debug("Got a {$limit}th registration.");
111                 $regtime = strtotime($reg->created);
112                 $now     = time();
113                 $this->debug("Comparing {$regtime} to {$now}");
114                 if ($now - $regtime < $seconds) {
115                     // TRANS: Exception thrown when too many user have registered from one IP address within a given time frame.
116                     throw new Exception(_m('Too many registrations. Take a break and try again later.'));
117                 }
118             }
119         }
120
121         // Check for silenced users
122
123         if ($this->silenced) {
124             $ids = Registration_ip::usersByIP($ipaddress);
125             foreach ($ids as $id) {
126                 $profile = Profile::getKV('id', $id);
127                 if ($profile && $profile->isSilenced()) {
128                     // TRANS: Exception thrown when attempting to register from an IP address from which silenced users have registered.
129                     throw new Exception(_m('A banned user has registered from this address.'));
130                 }
131             }
132         }
133
134         return true;
135     }
136
137     function onEndShowSections(Action $action)
138     {
139         if (!$action instanceof ShowstreamAction) {
140             // early return for actions we're not interested in
141             return true;
142         }
143
144         $scoped = $action->getScoped();
145         if (!$scoped instanceof Profile || !$scoped->hasRight(self::VIEWMODLOG)) {
146             // only continue if we are allowed to VIEWMODLOG
147             return true;
148         }
149
150         $ri = Registration_ip::getKV('user_id', $profile->id);
151         $ipaddress = null;
152         if ($ri instanceof Registration_ip) {
153             $ipaddress = $ri->ipaddress;
154             unset($ri);
155         }
156
157         $action->elementStart('div', array('id' => 'entity_mod_log',
158                                            'class' => 'section'));
159
160         $action->element('h2', null, _('Registration IP'));
161
162         $action->element('strong', null, _('Registered from:'));
163         $action->element('span', ['class'=>'ipaddress'], $ipaddress ?: 'unknown');
164
165         $action->elementEnd('div');
166     }
167
168     /**
169      * Called after someone registers, by any means.
170      *
171      * We record the successful registration and IP address.
172      *
173      * @param Profile $profile new user's profile
174      *
175      * @return boolean hook value
176      */
177     public function onEndUserRegister(Profile $profile)
178     {
179         $ipaddress = $this->_getIpAddress();
180
181         if (empty($ipaddress)) {
182             // User registration can happen from command-line scripts etc.
183             return true;
184         }
185
186         $reg = new Registration_ip();
187
188         $reg->user_id   = $profile->id;
189         $reg->ipaddress = $ipaddress;
190         $reg->created   = common_sql_now();
191
192         $result = $reg->insert();
193
194         if ($result === false) {
195             common_log_db_error($reg, 'INSERT', __FILE__);
196             // @todo throw an exception?
197         }
198
199         return true;
200     }
201
202     /**
203      * Check the version of the plugin.
204      *
205      * @param array &$versions Version array.
206      *
207      * @return boolean hook value
208      */
209     public function onPluginVersion(array &$versions)
210     {
211         $versions[] = array('name' => 'RegisterThrottle',
212                             'version' => GNUSOCIAL_VERSION,
213                             'author' => 'Evan Prodromou',
214                             'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
215                             'description' =>
216                             // TRANS: Plugin description.
217                             _m('Throttles excessive registration from a single IP address.'));
218         return true;
219     }
220
221     /**
222      * Gets the current IP address.
223      *
224      * @return string IP address or null if not found.
225      */
226     private function _getIpAddress()
227     {
228         $keys = array('HTTP_X_FORWARDED_FOR',
229                       'HTTP_X_CLIENT',
230                       'CLIENT-IP',
231                       'REMOTE_ADDR');
232
233         foreach ($keys as $k) {
234             if (!empty($_SERVER[$k])) {
235                 return $_SERVER[$k];
236             }
237         }
238
239         return null;
240     }
241
242     /**
243      * Gets the Nth registration with the given IP address.
244      *
245      * @param string  $ipaddress Address to key on
246      * @param integer $n         Nth address
247      *
248      * @return Registration_ip nth registration or null if not found.
249      */
250     private function _getNthReg($ipaddress, $n)
251     {
252         $reg = new Registration_ip();
253
254         $reg->ipaddress = $ipaddress;
255
256         $reg->orderBy('created DESC');
257         $reg->limit($n - 1, 1);
258
259         if ($reg->find(true)) {
260             return $reg;
261         } else {
262             return null;
263         }
264     }
265
266     /**
267      * When silencing a user, silence all other users registered from that IP
268      * address.
269      *
270      * @param Profile $profile Person getting a new role
271      * @param string  $role    Role being assigned like 'moderator' or 'silenced'
272      *
273      * @return boolean hook value
274      */
275     public function onEndGrantRole($profile, $role)
276     {
277         if (!self::$enabled) {
278             return true;
279         }
280
281         if ($role != Profile_role::SILENCED) {
282             return true;
283         }
284
285         if (!$this->silenced) {
286             return true;
287         }
288
289         $ri = Registration_ip::getKV('user_id', $profile->id);
290
291         if (empty($ri)) {
292             return true;
293         }
294
295         $ids = Registration_ip::usersByIP($ri->ipaddress);
296
297         foreach ($ids as $id) {
298             if ($id == $profile->id) {
299                 continue;
300             }
301
302             $other = Profile::getKV('id', $id);
303
304             if (empty($other)) {
305                 continue;
306             }
307
308             if ($other->isSilenced()) {
309                 continue;
310             }
311
312             $old = self::$enabled;
313             self::$enabled = false;
314             $other->silence();
315             self::$enabled = $old;
316         }
317     }
318 }