]> jfr.im git - uguu.git/blob - src/Classes/Database.php
bug fixes
[uguu.git] / src / Classes / Database.php
1 <?php
2
3 /**
4 * Uguu
5 *
6 * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 namespace Pomf\Uguu\Classes;
23
24 use Exception;
25 use PDO;
26
27 class Database
28 {
29 private PDO $DB;
30
31 /**
32 * Sets the value of the DB variable.
33 *
34 * @param $DB PDO The database connection.
35 */
36 public function setDB(PDO $DB): void
37 {
38 $this->DB = $DB;
39 }
40
41 /**
42 * Checks if a file name exists in the database
43 *
44 * @param $name string The name of the file.
45 *
46 * @return int The number of rows that match the query.
47 * @throws \Exception
48 */
49 public function dbCheckNameExists(string $name): int
50 {
51 try {
52 $q = $this->DB->prepare('SELECT COUNT(filename) FROM files WHERE filename = (:name)');
53 $q->bindValue(':name', $name);
54 $q->execute();
55 return $q->fetchColumn();
56 } catch (Exception) {
57 throw new Exception('Cant check if name exists in DB.', 500);
58 }
59 }
60
61 /**
62 * Checks if the file is blacklisted
63 *
64 * @param $FILE_INFO array An array containing the following:
65 *
66 * @throws \Exception
67 */
68 public function checkFileBlacklist(array $FILE_INFO): void
69 {
70 try {
71 $q = $this->DB->prepare('SELECT hash, COUNT(*) AS count FROM blacklist WHERE hash = (:hash)');
72 $q->bindValue(':hash', $FILE_INFO['SHA1']);
73 $q->execute();
74 $result = $q->fetch();
75 if ($result['count'] > 0) {
76 throw new Exception('File blacklisted!', 415);
77 }
78 } catch (Exception) {
79 throw new Exception('Cant check blacklist DB.', 500);
80 }
81 }
82
83 /**
84 * Checks if the file already exists in the database
85 *
86 * @param $hash string The hash of the file you want to check for.
87 *
88 * @throws \Exception
89 */
90 public function antiDupe(string $hash): array
91 {
92 try {
93 $q = $this->DB->prepare(
94 'SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash)',
95 );
96 $q->bindValue(':hash', $hash);
97 $q->execute();
98 $result = $q->fetch();
99 if ($result['count'] > 0) {
100 return [
101 'result' => true,
102 'name' => $result['filename'],
103 ];
104 } else {
105 return [
106 'result' => false
107 ];
108 }
109 } catch (Exception) {
110 throw new Exception('Cant check for dupes in DB.', 500);
111 }
112 }
113
114 /**
115 * Inserts a new file into the database
116 *
117 * @param $FILE_INFO array
118 * @param $fingerPrintInfo array
119 *
120 * @throws \Exception
121 */
122 public function newIntoDB(array $FILE_INFO, array $fingerPrintInfo): void
123 {
124 try {
125 $q = $this->DB->prepare(
126 'INSERT INTO files (hash, originalname, filename, size, date, ip)' .
127 'VALUES (:hash, :orig, :name, :size, :date, :ip)',
128 );
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']);
135 $q->execute();
136 } catch (Exception) {
137 throw new Exception('Cant insert into DB.', 500);
138 }
139 }
140
141 /**
142 * Creates a new row in the database with the information provided
143 *
144 * @param $fingerPrintInfo array
145 */
146 public function createRateLimit(array $fingerPrintInfo): void
147 {
148 $q = $this->DB->prepare(
149 'INSERT INTO timestamp (iphash, files, time)' .
150 'VALUES (:iphash, :files, :time)',
151 );
152 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
153 $q->bindValue(':files', $fingerPrintInfo['files_amount']);
154 $q->bindValue(':time', $fingerPrintInfo['timestamp']);
155 $q->execute();
156 }
157
158 /**
159 * Update the rate limit table with the new file count and timestamp
160 *
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:
164 */
165 public function updateRateLimit(int $fCount, bool $iStamp, array $fingerPrintInfo): void
166 {
167 if ($iStamp) {
168 $q = $this->DB->prepare(
169 'UPDATE ratelimit SET files = (:files), time = (:time) WHERE iphash = (:iphash)',
170 );
171 $q->bindValue(':time', $fingerPrintInfo['timestamp']);
172 } else {
173 $q = $this->DB->prepare(
174 'UPDATE ratelimit SET files = (:files) WHERE iphash = (:iphash)',
175 );
176 }
177 $q->bindValue(':files', $fCount);
178 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
179 $q->execute();
180 }
181
182 /**
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
185 *
186 * @param $fingerPrintInfo array An array containing the following:
187 *
188 * @return bool A boolean value.
189 */
190 public function checkRateLimit(array $fingerPrintInfo): bool
191 {
192 $q = $this->DB->prepare(
193 'SELECT files, time, iphash, COUNT(*) AS count FROM ratelimit WHERE iphash = (:iphash)',
194 );
195 $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
196 $q->execute();
197 $result = $q->fetch();
198 $nTime = $fingerPrintInfo['timestamp'] - (60);
199 switch (true) {
200 //If more then 100 files trigger rate-limit
201 case $result['files'] > 100:
202 return true;
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);
206 break;
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);
210 break;
211 //If there is no other match a record does not exist, create one
212 default:
213 $this->createRateLimit($fingerPrintInfo);
214 break;
215 }
216 return false;
217 }
218 }