trafficlimiter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  13. * trafficlimiter
  14. *
  15. * Handles traffic limiting, so no user does more than one call per 10 seconds.
  16. */
  17. class trafficlimiter extends persistence
  18. {
  19. /**
  20. * time limit in seconds, defaults to 10s
  21. *
  22. * @access private
  23. * @static
  24. * @var int
  25. */
  26. private static $_limit = 10;
  27. /**
  28. * key to fetch IP address
  29. *
  30. * @access private
  31. * @static
  32. * @var string
  33. */
  34. private static $_ipKey = 'REMOTE_ADDR';
  35. /**
  36. * set the time limit in seconds
  37. *
  38. * @access public
  39. * @static
  40. * @param int $limit
  41. * @return void
  42. */
  43. public static function setLimit($limit)
  44. {
  45. self::$_limit = $limit;
  46. }
  47. /**
  48. * set configuration options of the traffic limiter
  49. *
  50. * @access public
  51. * @static
  52. * @param configuration $conf
  53. * @return void
  54. */
  55. public static function setConfiguration(configuration $conf)
  56. {
  57. self::setLimit($conf->getKey('limit', 'traffic'));
  58. self::setPath($conf->getKey('dir', 'traffic'));
  59. if (($option = $conf->getKey('header', 'traffic')) !== null)
  60. {
  61. $httpHeader = 'HTTP_' . $option;
  62. if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader]))
  63. {
  64. self::$_ipKey = $httpHeader;
  65. }
  66. }
  67. }
  68. /**
  69. * get the current visitors IP address
  70. *
  71. * @access public
  72. * @static
  73. * @return string
  74. */
  75. public static function getIp()
  76. {
  77. return $_SERVER[self::$_ipKey];
  78. }
  79. /**
  80. * traffic limiter
  81. *
  82. * Make sure the IP address makes at most 1 request every 10 seconds.
  83. *
  84. * @access public
  85. * @static
  86. * @throws Exception
  87. * @return bool
  88. */
  89. public static function canPass()
  90. {
  91. // disable limits if set to less then 1
  92. if (self::$_limit < 1) return true;
  93. $ip = hash_hmac('sha256', self::getIp(), serversalt::get());
  94. $file = 'traffic_limiter.php';
  95. if (!self::_exists($file))
  96. {
  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. {
  110. if ($time + self::$_limit < $now)
  111. {
  112. unset($tl[$key]);
  113. }
  114. }
  115. if (array_key_exists($ip, $tl) && ($tl[$ip] + self::$_limit >= $now))
  116. {
  117. $result = false;
  118. } else {
  119. $tl[$ip] = time();
  120. $result = true;
  121. }
  122. self::_store(
  123. $file,
  124. '<?php' . PHP_EOL .
  125. '$GLOBALS[\'traffic_limiter\'] = ' .
  126. var_export($tl, true) . ';' . PHP_EOL
  127. );
  128. return $result;
  129. }
  130. }