]>
Commit | Line | Data |
---|---|---|
e480c0e5 | 1 | <?php |
8f7f8840 | 2 | /** |
cec6349e | 3 | * Uguu |
8f7f8840 | 4 | * |
52053519 | 5 | * @copyright Copyright (c) 2022-2023 Go Johansson (nokonoko) <neku@pomf.se> |
8f7f8840 | 6 | * |
cec6349e GJ |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
8f7f8840 | 11 | * |
cec6349e GJ |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
8f7f8840 | 16 | * |
cec6349e GJ |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
e480c0e5 | 19 | */ |
52053519 | 20 | |
f059e2cf | 21 | namespace Pomf\Uguu\Classes; |
52053519 | 22 | |
cec6349e | 23 | use PDO; |
52053519 GJ |
24 | |
25 | class Database | |
f0b5e51c | 26 | { |
52053519 GJ |
27 | public function dbCheckNameExists(string $name):bool |
28 | { | |
29 | $query = match ($this->dbType) { | |
30 | 'pgsql' => 'SELECT EXISTS(SELECT id FROM files WHERE filename = (:name)), filename FROM files WHERE filename = (:name) LIMIT 1', | |
31 | default => 'SELECT filename FROM files WHERE filename = (:name) AND EXISTS (SELECT id FROM files WHERE filename = (:name)) LIMIT 1' | |
32 | }; | |
33 | $q = $this->DB->prepare($query); | |
f0b5e51c GJ |
34 | $q->bindValue(':name', $name); |
35 | $q->execute(); | |
24383942 | 36 | $result = $q->fetch(); |
52053519 GJ |
37 | $q->closeCursor(); |
38 | if (isset($result['exists']) and $result['exists']) { | |
39 | return true; | |
40 | } elseif ($result) { | |
24383942 GJ |
41 | return true; |
42 | } | |
43 | return false; | |
e480c0e5 | 44 | } |
52053519 GJ |
45 | |
46 | public function checkFileBlacklist(string $hash):void | |
47 | { | |
48 | $query = match ($this->dbType) { | |
49 | 'pgsql' => 'SELECT EXISTS(SELECT id FROM blacklist WHERE hash = (:hash)), hash FROM blacklist WHERE hash = (:hash) LIMIT 1', | |
50 | default => 'SELECT id FROM blacklist WHERE EXISTS(SELECT id FROM blacklist WHERE hash = (:hash)) LIMIT 1' | |
51 | }; | |
52 | $q = $this->DB->prepare($query); | |
53 | $q->bindValue(':hash', $hash); | |
f0b5e51c GJ |
54 | $q->execute(); |
55 | $result = $q->fetch(); | |
52053519 GJ |
56 | $q->closeCursor(); |
57 | if (isset($result['exists']) and $result['exists']) { | |
58 | $this->response->error(415, 'File blacklisted.'); | |
59 | } elseif ($result) { | |
60 | $this->response->error(415, 'File blacklisted.'); | |
cec6349e | 61 | } |
e480c0e5 | 62 | } |
52053519 GJ |
63 | |
64 | public function antiDupe(string $hash):array | |
65 | { | |
66 | $query = match ($this->dbType) { | |
67 | 'pgsql' => 'SELECT EXISTS(SELECT id FROM files WHERE hash = (:hash)), filename FROM files WHERE hash = (:hash) LIMIT 1', | |
68 | default => 'SELECT filename FROM files WHERE hash = (:hash) AND EXISTS (SELECT id FROM files WHERE hash = (:hash)) LIMIT 1' | |
69 | }; | |
70 | $q = $this->DB->prepare($query); | |
f0b5e51c | 71 | $q->bindValue(':hash', $hash); |
cec6349e | 72 | $q->execute(); |
f0b5e51c | 73 | $result = $q->fetch(); |
52053519 GJ |
74 | $q->closeCursor(); |
75 | if (!$result) { | |
f0b5e51c | 76 | return [ |
52053519 | 77 | 'result' => false, |
f0b5e51c | 78 | ]; |
cec6349e | 79 | } else { |
f0b5e51c | 80 | return [ |
52053519 GJ |
81 | 'result' => true, |
82 | 'name' => $result['filename'], | |
f0b5e51c | 83 | ]; |
cec6349e | 84 | } |
cec6349e | 85 | } |
52053519 GJ |
86 | |
87 | public function newIntoDB(array $FILE_INFO, array $fingerPrintInfo):void | |
88 | { | |
e480c0e5 | 89 | $q = $this->DB->prepare( |
52053519 GJ |
90 | 'INSERT INTO files (hash, originalname, filename, size, date, ip)' . |
91 | 'VALUES (:hash, :orig, :name, :size, :date, :ip)', | |
e480c0e5 | 92 | ); |
f0b5e51c GJ |
93 | $q->bindValue(':hash', $FILE_INFO['SHA1']); |
94 | $q->bindValue(':orig', $FILE_INFO['NAME']); | |
52053519 | 95 | $q->bindValue(':name', $FILE_INFO['FILENAME']); |
f0b5e51c GJ |
96 | $q->bindValue(':size', $FILE_INFO['SIZE'], PDO::PARAM_INT); |
97 | $q->bindValue(':date', $fingerPrintInfo['timestamp']); | |
98 | $q->bindValue(':ip', $fingerPrintInfo['ip']); | |
cec6349e | 99 | $q->execute(); |
52053519 | 100 | $q->closeCursor(); |
f0b5e51c | 101 | } |
52053519 GJ |
102 | |
103 | public function createRateLimit(array $fingerPrintInfo):void | |
104 | { | |
24383942 | 105 | $q = $this->DB->prepare( |
52053519 GJ |
106 | 'INSERT INTO ratelimit (iphash, files, time)' . |
107 | 'VALUES (:iphash, :files, :time)', | |
24383942 GJ |
108 | ); |
109 | $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']); | |
110 | $q->bindValue(':files', $fingerPrintInfo['files_amount']); | |
111 | $q->bindValue(':time', $fingerPrintInfo['timestamp']); | |
112 | $q->execute(); | |
52053519 | 113 | $q->closeCursor(); |
24383942 | 114 | } |
52053519 GJ |
115 | |
116 | public function updateRateLimit(int $fCount, bool $iStamp, array $fingerPrintInfo):void | |
117 | { | |
24383942 GJ |
118 | if ($iStamp) { |
119 | $q = $this->DB->prepare( | |
52053519 | 120 | 'UPDATE ratelimit SET files = (:files), time = (:time) WHERE iphash = (:iphash)', |
24383942 GJ |
121 | ); |
122 | $q->bindValue(':time', $fingerPrintInfo['timestamp']); | |
123 | } else { | |
124 | $q = $this->DB->prepare( | |
52053519 | 125 | 'UPDATE ratelimit SET files = (:files) WHERE iphash = (:iphash)', |
24383942 GJ |
126 | ); |
127 | } | |
128 | $q->bindValue(':files', $fCount); | |
129 | $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']); | |
130 | $q->execute(); | |
52053519 | 131 | $q->closeCursor(); |
24383942 | 132 | } |
52053519 GJ |
133 | |
134 | public function compareTime(int $timestamp, int $seconds_d):bool | |
135 | { | |
136 | $diff = time() - $timestamp; | |
137 | if ($diff > $seconds_d) { | |
138 | return true; | |
139 | } | |
24383942 GJ |
140 | return false; |
141 | } | |
52053519 GJ |
142 | |
143 | public function checkRateLimit(array $fingerPrintInfo, int $rateTimeout, int $fileLimit):bool | |
144 | { | |
145 | $query = match ($this->dbType) { | |
146 | 'pgsql' => 'SELECT EXISTS(SELECT id FROM ratelimit WHERE iphash = (:iphash)), id, iphash, files, time FROM ratelimit WHERE iphash = (:iphash) LIMIT 1', | |
147 | default => 'SELECT * FROM ratelimit WHERE iphash = (:iphash) AND EXISTS (SELECT id FROM ratelimit WHERE iphash = (:iphash)) LIMIT 1' | |
148 | }; | |
149 | $q = $this->DB->prepare($query); | |
150 | $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']); | |
151 | $q->execute(); | |
152 | $result = $q->fetch(); | |
153 | $q->closeCursor(); | |
154 | //If there is no other match a record does not exist, create one. | |
155 | if (!$result) { | |
156 | $this->createRateLimit($fingerPrintInfo); | |
157 | return false; | |
158 | } | |
159 | // Apply rate-limit when file count reached and timeout not reached. | |
160 | if ($result['files'] === $fileLimit and !$this->compareTime($result['time'], $rateTimeout)) { | |
161 | return true; | |
162 | } | |
163 | // Update timestamp if timeout reached, reset file count and add the incoming file count. | |
164 | if ($this->compareTime($result['time'], $rateTimeout)) { | |
165 | $this->updateRateLimit($fingerPrintInfo['files_amount'], true, $fingerPrintInfo); | |
166 | return false; | |
167 | } | |
168 | // Add filecount, timeout not reached. | |
169 | if ($result['files'] < $fileLimit and !$this->compareTime($result['time'], $rateTimeout)) { | |
170 | $this->updateRateLimit($result['files'] + $fingerPrintInfo['files_amount'], false, $fingerPrintInfo); | |
171 | return false; | |
172 | } | |
24383942 GJ |
173 | return false; |
174 | } | |
52053519 | 175 | } |