]>
jfr.im git - uguu.git/blob - src/Classes/Database.php
6 * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Pomf\Uguu\Classes
;
34 * Sets the value of the DB variable.
36 * @param $DB PDO The database connection.
38 public function setDB(PDO
$DB): void
44 * Checks if a file name exists in the database
46 * @param $name string The name of the file.
48 * @return bool The number of rows that match the query.
51 public function dbCheckNameExists(string $name): bool
54 $q = $this->DB
->prepare('SELECT * FROM files WHERE EXISTS
55 (SELECT filename FROM files WHERE filename = (:name)) LIMIT 1');
56 $q->bindValue(':name', $name);
58 $result = $q->fetch();
64 throw new Exception('Cant check if name exists in DB.', 500);
69 * Checks if the file is blacklisted
71 * @param $FILE_INFO array An array containing the following:
75 public function checkFileBlacklist(array $FILE_INFO): void
78 $q = $this->DB
->prepare('SELECT * FROM blacklist WHERE EXISTS
79 (SELECT hash FROM blacklist WHERE hash = (:hash)) LIMIT 1');
80 $q->bindValue(':hash', $FILE_INFO['SHA1']);
82 $result = $q->fetch();
84 throw new Exception('File blacklisted!', 415);
87 throw new Exception('Cant check blacklist DB.', 500);
92 * Checks if the file already exists in the database
94 * @param $hash string The hash of the file you want to check for.
98 public function antiDupe(string $hash): array
101 $q = $this->DB
->prepare(
102 'SELECT * FROM files WHERE EXISTS
103 (SELECT filename FROM files WHERE hash = (:hash)) LIMIT 1',
105 $q->bindValue(':hash', $hash);
107 $result = $q->fetch();
111 'name' => $result['filename'],
118 } catch (Exception
) {
119 throw new Exception('Cant check for dupes in DB.', 500);
124 * Inserts a new file into the database
126 * @param $FILE_INFO array
127 * @param $fingerPrintInfo array
131 public function newIntoDB(array $FILE_INFO, array $fingerPrintInfo): void
134 $q = $this->DB
->prepare(
135 'INSERT INTO files (hash, originalname, filename, size, date, ip)' .
136 'VALUES (:hash, :orig, :name, :size, :date, :ip)',
138 $q->bindValue(':hash', $FILE_INFO['SHA1']);
139 $q->bindValue(':orig', $FILE_INFO['NAME']);
140 $q->bindValue(':name', $FILE_INFO['NEW_NAME']);
141 $q->bindValue(':size', $FILE_INFO['SIZE'], PDO
::PARAM_INT
);
142 $q->bindValue(':date', $fingerPrintInfo['timestamp']);
143 $q->bindValue(':ip', $fingerPrintInfo['ip']);
145 } catch (Exception
) {
146 throw new Exception('Cant insert into DB.', 500);
151 * Creates a new row in the database with the information provided
153 * @param $fingerPrintInfo array
157 public function createRateLimit(array $fingerPrintInfo): void
160 $q = $this->DB
->prepare(
161 'INSERT INTO ratelimit (iphash, files, time)' .
162 'VALUES (:iphash, :files, :time)',
164 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
165 $q->bindValue(':files', $fingerPrintInfo['files_amount']);
166 $q->bindValue(':time', $fingerPrintInfo['timestamp']);
168 } catch (Exception
$e) {
169 throw new Exception(500, $e->getMessage());
174 * Update the rate limit table with the new file count and timestamp
176 * @param $fCount int The number of files uploaded by the user.
177 * @param $iStamp bool A boolean value that determines whether or not to update the timestamp.
178 * @param $fingerPrintInfo array An array containing the following keys:
182 public function updateRateLimit(int $fCount, bool $iStamp, array $fingerPrintInfo): void
186 $q = $this->DB
->prepare(
187 'UPDATE ratelimit SET files = (:files), time = (:time) WHERE iphash = (:iphash)',
189 $q->bindValue(':time', $fingerPrintInfo['timestamp']);
191 $q = $this->DB
->prepare(
192 'UPDATE ratelimit SET files = (:files) WHERE iphash = (:iphash)',
195 $q->bindValue(':files', $fCount);
196 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
198 } catch (Exception
$e) {
199 throw new Exception(500, $e->getMessage());
206 public function compareTime(int $timestamp, int $seconds_d): bool
208 $dateTime_end = new DateTime('now', new DateTimeZone('Europe/Stockholm'));
209 $dateTime_start = new DateTime();
210 $dateTime_start->setTimestamp($timestamp);
211 $diff = strtotime($dateTime_end->format('Y-m-d H:i:s')) - strtotime($dateTime_start->format('Y-m-d H:i:s'));
212 if ($diff > $seconds_d) {
219 * Checks if the user has uploaded more than 100 files in the last minute, if so it returns true,
220 * if not it updates the database with the new file
221 * count and timestamp
223 * @param $fingerPrintInfo array An array containing the following:
225 * @return bool A boolean value.
228 public function checkRateLimit(array $fingerPrintInfo, int $rateTimeout, int $fileLimit): bool
230 $q = $this->DB
->prepare(
231 'SELECT files, time, iphash, COUNT(*) AS count FROM ratelimit WHERE iphash = (:iphash)',
233 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
235 $result = $q->fetch();
237 //If there is no other match a record does not exist, create one.
238 if (!$result['count'] > 0) {
239 $this->createRateLimit($fingerPrintInfo);
243 // Apply rate-limit when file count reached and timeout not reached.
244 if ($result['files'] === $fileLimit and !$this->compareTime($result['time'], $rateTimeout)) {
248 // Update timestamp if timeout reached.
249 if ($this->compareTime($result['time'], $rateTimeout)) {
250 $this->updateRateLimit($fingerPrintInfo['files_amount'], true, $fingerPrintInfo);
254 // Add filecount, timeout not reached.
255 if ($result['files'] < $fileLimit and !$this->compareTime($result['time'], $rateTimeout)) {
256 $this->updateRateLimit($result['files'] +
$fingerPrintInfo['files_amount'], false, $fingerPrintInfo);