]>
jfr.im git - uguu.git/blob - src/Classes/Upload.php
17e79f8301f53e42d0eea7d5a16eca071f3fa22c
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/>.
22 namespace Pomf\Uguu\Classes
;
26 class Upload
extends Response
28 public array $FILE_INFO;
29 public array $fingerPrintInfo;
30 private mixed $Connector;
36 public function reFiles($files): array
38 $this->Connector
= new Connector();
39 $this->Connector
->setDB($this->Connector
->DB
);
41 $files = $this->diverseArray($files);
42 foreach ($files as $file) {
43 $hash = sha1_file($file['tmp_name']);
45 'TEMP_NAME' => $file['tmp_name'],
46 'NAME' => strip_tags($file['name']),
47 'SIZE' => $file['size'],
49 'EXTENSION' => $this->fileExtension($file),
50 'MIME' => $this->fileMIME($file),
51 'NEW_NAME' => $this->generateName($this->fileExtension($file), $hash)
54 $this->FILE_INFO
['TEMP_NAME'],
55 $this->FILE_INFO
['NAME'],
56 $this->FILE_INFO
['SIZE'],
57 $this->FILE_INFO
['SHA1'],
58 $this->FILE_INFO
['EXTENSION'],
59 $this->FILE_INFO
['MIME']
64 public function diverseArray($files): array
67 foreach ($files as $key1 => $value1) {
68 foreach ($value1 as $key2 => $value2) {
69 $result[$key2][$key1] = $value2;
78 public function uploadFile(): array
81 if ($this->Connector
->CONFIG
['RATE_LIMIT']) {
82 $this->Connector
->checkRateLimit($this->fingerPrintInfo
);
85 if ($this->Connector
->CONFIG
['BLACKLIST_DB']) {
86 $this->Connector
->checkFileBlacklist($this->FILE_INFO
);
89 if ($this->Connector
->CONFIG
['FILTER_MODE'] && empty($this->FILE_INFO
['EXTENSION'])) {
90 $this->checkMimeBlacklist();
93 if ($this->Connector
->CONFIG
['FILTER_MODE'] && !empty($this->FILE_INFO
['EXTENSION'])) {
94 $this->checkMimeBlacklist();
95 $this->checkExtensionBlacklist();
98 if (!is_dir($this->Connector
->CONFIG
['FILES_ROOT'])) {
99 throw new Exception('File storage path not accessible.', 500);
103 !move_uploaded_file($this->FILE_INFO
['TEMP_NAME'], $this->Connector
->CONFIG
['FILES_ROOT'] .
104 $this->FILE_INFO
['NEW_NAME'])
106 throw new Exception('Failed to move file to destination', 500);
109 if (!chmod($this->Connector
->CONFIG
['FILES_ROOT'] . $this->FILE_INFO
['NEW_NAME'], 0644)) {
110 throw new Exception('Failed to change file permissions', 500);
113 if (!$this->Connector
->CONFIG
['LOG_IP']) {
114 $this->fingerPrintInfo
['ip'] = null;
117 $this->Connector
->newIntoDB($this->FILE_INFO
, $this->fingerPrintInfo
);
120 'hash' => $this->FILE_INFO
['SHA1'],
121 'name' => $this->FILE_INFO
['NAME'],
122 'url' => $this->Connector
->CONFIG
['FILES_URL'] . '/' . $this->FILE_INFO
['NEW_NAME'],
123 'size' => $this->FILE_INFO
['SIZE']
127 public function fingerPrint($files_amount): void
129 $this->fingerPrintInfo
= [
130 'timestamp' => time(),
131 'useragent' => $_SERVER['HTTP_USER_AGENT'],
132 'ip' => $_SERVER['REMOTE_ADDR'],
133 'ip_hash' => hash('sha1', $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']),
134 'files_amount' => $files_amount
139 public function fileMIME($file): string
141 $finfo = finfo_open(FILEINFO_MIME_TYPE
);
142 return finfo_file($finfo, $file['tmp_name']);
145 public function fileExtension($file): ?string
147 $extension = explode('.', $file['name']);
148 if (substr_count($file['name'], '.') > 0) {
149 return end($extension);
159 public function checkMimeBlacklist(): void
161 if (in_array($this->FILE_INFO
['MIME'], $this->Connector
->CONFIG
['BLOCKED_MIME'])) {
162 throw new Exception('Filetype not allowed.', 415);
167 * Check if file extension is blacklisted
168 * if it does throw an exception.
172 public function checkExtensionBlacklist(): void
174 if (in_array($this->FILE_INFO
['EXTENSION'], $this->Connector
->CONFIG
['BLOCKED_EXTENSIONS'])) {
175 throw new Exception('Filetype not allowed.', 415);
182 public function generateName($extension, $hash): string
184 $a = $this->Connector
->antiDupe($hash);
187 if ($this->Connector
->CONFIG
['FILES_RETRIES'] === 0) {
188 throw new Exception('Gave up trying to find an unused name!', 500);
192 for ($i = 0; $i < $this->Connector
->CONFIG
['NAME_LENGTH']; ++
$i) {
193 $NEW_NAME .= $this->Connector
->CONFIG
['ID_CHARSET']
194 [mt_rand(0, strlen($this->Connector
->CONFIG
['ID_CHARSET']))];
197 if (!is_null($extension)) {
198 $NEW_NAME .= '.' . $extension;
200 } while ($this->Connector
->dbCheckNameExists($NEW_NAME) > 0);