Passed
Branch wip_sessions (2e0cc8)
by Nils
04:59
created

purgeTemporaryFiles()   C

Complexity

Conditions 16
Paths 19

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 19
c 1
b 0
f 0
nc 19
nop 0
dl 0
loc 40
rs 5.5666

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 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      task_maintenance_purge_old_files.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
use TeampassClasses\NestedTree\NestedTree;
26
use TeampassClasses\SessionManager\SessionManager;
27
use TeampassClasses\Language\Language;
28
29
30
// Load functions
31
require_once __DIR__.'/../sources/main.functions.php';
32
33
// init
34
loadClasses('DB');
35
$lang = new Language();
36
37
// Load config if $SETTINGS not defined
38
try {
39
    include_once __DIR__.'/../includes/config/tp.config.php';
40
} catch (Exception $e) {
41
    throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
42
}
43
44
// Define Timezone
45
date_default_timezone_set(isset($SETTINGS['timezone']) === true ? $SETTINGS['timezone'] : 'UTC');
46
47
// Set header properties
48
header('Content-type: text/html; charset=utf-8');
49
header('Cache-Control: no-cache, no-store, must-revalidate');
50
error_reporting(E_ERROR);
51
// increase the maximum amount of time a script is allowed to run
52
set_time_limit($SETTINGS['task_maximum_run_time']);
53
54
// --------------------------------- //
55
56
require_once __DIR__.'/background_tasks___functions.php';
57
58
// log start
59
$logID = doLog('start', 'do_maintenance - purge-old-files', 1);
60
61
// Perform maintenance tasks
62
purgeTemporaryFiles();
63
64
// log end
65
doLog('end', '', 1, $logID);
66
/**
67
 * Purge old files in FILES and UPLOAD folders.
68
 *
69
 * @return void
70
 */
71
function purgeTemporaryFiles(): void
72
{
73
    // Load expected files
74
    require_once __DIR__. '/../sources/main.functions.php';
75
    include __DIR__. '/../includes/config/tp.config.php';
76
77
    if (isset($SETTINGS) === true) {
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $SETTINGS seems to never exist and therefore isset should always be false.
Loading history...
78
        //read folder
79
        if (is_dir($SETTINGS['path_to_files_folder']) === true) {
80
            $dir = opendir($SETTINGS['path_to_files_folder']);
81
            if ($dir !== false) {
82
                //delete file FILES
83
                while (false !== ($f = readdir($dir))) {
84
                    if ($f !== '.' && $f !== '..' && $f !== '.htaccess') {
85
                        if (file_exists($dir . $f) && ((time() - filectime($dir . $f)) > 604800)) {
86
                            fileDelete($dir . '/' . $f, $SETTINGS);
87
                        }
88
                    }
89
                }
90
91
                //Close dir
92
                closedir($dir);
93
            }
94
        }
95
96
        //read folder  UPLOAD
97
        if (is_dir($SETTINGS['path_to_upload_folder']) === true) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $SETTINGS seems to be never defined.
Loading history...
98
            $dir = opendir($SETTINGS['path_to_upload_folder']);
99
100
            if ($dir !== false) {
101
                //delete file
102
                while (false !== ($f = readdir($dir))) {
103
                    if ($f !== '.' && $f !== '..') {
104
                        if (strpos($f, '_delete.') > 0) {
105
                            fileDelete($SETTINGS['path_to_upload_folder'] . '/' . $f, $SETTINGS);
106
                        }
107
                    }
108
                }
109
                //Close dir
110
                closedir($dir);
111
            }
112
        }
113
    }
114
    
115
}