]> jfr.im git - uguu.git/blobdiff - static/php/includes/Upload.class.php
1.5.0 changes
[uguu.git] / static / php / includes / Upload.class.php
index 8c7c0734596cb36e5de623e4ba581bc58534dd66..31bb2d1ab628f581900006111735d6cdb990d6a5 100644 (file)
@@ -39,7 +39,7 @@ class Upload
     public static string $TEMP_FILE;
 
 
-    public function reFiles($files): array
+    public static function reFiles($files): array
     {
         $result = [];
         $files = self::diverseArray($files);
@@ -54,7 +54,7 @@ class Upload
         return $result;
     }
 
-    public function diverseArray($files): array
+    public static function diverseArray($files): array
     {
         $result = [];
 
@@ -69,16 +69,27 @@ class Upload
     /**
      * @throws Exception
      */
-    public function uploadFile(): array
+    public static function uploadFile(): array
     {
-        (new Settings())->loadConfig();
+        Settings::loadConfig();
+        self::fileInfo();
 
-        if (Settings::$ANTI_DUPE) {
-            (new Database())->antiDupe();
+        if (Settings::$BLACKLIST_DB) {
+            Database::checkFileBlacklist();
+        }
+
+        if (Settings::$FILTER_MODE) {
+            self::checkMimeBlacklist();
+            self::checkExtensionBlacklist();
         }
 
-        (new Upload())->generateName();
+        if (Settings::$ANTI_DUPE) {
+            Database::antiDupe();
+        }
 
+        if (!Settings::$ANTI_DUPE) {
+            self::generateName();
+        }
 
         if (!is_dir(Settings::$FILES_ROOT)) {
             throw new Exception('File storage path not accessible.', 500);
@@ -92,7 +103,7 @@ class Upload
             throw new Exception('Failed to change file permissions', 500);
         }
 
-        (new Database())->newIntoDB();
+        Database::newIntoDB();
 
         if (Settings::$SSL) {
             $preURL = 'https://';
@@ -107,12 +118,13 @@ class Upload
             'size' => self::$FILE_SIZE
         ];
     }
-    public function fileInfo()
+
+    public static function fileInfo()
     {
         if (isset($_FILES['files'])) {
             $finfo = finfo_open(FILEINFO_MIME_TYPE);
             self::$FILE_MIME = finfo_file($finfo, self::$TEMP_FILE);
-            $extension = explode('.',self::$FILE_NAME,2);
+            $extension = explode('.', self::$FILE_NAME, 2);
             self::$FILE_EXTENSION = $extension['1'];
             finfo_close($finfo);
 
@@ -123,47 +135,26 @@ class Upload
             }
         }
     }
+
     /**
      * @throws Exception
      */
-    public function generateName(): string
+    public static function checkMimeBlacklist()
     {
-        (new Upload())->fileInfo();
-
-        do {
-            if (Settings::$FILES_RETRIES === 0) {
-                throw new Exception('Gave up trying to find an unused name!', 500);
-            }
-
-            self::$NEW_NAME = '';
-            for ($i = 0; $i < Settings::$NAME_LENGTH; ++$i) {
-                self::$NEW_NAME .= Settings::$ID_CHARSET[mt_rand(0, strlen(Settings::$ID_CHARSET))];
-            }
-
-            if(isset(self::$FILE_EXTENSION)){
-                self::$NEW_NAME_FULL = self::$NEW_NAME;
-                self::$NEW_NAME_FULL .= '.'.self::$FILE_EXTENSION;
-            }
-
-            if (Settings::$BLACKLIST_DB) {
-                (new Database())->checkFileBlacklist();
-            }
-
-            if (Settings::$FILTER_MODE) {
-                self::checkMimeBlacklist();
-                self::checkExtensionBlacklist();
-            }
-        } while ((new Database())->dbCheckNameExists() > 0);
-
-        return self::$NEW_NAME_FULL;
+        if (in_array(self::$FILE_MIME, Settings::$BLOCKED_MIME)) {
+            throw new Exception('Filetype not allowed.', 415);
+        }
     }
 
     /**
+     * Check if file extension is blacklisted
+     * if it does throw an exception.
+     *
      * @throws Exception
      */
-    public function checkMimeBlacklist()
+    public static function checkExtensionBlacklist()
     {
-        if (in_array(self::$FILE_MIME, Settings::$BLOCKED_MIME)) {
+        if (in_array(self::$FILE_EXTENSION, Settings::$BLOCKED_EXTENSIONS)) {
             throw new Exception('Filetype not allowed.', 415);
         }
     }
@@ -171,10 +162,22 @@ class Upload
     /**
      * @throws Exception
      */
-    public function checkExtensionBlacklist()
+    public static function generateName()
     {
-        if (in_array(self::$FILE_EXTENSION, Settings::$BLOCKED_EXTENSIONS)) {
-            throw new Exception('Filetype not allowed.', 415);
-        }
+        do {
+            if (Settings::$FILES_RETRIES === 0) {
+                throw new Exception('Gave up trying to find an unused name!', 500);
+            }
+
+            self::$NEW_NAME = '';
+            for ($i = 0; $i < Settings::$NAME_LENGTH; ++$i) {
+                self::$NEW_NAME .= Settings::$ID_CHARSET[mt_rand(0, strlen(Settings::$ID_CHARSET))];
+            }
+
+            if (isset(self::$FILE_EXTENSION)) {
+                self::$NEW_NAME_FULL = self::$NEW_NAME;
+                self::$NEW_NAME_FULL .= '.' . self::$FILE_EXTENSION;
+            }
+        } while (Database::dbCheckNameExists() > 0);
     }
-}
+}
\ No newline at end of file