]>
Commit | Line | Data |
---|---|---|
f0210517 | 1 | <?php |
9184e8d4 GJ |
2 | /* |
3 | * Uguu | |
4 | * | |
5 | * @copyright Copyright (c) 2022-2024 Go Johansson (nokonoko) <neku@pomf.se> | |
6 | * | |
7 | * Note that this was previously distributed under the MIT license 2015-2022. | |
8 | * | |
9 | * If you are a company that wants to use Uguu I urge you to contact me to | |
10 | * solve any potential license issues rather then using pre-2022 code. | |
11 | * | |
12 | * A special thanks goes out to the open source community around the world | |
13 | * for supporting and being the backbone of projects like Uguu. | |
14 | * | |
15 | * This project can be found at <https://github.com/nokonoko/Uguu>. | |
16 | * | |
17 | * This program is free software: you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation, either version 3 of the License, or | |
20 | * (at your option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU General Public License | |
28 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
29 | */ | |
4469e4dc | 30 | |
9184e8d4 GJ |
31 | namespace Pomf\Uguu\Classes; |
32 | ||
33 | use PDO; | |
4469e4dc | 34 | |
9184e8d4 GJ |
35 | class expireChecker |
36 | { | |
37 | public PDO $DB; | |
38 | public string $dbType; | |
39 | public array $CONFIG; | |
40 | public string $timeUnit; | |
41 | /** | |
42 | * @var \Pomf\Uguu\Classes\Response | |
43 | */ | |
44 | private Response $response; | |
4469e4dc | 45 | |
9184e8d4 | 46 | public function checkDB():bool|array |
4469e4dc | 47 | { |
9184e8d4 GJ |
48 | if (is_int($this->CONFIG['expireTime'])) { |
49 | $this->timeUnit = strtoupper($this->CONFIG['expireTimeUnit']); | |
50 | if (!in_array($this->timeUnit, ['SECONDS', 'MINUTES', 'HOURS', 'DAYS', 'WEEKS', 'MONTHS', 'YEARS'])) { | |
51 | $this->timeUnit = "HOURS"; | |
f0210517 GJ |
52 | } |
53 | $query = match ($this->dbType) { | |
9184e8d4 GJ |
54 | 'pgsql' => 'SELECT id, filename FROM files WHERE date < EXTRACT(epoch from NOW() - INTERVAL \'' . $this->CONFIG['expireTime'] . ' ' . $this->timeUnit . '\')', |
55 | default => 'SELECT id, filename FROM files WHERE date <= strftime(\'%s\', datetime(\'now\', \'-' . $this->CONFIG['expireTime'] . ' ' . $this->timeUnit . '\'));' | |
f0210517 GJ |
56 | }; |
57 | $q = $this->DB->prepare($query); | |
58 | $q->execute(); | |
9184e8d4 | 59 | $result = $q->fetchAll(PDO::FETCH_ASSOC); |
f0210517 | 60 | $q->closeCursor(); |
9184e8d4 GJ |
61 | $returnArray = [ |
62 | 'ids' => [], | |
63 | 'filenames' => [], | |
64 | ]; | |
65 | foreach ($result as $array) { | |
66 | $returnArray['ids'][] = $array['id']; | |
67 | $returnArray['filenames'][] = $array['filename']; | |
68 | } | |
69 | return $returnArray; | |
70 | } else { | |
71 | return false; | |
f0210517 | 72 | } |
9184e8d4 | 73 | } |
4469e4dc | 74 | |
9184e8d4 GJ |
75 | public function cleanRateLimitDB():void |
76 | { | |
77 | $query = match ($this->dbType) { | |
78 | 'pgsql' => 'DELETE FROM ratelimit WHERE time < EXTRACT(epoch from NOW() - INTERVAL \'24 HOURS\')', | |
79 | default => 'DELETE FROM ratelimit WHERE time <= strftime(\'%s\', datetime(\'now\', \'-24 HOURS\'));' | |
80 | }; | |
81 | $q = $this->DB->prepare($query); | |
82 | $q->execute(); | |
83 | $q->closeCursor(); | |
84 | } | |
85 | ||
86 | public function deleteFiles(array $filenames):void | |
87 | { | |
88 | foreach ($filenames as $filename) { | |
89 | unlink($this->CONFIG['FILES_ROOT'] . $filename); | |
f0210517 | 90 | } |
9184e8d4 | 91 | } |
4469e4dc | 92 | |
9184e8d4 GJ |
93 | public function deleteFromDB(array $ids):void |
94 | { | |
95 | foreach ($ids as $id) { | |
96 | $query = match ($this->dbType) { | |
97 | 'pgsql' => 'DELETE FROM files WHERE id = (:id)', | |
98 | default => 'DELETE FROM files WHERE id = (:id)' | |
99 | }; | |
100 | $q = $this->DB->prepare($query); | |
101 | $q->bindValue(':id', $id); | |
102 | $q->execute(); | |
103 | $q->closeCursor(); | |
f0210517 | 104 | } |
9184e8d4 | 105 | } |
4469e4dc | 106 | |
9184e8d4 GJ |
107 | /** |
108 | * Reads the config.json file and populates the CONFIG property with the settings | |
109 | * Also assembles the PDO DB connection and registers error handlers. | |
110 | * | |
111 | */ | |
112 | public function __construct() | |
113 | { | |
114 | $this->response = new Response('json'); | |
115 | if (!file_exists(__DIR__ . '/../config.json')) { | |
116 | $this->response->error(500, 'Cant read settings file.'); | |
f0210517 | 117 | } |
9184e8d4 GJ |
118 | $this->CONFIG = json_decode( |
119 | file_get_contents(__DIR__ . '/../config.json'), | |
120 | true, | |
121 | ); | |
122 | ini_set('display_errors', 0); | |
123 | $this->dbType = $this->CONFIG['DB_MODE']; | |
124 | $this->DB = new PDO( | |
125 | $this->CONFIG['DB_MODE'] . ':' . $this->CONFIG['DB_PATH'], | |
126 | $this->CONFIG['DB_USER'], | |
127 | $this->CONFIG['DB_PASS'], | |
128 | ); | |
f0210517 | 129 | } |
9184e8d4 | 130 | } |