-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleaner.php
65 lines (55 loc) · 1.69 KB
/
cleaner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
// config json files cleaner
require_once __DIR__ . '/func.php';
$isCli = (php_sapi_name() === 'cli' || defined('STDIN') || (empty($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['HTTP_USER_AGENT']) && count($_SERVER['argv']) > 0));
if (!$isCli) {
header('Content-Type:text/plain; charset=UTF-8');
}
if (!$isCli) {
exit('web server access disallowed');
}
// Directory where JSON files are located
$directories = [
__DIR__ . '/config/',
__DIR__ . '/.cache/',
__DIR__ . '/tmp/',
__DIR__ . '/tmp/cookies/',
__DIR__ . '/tmp/sessions/',
__DIR__ . '/tmp/runners/',
__DIR__ . '/tmp/logs/',
__DIR__ . '/backups/'
];
// Get the current timestamp
$current_time = time();
// Calculate the timestamp for 1 week ago
$oneWeekAgo = strtotime('-1 week');
foreach ($directories as $directory) {
if (!file_exists($directory)) {
continue;
}
// Open the directory
if ($handle = opendir($directory)) {
// Loop through each file in the directory
while (false !== ($file = readdir($handle))) {
$filePath = realpath($directory . '/' . $file);
if (!is_file($filePath)) {
continue;
}
// skip database deletion
$pattern = '/\.(db|sqlite|sqlite3|mmdb|.*-wal|.*-shm)$/i';
if (preg_match($pattern, $filePath)) {
echo "$filePath excluded";
continue;
}
// Get the last modification time of the file
$file_mtime = filemtime($filePath);
// File was last modified more than 1 week ago.
if ($file_mtime < $oneWeekAgo) {
// Remove the file
echo "File $file removed (" . (unlink($filePath) ? 'success' : 'failed') . ")" . PHP_EOL;
}
}
// Close the directory handle
closedir($handle);
}
}