1
0

Expiration.php 4.9 KB

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