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