Expiration.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Versions;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\IConfig;
  28. use Psr\Log\LoggerInterface;
  29. class Expiration {
  30. // how long do we keep files a version if no other value is defined in the config file (unit: days)
  31. public const NO_OBLIGATION = -1;
  32. /** @var ITimeFactory */
  33. private $timeFactory;
  34. /** @var string */
  35. private $retentionObligation;
  36. /** @var int */
  37. private $minAge;
  38. /** @var int */
  39. private $maxAge;
  40. /** @var bool */
  41. private $canPurgeToSaveSpace;
  42. /** @var LoggerInterface */
  43. private $logger;
  44. public function __construct(IConfig $config, ITimeFactory $timeFactory, LoggerInterface $logger) {
  45. $this->timeFactory = $timeFactory;
  46. $this->logger = $logger;
  47. $this->retentionObligation = $config->getSystemValue('versions_retention_obligation', 'auto');
  48. if ($this->retentionObligation !== 'disabled') {
  49. $this->parseRetentionObligation();
  50. }
  51. }
  52. /**
  53. * Is versions expiration enabled
  54. * @return bool
  55. */
  56. public function isEnabled(): bool {
  57. return $this->retentionObligation !== 'disabled';
  58. }
  59. /**
  60. * Is default expiration active
  61. */
  62. public function shouldAutoExpire(): bool {
  63. return $this->minAge === self::NO_OBLIGATION
  64. || $this->maxAge === self::NO_OBLIGATION;
  65. }
  66. /**
  67. * Check if given timestamp in expiration range
  68. * @param int $timestamp
  69. * @param bool $quotaExceeded
  70. * @return bool
  71. */
  72. public function isExpired(int $timestamp, bool $quotaExceeded = false): bool {
  73. // No expiration if disabled
  74. if (!$this->isEnabled()) {
  75. return false;
  76. }
  77. // Purge to save space (if allowed)
  78. if ($quotaExceeded && $this->canPurgeToSaveSpace) {
  79. return true;
  80. }
  81. $time = $this->timeFactory->getTime();
  82. // Never expire dates in future e.g. misconfiguration or negative time
  83. // adjustment
  84. if ($time < $timestamp) {
  85. return false;
  86. }
  87. // Purge as too old
  88. if ($this->maxAge !== self::NO_OBLIGATION) {
  89. $maxTimestamp = $time - ($this->maxAge * 86400);
  90. $isOlderThanMax = $timestamp < $maxTimestamp;
  91. } else {
  92. $isOlderThanMax = false;
  93. }
  94. if ($this->minAge !== self::NO_OBLIGATION) {
  95. // older than Min obligation and we are running out of quota?
  96. $minTimestamp = $time - ($this->minAge * 86400);
  97. $isMinReached = ($timestamp < $minTimestamp) && $quotaExceeded;
  98. } else {
  99. $isMinReached = false;
  100. }
  101. return $isOlderThanMax || $isMinReached;
  102. }
  103. /**
  104. * Get maximal retention obligation as a timestamp
  105. *
  106. * @return int|false
  107. */
  108. public function getMaxAgeAsTimestamp() {
  109. $maxAge = false;
  110. if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) {
  111. $time = $this->timeFactory->getTime();
  112. $maxAge = $time - ($this->maxAge * 86400);
  113. }
  114. return $maxAge;
  115. }
  116. /**
  117. * Read versions_retention_obligation, validate it
  118. * and set private members accordingly
  119. */
  120. private function parseRetentionObligation(): void {
  121. $splitValues = explode(',', $this->retentionObligation);
  122. if (!isset($splitValues[0])) {
  123. $minValue = 'auto';
  124. } else {
  125. $minValue = trim($splitValues[0]);
  126. }
  127. if (!isset($splitValues[1])) {
  128. $maxValue = 'auto';
  129. } else {
  130. $maxValue = trim($splitValues[1]);
  131. }
  132. $isValid = true;
  133. // Validate
  134. if (!ctype_digit($minValue) && $minValue !== 'auto') {
  135. $isValid = false;
  136. $this->logger->warning(
  137. $minValue . ' is not a valid value for minimal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.',
  138. ['app' => 'files_versions']
  139. );
  140. }
  141. if (!ctype_digit($maxValue) && $maxValue !== 'auto') {
  142. $isValid = false;
  143. $this->logger->warning(
  144. $maxValue . ' is not a valid value for maximal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.',
  145. ['app' => 'files_versions']
  146. );
  147. }
  148. if (!$isValid) {
  149. $minValue = 'auto';
  150. $maxValue = 'auto';
  151. }
  152. if ($minValue === 'auto' && $maxValue === 'auto') {
  153. // Default: Delete anytime if space needed
  154. $this->minAge = self::NO_OBLIGATION;
  155. $this->maxAge = self::NO_OBLIGATION;
  156. $this->canPurgeToSaveSpace = true;
  157. } elseif ($minValue !== 'auto' && $maxValue === 'auto') {
  158. // Keep for X days but delete anytime if space needed
  159. $this->minAge = (int)$minValue;
  160. $this->maxAge = self::NO_OBLIGATION;
  161. $this->canPurgeToSaveSpace = true;
  162. } elseif ($minValue === 'auto' && $maxValue !== 'auto') {
  163. // Delete anytime if space needed, Delete all older than max automatically
  164. $this->minAge = self::NO_OBLIGATION;
  165. $this->maxAge = (int)$maxValue;
  166. $this->canPurgeToSaveSpace = true;
  167. } elseif ($minValue !== 'auto' && $maxValue !== 'auto') {
  168. // Delete all older than max OR older than min if space needed
  169. // Max < Min as per https://github.com/owncloud/core/issues/16301
  170. if ($maxValue < $minValue) {
  171. $maxValue = $minValue;
  172. }
  173. $this->minAge = (int)$minValue;
  174. $this->maxAge = (int)$maxValue;
  175. $this->canPurgeToSaveSpace = false;
  176. }
  177. }
  178. }