Expiration.php 4.6 KB

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