|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Teampass - a collaborative passwords manager. |
|
4
|
|
|
* --- |
|
5
|
|
|
* This library is distributed in the hope that it will be useful, |
|
6
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
7
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
8
|
|
|
* --- |
|
9
|
|
|
* |
|
10
|
|
|
* @project Teampass |
|
11
|
|
|
* @version |
|
12
|
|
|
* @file background_tasks___do_maintenance.php |
|
13
|
|
|
* --- |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nils Laumaillé ([email protected]) |
|
16
|
|
|
* |
|
17
|
|
|
* @copyright 2009-2023 Teampass.net |
|
18
|
|
|
* |
|
19
|
|
|
* @license https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0 |
|
20
|
|
|
* --- |
|
21
|
|
|
* |
|
22
|
|
|
* @see https://www.teampass.net |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
require_once __DIR__.'/../sources/SecureHandler.php'; |
|
26
|
|
|
session_name('teampass_session'); |
|
27
|
|
|
session_start(); |
|
28
|
|
|
$_SESSION['CPM'] = 1; |
|
29
|
|
|
|
|
30
|
|
|
// Load config |
|
31
|
|
|
require_once __DIR__.'/../includes/config/tp.config.php'; |
|
32
|
|
|
require_once __DIR__.'/background_tasks___functions.php'; |
|
33
|
|
|
|
|
34
|
|
|
// increase the maximum amount of time a script is allowed to run |
|
35
|
|
|
set_time_limit($SETTINGS['task_maximum_run_time']); |
|
36
|
|
|
|
|
37
|
|
|
// Do checks |
|
38
|
|
|
require_once $SETTINGS['cpassman_dir'].'/includes/config/include.php'; |
|
39
|
|
|
require_once $SETTINGS['cpassman_dir'].'/includes/config/settings.php'; |
|
40
|
|
|
header('Content-type: text/html; charset=utf-8'); |
|
41
|
|
|
header('Cache-Control: no-cache, must-revalidate'); |
|
42
|
|
|
require_once $SETTINGS['cpassman_dir'].'/sources/main.functions.php'; |
|
43
|
|
|
|
|
44
|
|
|
// Connect to mysql server |
|
45
|
|
|
require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Database/Meekrodb/db.class.php'; |
|
46
|
|
|
if (defined('DB_PASSWD_CLEAR') === false) { |
|
47
|
|
|
define('DB_PASSWD_CLEAR', defuseReturnDecrypted(DB_PASSWD, $SETTINGS)); |
|
48
|
|
|
} |
|
49
|
|
|
DB::$host = DB_HOST; |
|
50
|
|
|
DB::$user = DB_USER; |
|
51
|
|
|
DB::$password = DB_PASSWD_CLEAR; |
|
52
|
|
|
DB::$dbName = DB_NAME; |
|
53
|
|
|
DB::$port = DB_PORT; |
|
54
|
|
|
DB::$encoding = DB_ENCODING; |
|
55
|
|
|
DB::$ssl = DB_SSL; |
|
56
|
|
|
DB::$connect_options = DB_CONNECT_OPTIONS; |
|
57
|
|
|
|
|
58
|
|
|
// log start |
|
59
|
|
|
$logID = doLog('start', 'do_maintenance', (isset($SETTINGS['enable_tasks_log']) === true ? (int) $SETTINGS['enable_tasks_log'] : 0)); |
|
60
|
|
|
|
|
61
|
|
|
// Perform maintenance tasks |
|
62
|
|
|
foreach(json_decode($SETTINGS['maintenance_job_tasks']) as $task) { |
|
63
|
|
|
if ($task === "users-personal-folder") createUserPersonalFolder(); |
|
64
|
|
|
elseif ($task === "clean-orphan-objects") cleanOrphanObjects(); |
|
65
|
|
|
elseif ($task === "purge-old-files") purgeTemporaryFiles(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// log end |
|
69
|
|
|
doLog('end', '', (isset($SETTINGS['enable_tasks_log']) === true ? (int) $SETTINGS['enable_tasks_log'] : 0), $logID); |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Permits to create the personal folder for each user. |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
function createUserPersonalFolder(): void |
|
78
|
|
|
{ |
|
79
|
|
|
//Libraries call |
|
80
|
|
|
require_once __DIR__. '/../sources/main.functions.php'; |
|
81
|
|
|
require_once __DIR__. '/../includes/libraries/Tree/NestedTree/NestedTree.php'; |
|
82
|
|
|
$tree = new Tree\NestedTree\NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title'); |
|
83
|
|
|
|
|
84
|
|
|
//get through all users |
|
85
|
|
|
$rows = DB::query( |
|
86
|
|
|
'SELECT id, login, email |
|
87
|
|
|
FROM ' . prefixTable('users') . ' |
|
88
|
|
|
WHERE id NOT IN ('.OTV_USER_ID.', '.TP_USER_ID.', '.SSH_USER_ID.', '.API_USER_ID.') |
|
89
|
|
|
ORDER BY login ASC' |
|
90
|
|
|
); |
|
91
|
|
|
foreach ($rows as $record) { |
|
92
|
|
|
//update PF field for user |
|
93
|
|
|
DB::update( |
|
94
|
|
|
prefixTable('users'), |
|
95
|
|
|
array( |
|
96
|
|
|
'personal_folder' => '1', |
|
97
|
|
|
), |
|
98
|
|
|
'id = %i', |
|
99
|
|
|
$record['id'] |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
//if folder doesn't exist then create it |
|
103
|
|
|
$data = DB::queryfirstrow( |
|
104
|
|
|
'SELECT id |
|
105
|
|
|
FROM ' . prefixTable('nested_tree') . ' |
|
106
|
|
|
WHERE title = %s AND parent_id = %i', |
|
107
|
|
|
$record['id'], |
|
108
|
|
|
0 |
|
109
|
|
|
); |
|
110
|
|
|
$counter = DB::count(); |
|
111
|
|
|
if ($counter === 0) { |
|
112
|
|
|
//If not exist then add it |
|
113
|
|
|
DB::insert( |
|
114
|
|
|
prefixTable('nested_tree'), |
|
115
|
|
|
array( |
|
116
|
|
|
'parent_id' => '0', |
|
117
|
|
|
'title' => $record['id'], |
|
118
|
|
|
'personal_folder' => '1', |
|
119
|
|
|
'categories' => '', |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
//rebuild fuild tree folder |
|
124
|
|
|
$tree->rebuild(); |
|
125
|
|
|
} else { |
|
126
|
|
|
//If exists then update it |
|
127
|
|
|
DB::update( |
|
128
|
|
|
prefixTable('nested_tree'), |
|
129
|
|
|
array( |
|
130
|
|
|
'personal_folder' => '1', |
|
131
|
|
|
), |
|
132
|
|
|
'title=%s AND parent_id=%i', |
|
133
|
|
|
$record['id'], |
|
134
|
|
|
0 |
|
135
|
|
|
); |
|
136
|
|
|
//rebuild fuild tree folder |
|
137
|
|
|
$tree->rebuild(); |
|
138
|
|
|
|
|
139
|
|
|
// Get an array of all folders |
|
140
|
|
|
$folders = $tree->getDescendants($data['id'], false, true, true); |
|
141
|
|
|
foreach ($folders as $folder) { |
|
142
|
|
|
//update PF field for user |
|
143
|
|
|
DB::update( |
|
144
|
|
|
prefixTable('nested_tree'), |
|
145
|
|
|
array( |
|
146
|
|
|
'personal_folder' => '1', |
|
147
|
|
|
), |
|
148
|
|
|
'id = %s', |
|
149
|
|
|
$folder |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Delete all orphan objects from DB |
|
158
|
|
|
* |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
|
|
function cleanOrphanObjects(): void |
|
162
|
|
|
{ |
|
163
|
|
|
//Libraries call |
|
164
|
|
|
require_once __DIR__. '/../sources/main.functions.php'; |
|
165
|
|
|
include __DIR__. '/../includes/config/tp.config.php'; |
|
166
|
|
|
require_once __DIR__. '/../includes/libraries/Tree/NestedTree/NestedTree.php'; |
|
167
|
|
|
$tree = new Tree\NestedTree\NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title'); |
|
168
|
|
|
|
|
169
|
|
|
//init |
|
170
|
|
|
$foldersIds = array(); |
|
171
|
|
|
|
|
172
|
|
|
// Get an array of all folders |
|
173
|
|
|
$folders = $tree->getDescendants(); |
|
174
|
|
|
foreach ($folders as $folder) { |
|
175
|
|
|
if (!in_array($folder->id, $foldersIds)) { |
|
176
|
|
|
array_push($foldersIds, $folder->id); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$items = DB::query('SELECT id,label FROM ' . prefixTable('items') . ' WHERE id_tree NOT IN %li', $foldersIds); |
|
181
|
|
|
foreach ($items as $item) { |
|
182
|
|
|
//Delete item |
|
183
|
|
|
DB::DELETE(prefixTable('items'), 'id = %i', $item['id']); |
|
184
|
|
|
|
|
185
|
|
|
// Delete if template related to item |
|
186
|
|
|
DB::delete( |
|
187
|
|
|
prefixTable('templates'), |
|
188
|
|
|
'item_id = %i', |
|
189
|
|
|
$item['id'] |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
//log |
|
193
|
|
|
DB::DELETE(prefixTable('log_items'), 'id_item = %i', $item['id']); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// delete orphan items |
|
197
|
|
|
$rows = DB::query( |
|
198
|
|
|
'SELECT id |
|
199
|
|
|
FROM ' . prefixTable('items') . ' |
|
200
|
|
|
ORDER BY id ASC' |
|
201
|
|
|
); |
|
202
|
|
|
foreach ($rows as $item) { |
|
203
|
|
|
DB::query( |
|
204
|
|
|
'SELECT * FROM ' . prefixTable('log_items') . ' WHERE id_item = %i AND action = %s', |
|
205
|
|
|
$item['id'], |
|
206
|
|
|
'at_creation' |
|
207
|
|
|
); |
|
208
|
|
|
$counter = DB::count(); |
|
209
|
|
|
if ($counter === 0) { |
|
210
|
|
|
DB::DELETE(prefixTable('items'), 'id = %i', $item['id']); |
|
211
|
|
|
DB::DELETE(prefixTable('categories_items'), 'item_id = %i', $item['id']); |
|
212
|
|
|
DB::DELETE(prefixTable('log_items'), 'id_item = %i', $item['id']); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
// Update CACHE table |
|
217
|
|
|
updateCacheTable('reload', $SETTINGS, null); |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Purge old files in FILES and UPLOAD folders. |
|
222
|
|
|
* |
|
223
|
|
|
* @return void |
|
224
|
|
|
*/ |
|
225
|
|
|
function purgeTemporaryFiles(): void |
|
226
|
|
|
{ |
|
227
|
|
|
// Load expected files |
|
228
|
|
|
require_once __DIR__. '/../sources/main.functions.php'; |
|
229
|
|
|
include __DIR__. '/../includes/config/tp.config.php'; |
|
230
|
|
|
|
|
231
|
|
|
//read folder |
|
232
|
|
|
if (is_dir($SETTINGS['path_to_files_folder']) === true) { |
|
|
|
|
|
|
233
|
|
|
$dir = opendir($SETTINGS['path_to_files_folder']); |
|
234
|
|
|
if ($dir !== false) { |
|
235
|
|
|
//delete file FILES |
|
236
|
|
|
while (false !== ($f = readdir($dir))) { |
|
237
|
|
|
if ($f !== '.' && $f !== '..' && $f !== '.htaccess') { |
|
238
|
|
|
if (file_exists($dir . $f) && ((time() - filectime($dir . $f)) > 604800)) { |
|
239
|
|
|
fileDelete($dir . '/' . $f, $SETTINGS); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
//Close dir |
|
245
|
|
|
closedir($dir); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
//read folder UPLOAD |
|
250
|
|
|
if (is_dir($SETTINGS['path_to_upload_folder']) === true) { |
|
251
|
|
|
$dir = opendir($SETTINGS['path_to_upload_folder']); |
|
252
|
|
|
|
|
253
|
|
|
if ($dir !== false) { |
|
254
|
|
|
//delete file |
|
255
|
|
|
while (false !== ($f = readdir($dir))) { |
|
256
|
|
|
if ($f !== '.' && $f !== '..') { |
|
257
|
|
|
if (strpos($f, '_delete.') > 0) { |
|
258
|
|
|
fileDelete($SETTINGS['path_to_upload_folder'] . '/' . $f, $SETTINGS); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
//Close dir |
|
263
|
|
|
closedir($dir); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
} |