Expiration.php 4.9 KB

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