]>
Commit | Line | Data |
---|---|---|
f0210517 GJ |
1 | <?php |
2 | ||
3 | namespace Pomf\Uguu\Classes; | |
4 | use PDO; | |
5 | class expireChecker | |
6 | { | |
7 | public PDO $DB; | |
8 | public string $dbType; | |
9 | public array $CONFIG; | |
10 | public string $timeUnit; | |
11 | ||
12 | public function checkDB(): bool | array { | |
13 | if(is_int($this->CONFIG['expireTime'])) { | |
14 | $this->timeUnit = strtoupper($this->CONFIG['expireTimeUnit']); | |
15 | if(!in_array($this->timeUnit, ['SECONDS', 'MINUTES', 'HOURS', 'DAYS', 'WEEKS', 'MONTHS', 'YEARS'])){ | |
16 | $this->timeUnit = "HOURS"; | |
17 | } | |
18 | $query = match ($this->dbType) { | |
19 | 'pgsql' => 'SELECT id, filename FROM files WHERE date < EXTRACT(epoch from NOW() - INTERVAL \'' . $this->CONFIG['expireTime'] . ' ' . $this->timeUnit . '\')', | |
20 | default => 'SELECT id, filename FROM files WHERE date <= strftime(\'%s\', datetime(\'now\', \'-' . $this->CONFIG['expireTime'] . ' ' . $this->timeUnit . '\'));' | |
21 | }; | |
22 | $q = $this->DB->prepare($query); | |
23 | $q->execute(); | |
24 | $result = $q->fetchAll(PDO::FETCH_ASSOC); | |
25 | $q->closeCursor(); | |
26 | $returnArray = [ | |
27 | 'ids' => [], | |
28 | 'filenames' => [] | |
29 | ]; | |
30 | foreach ($result as $array){ | |
31 | $returnArray['ids'][] = $array['id']; | |
32 | $returnArray['filenames'][] = $array['filename']; | |
33 | } | |
34 | return $returnArray; | |
35 | } else { | |
36 | return false; | |
37 | } | |
38 | } | |
39 | ||
f195c753 GJ |
40 | public function cleanRateLimitDB(): void { |
41 | $query = match ($this->dbType) { | |
42 | 'pgsql' => 'DELETE FROM ratelimit WHERE time < EXTRACT(epoch from NOW() - INTERVAL \'24 HOURS\')', | |
43 | default => 'DELETE FROM ratelimit WHERE time <= strftime(\'%s\', datetime(\'now\', \'-24 HOURS\'));' | |
44 | }; | |
45 | $q = $this->DB->prepare($query); | |
46 | $q->execute(); | |
47 | $q->closeCursor(); | |
48 | } | |
f0210517 GJ |
49 | public function deleteFiles(array $filenames): void { |
50 | foreach ($filenames as $filename) { | |
51 | unlink($this->CONFIG['FILES_ROOT'] . $filename); | |
52 | } | |
53 | } | |
54 | ||
55 | public function deleteFromDB(array $ids): void { | |
56 | foreach ($ids as $id) { | |
57 | $query = match ($this->dbType) { | |
58 | 'pgsql' => 'DELETE FROM files WHERE id = (:id)', | |
59 | default => 'DELETE FROM files WHERE id = (:id)' | |
60 | }; | |
61 | $q = $this->DB->prepare($query); | |
62 | $q->bindValue(':id', $id); | |
63 | $q->execute(); | |
64 | $q->closeCursor(); | |
65 | } | |
66 | } | |
67 | ||
68 | /** | |
69 | * Reads the config.json file and populates the CONFIG property with the settings | |
70 | * Also assembles the PDO DB connection and registers error handlers. | |
71 | * | |
72 | */ | |
73 | public function __construct() | |
74 | { | |
75 | $this->response = new Response('json'); | |
76 | if (!file_exists(__DIR__ . '/../config.json')) { | |
77 | $this->response->error(500, 'Cant read settings file.'); | |
78 | } | |
79 | $this->CONFIG = json_decode( | |
80 | file_get_contents(__DIR__ . '/../config.json'), | |
81 | true, | |
82 | ); | |
83 | ini_set('display_errors', 0); | |
84 | $this->dbType = $this->CONFIG['DB_MODE']; | |
85 | $this->DB = new PDO( | |
86 | $this->CONFIG['DB_MODE'] . ':' . $this->CONFIG['DB_PATH'], | |
87 | $this->CONFIG['DB_USER'], | |
88 | $this->CONFIG['DB_PASS'] | |
89 | ); | |
90 | } | |
91 | } | |
92 | ||
93 | ||
94 | ||
95 | ||
96 |