]> jfr.im git - uguu.git/blob - src/Classes/expireChecker.php
replace name generator method
[uguu.git] / src / Classes / expireChecker.php
1 <?php
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 */
30
31 namespace Pomf\Uguu\Classes;
32
33 use PDO;
34
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;
45
46 public function checkDB():bool|array
47 {
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";
52 }
53 $query = match ($this->dbType) {
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 . '\'));'
56 };
57 $q = $this->DB->prepare($query);
58 $q->execute();
59 $result = $q->fetchAll(PDO::FETCH_ASSOC);
60 $q->closeCursor();
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;
72 }
73 }
74
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);
90 }
91 }
92
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();
104 }
105 }
106
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.');
117 }
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 );
129 }
130 }