PurgeLimiter.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.5
  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. }
  51. /**
  52. * check if the purge can be performed
  53. *
  54. * @access public
  55. * @static
  56. * @return bool
  57. */
  58. public static function canPurge()
  59. {
  60. // disable limits if set to less then 1
  61. if (self::$_limit < 1) {
  62. return true;
  63. }
  64. $now = time();
  65. $pl = (int) self::$_store->getValue('purge_limiter');
  66. if ($pl + self::$_limit >= $now) {
  67. return false;
  68. }
  69. $hasStored = self::$_store->setValue((string) $now, 'purge_limiter');
  70. if (!$hasStored) {
  71. error_log('failed to store the purge limiter, skipping purge cycle to avoid getting stuck in a purge loop');
  72. }
  73. return $hasStored;
  74. }
  75. }