purgelimiter.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. * purgelimiter
  14. *
  15. * Handles purge limiting, so purging is not triggered to often.
  16. */
  17. class purgelimiter extends persistence
  18. {
  19. /**
  20. * time limit in seconds, defaults to 300s
  21. *
  22. * @access private
  23. * @static
  24. * @var int
  25. */
  26. private static $_limit = 300;
  27. /**
  28. * set the time limit in seconds
  29. *
  30. * @access public
  31. * @static
  32. * @param int $limit
  33. * @return void
  34. */
  35. public static function setLimit($limit)
  36. {
  37. self::$_limit = $limit;
  38. }
  39. /**
  40. * set configuration options of the traffic limiter
  41. *
  42. * @access public
  43. * @static
  44. * @param configuration $conf
  45. * @return void
  46. */
  47. public static function setConfiguration(configuration $conf)
  48. {
  49. self::setLimit($conf->getKey('limit', 'purge'));
  50. self::setPath($conf->getKey('dir', 'purge'));
  51. }
  52. /**
  53. * check if the purge can be performed
  54. *
  55. * @access public
  56. * @static
  57. * @throws Exception
  58. * @return bool
  59. */
  60. public static function canPurge()
  61. {
  62. // disable limits if set to less then 1
  63. if (self::$_limit < 1) return true;
  64. $file = 'purge_limiter.php';
  65. $now = time();
  66. if (!self::_exists($file))
  67. {
  68. self::_store(
  69. $file,
  70. '<?php' . PHP_EOL .
  71. '$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
  72. );
  73. }
  74. $path = self::getPath($file);
  75. require $path;
  76. $pl = $GLOBALS['purge_limiter'];
  77. if ($pl + self::$_limit >= $now)
  78. {
  79. $result = false;
  80. }
  81. else
  82. {
  83. $result = true;
  84. self::_store(
  85. $file,
  86. '<?php' . PHP_EOL .
  87. '$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
  88. );
  89. }
  90. return $result;
  91. }
  92. }