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