2015-08-17 14:40:03 -04:00
< ? php
2025-06-30 09:04:05 -04:00
2015-08-17 14:40:03 -04:00
/**
2024-06-02 09:26:54 -04:00
* SPDX - FileCopyrightText : 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2015-08-17 14:40:03 -04:00
*/
namespace OCA\Files_Versions ;
2019-11-22 14:52:10 -05:00
use OCP\AppFramework\Utility\ITimeFactory ;
use OCP\IConfig ;
2021-07-14 08:48:23 -04:00
use Psr\Log\LoggerInterface ;
2015-08-17 14:40:03 -04:00
class Expiration {
// how long do we keep files a version if no other value is defined in the config file (unit: days)
2020-04-10 10:54:27 -04:00
public const NO_OBLIGATION = - 1 ;
2015-08-17 14:40:03 -04:00
/** @var string */
private $retentionObligation ;
/** @var int */
private $minAge ;
/** @var int */
private $maxAge ;
/** @var bool */
private $canPurgeToSaveSpace ;
2024-10-18 06:04:22 -04:00
public function __construct (
IConfig $config ,
private ITimeFactory $timeFactory ,
private LoggerInterface $logger ,
) {
2015-08-17 14:40:03 -04:00
$this -> retentionObligation = $config -> getSystemValue ( 'versions_retention_obligation' , 'auto' );
if ( $this -> retentionObligation !== 'disabled' ) {
$this -> parseRetentionObligation ();
}
}
/**
* Is versions expiration enabled
* @ return bool
*/
2021-07-14 08:48:23 -04:00
public function isEnabled () : bool {
2015-08-17 14:40:03 -04:00
return $this -> retentionObligation !== 'disabled' ;
}
/**
* Is default expiration active
*/
2021-07-14 08:48:23 -04:00
public function shouldAutoExpire () : bool {
2015-08-17 14:40:03 -04:00
return $this -> minAge === self :: NO_OBLIGATION
|| $this -> maxAge === self :: NO_OBLIGATION ;
}
/**
* Check if given timestamp in expiration range
* @ param int $timestamp
* @ param bool $quotaExceeded
* @ return bool
*/
2021-07-14 08:48:23 -04:00
public function isExpired ( int $timestamp , bool $quotaExceeded = false ) : bool {
2015-08-17 14:40:03 -04:00
// No expiration if disabled
if ( ! $this -> isEnabled ()) {
return false ;
}
// Purge to save space (if allowed)
if ( $quotaExceeded && $this -> canPurgeToSaveSpace ) {
return true ;
}
$time = $this -> timeFactory -> getTime ();
// Never expire dates in future e.g. misconfiguration or negative time
// adjustment
2020-10-05 09:12:57 -04:00
if ( $time < $timestamp ) {
2015-08-17 14:40:03 -04:00
return false ;
}
// Purge as too old
if ( $this -> maxAge !== self :: NO_OBLIGATION ) {
$maxTimestamp = $time - ( $this -> maxAge * 86400 );
$isOlderThanMax = $timestamp < $maxTimestamp ;
} else {
$isOlderThanMax = false ;
}
if ( $this -> minAge !== self :: NO_OBLIGATION ) {
// older than Min obligation and we are running out of quota?
$minTimestamp = $time - ( $this -> minAge * 86400 );
$isMinReached = ( $timestamp < $minTimestamp ) && $quotaExceeded ;
} else {
$isMinReached = false ;
}
return $isOlderThanMax || $isMinReached ;
}
2025-02-19 05:55:56 -05:00
/**
* Get minimal retention obligation as a timestamp
*
* @ return int | false
*/
public function getMinAgeAsTimestamp () {
$minAge = false ;
if ( $this -> isEnabled () && $this -> minAge !== self :: NO_OBLIGATION ) {
$time = $this -> timeFactory -> getTime ();
$minAge = $time - ( $this -> minAge * 86400 );
}
return $minAge ;
}
2015-08-31 16:52:00 -04:00
/**
* Get maximal retention obligation as a timestamp
2021-07-14 08:48:23 -04:00
*
* @ return int | false
2015-08-31 16:52:00 -04:00
*/
2020-04-09 07:53:40 -04:00
public function getMaxAgeAsTimestamp () {
2015-08-31 16:52:00 -04:00
$maxAge = false ;
if ( $this -> isEnabled () && $this -> maxAge !== self :: NO_OBLIGATION ) {
$time = $this -> timeFactory -> getTime ();
$maxAge = $time - ( $this -> maxAge * 86400 );
}
return $maxAge ;
}
2015-09-11 11:17:24 -04:00
/**
2020-04-09 10:09:23 -04:00
* Read versions_retention_obligation , validate it
2020-04-08 16:24:54 -04:00
* and set private members accordingly
*/
2021-07-14 08:48:23 -04:00
private function parseRetentionObligation () : void {
2015-08-17 14:40:03 -04:00
$splitValues = explode ( ',' , $this -> retentionObligation );
if ( ! isset ( $splitValues [ 0 ])) {
2015-08-20 11:32:41 -04:00
$minValue = 'auto' ;
2015-08-17 14:40:03 -04:00
} else {
$minValue = trim ( $splitValues [ 0 ]);
}
2015-08-20 11:32:41 -04:00
if ( ! isset ( $splitValues [ 1 ])) {
2015-10-05 13:52:25 -04:00
$maxValue = 'auto' ;
2015-08-17 14:40:03 -04:00
} else {
$maxValue = trim ( $splitValues [ 1 ]);
}
2015-08-20 11:32:41 -04:00
$isValid = true ;
// Validate
if ( ! ctype_digit ( $minValue ) && $minValue !== 'auto' ) {
$isValid = false ;
2021-07-14 08:48:23 -04:00
$this -> logger -> warning (
2015-08-20 11:32:41 -04:00
$minValue . ' is not a valid value for minimal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.' ,
2020-10-05 09:12:57 -04:00
[ 'app' => 'files_versions' ]
2015-08-20 11:32:41 -04:00
);
}
if ( ! ctype_digit ( $maxValue ) && $maxValue !== 'auto' ) {
$isValid = false ;
2021-07-14 08:48:23 -04:00
$this -> logger -> warning (
2015-08-20 11:32:41 -04:00
$maxValue . ' is not a valid value for maximal versions retention obligation. Check versions_retention_obligation in your config.php. Falling back to auto.' ,
2020-10-05 09:12:57 -04:00
[ 'app' => 'files_versions' ]
2015-08-20 11:32:41 -04:00
);
}
if ( ! $isValid ) {
$minValue = 'auto' ;
$maxValue = 'auto' ;
}
2015-08-17 14:40:03 -04:00
if ( $minValue === 'auto' && $maxValue === 'auto' ) {
2015-08-20 11:32:41 -04:00
// Default: Delete anytime if space needed
$this -> minAge = self :: NO_OBLIGATION ;
2015-08-17 14:40:03 -04:00
$this -> maxAge = self :: NO_OBLIGATION ;
$this -> canPurgeToSaveSpace = true ;
} elseif ( $minValue !== 'auto' && $maxValue === 'auto' ) {
// Keep for X days but delete anytime if space needed
2018-01-25 17:06:53 -05:00
$this -> minAge = ( int ) $minValue ;
2015-08-17 14:40:03 -04:00
$this -> maxAge = self :: NO_OBLIGATION ;
$this -> canPurgeToSaveSpace = true ;
} elseif ( $minValue === 'auto' && $maxValue !== 'auto' ) {
// Delete anytime if space needed, Delete all older than max automatically
$this -> minAge = self :: NO_OBLIGATION ;
2018-01-25 17:06:53 -05:00
$this -> maxAge = ( int ) $maxValue ;
2015-08-17 14:40:03 -04:00
$this -> canPurgeToSaveSpace = true ;
} elseif ( $minValue !== 'auto' && $maxValue !== 'auto' ) {
// Delete all older than max OR older than min if space needed
// Max < Min as per https://github.com/owncloud/core/issues/16301
if ( $maxValue < $minValue ) {
$maxValue = $minValue ;
}
2018-01-25 17:06:53 -05:00
$this -> minAge = ( int ) $minValue ;
$this -> maxAge = ( int ) $maxValue ;
2015-08-17 14:40:03 -04:00
$this -> canPurgeToSaveSpace = false ;
}
}
}