PurgeLimiter.php 2.1 KB

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