]>
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
;
32 * Sets the value of the DB variable.
34 * @param $DB PDO The database connection.
36 public function setDB(PDO
$DB): void
42 * Checks if a file name exists in the database
44 * @param $name string The name of the file.
46 * @return int The number of rows that match the query.
49 public function dbCheckNameExists(string $name): int
52 $q = $this->DB
->prepare('SELECT COUNT(filename) FROM files WHERE filename = (:name)');
53 $q->bindValue(':name', $name);
55 return $q->fetchColumn();
57 throw new Exception('Cant check if name exists in DB.', 500);
62 * Checks if the file is blacklisted
64 * @param $FILE_INFO array An array containing the following:
68 public function checkFileBlacklist(array $FILE_INFO): void
71 $q = $this->DB
->prepare('SELECT hash, COUNT(*) AS count FROM blacklist WHERE hash = (:hash)');
72 $q->bindValue(':hash', $FILE_INFO['SHA1']);
74 $result = $q->fetch();
75 if ($result['count'] > 0) {
76 throw new Exception('File blacklisted!', 415);
79 throw new Exception('Cant check blacklist DB.', 500);
84 * Checks if the file already exists in the database
86 * @param $hash string The hash of the file you want to check for.
90 public function antiDupe(string $hash): array
93 $q = $this->DB
->prepare(
94 'SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash)',
96 $q->bindValue(':hash', $hash);
98 $result = $q->fetch();
99 if ($result['count'] > 0) {
102 'name' => $result['filename'],
109 } catch (Exception
) {
110 throw new Exception('Cant check for dupes in DB.', 500);
115 * Inserts a new file into the database
117 * @param $FILE_INFO array
118 * @param $fingerPrintInfo array
122 public function newIntoDB(array $FILE_INFO, array $fingerPrintInfo): void
125 $q = $this->DB
->prepare(
126 'INSERT INTO files (hash, originalname, filename, size, date, ip)' .
127 'VALUES (:hash, :orig, :name, :size, :date, :ip)',
129 $q->bindValue(':hash', $FILE_INFO['SHA1']);
130 $q->bindValue(':orig', $FILE_INFO['NAME']);
131 $q->bindValue(':name', $FILE_INFO['NEW_NAME']);
132 $q->bindValue(':size', $FILE_INFO['SIZE'], PDO
::PARAM_INT
);
133 $q->bindValue(':date', $fingerPrintInfo['timestamp']);
134 $q->bindValue(':ip', $fingerPrintInfo['ip']);
136 } catch (Exception
) {
137 throw new Exception('Cant insert into DB.', 500);
142 * Creates a new row in the database with the information provided
144 * @param $fingerPrintInfo array
146 public function createRateLimit(array $fingerPrintInfo): void
148 $q = $this->DB
->prepare(
149 'INSERT INTO timestamp (iphash, files, time)' .
150 'VALUES (:iphash, :files, :time)',
152 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
153 $q->bindValue(':files', $fingerPrintInfo['files_amount']);
154 $q->bindValue(':time', $fingerPrintInfo['timestamp']);
159 * Update the rate limit table with the new file count and timestamp
161 * @param $fCount int The number of files uploaded by the user.
162 * @param $iStamp boolean A boolean value that determines whether or not to update the timestamp.
163 * @param $fingerPrintInfo array An array containing the following keys:
165 public function updateRateLimit(int $fCount, bool $iStamp, array $fingerPrintInfo): void
168 $q = $this->DB
->prepare(
169 'UPDATE ratelimit SET files = (:files), time = (:time) WHERE iphash = (:iphash)',
171 $q->bindValue(':time', $fingerPrintInfo['timestamp']);
173 $q = $this->DB
->prepare(
174 'UPDATE ratelimit SET files = (:files) WHERE iphash = (:iphash)',
177 $q->bindValue(':files', $fCount);
178 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
183 * Checks if the user has uploaded more than 100 files in the last minute, if so it returns true, if not it updates the database with the new file
184 * count and timestamp
186 * @param $fingerPrintInfo array An array containing the following:
188 * @return bool A boolean value.
190 public function checkRateLimit(array $fingerPrintInfo): bool
192 $q = $this->DB
->prepare(
193 'SELECT files, time, iphash, COUNT(*) AS count FROM ratelimit WHERE iphash = (:iphash)',
195 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
197 $result = $q->fetch();
198 $nTime = $fingerPrintInfo['timestamp'] - (60);
200 //If more then 100 files trigger rate-limit
201 case $result['files'] > 100:
203 //if timestamp is older than one minute, set new files count and timestamp
204 case $result['time'] < $nTime:
205 $this->updateRateLimit($fingerPrintInfo['files_amount'], true, $fingerPrintInfo);
207 //if timestamp isn't older than one-minute update the files count
208 case $result['time'] > $nTime:
209 $this->updateRateLimit($fingerPrintInfo['files_amount'] +
$result['files'], false, $fingerPrintInfo);
211 //If there is no other match a record does not exist, create one
213 $this->createRateLimit($fingerPrintInfo);