]>
jfr.im git - uguu.git/blob - static/php/includes/Core.namespace.php
5 * @copyright Copyright (c) 2022 Go Johansson (nekunekus) <neku@pomf.se> <github.com/nokonoko>
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.
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.
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/>.
29 public static mixed $DB;
31 public static string $DB_MODE;
32 public static string $DB_PATH;
33 public static string $DB_USER;
34 public static string $DB_PASS;
36 public static bool $LOG_IP;
37 public static bool $ANTI_DUPE;
38 public static bool $BLACKLIST_DB;
39 public static bool $FILTER_MODE;
41 public static string $FILES_ROOT;
42 public static int $FILES_RETRIES;
44 public static bool $SSL;
45 public static string $URL;
47 public static int $NAME_LENGTH;
48 public static string $ID_CHARSET;
49 public static array $DOUBLE_DOTS;
50 public static array $BLOCKED_EXTENSIONS;
51 public static array $BLOCKED_MIME;
54 public function __construct()
56 $settings_array = json_decode(file_get_contents('/Users/go.johansson/PERSONAL_REPOS/Uguu/dist.json'), true);
57 self
::$DB_MODE = $settings_array['DB_MODE'];
58 self
::$DB_PATH = $settings_array['DB_PATH'];
59 self
::$DB_USER = $settings_array['DB_USER'];
60 self
::$DB_PASS = $settings_array['DB_PASS'];
61 self
::$LOG_IP = $settings_array['LOG_IP'];
62 self
::$ANTI_DUPE = $settings_array['ANTI_DUPE'];
63 self
::$BLACKLIST_DB = $settings_array['BLACKLIST_DB'];
64 self
::$FILTER_MODE = $settings_array['FILTER_MODE'];
65 self
::$FILES_ROOT = $settings_array['FILES_ROOT'];
66 self
::$FILES_RETRIES = $settings_array['FILES_RETRIES'];
67 self
::$SSL = $settings_array['SSL'];
68 self
::$URL = $settings_array['URL'];
69 self
::$NAME_LENGTH = $settings_array['NAME_LENGTH'];
70 self
::$ID_CHARSET = $settings_array['ID_CHARSET'];
71 self
::$BLOCKED_EXTENSIONS = $settings_array['BLOCKED_EXTENSIONS'];
72 self
::$BLOCKED_MIME = $settings_array['BLOCKED_MIME'];
73 self
::$DOUBLE_DOTS = array($settings_array['DOUBLE_DOTS']);
79 public static array $GRILLS;
81 public function __construct()
83 self
::$GRILLS = array_slice(scandir('/Users/go.johansson/PERSONAL_REPOS/Uguu/dist/img/grills/'), 2);
87 public static function showGrills()
89 if (!headers_sent()) {
90 header('Location: ' . self
::$GRILLS[array_rand(self
::$GRILLS)], true, 303);
97 public function returnError($code, $message, $filename): bool|string
99 http_response_code($code);
100 header('Content-Type: application/json; charset=UTF-8');
102 return json_encode(array(
106 'description' => $message
107 ), JSON_FORCE_OBJECT
);
110 public function cleanAndDie()
112 Settings
::$DB = null;
115 public function returnSuccess($files): bool|string
117 http_response_code('200');
118 header('Content-Type: application/json; charset=UTF-8');
119 return json_encode(array(
122 ), JSON_PRETTY_PRINT
);
130 public function __construct()
132 Settings
::$DB = new PDO(
133 Settings
::$DB_MODE . ':' . Settings
::$DB_PATH, Settings
::$DB_USER,
138 public function dbCheckNameExists()
140 $q = Settings
::$DB->prepare('SELECT COUNT(filename) FROM files WHERE filename = (:name)');
141 $q->bindValue(':name', Upload
::$NEW_NAME_FULL);
143 return $q->fetchColumn();
146 public function checkFileBlacklist()
148 $q = Settings
::$DB->prepare('SELECT hash, COUNT(*) AS count FROM blacklist WHERE hash = (:hash)');
149 $q->bindValue(':hash', Upload
::$SHA1, PDO
::PARAM_STR
);
151 $result = $q->fetch();
152 if ($result['count'] > 0) {
153 (new Response())->returnError('415', 'File blacklisted!', Upload
::$FILE_NAME);
157 public function antiDupe(): ?array
159 $q = Settings
::$DB->prepare(
160 'SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) AND size = (:size)'
162 $q->bindValue(':hash', Upload
::$SHA1, PDO
::PARAM_STR
);
163 $q->bindValue(':size', Upload
::$FILE_SIZE, PDO
::PARAM_INT
);
165 $result = $q->fetch();
166 if ($result['count'] > 0) {
168 'hash' => Upload
::$SHA1,
169 'name' => Upload
::$FILE_NAME,
170 'url' => Settings
::$URL . rawurlencode($result['filename']),
171 'size' => Upload
::$FILE_SIZE
177 public function newIntoDB()
179 $q = Settings
::$DB->prepare(
180 'INSERT INTO files (hash, originalname, filename, size, date, ip)' .
181 'VALUES (:hash, :orig, :name, :size, :date, :ip)'
183 $q->bindValue(':hash', Upload
::$SHA1, PDO
::PARAM_STR
);
184 $q->bindValue(':orig', strip_tags(Upload
::$FILE_NAME), PDO
::PARAM_STR
);
185 $q->bindValue(':name', Upload
::$NEW_NAME_FULL, PDO
::PARAM_STR
);
186 $q->bindValue(':size', Upload
::$FILE_SIZE, PDO
::PARAM_INT
);
187 $q->bindValue(':date', time(), PDO
::PARAM_STR
);
188 $q->bindValue(':ip', Upload
::$IP, PDO
::PARAM_STR
);