Passed
Pull Request — master (#4676)
by Nils
05:55
created

handleTaskStep()   C

Complexity

Conditions 13
Paths 10

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 28
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 49
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This file is part of the TeamPass project.
6
 * 
7
 * TeamPass is free software: you can redistribute it and/or modify it
8
 * under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, version 3 of the License.
10
 * 
11
 * TeamPass is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 * 
19
 * Certain components of this file may be under different licenses. For
20
 * details, see the `licenses` directory or individual file headers.
21
 * ---
22
 * @file      background_tasks___items_handler.php
23
 * @author    Nils Laumaillé ([email protected])
24
 * @copyright 2009-2025 Teampass.net
25
 * @license   GPL-3.0
26
 * @see       https://www.teampass.net
27
 */
28
29
use TeampassClasses\NestedTree\NestedTree;
30
use TeampassClasses\SessionManager\SessionManager;
31
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
32
use TeampassClasses\Language\Language;
33
use TeampassClasses\ConfigManager\ConfigManager;
34
35
// Load functions
36
require_once __DIR__.'/../sources/main.functions.php';
37
38
// init
39
loadClasses('DB');
40
$session = SessionManager::getSession();
41
$request = SymfonyRequest::createFromGlobals();
42
$lang = new Language('english');
43
44
// Load config
45
$configManager = new ConfigManager();
46
$SETTINGS = $configManager->getAllSettings();
47
48
// Define Timezone
49
date_default_timezone_set($SETTINGS['timezone'] ?? 'UTC');
50
51
// Set header properties
52
header('Content-type: text/html; charset=utf-8');
53
header('Cache-Control: no-cache, no-store, must-revalidate');
54
error_reporting(E_ERROR);
55
// increase the maximum amount of time a script is allowed to run
56
set_time_limit($SETTINGS['task_maximum_run_time']);
57
58
// --------------------------------- //
59
60
require_once __DIR__.'/background_tasks___functions.php';
61
62
$tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
63
64
// Get PHP binary
65
$phpBinaryPath = getPHPBinary();
66
67
// log start
68
$logID = doLog('start', 'item_keys', (isset($SETTINGS['enable_tasks_log']) === true ? (int) $SETTINGS['enable_tasks_log'] : 0));
69
70
71
// Manage the tasks in queue.
72
// Task to treat selection is:
73
// 1- take first is_in_progress === 1
74
// 2- take first is_in_progress === 0 and finished_at === null
75
DB::debugmode(false);
76
$process_to_perform = DB::queryFirstRow(
77
    'SELECT *
78
    FROM ' . prefixTable('background_tasks') . '
79
    WHERE is_in_progress = %i AND process_type IN ("item_copy", "new_item", "update_item", "item_update_create_keys")
80
    ORDER BY increment_id ASC',
81
    1
82
);
83
84
if (DB::count() > 0) {
85
    // handle tasks inside this process
86
    if (WIP === true) error_log("Process in progress: ".$process_to_perform['increment_id']);
87
    handleTask(
88
        $process_to_perform['increment_id'],
89
        json_decode($process_to_perform['arguments'], true),
90
        $SETTINGS,
91
    );
92
} else {
93
    // search for next process to handle
94
    $process_to_perform = DB::queryFirstRow(
95
        'SELECT *
96
        FROM ' . prefixTable('background_tasks') . '
97
        WHERE is_in_progress = %i AND (finished_at = "" OR finished_at IS NULL) AND process_type IN ("item_copy", "new_item", "update_item", "item_update_create_keys")
98
        ORDER BY increment_id ASC',
99
        0
100
    );
101
    
102
    if (DB::count() > 0) {
103
        if (WIP === true) error_log("New process ta start: ".$process_to_perform['increment_id']);
104
        // update DB - started_at
105
        DB::update(
106
            prefixTable('background_tasks'),
107
            array(
108
                'started_at' => time(),
109
            ),
110
            'increment_id = %i',
111
            $process_to_perform['increment_id']
112
        );
113
114
        provideLog('[PROCESS][#'. $process_to_perform['increment_id'].'][START]', $SETTINGS);
115
        handleTask(
116
            $process_to_perform['increment_id'],
117
            json_decode($process_to_perform['arguments'], true),
118
            $SETTINGS,
119
            $process_to_perform['item_id']
120
        );
121
    } else {
122
123
    }
124
}
125
126
127
// Do special tasks
128
performRecuringItemTasks($SETTINGS);
129
130
// log end
131
doLog('end', '', (isset($SETTINGS['enable_tasks_log']) === true ? (int) $SETTINGS['enable_tasks_log'] : 0), $logID);
132
133
// Check if $argv is defined and is an array
134
if (!isset($argv) || !is_array($argv)) {
135
    $argv = [];
136
}
137
138
// The main process run new iteratives process for each subtask
139
if (!in_array('--child', $argv)) {
140
    // Save subtasks start time
141
    $start_time = time();
142
143
    // Run new subtasks until there are no more to handle or we have exceeded
144
    // the execution minute (the next execution will continue)
145
    do {
146
        // Search if there are remaining tasks
147
        $process_to_perform = DB::queryFirstField(
148
            'SELECT 1
149
            FROM ' . prefixTable('background_tasks') . '
150
            WHERE is_in_progress = %i AND process_type IN (
151
                "item_copy",
152
                "new_item",
153
                "update_item",
154
                "item_update_create_keys"
155
            )
156
            ORDER BY increment_id DESC LIMIT 1',
157
            1
158
        );
159
160
        // No more tasks, exit
161
        if ($process_to_perform !== 1)
162
            break;
163
164
        // Run next task
165
        $process = new Symfony\Component\Process\Process([
166
            $phpBinaryPath,
167
            __FILE__,
168
            '--child'
169
        ]);
170
        $process->start();  
171
        $process->wait();
172
173
    } while (time() - $start_time < 60);
174
}
175
176
177
/**
178
 * Handle the task
179
 *
180
 * @param int   $processId
181
 * @param array $ProcessArguments
182
 * @param array $SETTINGS
183
 *
184
 * @return bool
185
 */
186
function handleTask(int $processId, array $ProcessArguments, array $SETTINGS, int $itemId = null): bool
187
{
188
    provideLog('[PROCESS][#'. $processId.'][START]', $SETTINGS);
189
    $task_to_perform = DB::queryFirstRow(
190
        'SELECT *
191
        FROM ' . prefixTable('background_subtasks') . '
192
        WHERE task_id = %i AND finished_at IS NULL
193
        ORDER BY increment_id ASC',
194
        $processId
195
    );
196
197
    // get the process object
198
    //$processObject = json_decode($ProcessArguments['object_key'], true);
199
    
200
    if (DB::count() > 0) {
201
        // check if a linux process is not currently on going
202
        // if sub_task_in_progress === 1 then exit
203
        if ((int) $task_to_perform['sub_task_in_progress'] !== 0) {
204
            // Task is currently being in progress by another server process
205
            provideLog('[TASK][#'. $task_to_perform['increment_id'].'][WARNING] Similar task already being processes', $SETTINGS);
206
            return false;
207
        }
208
209
        // handle next task
210
        $args = json_decode($task_to_perform['task'], true);
211
        provideLog('[TASK][#'. $task_to_perform['increment_id'].'][START]Task '.$args['step'], $SETTINGS);
212
213
        // flag as in progress
214
        DB::update(
215
            prefixTable('background_tasks'),
216
            array(
217
                'updated_at' => time(),
218
                'is_in_progress' => 1,
219
            ),
220
            'increment_id = %i',
221
            $processId
222
        );
223
224
        // flag task as on going
225
        if ((int) $args['index'] === 0) {
226
            DB::update(
227
                prefixTable('background_subtasks'),
228
                array(
229
                    'is_in_progress' => 1,
230
                ),
231
                'increment_id = %i',
232
                $task_to_perform['increment_id']
233
            );
234
        }
235
236
        // flag sub task in progress as on going
237
        DB::update(
238
            prefixTable('background_subtasks'),
239
            array(
240
                'sub_task_in_progress' => 1,
241
            ),
242
            'increment_id = %i',
243
            $task_to_perform['increment_id']
244
        );
245
246
        // handle the task step
247
        handleTaskStep($args, $ProcessArguments, $SETTINGS);
248
249
        // update the task status
250
        DB::update(
251
            prefixTable('background_subtasks'),
252
            array(
253
                'sub_task_in_progress' => 0,    // flag sub task is no more in prgoress
254
                'task' => json_encode(["status" => "Done"]),
255
                'is_in_progress' => -1,
256
                'finished_at' => time(),
257
                'updated_at' => time(),
258
            ),
259
            'increment_id = %i',
260
            $task_to_perform['increment_id']
261
        );
262
263
        provideLog('[TASK]['.$args['step'].'] starting at '.$args['index'].' is done.', $SETTINGS);
264
265
        // are all tasks done?
266
        DB::query(
267
            'SELECT *
268
            FROM ' . prefixTable('background_subtasks') . '
269
            WHERE task_id = %i AND finished_at IS NULL',
270
            $processId
271
        );
272
        if (DB::count() === 0) {
273
            // all tasks are done
274
            provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS);
275
            DB::debugmode(false);
276
            DB::update(
277
                prefixTable('background_tasks'),
278
                array(
279
                    'finished_at' => time(),
280
                    'is_in_progress' => -1,
281
                    'arguments' => json_encode([
282
                        'new_user_id' => isset($ProcessArguments['new_user_id']) === true ? $ProcessArguments['new_user_id'] : '',
283
                    ])
284
                ),
285
                'increment_id = %i',
286
                $processId
287
            );
288
289
            // if item was being updated then remove the edition lock
290
            if (is_null($itemId) === false) {
291
                DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId);
292
            }
293
        }
294
        return false;
295
296
    } else {
297
        // no more task to perform
298
        provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS);
299
        DB::update(
300
            prefixTable('background_tasks'),
301
            array(
302
                'finished_at' => time(),
303
                'is_in_progress' => -1,
304
                'arguments' => json_encode([
305
                    'item_id' => isset($ProcessArguments['item_id']) === true ? $ProcessArguments['item_id'] : '',
306
                ])
307
            ),
308
            'increment_id = %i',
309
            $processId
310
        );
311
312
        // if item was being updated then remove the edition lock
313
        if (is_null($itemId) === false) {
314
            DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId);
315
        }
316
    }
317
    return false;
318
}
319
320
/**
321
 * Handle the task step
322
 *
323
 * @param array $args
324
 * @param array $ProcessArguments
325
 * @param array $SETTINGS
326
 *
327
 * @return void
328
 */
329
function handleTaskStep(
330
    array $args,
331
    array $ProcessArguments,
332
    array $SETTINGS
333
)
334
{
335
    // perform the task step "create_users_files_key"
336
    if ($args['step'] === 'create_users_files_key') {
337
        // Loop on all files for this item
338
        // and encrypt them for each user
339
        if (WIP === true) provideLog('[DEBUG] '.print_r($args['files_keys'], true), $SETTINGS);
340
        foreach($args['files_keys'] as $file) {
341
            storeUsersShareKey(
342
                prefixTable('sharekeys_items'),
343
                0,
344
                (int) $file['object_id'],
345
                (string) $file['object_key'],
346
                false,
347
                false,
348
                [],
349
                array_key_exists('all_users_except_id', $ProcessArguments) === true ? $ProcessArguments['all_users_except_id'] : -1,
350
            );
351
        }
352
    } elseif ($args['step'] === 'create_users_fields_key') {
353
        // Loop on all encrypted fields for this item
354
        // and encrypt them for each user
355
        if (WIP === true) provideLog('[DEBUG] '.print_r($args, true), $SETTINGS);
356
        foreach($args['fields_keys'] as $field) {
357
            storeUsersShareKey(
358
                prefixTable('sharekeys_fields'),
359
                0,
360
                (int) $field['object_id'],
361
                (string) $field['object_key'],
362
                false,
363
                false,
364
                [],
365
                array_key_exists('all_users_except_id', $ProcessArguments) === true ? $ProcessArguments['all_users_except_id'] : -1,
366
            );
367
        }
368
    } elseif ($args['step'] === 'create_users_pwd_key') {
369
        storeUsersShareKey(
370
            prefixTable('sharekeys_items'),
371
            0,
372
            (int) $ProcessArguments['item_id'],
373
            (string) (array_key_exists('pwd', $ProcessArguments) === true ? $ProcessArguments['pwd'] : (array_key_exists('object_key', $ProcessArguments) === true ? $ProcessArguments['object_key'] : '')),
374
            false,
375
            false,
376
            [],
377
            array_key_exists('all_users_except_id', $ProcessArguments) === true ? $ProcessArguments['all_users_except_id'] : -1
378
        );
379
    }
380
}
381
382
/**
383
 * Perform recuring tasks
384
 * 
385
 * @param array $SETTINGS
386
 * 
387
 * @return void
388
 */
389
function performRecuringItemTasks($SETTINGS): void
390
{
391
    // Clean multeple items edition
392
    DB::query(
393
        'DELETE i1 FROM '.prefixTable('items_edition').' i1
394
        JOIN (
395
            SELECT user_id, item_id, MIN(timestamp) AS oldest_timestamp
396
            FROM '.prefixTable('items_edition').'
397
            GROUP BY user_id, item_id
398
        ) i2 ON i1.user_id = i2.user_id AND i1.item_id = i2.item_id
399
        WHERE i1.timestamp > i2.oldest_timestamp'
400
    );
401
402
    // Handle item tokens expiration
403
    // Delete entry if token has expired
404
    // Based upon SETTINGS['delay_item_edition'] or EDITION_LOCK_PERIOD (1 day by default)
405
    DB::query(
406
        'DELETE FROM '.prefixTable('items_edition').'
407
        WHERE timestamp < %i',
408
        ($SETTINGS['delay_item_edition'] > 0) ? time() - ($SETTINGS['delay_item_edition']*60) : time() - EDITION_LOCK_PERIOD
409
    );
410
}
411