RegisterThrottlePlugin.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /**
  77. * Called when someone tries to register.
  78. *
  79. * We check the IP here to determine if it goes over any of our
  80. * configured limits.
  81. *
  82. * @param Action $action Action that is being executed
  83. *
  84. * @return boolean hook value
  85. */
  86. public function onStartRegistrationTry($action)
  87. {
  88. $ipaddress = $this->_getIpAddress();
  89. if (empty($ipaddress)) {
  90. // TRANS: Server exception thrown when no IP address can be found for a registation attempt.
  91. throw new ServerException(_m('Cannot find IP address.'));
  92. }
  93. foreach ($this->regLimits as $seconds => $limit) {
  94. $this->debug("Checking $seconds ($limit)");
  95. $reg = $this->_getNthReg($ipaddress, $limit);
  96. if (!empty($reg)) {
  97. $this->debug("Got a {$limit}th registration.");
  98. $regtime = strtotime($reg->created);
  99. $now = time();
  100. $this->debug("Comparing {$regtime} to {$now}");
  101. if ($now - $regtime < $seconds) {
  102. // TRANS: Exception thrown when too many user have registered from one IP address within a given time frame.
  103. throw new Exception(_m('Too many registrations. Take a break and try again later.'));
  104. }
  105. }
  106. }
  107. // Check for silenced users
  108. if ($this->silenced) {
  109. $ids = Registration_ip::usersByIP($ipaddress);
  110. foreach ($ids as $id) {
  111. $profile = Profile::getKV('id', $id);
  112. if ($profile && $profile->isSilenced()) {
  113. // TRANS: Exception thrown when attempting to register from an IP address from which silenced users have registered.
  114. throw new Exception(_m('A banned user has registered from this address.'));
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120. /**
  121. * Called after someone registers, by any means.
  122. *
  123. * We record the successful registration and IP address.
  124. *
  125. * @param Profile $profile new user's profile
  126. *
  127. * @return boolean hook value
  128. */
  129. public function onEndUserRegister(Profile $profile)
  130. {
  131. $ipaddress = $this->_getIpAddress();
  132. if (empty($ipaddress)) {
  133. // User registration can happen from command-line scripts etc.
  134. return true;
  135. }
  136. $reg = new Registration_ip();
  137. $reg->user_id = $profile->id;
  138. $reg->ipaddress = $ipaddress;
  139. $reg->created = common_sql_now();
  140. $result = $reg->insert();
  141. if ($result === false) {
  142. common_log_db_error($reg, 'INSERT', __FILE__);
  143. // @todo throw an exception?
  144. }
  145. return true;
  146. }
  147. /**
  148. * Check the version of the plugin.
  149. *
  150. * @param array &$versions Version array.
  151. *
  152. * @return boolean hook value
  153. */
  154. public function onPluginVersion(&$versions)
  155. {
  156. $versions[] = array('name' => 'RegisterThrottle',
  157. 'version' => GNUSOCIAL_VERSION,
  158. 'author' => 'Evan Prodromou',
  159. 'homepage' => 'http://status.net/wiki/Plugin:RegisterThrottle',
  160. 'description' =>
  161. // TRANS: Plugin description.
  162. _m('Throttles excessive registration from a single IP address.'));
  163. return true;
  164. }
  165. /**
  166. * Gets the current IP address.
  167. *
  168. * @return string IP address or null if not found.
  169. */
  170. private function _getIpAddress()
  171. {
  172. $keys = array('HTTP_X_FORWARDED_FOR',
  173. 'HTTP_X_CLIENT',
  174. 'CLIENT-IP',
  175. 'REMOTE_ADDR');
  176. foreach ($keys as $k) {
  177. if (!empty($_SERVER[$k])) {
  178. return $_SERVER[$k];
  179. }
  180. }
  181. return null;
  182. }
  183. /**
  184. * Gets the Nth registration with the given IP address.
  185. *
  186. * @param string $ipaddress Address to key on
  187. * @param integer $n Nth address
  188. *
  189. * @return Registration_ip nth registration or null if not found.
  190. */
  191. private function _getNthReg($ipaddress, $n)
  192. {
  193. $reg = new Registration_ip();
  194. $reg->ipaddress = $ipaddress;
  195. $reg->orderBy('created DESC');
  196. $reg->limit($n - 1, 1);
  197. if ($reg->find(true)) {
  198. return $reg;
  199. } else {
  200. return null;
  201. }
  202. }
  203. /**
  204. * When silencing a user, silence all other users registered from that IP
  205. * address.
  206. *
  207. * @param Profile $profile Person getting a new role
  208. * @param string $role Role being assigned like 'moderator' or 'silenced'
  209. *
  210. * @return boolean hook value
  211. */
  212. public function onEndGrantRole($profile, $role)
  213. {
  214. if (!self::$enabled) {
  215. return true;
  216. }
  217. if ($role != Profile_role::SILENCED) {
  218. return true;
  219. }
  220. if (!$this->silenced) {
  221. return true;
  222. }
  223. $ri = Registration_ip::getKV('user_id', $profile->id);
  224. if (empty($ri)) {
  225. return true;
  226. }
  227. $ids = Registration_ip::usersByIP($ri->ipaddress);
  228. foreach ($ids as $id) {
  229. if ($id == $profile->id) {
  230. continue;
  231. }
  232. $other = Profile::getKV('id', $id);
  233. if (empty($other)) {
  234. continue;
  235. }
  236. if ($other->isSilenced()) {
  237. continue;
  238. }
  239. $old = self::$enabled;
  240. self::$enabled = false;
  241. $other->silence();
  242. self::$enabled = $old;
  243. }
  244. }
  245. }