Expiration.php 5.4 KB

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