Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

apps/files_trashbin/lib/expiration.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Victor Dubiniuk <[email protected]>
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
24
namespace OCA\Files_Trashbin;
25
26
use \OCP\IConfig;
27
use \OCP\AppFramework\Utility\ITimeFactory;
28
29
class Expiration {
30
31
	// how long do we keep files in the trash bin if no other value is defined in the config file (unit: days)
32
	const DEFAULT_RETENTION_OBLIGATION = 30;
33
	const NO_OBLIGATION = -1;
34
35
	/** @var ITimeFactory */
36
	private $timeFactory;
37
38
	/** @var string */
39
	private $retentionObligation;
40
41
	/** @var int */
42
	private $minAge;
43
44
	/** @var int */
45
	private $maxAge;
46
47
	/** @var bool */
48
	private $canPurgeToSaveSpace;
49
50 View Code Duplication
	public function __construct(IConfig $config,ITimeFactory $timeFactory){
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
		$this->timeFactory = $timeFactory;
52
		$this->retentionObligation = $config->getSystemValue('trashbin_retention_obligation', 'auto');
53
54
		if ($this->retentionObligation !== 'disabled') {
55
			$this->parseRetentionObligation();
56
		}
57
	}
58
59
	/**
60
	 * Is trashbin expiration enabled
61
	 * @return bool
62
	 */
63
	public function isEnabled(){
64
		return $this->retentionObligation !== 'disabled';
65
	}
66
67
	/**
68
	 * Check if given timestamp in expiration range
69
	 * @param int $timestamp
70
	 * @param bool $quotaExceeded
71
	 * @return bool
72
	 */
73 View Code Duplication
	public function isExpired($timestamp, $quotaExceeded = false){
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
		// No expiration if disabled
75
		if (!$this->isEnabled()) {
76
			return false;
77
		}
78
79
		// Purge to save space (if allowed)
80
		if ($quotaExceeded && $this->canPurgeToSaveSpace) {
81
			return true;
82
		}
83
84
		$time = $this->timeFactory->getTime();
85
		// Never expire dates in future e.g. misconfiguration or negative time
86
		// adjustment
87
		if ($time<$timestamp) {
88
			return false;
89
		}
90
91
		// Purge as too old
92
		if ($this->maxAge !== self::NO_OBLIGATION) {
93
			$maxTimestamp = $time - ($this->maxAge * 86400);
94
			$isOlderThanMax = $timestamp < $maxTimestamp;
95
		} else {
96
			$isOlderThanMax = false;
97
		}
98
99
		if ($this->minAge !== self::NO_OBLIGATION) {
100
			// older than Min obligation and we are running out of quota?
101
			$minTimestamp = $time - ($this->minAge * 86400);
102
			$isMinReached = ($timestamp < $minTimestamp) && $quotaExceeded;
103
		} else {
104
			$isMinReached = false;
105
		}
106
107
		return $isOlderThanMax || $isMinReached;
108
	}
109
110
	/**
111
	 * @return bool|int
112
	 */
113 View Code Duplication
	public function getMaxAgeAsTimestamp() {
114
		$maxAge = false;
115
		if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) {
116
			$time = $this->timeFactory->getTime();
117
			$maxAge = $time - ($this->maxAge * 86400);
118
		}
119
		return $maxAge;
120
	}
121
122
	private function parseRetentionObligation(){
123
		$splitValues = explode(',', $this->retentionObligation);
124 View Code Duplication
		if (!isset($splitValues[0])) {
125
			$minValue = self::DEFAULT_RETENTION_OBLIGATION;
126
		} else {
127
			$minValue = trim($splitValues[0]);
128
		}
129
130
		if (!isset($splitValues[1]) && $minValue === 'auto') {
131
			$maxValue = 'auto';
132
		} elseif (!isset($splitValues[1])) {
133
			$maxValue = self::DEFAULT_RETENTION_OBLIGATION;
134
		} else {
135
			$maxValue = trim($splitValues[1]);
136
		}
137
138 View Code Duplication
		if ($minValue === 'auto' && $maxValue === 'auto') {
139
			// Default: Keep for 30 days but delete anytime if space needed
140
			$this->minAge = self::DEFAULT_RETENTION_OBLIGATION;
141
			$this->maxAge = self::NO_OBLIGATION;
142
			$this->canPurgeToSaveSpace = true;
143
		} elseif ($minValue !== 'auto' && $maxValue === 'auto') {
144
			// Keep for X days but delete anytime if space needed
145
			$this->minAge = intval($minValue);
146
			$this->maxAge = self::NO_OBLIGATION;
147
			$this->canPurgeToSaveSpace = true;
148
		} elseif ($minValue === 'auto' && $maxValue !== 'auto') {
149
			// Delete anytime if space needed, Delete all older than max automatically
150
			$this->minAge = self::NO_OBLIGATION;
151
			$this->maxAge = intval($maxValue);
152
			$this->canPurgeToSaveSpace = true;
153
		} elseif ($minValue !== 'auto' && $maxValue !== 'auto') {
154
			// Delete all older than max OR older than min if space needed
155
156
			// Max < Min as per https://github.com/owncloud/core/issues/16300
157
			if ($maxValue < $minValue) {
158
				$maxValue = $minValue;
159
			}
160
161
			$this->minAge = intval($minValue);
162
			$this->maxAge = intval($maxValue);
163
			$this->canPurgeToSaveSpace = false;
164
		}
165
	}
166
}
167