]> jfr.im git - uguu.git/blobdiff - src/Classes/Upload.php
add extension determination when its not provided
[uguu.git] / src / Classes / Upload.php
index e7becc081b3af7afed0cd0fc3036df0315461239..8821dbc0028cd7620145b55b7cb6a1f135e64bfc 100644 (file)
                 $USER_AGENT = filter_var($_SERVER['HTTP_USER_AGENT'], FILTER_SANITIZE_ENCODED);
                 $ip = null;
                 if ($this->Connector->CONFIG['LOG_IP']) {
-                    $ip = $_SERVER['REMOTE_ADDR'];
+                    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
                 }
                 $this->fingerPrintInfo = [
                    'timestamp'    => time(),
             $extension = explode('.', $file['name']);
             $dotCount = substr_count($file['name'], '.');
             return match ($dotCount) {
-                0 => null,
+                0 => $this->lookupExtension($file['type']),
                 1 => end($extension),
                 2 => $this->doubleDotExtension($extension),
                 default => end($extension)
             } while ($this->Connector->dbCheckNameExists($NEW_NAME));
             return $NEW_NAME;
         }
-    }
\ No newline at end of file
+
+        private function lookupExtension(string $mimetype):string
+        {
+            $types = [
+                'image/gif' => 'gif',
+                'image/jpeg' => 'jpg',
+                'image/avif' => 'avif',
+                'image/png' => 'png',
+                'image/tiff' => 'tiff',
+                'image/vnd.wap.wbmp' => 'wbmp',
+                'image/webp' => 'webp',
+                'image/x-icon' => 'ico',
+                'image/x-jng' => 'jng',
+                'image/x-ms-bmp' => 'bmp',
+                'application/pdf' => 'pdf',
+                'application/postscript' => 'ps',
+                'application/x-7z-compressed' => '7z',
+                'application/zip' => 'zip',
+                'audio/midi' => 'mid',
+                'audio/mpeg' => 'mp3',
+                'audio/ogg' => 'ogg',
+                'audio/x-m4a' => 'm4a',
+                'audio/x-realaudio' => 'ra',
+                'video/3gpp' => '3gpp',
+                'video/mp2t' => 'ts',
+                'video/mp4' => 'mp4',
+                'video/mpeg' => 'mpeg',
+                'video/quicktime' => 'mov',
+                'video/webm' => 'webm',
+                'video/x-flv' => 'flv',
+                'video/x-m4v' => 'm4v',
+                'video/x-mng' => 'mng',
+                'video/x-ms-asf' => 'asx',
+                'video/x-ms-wmv' => 'wmv',
+                'video/x-msvideo' => 'avi',
+            ];
+            if (isset($types[$mimetype])) {
+                return $types[$mimetype];
+            } else {
+                $this->Connector->response->error(400, 'Unknown MIME type. Add a file extension to your filename.');
+            }
+        }
+    }