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

scripts/task_maintenance_purge_old_files.php (1 issue)

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      task_maintenance_purge_old_files.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\Language\Language;
30
use TeampassClasses\ConfigManager\ConfigManager;
31
32
33
// Load functions
34
require_once __DIR__.'/../sources/main.functions.php';
35
36
// init
37
loadClasses('DB');
38
$lang = new Language('english');
39
40
// Load config
41
$configManager = new ConfigManager();
42
$SETTINGS = $configManager->getAllSettings();
43
44
// Define Timezone
45
date_default_timezone_set($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
    $SETTINGS = $SETTINGS ?? [];
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...
76
77
    // $SETTINGS is set then read folder
78
    if (is_dir($SETTINGS['path_to_files_folder']) === true) {
79
        $dir = opendir($SETTINGS['path_to_files_folder']);
80
        if ($dir !== false) {
81
            //delete file FILES
82
            while (false !== ($f = readdir($dir))) {
83
                if ($f !== '.' && $f !== '..' && $f !== '.htaccess') {
84
                    $filePath = $SETTINGS['path_to_files_folder'] . '/' . $f;
85
                    if (file_exists($filePath) && ((time() - filectime($filePath)) > 604800)) {
86
                        fileDelete($filePath, $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) {
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
}