1
0

PurgeLimiter.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.3.3
  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. */
  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. */
  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) {
  64. return true;
  65. }
  66. $now = time();
  67. $file = 'purge_limiter.php';
  68. if (self::_exists($file)) {
  69. require self::getPath($file);
  70. $pl = $GLOBALS['purge_limiter'];
  71. if ($pl + self::$_limit >= $now) {
  72. return false;
  73. }
  74. }
  75. $content = '<?php' . PHP_EOL . '$GLOBALS[\'purge_limiter\'] = ' . $now . ';';
  76. self::_store($file, $content);
  77. return true;
  78. }
  79. }