]>
jfr.im git - uguu.git/blob - static/php/includes/Core.namespace.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/>.
25 require_once 'Upload.class.php';
34 public static mixed $DB;
36 public static string $DB_MODE;
37 public static string $DB_PATH;
38 public static string $DB_USER;
39 public static string $DB_PASS;
41 public static bool $LOG_IP;
42 public static bool $ANTI_DUPE;
43 public static bool $BLACKLIST_DB;
44 public static bool $FILTER_MODE;
46 public static string $FILES_ROOT;
47 public static int $FILES_RETRIES;
49 public static bool $SSL;
50 public static string $URL;
52 public static int $NAME_LENGTH;
53 public static string $ID_CHARSET;
54 public static array $BLOCKED_EXTENSIONS;
55 public static array $BLOCKED_MIME;
61 public static function loadConfig()
63 if (!file_exists('/var/www/uguu/dist.json')) {
64 throw new Exception('Cant read settings file.', 500);
67 $settings_array = json_decode(
68 file_get_contents('/var/www/uguu/dist.json'),
71 self
::$DB_MODE = $settings_array['DB_MODE'];
72 self
::$DB_PATH = $settings_array['DB_PATH'];
73 self
::$DB_USER = $settings_array['DB_USER'];
74 self
::$DB_PASS = $settings_array['DB_PASS'];
75 self
::$LOG_IP = $settings_array['LOG_IP'];
76 self
::$ANTI_DUPE = $settings_array['ANTI_DUPE'];
77 self
::$BLACKLIST_DB = $settings_array['BLACKLIST_DB'];
78 self
::$FILTER_MODE = $settings_array['FILTER_MODE'];
79 self
::$FILES_ROOT = $settings_array['FILES_ROOT'];
80 self
::$FILES_RETRIES = $settings_array['FILES_RETRIES'];
81 self
::$SSL = $settings_array['SSL'];
82 self
::$URL = $settings_array['URL'];
83 self
::$NAME_LENGTH = $settings_array['NAME_LENGTH'];
84 self
::$ID_CHARSET = $settings_array['ID_CHARSET'];
85 self
::$BLOCKED_EXTENSIONS = $settings_array['BLOCKED_EXTENSIONS'];
86 self
::$BLOCKED_MIME = $settings_array['BLOCKED_MIME'];
88 throw new Exception('Cant populate settings.', 500);
90 (new Database())->assemblePDO();
96 public static array $GRILLS;
98 public static function showGrills()
101 if (!headers_sent()) {
103 'Location: /img/grills/' .
104 self
::$GRILLS[array_rand(self
::$GRILLS)],
111 public static function loadGrills()
113 self
::$GRILLS = array_slice(scandir('img/grills/'), 2);
121 public function __construct($response_type = null)
123 switch ($response_type) {
125 header('Content-Type: text/csv; charset=UTF-8');
126 $this->type
= $response_type;
129 header('Content-Type: text/html; charset=UTF-8');
130 $this->type
= $response_type;
133 header('Content-Type: application/json; charset=UTF-8');
134 $this->type
= $response_type;
137 header('Content-Type: text/plain; charset=UTF-8');
138 $this->type
= 'text';
141 header('Content-Type: text/plain; charset=UTF-8');
142 $this->type
= $response_type;
145 header('Content-Type: application/json; charset=UTF-8');
146 $this->type
= 'json';
147 $this->error(400, 'Invalid response type. Valid options are: csv, html, json, text.');
152 public function error($code, $desc)
156 switch ($this->type
) {
158 $response = $this->csvError($desc);
161 $response = $this->htmlError($code, $desc);
164 $response = $this->jsonError($code, $desc);
167 $response = $this->textError($code, $desc);
170 http_response_code($code);
174 private static function csvError($description): string
176 return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
179 private static function htmlError($code, $description): string
181 return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
184 private static function jsonError($code, $description): bool|string
188 'errorcode' => $code,
189 'description' => $description,
190 ], JSON_PRETTY_PRINT
);
194 private static function textError($code, $description): string
196 return 'ERROR: (' . $code . ') ' . $description;
199 public function send($files)
203 switch ($this->type
) {
205 $response = $this->csvSuccess($files);
208 $response = $this->htmlSuccess($files);
211 $response = $this->jsonSuccess($files);
214 $response = $this->textSuccess($files);
218 http_response_code(200); // "200 OK". Success.
222 private static function csvSuccess($files): string
224 $result = '"name","url","hash","size"' . "\r\n";
225 foreach ($files as $file) {
226 $result .= '"' . $file['name'] . '"' . ',' .
227 '"' . $file['url'] . '"' . ',' .
228 '"' . $file['hash'] . '"' . ',' .
229 '"' . $file['size'] . '"' . "\r\n";
235 private static function htmlSuccess($files): string
239 foreach ($files as $file) {
240 $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>';
246 private static function jsonSuccess($files): bool|string
251 ], JSON_PRETTY_PRINT
);
254 private static function textSuccess($files): string
258 foreach ($files as $file) {
259 $result .= $file['url'] . "\n";
271 public static function assemblePDO()
274 Settings
::$DB = new PDO(
275 Settings
::$DB_MODE . ':' . Settings
::$DB_PATH, Settings
::$DB_USER,
278 } catch (Exception
) {
279 throw new Exception('Cant connect to DB.', 500);
286 public function dbCheckNameExists()
289 $q = Settings
::$DB->prepare('SELECT COUNT(filename) FROM files WHERE filename = (:name)');
290 $q->bindValue(':name', Upload
::$NEW_NAME_FULL);
292 return $q->fetchColumn();
293 } catch (Exception
) {
294 throw new Exception('Cant check if name exists in DB.', 500);
301 public function checkFileBlacklist()
304 $q = Settings
::$DB->prepare('SELECT hash, COUNT(*) AS count FROM blacklist WHERE hash = (:hash)');
305 $q->bindValue(':hash', Upload
::$SHA1, PDO
::PARAM_STR
);
307 $result = $q->fetch();
308 if ($result['count'] > 0) {
309 throw new Exception('File blacklisted!', 415);
311 } catch (Exception
) {
312 throw new Exception('Cant check blacklist DB.', 500);
319 public function antiDupe()
322 $q = Settings
::$DB->prepare(
323 'SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) AND size = (:size)'
325 $q->bindValue(':hash', Upload
::$SHA1, PDO
::PARAM_STR
);
326 $q->bindValue(':size', Upload
::$FILE_SIZE, PDO
::PARAM_INT
);
328 $result = $q->fetch();
329 if ($result['count'] > 0) {
330 return $result['filename'];
332 } catch (Exception
) {
333 throw new Exception('Cant check for dupes in DB.', 500);
340 public function newIntoDB()
343 $q = Settings
::$DB->prepare(
344 'INSERT INTO files (hash, originalname, filename, size, date, ip)' .
345 'VALUES (:hash, :orig, :name, :size, :date, :ip)'
347 $q->bindValue(':hash', Upload
::$SHA1, PDO
::PARAM_STR
);
348 $q->bindValue(':orig', strip_tags(Upload
::$FILE_NAME), PDO
::PARAM_STR
);
349 $q->bindValue(':name', Upload
::$NEW_NAME_FULL, PDO
::PARAM_STR
);
350 $q->bindValue(':size', Upload
::$FILE_SIZE, PDO
::PARAM_INT
);
351 $q->bindValue(':date', time(), PDO
::PARAM_STR
);
352 $q->bindValue(':ip', Upload
::$IP, PDO
::PARAM_STR
);
354 } catch (Exception
) {
355 throw new Exception('Cant insert into DB.', 500);