]>
Commit | Line | Data |
---|---|---|
044a28cd GJ |
1 | <?php |
2 | /* | |
3 | * Uguu | |
4 | * | |
5156099c | 5 | * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se> |
044a28cd GJ |
6 | * |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
21 | ||
22 | require_once 'Core.namespace.php'; | |
23 | ||
24 | use Core\Database as Database; | |
044a28cd GJ |
25 | use Core\Settings as Settings; |
26 | ||
27 | class Upload | |
28 | { | |
82202428 | 29 | |
044a28cd GJ |
30 | public static string $FILE_NAME; |
31 | public static string $FILE_EXTENSION; | |
32 | public static string $FILE_MIME; | |
33 | public static string $SHA1; | |
044a28cd GJ |
34 | public static string $NEW_NAME; |
35 | public static string $NEW_NAME_FULL; | |
36 | public static string $IP; | |
044a28cd | 37 | |
82202428 GJ |
38 | public static string $FILE_SIZE; |
39 | public static string $TEMP_FILE; | |
40 | ||
41 | ||
42 | public function reFiles($files): array | |
43 | { | |
44 | $result = []; | |
45 | $files = self::diverseArray($files); | |
46 | ||
47 | foreach ($files as $file) { | |
48 | self::$FILE_NAME = $file['name']; | |
49 | self::$FILE_SIZE = $file['size']; | |
50 | self::$TEMP_FILE = $file['tmp_name']; | |
45bc029d GJ |
51 | self::$SHA1 = sha1_file(self::$TEMP_FILE); |
52 | $result[] = [self::$FILE_NAME, self::$FILE_SIZE, self::$TEMP_FILE, self::$SHA1]; | |
82202428 GJ |
53 | } |
54 | return $result; | |
55 | } | |
56 | ||
57 | public function diverseArray($files): array | |
58 | { | |
59 | $result = []; | |
60 | ||
61 | foreach ($files as $key1 => $value1) { | |
62 | foreach ($value1 as $key2 => $value2) { | |
63 | $result[$key2][$key1] = $value2; | |
64 | } | |
65 | } | |
66 | return $result; | |
67 | } | |
68 | ||
4c21cfa0 GJ |
69 | /** |
70 | * @throws Exception | |
71 | */ | |
72 | public function uploadFile(): array | |
044a28cd | 73 | { |
82202428 | 74 | (new Settings())->loadConfig(); |
0a3934c2 | 75 | (new Upload())->fileInfo(); |
82202428 | 76 | |
0a3934c2 GJ |
77 | if (Settings::$BLACKLIST_DB) { |
78 | (new Database())->checkFileBlacklist(); | |
044a28cd GJ |
79 | } |
80 | ||
0a3934c2 GJ |
81 | if (Settings::$FILTER_MODE) { |
82 | self::checkMimeBlacklist(); | |
83 | self::checkExtensionBlacklist(); | |
84 | } | |
82202428 | 85 | |
0a3934c2 GJ |
86 | if (Settings::$ANTI_DUPE) { |
87 | $result = (new Database())->antiDupe(); | |
88 | if (isset($result)) { | |
89 | self::$NEW_NAME_FULL = $result; | |
90 | } else { | |
91 | (new Upload())->generateName(); | |
92 | } | |
93 | } | |
94 | ||
95 | if (!Settings::$ANTI_DUPE) { | |
96 | (new Upload())->generateName(); | |
97 | } | |
044a28cd | 98 | |
82202428 GJ |
99 | if (!is_dir(Settings::$FILES_ROOT)) { |
100 | throw new Exception('File storage path not accessible.', 500); | |
101 | } | |
102 | ||
103 | if (!move_uploaded_file(self::$TEMP_FILE, Settings::$FILES_ROOT . self::$NEW_NAME_FULL)) { | |
104 | throw new Exception('Failed to move file to destination', 500); | |
044a28cd GJ |
105 | } |
106 | ||
107 | if (!chmod(Settings::$FILES_ROOT . self::$NEW_NAME_FULL, 0644)) { | |
82202428 | 108 | throw new Exception('Failed to change file permissions', 500); |
044a28cd GJ |
109 | } |
110 | ||
111 | (new Database())->newIntoDB(); | |
112 | ||
82202428 GJ |
113 | if (Settings::$SSL) { |
114 | $preURL = 'https://'; | |
115 | } else { | |
116 | $preURL = 'http://'; | |
117 | } | |
118 | ||
119 | return [ | |
044a28cd GJ |
120 | 'hash' => self::$SHA1, |
121 | 'name' => self::$FILE_NAME, | |
82202428 | 122 | 'url' => $preURL . Settings::$URL . '/' . rawurlencode(self::$NEW_NAME_FULL), |
044a28cd | 123 | 'size' => self::$FILE_SIZE |
82202428 | 124 | ]; |
044a28cd | 125 | } |
0a3934c2 | 126 | |
8fa8e4d3 GJ |
127 | public function fileInfo() |
128 | { | |
129 | if (isset($_FILES['files'])) { | |
8fa8e4d3 GJ |
130 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
131 | self::$FILE_MIME = finfo_file($finfo, self::$TEMP_FILE); | |
0a3934c2 | 132 | $extension = explode('.', self::$FILE_NAME, 2); |
8fa8e4d3 GJ |
133 | self::$FILE_EXTENSION = $extension['1']; |
134 | finfo_close($finfo); | |
044a28cd | 135 | |
8fa8e4d3 GJ |
136 | if (Settings::$LOG_IP) { |
137 | self::$IP = $_SERVER['REMOTE_ADDR']; | |
138 | } else { | |
139 | self::$IP = '0'; | |
140 | } | |
141 | } | |
142 | } | |
0a3934c2 | 143 | |
4c21cfa0 GJ |
144 | /** |
145 | * @throws Exception | |
146 | */ | |
0a3934c2 | 147 | public function checkMimeBlacklist() |
044a28cd | 148 | { |
0a3934c2 GJ |
149 | if (in_array(self::$FILE_MIME, Settings::$BLOCKED_MIME)) { |
150 | throw new Exception('Filetype not allowed.', 415); | |
151 | } | |
152 | } | |
153 | ||
154 | /** | |
155 | * @throws Exception | |
156 | */ | |
157 | public function checkExtensionBlacklist() | |
158 | { | |
159 | if (in_array(self::$FILE_EXTENSION, Settings::$BLOCKED_EXTENSIONS)) { | |
160 | throw new Exception('Filetype not allowed.', 415); | |
161 | } | |
162 | } | |
044a28cd | 163 | |
0a3934c2 GJ |
164 | /** |
165 | * @throws Exception | |
166 | */ | |
167 | public function generateName(): string | |
168 | { | |
044a28cd | 169 | do { |
044a28cd | 170 | if (Settings::$FILES_RETRIES === 0) { |
82202428 | 171 | throw new Exception('Gave up trying to find an unused name!', 500); |
044a28cd GJ |
172 | } |
173 | ||
82202428 | 174 | self::$NEW_NAME = ''; |
044a28cd GJ |
175 | for ($i = 0; $i < Settings::$NAME_LENGTH; ++$i) { |
176 | self::$NEW_NAME .= Settings::$ID_CHARSET[mt_rand(0, strlen(Settings::$ID_CHARSET))]; | |
177 | } | |
178 | ||
0a3934c2 | 179 | if (isset(self::$FILE_EXTENSION)) { |
8fa8e4d3 | 180 | self::$NEW_NAME_FULL = self::$NEW_NAME; |
0a3934c2 | 181 | self::$NEW_NAME_FULL .= '.' . self::$FILE_EXTENSION; |
044a28cd GJ |
182 | } |
183 | } while ((new Database())->dbCheckNameExists() > 0); | |
184 | ||
185 | return self::$NEW_NAME_FULL; | |
186 | } | |
0a3934c2 | 187 | } |