Expiration.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\IConfig;
  10. use Psr\Log\LoggerInterface;
  11. class Expiration {
  12. // how long do we keep files a version if no other value is defined in the config file (unit: days)
  13. public const NO_OBLIGATION = -1;
  14. /** @var ITimeFactory */
  15. private $timeFactory;
  16. /** @var string */
  17. private $retentionObligation;
  18. /** @var int */
  19. private $minAge;
  20. /** @var int */
  21. private $maxAge;
  22. /** @var bool */
  23. private $canPurgeToSaveSpace;
  24. /** @var LoggerInterface */
  25. private $logger;
  26. public function __construct(IConfig $config, ITimeFactory $timeFactory, LoggerInterface $logger) {
  27. $this->timeFactory = $timeFactory;
  28. $this->logger = $logger;
  29. $this->retentionObligation = $config->getSystemValue('versions_retention_obligation', 'auto');
  30. if ($this->retentionObligation !== 'disabled') {
  31. $this->parseRetentionObligation();
  32. }
  33. }
  34. /**
  35. * Is versions expiration enabled
  36. * @return bool
  37. */
  38. public function isEnabled(): bool {
  39. return $this->retentionObligation !== 'disabled';
  40. }
  41. /**
  42. * Is default expiration active
  43. */
  44. public function shouldAutoExpire(): bool {
  45. return $this->minAge === self::NO_OBLIGATION
  46. || $this->maxAge === self::NO_OBLIGATION;
  47. }
  48. /**
  49. * Check if given timestamp in expiration range
  50. * @param int $timestamp
  51. * @param bool $quotaExceeded
  52. * @return bool
  53. */
  54. public function isExpired(int $timestamp, bool $quotaExceeded = false): bool {
  55. // No expiration if disabled
  56. if (!$this->isEnabled()) {
  57. return false;
  58. }
  59. // Purge to save space (if allowed)
  60. if ($quotaExceeded && $this->canPurgeToSaveSpace) {
  61. return true;
  62. }
  63. $time = $this->timeFactory->getTime();
  64. // Never expire dates in future e.g. misconfiguration or negative time
  65. // adjustment
  66. if ($time < $timestamp) {
  67. return false;
  68. }
  69. // Purge as too old
  70. if ($this->maxAge !== self::NO_OBLIGATION) {
  71. $maxTimestamp = $time - ($this->maxAge * 86400);
  72. $isOlderThanMax = $timestamp < $maxTimestamp;
  73. } else {
  74. $isOlderThanMax = false;
  75. }
  76. if ($this->minAge !== self::NO_OBLIGATION) {
  77. // older than Min obligation and we are running out of quota?
  78. $minTimestamp = $time - ($this->minAge * 86400);
  79. $isMinReached = ($timestamp < $minTimestamp) && $quotaExceeded;
  80. } else {
  81. $isMinReached = false;
  82. }
  83. return $isOlderThanMax || $isMinReached;
  84. }
  85. /**
  86. * Get maximal retention obligation as a timestamp
  87. *
  88. * @return int|false
  89. */
  90. public function getMaxAgeAsTimestamp() {
  91. $maxAge = false;
  92. if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) {
  93. $time = $this->timeFactory->getTime();
  94. $maxAge = $time - ($this->maxAge * 86400);
  95. }
  96. return $maxAge;
  97. }
  98. /**
  99. * Read versions_retention_obligation, validate it
  100. * and set private members accordingly
  101. */
  102. private function parseRetentionObligation(): void {
  103. $splitValues = explode(',', $this->retentionObligation);
  104. if (!isset($splitValues[0])) {
  105. $minValue = 'auto';
  106. } else {
  107. $minValue = trim($splitValues[0]);
  108. }
  109. if (!isset($splitValues[1])) {
  110. $maxValue = 'auto';
  111. } else {
  112. $maxValue = trim($splitValues[1]);
  113. }
  114. $isValid = true;
  115. // Validate
  116. if (!ctype_digit($minValue) && $minValue !== 'auto') {
  117. $isValid = false;
  118. $this->logger->warning(
  119. $minValue . ' is not a valid value for minimal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.',
  120. ['app' => 'files_versions']
  121. );
  122. }
  123. if (!ctype_digit($maxValue) && $maxValue !== 'auto') {
  124. $isValid = false;
  125. $this->logger->warning(
  126. $maxValue . ' is not a valid value for maximal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.',
  127. ['app' => 'files_versions']
  128. );
  129. }
  130. if (!$isValid) {
  131. $minValue = 'auto';
  132. $maxValue = 'auto';
  133. }
  134. if ($minValue === 'auto' && $maxValue === 'auto') {
  135. // Default: Delete anytime if space needed
  136. $this->minAge = self::NO_OBLIGATION;
  137. $this->maxAge = self::NO_OBLIGATION;
  138. $this->canPurgeToSaveSpace = true;
  139. } elseif ($minValue !== 'auto' && $maxValue === 'auto') {
  140. // Keep for X days but delete anytime if space needed
  141. $this->minAge = (int)$minValue;
  142. $this->maxAge = self::NO_OBLIGATION;
  143. $this->canPurgeToSaveSpace = true;
  144. } elseif ($minValue === 'auto' && $maxValue !== 'auto') {
  145. // Delete anytime if space needed, Delete all older than max automatically
  146. $this->minAge = self::NO_OBLIGATION;
  147. $this->maxAge = (int)$maxValue;
  148. $this->canPurgeToSaveSpace = true;
  149. } elseif ($minValue !== 'auto' && $maxValue !== 'auto') {
  150. // Delete all older than max OR older than min if space needed
  151. // Max < Min as per https://github.com/owncloud/core/issues/16301
  152. if ($maxValue < $minValue) {
  153. $maxValue = $minValue;
  154. }
  155. $this->minAge = (int)$minValue;
  156. $this->maxAge = (int)$maxValue;
  157. $this->canPurgeToSaveSpace = false;
  158. }
  159. }
  160. }