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