expiration.php 4.6 KB

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