trafficlimiter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.22
  11. */
  12. namespace PrivateBin;
  13. /**
  14. * trafficlimiter
  15. *
  16. * Handles traffic limiting, so no user does more than one call per 10 seconds.
  17. */
  18. class trafficlimiter extends persistence
  19. {
  20. /**
  21. * time limit in seconds, defaults to 10s
  22. *
  23. * @access private
  24. * @static
  25. * @var int
  26. */
  27. private static $_limit = 10;
  28. /**
  29. * key to fetch IP address
  30. *
  31. * @access private
  32. * @static
  33. * @var string
  34. */
  35. private static $_ipKey = 'REMOTE_ADDR';
  36. /**
  37. * set the time limit in seconds
  38. *
  39. * @access public
  40. * @static
  41. * @param int $limit
  42. * @return void
  43. */
  44. public static function setLimit($limit)
  45. {
  46. self::$_limit = $limit;
  47. }
  48. /**
  49. * set configuration options of the traffic limiter
  50. *
  51. * @access public
  52. * @static
  53. * @param configuration $conf
  54. * @return void
  55. */
  56. public static function setConfiguration(configuration $conf)
  57. {
  58. self::setLimit($conf->getKey('limit', 'traffic'));
  59. self::setPath($conf->getKey('dir', 'traffic'));
  60. if (($option = $conf->getKey('header', 'traffic')) !== null) {
  61. $httpHeader = 'HTTP_' . $option;
  62. if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader])) {
  63. self::$_ipKey = $httpHeader;
  64. }
  65. }
  66. }
  67. /**
  68. * get the current visitors IP address
  69. *
  70. * @access public
  71. * @static
  72. * @return string
  73. */
  74. public static function getIp()
  75. {
  76. return $_SERVER[self::$_ipKey];
  77. }
  78. /**
  79. * traffic limiter
  80. *
  81. * Make sure the IP address makes at most 1 request every 10 seconds.
  82. *
  83. * @access public
  84. * @static
  85. * @throws Exception
  86. * @return bool
  87. */
  88. public static function canPass()
  89. {
  90. // disable limits if set to less then 1
  91. if (self::$_limit < 1) {
  92. return true;
  93. }
  94. $ip = hash_hmac('sha256', self::getIp(), serversalt::get());
  95. $file = 'traffic_limiter.php';
  96. if (!self::_exists($file)) {
  97. self::_store(
  98. $file,
  99. '<?php' . PHP_EOL .
  100. '$GLOBALS[\'traffic_limiter\'] = array();' . PHP_EOL
  101. );
  102. }
  103. $path = self::getPath($file);
  104. require $path;
  105. $now = time();
  106. $tl = $GLOBALS['traffic_limiter'];
  107. // purge file of expired IPs to keep it small
  108. foreach ($tl as $key => $time) {
  109. if ($time + self::$_limit < $now) {
  110. unset($tl[$key]);
  111. }
  112. }
  113. if (array_key_exists($ip, $tl) && ($tl[$ip] + self::$_limit >= $now)) {
  114. $result = false;
  115. } else {
  116. $tl[$ip] = time();
  117. $result = true;
  118. }
  119. self::_store(
  120. $file,
  121. '<?php' . PHP_EOL .
  122. '$GLOBALS[\'traffic_limiter\'] = ' .
  123. var_export($tl, true) . ';' . PHP_EOL
  124. );
  125. return $result;
  126. }
  127. }