|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Lukas Reschke <[email protected]> |
|
4
|
|
|
* @author Victor Dubiniuk <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
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_Trashbin\BackgroundJob; |
|
24
|
|
|
|
|
25
|
|
|
use OCP\IConfig; |
|
26
|
|
|
use OCP\IUserManager; |
|
27
|
|
|
use OCA\Files_Trashbin\AppInfo\Application; |
|
28
|
|
|
use OCA\Files_Trashbin\Expiration; |
|
29
|
|
|
use OCA\Files_Trashbin\Helper; |
|
30
|
|
|
use OCA\Files_Trashbin\Trashbin; |
|
31
|
|
|
|
|
32
|
|
|
class ExpireTrash extends \OC\BackgroundJob\TimedJob { |
|
33
|
|
|
|
|
34
|
|
|
const ITEMS_PER_SESSION = 1000; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Expiration |
|
38
|
|
|
*/ |
|
39
|
|
|
private $expiration; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var IConfig |
|
43
|
|
|
*/ |
|
44
|
|
|
private $config; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var IUserManager |
|
48
|
|
|
*/ |
|
49
|
|
|
private $userManager; |
|
50
|
|
|
|
|
51
|
|
|
const USERS_PER_SESSION = 1000; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param IConfig|null $config |
|
55
|
|
|
* @param IUserManager|null $userManager |
|
56
|
|
|
* @param Expiration|null $expiration |
|
57
|
|
|
*/ |
|
58
|
20 |
|
public function __construct(IConfig $config = null, |
|
59
|
|
|
IUserManager $userManager = null, |
|
60
|
|
|
Expiration $expiration = null) { |
|
61
|
|
|
// Run once per 30 minutes |
|
62
|
20 |
|
$this->setInterval(60 * 30); |
|
63
|
|
|
|
|
64
|
20 |
|
if (is_null($expiration) || is_null($userManager) || is_null($config)) { |
|
65
|
19 |
|
$this->fixDIForJobs(); |
|
66
|
19 |
|
} else { |
|
67
|
1 |
|
$this->config = $config; |
|
68
|
1 |
|
$this->userManager = $userManager; |
|
69
|
1 |
|
$this->expiration = $expiration; |
|
70
|
|
|
} |
|
71
|
20 |
|
} |
|
72
|
|
|
|
|
73
|
19 |
|
protected function fixDIForJobs() { |
|
74
|
19 |
|
$application = new Application(); |
|
75
|
19 |
|
$this->config = \OC::$server->getConfig(); |
|
76
|
19 |
|
$this->userManager = \OC::$server->getUserManager(); |
|
77
|
19 |
|
$this->expiration = $application->getContainer()->query('Expiration'); |
|
78
|
19 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $argument |
|
82
|
|
|
* @throws \Exception |
|
83
|
|
|
*/ |
|
84
|
1 |
|
protected function run($argument) { |
|
85
|
1 |
|
$maxAge = $this->expiration->getMaxAgeAsTimestamp(); |
|
86
|
1 |
|
if (!$maxAge) { |
|
|
|
|
|
|
87
|
1 |
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$offset = $this->config->getAppValue('files_trashbin', 'cronjob_user_offset', 0); |
|
91
|
|
|
$users = $this->userManager->search('', self::USERS_PER_SESSION, $offset); |
|
92
|
|
|
if (!count($users)) { |
|
93
|
|
|
// No users found, reset offset and retry |
|
94
|
|
|
$offset = 0; |
|
95
|
|
|
$users = $this->userManager->search('', self::USERS_PER_SESSION); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$offset += self::USERS_PER_SESSION; |
|
99
|
|
|
$this->config->setAppValue('files_trashbin', 'cronjob_user_offset', $offset); |
|
100
|
|
|
|
|
101
|
|
|
foreach ($users as $user) { |
|
102
|
|
|
$uid = $user->getUID(); |
|
103
|
|
|
if (!$this->setupFS($uid)) { |
|
104
|
|
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime'); |
|
107
|
|
|
Trashbin::deleteExpiredFiles($dirContent, $uid); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
\OC_Util::tearDownFS(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Act on behalf on trash item owner |
|
115
|
|
|
* @param string $user |
|
116
|
|
|
* @return boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
private function setupFS($user){ |
|
119
|
|
|
if (!$this->userManager->userExists($user)) { |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
//Check if this user has a trashbin directory |
|
124
|
|
|
$view = new \OC\Files\View('/' . $user); |
|
125
|
|
|
if (!$view->is_dir('/files_trashbin/files')){ |
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
\OC_Util::tearDownFS(); |
|
130
|
|
|
\OC_Util::setupFS($user); |
|
131
|
|
|
|
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: