1
0

purgelimiter.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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) return true;
  65. $file = 'purge_limiter.php';
  66. $now = time();
  67. if (!self::_exists($file))
  68. {
  69. self::_store(
  70. $file,
  71. '<?php' . PHP_EOL .
  72. '$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
  73. );
  74. }
  75. $path = self::getPath($file);
  76. require $path;
  77. $pl = $GLOBALS['purge_limiter'];
  78. if ($pl + self::$_limit >= $now)
  79. {
  80. $result = false;
  81. }
  82. else
  83. {
  84. $result = true;
  85. self::_store(
  86. $file,
  87. '<?php' . PHP_EOL .
  88. '$GLOBALS[\'purge_limiter\'] = ' . $now . ';' . PHP_EOL
  89. );
  90. }
  91. return $result;
  92. }
  93. }