trafficlimiter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. {
  62. $httpHeader = 'HTTP_' . $option;
  63. if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader]))
  64. {
  65. self::$_ipKey = $httpHeader;
  66. }
  67. }
  68. }
  69. /**
  70. * get the current visitors IP address
  71. *
  72. * @access public
  73. * @static
  74. * @return string
  75. */
  76. public static function getIp()
  77. {
  78. return $_SERVER[self::$_ipKey];
  79. }
  80. /**
  81. * traffic limiter
  82. *
  83. * Make sure the IP address makes at most 1 request every 10 seconds.
  84. *
  85. * @access public
  86. * @static
  87. * @throws Exception
  88. * @return bool
  89. */
  90. public static function canPass()
  91. {
  92. // disable limits if set to less then 1
  93. if (self::$_limit < 1) return true;
  94. $ip = hash_hmac('sha256', self::getIp(), serversalt::get());
  95. $file = 'traffic_limiter.php';
  96. if (!self::_exists($file))
  97. {
  98. self::_store(
  99. $file,
  100. '<?php' . PHP_EOL .
  101. '$GLOBALS[\'traffic_limiter\'] = array();' . PHP_EOL
  102. );
  103. }
  104. $path = self::getPath($file);
  105. require $path;
  106. $now = time();
  107. $tl = $GLOBALS['traffic_limiter'];
  108. // purge file of expired IPs to keep it small
  109. foreach ($tl as $key => $time)
  110. {
  111. if ($time + self::$_limit < $now)
  112. {
  113. unset($tl[$key]);
  114. }
  115. }
  116. if (array_key_exists($ip, $tl) && ($tl[$ip] + self::$_limit >= $now))
  117. {
  118. $result = false;
  119. } else {
  120. $tl[$ip] = time();
  121. $result = true;
  122. }
  123. self::_store(
  124. $file,
  125. '<?php' . PHP_EOL .
  126. '$GLOBALS[\'traffic_limiter\'] = ' .
  127. var_export($tl, true) . ';' . PHP_EOL
  128. );
  129. return $result;
  130. }
  131. }