forked from fsi-tue/eei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune_events.php
59 lines (49 loc) · 1.75 KB
/
prune_events.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
<?php
/* Author: Lukas Oertel git@luoe.dev
*
* Deletes old events from EEI, this script is called by Github Actions weekly
*/
global $events, $fp;
require_once('config.php');
require_once('event_data.php');
$ALL_FILES = scandir($fp);
// get all filepaths (for detecting orphans)
$event_filepaths = array();
foreach ($events as $event_id) {
$event_filepaths[] = $event_id['path'];
}
$deleted_events = array();
// get all files in $fp
// i.e. for detecting orphans not in events.php
$ALL_FILES = array_diff(scandir($fp), array('.', '..'));
foreach ($ALL_FILES as &$file) {
$file = $fp . $file;
}
if ($ALL_FILES != $event_filepaths) {
// remove orphans, i.e. files that are not in events_data.php
$orphans = array_diff(array_merge($ALL_FILES, $event_filepaths), $event_filepaths);
unset($file);
foreach ($orphans as $file) {
if (unlink($file)) {
echo("deleted orphan " . $file . PHP_EOL);
$deleted_events[] = $file;
} else {
http_response_code(500);
exit("File could not be deleted. Possible file permission problem (uid/gid for PHP-FPM instances: 82)");
}
}
}
foreach ($events as $event_id) {
// delete list of participants if event was more than two weeks ago
if (file_exists($event_id['path']) && time() >= ($event_id["startUTS"] + (86400 * 14))) {
if (unlink($event_id["path"])) {
echo("deleted " . $event_id['path'] . PHP_EOL);
$deleted_events[] = $event_id['path'];
} else {
http_response_code(500);
exit("File could not be deleted. Possible file permission problem (uid/gid for PHP-FPM instances: 82)");
}
}
}
echo "deleted " . count($deleted_events) . " events in total" . PHP_EOL;
exit;