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