Expiration.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Versions;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\IConfig;
  27. class Expiration {
  28. // how long do we keep files a version if no other value is defined in the config file (unit: days)
  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('versions_retention_obligation', 'auto');
  43. if ($this->retentionObligation !== 'disabled') {
  44. $this->parseRetentionObligation();
  45. }
  46. }
  47. /**
  48. * Is versions expiration enabled
  49. * @return bool
  50. */
  51. public function isEnabled(){
  52. return $this->retentionObligation !== 'disabled';
  53. }
  54. /**
  55. * Is default expiration active
  56. */
  57. public function shouldAutoExpire(){
  58. return $this->minAge === self::NO_OBLIGATION
  59. || $this->maxAge === self::NO_OBLIGATION;
  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. * Get maximal retention obligation as a timestamp
  100. * @return int
  101. */
  102. public function getMaxAgeAsTimestamp(){
  103. $maxAge = false;
  104. if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) {
  105. $time = $this->timeFactory->getTime();
  106. $maxAge = $time - ($this->maxAge * 86400);
  107. }
  108. return $maxAge;
  109. }
  110. /**
  111. * Read versions_retention_obligation, validate it
  112. * and set private members accordingly
  113. */
  114. private function parseRetentionObligation(){
  115. $splitValues = explode(',', $this->retentionObligation);
  116. if (!isset($splitValues[0])) {
  117. $minValue = 'auto';
  118. } else {
  119. $minValue = trim($splitValues[0]);
  120. }
  121. if (!isset($splitValues[1])) {
  122. $maxValue = 'auto';
  123. } else {
  124. $maxValue = trim($splitValues[1]);
  125. }
  126. $isValid = true;
  127. // Validate
  128. if (!ctype_digit($minValue) && $minValue !== 'auto') {
  129. $isValid = false;
  130. \OC::$server->getLogger()->warning(
  131. $minValue . ' is not a valid value for minimal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.',
  132. ['app'=>'files_versions']
  133. );
  134. }
  135. if (!ctype_digit($maxValue) && $maxValue !== 'auto') {
  136. $isValid = false;
  137. \OC::$server->getLogger()->warning(
  138. $maxValue . ' is not a valid value for maximal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.',
  139. ['app'=>'files_versions']
  140. );
  141. }
  142. if (!$isValid){
  143. $minValue = 'auto';
  144. $maxValue = 'auto';
  145. }
  146. if ($minValue === 'auto' && $maxValue === 'auto') {
  147. // Default: Delete anytime if space needed
  148. $this->minAge = self::NO_OBLIGATION;
  149. $this->maxAge = self::NO_OBLIGATION;
  150. $this->canPurgeToSaveSpace = true;
  151. } elseif ($minValue !== 'auto' && $maxValue === 'auto') {
  152. // Keep for X days but delete anytime if space needed
  153. $this->minAge = (int)$minValue;
  154. $this->maxAge = self::NO_OBLIGATION;
  155. $this->canPurgeToSaveSpace = true;
  156. } elseif ($minValue === 'auto' && $maxValue !== 'auto') {
  157. // Delete anytime if space needed, Delete all older than max automatically
  158. $this->minAge = self::NO_OBLIGATION;
  159. $this->maxAge = (int)$maxValue;
  160. $this->canPurgeToSaveSpace = true;
  161. } elseif ($minValue !== 'auto' && $maxValue !== 'auto') {
  162. // Delete all older than max OR older than min if space needed
  163. // Max < Min as per https://github.com/owncloud/core/issues/16301
  164. if ($maxValue < $minValue) {
  165. $maxValue = $minValue;
  166. }
  167. $this->minAge = (int)$minValue;
  168. $this->maxAge = (int)$maxValue;
  169. $this->canPurgeToSaveSpace = false;
  170. }
  171. }
  172. }