]> jfr.im git - uguu.git/blobdiff - static/php/includes/Core.namespace.php
refactoring
[uguu.git] / static / php / includes / Core.namespace.php
index 08c071fd3dc43f80d67837bc8d5dae7e906b5b27..fcd1d50af12c864c5e35031c3ab27767f93dac89 100644 (file)
@@ -3,7 +3,7 @@
 /*
  * Uguu
  *
- * @copyright Copyright (c) 2022 Go Johansson (nekunekus) <neku@pomf.se> <github.com/nokonoko>
+ * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@ namespace Core {
 
     require_once 'Upload.class.php';
 
+    use Exception;
     use PDO;
     use Upload as Upload;
 
@@ -50,19 +51,21 @@ namespace Core {
 
         public static int $NAME_LENGTH;
         public static string $ID_CHARSET;
-        public static array $DOUBLE_DOTS;
         public static array $BLOCKED_EXTENSIONS;
         public static array $BLOCKED_MIME;
 
 
+        /**
+         * @throws Exception
+         */
         public static function loadConfig()
         {
-            if (!file_exists('/Users/go.johansson/PERSONAL_REPOS/Uguu/dist.json')) {
-                throw new \Exception('Cant read settings file.', 500);
+            if (!file_exists('/var/www/uguu/dist.json')) {
+                throw new Exception('Cant read settings file.', 500);
             }
             try {
                 $settings_array = json_decode(
-                    file_get_contents('/Users/go.johansson/PERSONAL_REPOS/Uguu/dist.json'),
+                    file_get_contents('/var/www/uguu/dist.json'),
                     true
                 );
                 self::$DB_MODE = $settings_array['DB_MODE'];
@@ -81,11 +84,10 @@ namespace Core {
                 self::$ID_CHARSET = $settings_array['ID_CHARSET'];
                 self::$BLOCKED_EXTENSIONS = $settings_array['BLOCKED_EXTENSIONS'];
                 self::$BLOCKED_MIME = $settings_array['BLOCKED_MIME'];
-                self::$DOUBLE_DOTS = $settings_array['DOUBLE_DOTS'];
-            } catch (\Exception $e) {
-                throw new \Exception('Cant populate settings.', 500);
+            } catch (Exception) {
+                throw new Exception('Cant populate settings.', 500);
             }
-            (new Database())->assemblePDO();
+            Database::assemblePDO();
         }
     }
 
@@ -112,30 +114,10 @@ namespace Core {
         }
     }
 
-    /**
-     * The Response class is a do-it-all for getting responses out in different
-     * formats.
-     *
-     * @todo Create sub-classes to split and extend this god object.
-     */
     class Response
     {
-        /**
-         * Indicates response type used for routing.
-         *
-         * Valid strings are 'csv', 'html', 'json' and 'text'.
-         *
-         * @var string Response type
-         */
-        private $type;
+        private mixed $type;
 
-        /**
-         * Indicates requested response type.
-         *
-         * Valid strings are 'csv', 'html', 'json', 'gyazo' and 'text'.
-         *
-         * @param string|null $response_type Response type
-         */
         public function __construct($response_type = null)
         {
             switch ($response_type) {
@@ -167,14 +149,6 @@ namespace Core {
             }
         }
 
-        /**
-         * Routes error messages depending on response type.
-         *
-         * @param int $code HTTP status code number
-         * @param int $desc descriptive error message
-         *
-         * @return void
-         */
         public function error($code, $desc)
         {
             $response = null;
@@ -197,46 +171,17 @@ namespace Core {
             echo $response;
         }
 
-        /**
-         * Indicates with CSV body the request was invalid.
-         *
-         * @param int $description descriptive error message
-         *
-         * @return string error message in CSV format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function csvError($description)
+        private static function csvError($description): string
         {
             return '"error"' . "\r\n" . "\"$description\"" . "\r\n";
         }
 
-        /**
-         * Indicates with HTML body the request was invalid.
-         *
-         * @param int $code HTTP status code number
-         * @param int $description descriptive error message
-         *
-         * @return string error message in HTML format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function htmlError($code, $description)
+        private static function htmlError($code, $description): string
         {
             return '<p>ERROR: (' . $code . ') ' . $description . '</p>';
         }
 
-        /**
-         * Indicates with JSON body the request was invalid.
-         *
-         * @param int $code HTTP status code number
-         * @param int $description descriptive error message
-         *
-         * @return string error message in pretty-printed JSON format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function jsonError($code, $description)
+        private static function jsonError($code, $description): bool|string
         {
             return json_encode([
                 'success' => false,
@@ -245,28 +190,12 @@ namespace Core {
             ], JSON_PRETTY_PRINT);
         }
 
-        /**
-         * Indicates with plain text body the request was invalid.
-         *
-         * @param int $code HTTP status code number
-         * @param int $description descriptive error message
-         *
-         * @return string error message in plain text format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function textError($code, $description)
+
+        private static function textError($code, $description): string
         {
             return 'ERROR: (' . $code . ') ' . $description;
         }
 
-        /**
-         * Routes success messages depending on response type.
-         *
-         * @param mixed[] $files
-         *
-         * @return void
-         */
         public function send($files)
         {
             $response = null;
@@ -290,16 +219,7 @@ namespace Core {
             echo $response;
         }
 
-        /**
-         * Indicates with CSV body the request was successful.
-         *
-         * @param mixed[] $files
-         *
-         * @return string success message in CSV format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function csvSuccess($files)
+        private static function csvSuccess($files): string
         {
             $result = '"name","url","hash","size"' . "\r\n";
             foreach ($files as $file) {
@@ -312,16 +232,7 @@ namespace Core {
             return $result;
         }
 
-        /**
-         * Indicates with HTML body the request was successful.
-         *
-         * @param mixed[] $files
-         *
-         * @return string success message in HTML format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function htmlSuccess($files)
+        private static function htmlSuccess($files): string
         {
             $result = '';
 
@@ -332,16 +243,7 @@ namespace Core {
             return $result;
         }
 
-        /**
-         * Indicates with JSON body the request was successful.
-         *
-         * @param mixed[] $files
-         *
-         * @return string success message in pretty-printed JSON format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function jsonSuccess($files)
+        private static function jsonSuccess($files): bool|string
         {
             return json_encode([
                 'success' => true,
@@ -349,16 +251,7 @@ namespace Core {
             ], JSON_PRETTY_PRINT);
         }
 
-        /**
-         * Indicates with plain text body the request was successful.
-         *
-         * @param mixed[] $files
-         *
-         * @return string success message in plain text format
-         * @deprecated 2.1.0 Will be renamed to camelCase format.
-         *
-         */
-        private static function textSuccess($files)
+        private static function textSuccess($files): string
         {
             $result = '';
 
@@ -370,9 +263,11 @@ namespace Core {
         }
     }
 
-
     class Database
     {
+        /**
+         * @throws Exception
+         */
         public static function assemblePDO()
         {
             try {
@@ -380,24 +275,30 @@ namespace Core {
                     Settings::$DB_MODE . ':' . Settings::$DB_PATH, Settings::$DB_USER,
                     Settings::$DB_PASS
                 );
-            } catch (\Exception $e) {
-                throw new \Exception('Cant connect to DB.', 500);
+            } catch (Exception) {
+                throw new Exception('Cant connect to DB.', 500);
             }
         }
 
-        public function dbCheckNameExists()
+        /**
+         * @throws Exception
+         */
+        public static function dbCheckNameExists()
         {
             try {
                 $q = Settings::$DB->prepare('SELECT COUNT(filename) FROM files WHERE filename = (:name)');
                 $q->bindValue(':name', Upload::$NEW_NAME_FULL);
                 $q->execute();
                 return $q->fetchColumn();
-            } catch (\Exception $e) {
-                throw new \Exception('Cant check if name exists in DB.', 500);
+            } catch (Exception) {
+                throw new Exception('Cant check if name exists in DB.', 500);
             }
         }
 
-        public function checkFileBlacklist()
+        /**
+         * @throws Exception
+         */
+        public static function checkFileBlacklist()
         {
             try {
                 $q = Settings::$DB->prepare('SELECT hash, COUNT(*) AS count FROM blacklist WHERE hash = (:hash)');
@@ -405,14 +306,17 @@ namespace Core {
                 $q->execute();
                 $result = $q->fetch();
                 if ($result['count'] > 0) {
-                    throw new \Exception('File blacklisted!', 415);
+                    throw new Exception('File blacklisted!', 415);
                 }
-            } catch (\Exception $e) {
-                throw new \Exception('Cant check blacklist DB.', 500);
+            } catch (Exception) {
+                throw new Exception('Cant check blacklist DB.', 500);
             }
         }
 
-        public function antiDupe()
+        /**
+         * @throws Exception
+         */
+        public static function antiDupe()
         {
             try {
                 $q = Settings::$DB->prepare(
@@ -424,13 +328,18 @@ namespace Core {
                 $result = $q->fetch();
                 if ($result['count'] > 0) {
                     Upload::$NEW_NAME_FULL = $result['filename'];
+                } else {
+                    Upload::generateName();
                 }
-            } catch (\Exception $e) {
-                throw new \Exception('Cant check for dupes in DB.', 500);
+            } catch (Exception) {
+                throw new Exception('Cant check for dupes in DB.', 500);
             }
         }
 
-        public function newIntoDB()
+        /**
+         * @throws Exception
+         */
+        public static function newIntoDB()
         {
             try {
                 $q = Settings::$DB->prepare(
@@ -438,18 +347,17 @@ namespace Core {
                     'VALUES (:hash, :orig, :name, :size, :date, :ip)'
                 );
                 $q->bindValue(':hash', Upload::$SHA1, PDO::PARAM_STR);
-                $q->bindValue(':orig', strip_tags(Upload::$FILE_NAME), PDO::PARAM_STR);
+                $q->bindValue(':orig', Upload::$FILE_NAME, PDO::PARAM_STR);
                 $q->bindValue(':name', Upload::$NEW_NAME_FULL, PDO::PARAM_STR);
                 $q->bindValue(':size', Upload::$FILE_SIZE, PDO::PARAM_INT);
-                $q->bindValue(':date', time(), PDO::PARAM_STR);
+                $q->bindValue(':date', time(), PDO::PARAM_INT);
                 $q->bindValue(':ip', Upload::$IP, PDO::PARAM_STR);
                 $q->execute();
-            } catch (\Exception $e) {
-                throw new \Exception('Cant insert into DB.', 500);
+            } catch (Exception) {
+                throw new Exception('Cant insert into DB.', 500);
             }
         }
     }
 }
 
 
-