RegisterThrottlePlugin.php 9.9 KB

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