Browse Source

Apply PSR-1 and PSR-2 standard using php-cs-fixer --level=psr2

Manual fixes:
- Change method names to camelCase to comply with PSR-1 section 4.3
- Add namespace to Utils and Tokens classes to comply with PSR-1 section 3
- Capitalize src/helpers/, src/helpers/utils.php, and src/helpers/tokens.php
so they match with the class names. This also makes it possible to add them
to Composer's autoloader
merumelu 9 years ago
parent
commit
ebc9dfb129
9 changed files with 525 additions and 376 deletions
  1. 1 1
      api/index.php
  2. 9 6
      src/Helpers/Tokens.php
  3. 54 37
      src/Helpers/Utils.php
  4. 15 11
      src/dependencies.php
  5. 22 22
      src/middleware.php
  6. 58 48
      src/routes/asset.php
  7. 271 191
      src/routes/asset_edit.php
  8. 82 51
      src/routes/auth.php
  9. 13 9
      src/routes/user.php

+ 1 - 1
api/index.php

@@ -31,7 +31,7 @@ require __DIR__ . '/../src/middleware.php';
 
 
 // Register routes
 // Register routes
 
 
-foreach(glob(__DIR__ . "/../src/routes/*.php") as $filename) {
+foreach (glob(__DIR__ . "/../src/routes/*.php") as $filename) {
     require $filename;
     require $filename;
 }
 }
 
 

+ 9 - 6
src/helpers/tokens.php → src/Helpers/Tokens.php

@@ -1,15 +1,19 @@
 <?php
 <?php
 // Token format: <base64-encoded json-encoded data>&<base64-encoded id (composed of raw random bytes)>|<base64-encoded time>&<base64-encoded hmac>
 // Token format: <base64-encoded json-encoded data>&<base64-encoded id (composed of raw random bytes)>|<base64-encoded time>&<base64-encoded hmac>
 
 
+namespace Godot\AssetLibrary\Helpers;
+
 class Tokens
 class Tokens
 {
 {
-    var $c;
+    private $c;
+
     public function __construct($c)
     public function __construct($c)
     {
     {
         $this->c = $c;
         $this->c = $c;
     }
     }
 
 
-    function sign_token($token) {
+    private function signToken($token)
+    {
         return hash_hmac('sha256', $token, $this->c->settings['auth']['secret'], true);
         return hash_hmac('sha256', $token, $this->c->settings['auth']['secret'], true);
     }
     }
 
 
@@ -21,7 +25,7 @@ class Tokens
         $token_time = time();
         $token_time = time();
 
 
         $token_payload = base64_encode($token_data) . '&' . base64_encode($token_id) . '|' . base64_encode($token_time);
         $token_payload = base64_encode($token_data) . '&' . base64_encode($token_id) . '|' . base64_encode($token_time);
-        $token = $token_payload . '&' . base64_encode($this->sign_token($token_payload));
+        $token = $token_payload . '&' . base64_encode($this->signToken($token_payload));
 
 
         return $token;
         return $token;
     }
     }
@@ -29,7 +33,7 @@ class Tokens
     public function validate($token)
     public function validate($token)
     {
     {
         $token_parts = explode('&', $token);
         $token_parts = explode('&', $token);
-        if(count($token_parts) != 3) {
+        if (count($token_parts) != 3) {
             return false;
             return false;
         }
         }
 
 
@@ -39,11 +43,10 @@ class Tokens
 
 
         $token_payload = $token_parts[0] . '&' . $token_parts[1];
         $token_payload = $token_parts[0] . '&' . $token_parts[1];
 
 
-        if($token_signature !== $this->sign_token($token_payload) || time() > $token_time + $this->c->settings['auth']['tokenExpirationTime']) {
+        if ($token_signature !== $this->signToken($token_payload) || time() > $token_time + $this->c->settings['auth']['tokenExpirationTime']) {
             return false;
             return false;
         }
         }
 
 
         return $token_data;
         return $token_data;
     }
     }
 }
 }
-

+ 54 - 37
src/helpers/utils.php → src/Helpers/Utils.php

@@ -1,59 +1,63 @@
 <?php
 <?php
 
 
+namespace Godot\AssetLibrary\Helpers;
+
 class Utils
 class Utils
 {
 {
-    var $c;
+    private $c;
+
     public function __construct($c)
     public function __construct($c)
     {
     {
         $this->c = $c;
         $this->c = $c;
     }
     }
 
 
-    public function get_computed_download_url($repo_url, $provider, $commit, &$warning=null) // i.e. browse_url, download_provider, download_commit
+    public function getComputedDownloadUrl($repo_url, $provider, $commit, &$warning=null) // i.e. browse_url, download_provider, download_commit
     {
     {
         $repo_url = rtrim($repo_url, '/');
         $repo_url = rtrim($repo_url, '/');
-        if(is_int($provider)) {
+        if (is_int($provider)) {
             $provider = $this->c->constants['download_provider'][$provider];
             $provider = $this->c->constants['download_provider'][$provider];
         }
         }
         $warning_suffix = "Please, ensure that the URL and the repository provider are, indeed, correct.";
         $warning_suffix = "Please, ensure that the URL and the repository provider are, indeed, correct.";
         $light_warning_suffix = "Please, doublecheck that the URL and the repository provider are correct.";
         $light_warning_suffix = "Please, doublecheck that the URL and the repository provider are correct.";
         switch ($provider) {
         switch ($provider) {
             case 'GitHub':
             case 'GitHub':
-                if(sizeof(preg_grep('/^https:\/\/github\.com\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
+                if (sizeof(preg_grep('/^https:\/\/github\.com\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"https://github.com/<owner>/<name>\". $warning_suffix";
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"https://github.com/<owner>/<name>\". $warning_suffix";
                 }
                 }
                 return "$repo_url/archive/$commit.zip";
                 return "$repo_url/archive/$commit.zip";
             case 'GitLab':
             case 'GitLab':
-                if(sizeof(preg_grep('/^https:\/\/(gitlab\.com|[^\/]+)\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
+                if (sizeof(preg_grep('/^https:\/\/(gitlab\.com|[^\/]+)\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"https://<gitlab instance>/<owner>/<name>\". $warning_suffix";
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"https://<gitlab instance>/<owner>/<name>\". $warning_suffix";
-                } elseif(sizeof(preg_grep('/^https:\/\/(gitlab\.com)\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
+                } elseif (sizeof(preg_grep('/^https:\/\/(gitlab\.com)\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
                     $warning = "\"$repo_url\" might not be correct; it should be similar to \"https://gitlab.com/<owner>/<name>\", unless the asset is hosted on a custom instance of GitLab. $light_warning_suffix";
                     $warning = "\"$repo_url\" might not be correct; it should be similar to \"https://gitlab.com/<owner>/<name>\", unless the asset is hosted on a custom instance of GitLab. $light_warning_suffix";
                 }
                 }
                 return "$repo_url/repository/archive.zip?ref=$commit";
                 return "$repo_url/repository/archive.zip?ref=$commit";
             case 'BitBucket':
             case 'BitBucket':
-                if(sizeof(preg_grep('/^https:\/\/bitbucket\.org\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
+                if (sizeof(preg_grep('/^https:\/\/bitbucket\.org\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"https://bitbucket.org/<owner>/<name>\". $warning_suffix";
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"https://bitbucket.org/<owner>/<name>\". $warning_suffix";
                 }
                 }
                 return "$repo_url/get/$commit.zip";
                 return "$repo_url/get/$commit.zip";
             case 'Gogs':
             case 'Gogs':
-                if(sizeof(preg_grep('/^https?:\/\/[^\/]+?\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
+                if (sizeof(preg_grep('/^https?:\/\/[^\/]+?\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"http<s>://<gogs instance>/<owner>/<name>\". $warning_suffix";
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"http<s>://<gogs instance>/<owner>/<name>\". $warning_suffix";
                 }
                 }
                 $warning = "Since Gogs might be self-hosted, we can't be sure that \"$repo_url\" is a valid Gogs URL. $light_warning_suffix";
                 $warning = "Since Gogs might be self-hosted, we can't be sure that \"$repo_url\" is a valid Gogs URL. $light_warning_suffix";
                 return "$repo_url/archive/$commit.zip";
                 return "$repo_url/archive/$commit.zip";
             case 'cgit':
             case 'cgit':
-                if(sizeof(preg_grep('/^https?:\/\/[^\/]+?\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
+                if (sizeof(preg_grep('/^https?:\/\/[^\/]+?\/[^\/]+?\/[^\/]+?$/', [$repo_url])) == 0) {
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"http<s>://<cgit instance>/<owner>/<name>\". $warning_suffix";
                     $warning = "\"$repo_url\" doesn't look correct; it should be similar to \"http<s>://<cgit instance>/<owner>/<name>\". $warning_suffix";
                 }
                 }
                 $warning = "Since cgit might be self-hosted, we can't be sure that \"$repo_url\" is a valid cgit URL. $light_warning_suffix";
                 $warning = "Since cgit might be self-hosted, we can't be sure that \"$repo_url\" is a valid cgit URL. $light_warning_suffix";
                 return "$repo_url/snapshot/$commit.zip";
                 return "$repo_url/snapshot/$commit.zip";
-            default: return "$repo_url/$commit.zip"; // Obviously incorrect, but we would like to have some default case...
+            default:
+                return "$repo_url/$commit.zip"; // Obviously incorrect, but we would like to have some default case...
         }
         }
     }
     }
 
 
-    public function get_default_issues_url($repo_url, $provider) // i.e. browse_url, download_provider
+    public function getDefaultIssuesUrl($repo_url, $provider) // i.e. browse_url, download_provider
     {
     {
         $repo_url = rtrim($repo_url, '/');
         $repo_url = rtrim($repo_url, '/');
-        if(is_int($provider)) {
+        if (is_int($provider)) {
             $provider = $this->c->constants['download_provider'][$provider];
             $provider = $this->c->constants['download_provider'][$provider];
         }
         }
         switch ($provider) {
         switch ($provider) {
@@ -68,11 +72,13 @@ class Utils
         }
         }
     }
     }
 
 
-    public function error_reponse_if_not_user_has_level($currentStatus, &$response, $user, $required_level_name, $message = 'You are not authorized to do this')
+    public function errorResponseIfNotUserHasLevel($currentStatus, &$response, $user, $required_level_name, $message = 'You are not authorized to do this')
     {
     {
-        if($user === false || $currentStatus) return true;
+        if ($user === false || $currentStatus) {
+            return true;
+        }
 
 
-        if((int) $user['type'] < $this->c->constants['user_type'][$required_level_name]) {
+        if ((int) $user['type'] < $this->c->constants['user_type'][$required_level_name]) {
             $response = $response->withJson([
             $response = $response->withJson([
                 'error' => $message,
                 'error' => $message,
             ], 403);
             ], 403);
@@ -81,11 +87,13 @@ class Utils
         return false;
         return false;
     }
     }
 
 
-    public function error_reponse_if_missing_or_not_string($currentStatus, &$response, $object, $property)
+    public function errorResponseIfMissingOrNotString($currentStatus, &$response, $object, $property)
     {
     {
-        if($currentStatus) return true;
+        if ($currentStatus) {
+            return true;
+        }
 
 
-        if(!isset($object[$property]) || !is_string($object[$property]) || $object[$property] == "") {
+        if (!isset($object[$property]) || !is_string($object[$property]) || $object[$property] == "") {
             $response = $response->withJson([
             $response = $response->withJson([
                 'error' => $property . ' is required, and must be a string'
                 'error' => $property . ' is required, and must be a string'
             ], 400);
             ], 400);
@@ -94,11 +102,13 @@ class Utils
         return false;
         return false;
     }
     }
 
 
-    public function error_reponse_if_query_bad($currentStatus, &$response, $query, $message = 'An error occured while executing DB queries')
+    public function errorResponseIfQueryBad($currentStatus, &$response, $query, $message = 'An error occured while executing DB queries')
     {
     {
-        if($currentStatus) return true;
+        if ($currentStatus) {
+            return true;
+        }
 
 
-        if($query->errorCode() != '00000') {
+        if ($query->errorCode() != '00000') {
             $this->c->logger->error('DBError', $query->errorInfo());
             $this->c->logger->error('DBError', $query->errorInfo());
             $response = $response->withJson([
             $response = $response->withJson([
                 'error' => $message,
                 'error' => $message,
@@ -108,11 +118,13 @@ class Utils
         return false;
         return false;
     }
     }
 
 
-    public function error_reponse_if_query_no_results($currentStatus, &$response, $query, $message = 'DB returned no results')
+    public function errorResponseIfQueryNoResults($currentStatus, &$response, $query, $message = 'DB returned no results')
     {
     {
-        if($currentStatus) return true;
+        if ($currentStatus) {
+            return true;
+        }
 
 
-        if($query->rowCount() == 0) {
+        if ($query->rowCount() == 0) {
             $response = $response->withJson([
             $response = $response->withJson([
                 'error' => $message
                 'error' => $message
             ], 404);
             ], 404);
@@ -121,20 +133,24 @@ class Utils
         return false;
         return false;
     }
     }
 
 
-    public function ensure_logged_in($currentStatus, &$response, $body, &$user, &$token_data=null, $reset = false)
+    public function ensureLoggedIn($currentStatus, &$response, $body, &$user, &$token_data = null, $reset = false)
     {
     {
-        $currentStatus = $this->error_reponse_if_missing_or_not_string($currentStatus, $response, $body, 'token');
-        if($currentStatus) return true;
+        $currentStatus = $this->errorResponseIfMissingOrNotString($currentStatus, $response, $body, 'token');
+        if ($currentStatus) {
+            return true;
+        }
 
 
         $token_data = $this->c->tokens->validate($body['token']);
         $token_data = $this->c->tokens->validate($body['token']);
-        $error = $this->get_user_from_token_data(false, $response, $token_data, $user, $reset);
+        $error = $this->getUserFromTokenData(false, $response, $token_data, $user, $reset);
         return $error;
         return $error;
     }
     }
 
 
-    public function get_user_from_token_data($currentStatus, &$response, $token_data, &$user, $reset = false)
+    public function getUserFromTokenData($currentStatus, &$response, $token_data, &$user, $reset = false)
     {
     {
-        if($currentStatus) return true;
-        if(!$token_data) {
+        if ($currentStatus) {
+            return true;
+        }
+        if (!$token_data) {
             $response = $response->withJson([
             $response = $response->withJson([
                 'error' => 'Invalid token'
                 'error' => 'Invalid token'
             ], 403);
             ], 403);
@@ -146,10 +162,10 @@ class Utils
         //   $query = $this->c->queries['user']['get_one'];
         //   $query = $this->c->queries['user']['get_one'];
         //   $query->bindValue(':id', (int) $token_data->user_id, PDO::PARAM_INT);
         //   $query->bindValue(':id', (int) $token_data->user_id, PDO::PARAM_INT);
         // }
         // }
-        if(isset($token_data->session) && !$reset) {
+        if (isset($token_data->session) && !$reset) {
             $query = $this->c->queries['user']['get_by_session_token'];
             $query = $this->c->queries['user']['get_by_session_token'];
             $query->bindValue(":session_token", base64_decode($token_data->session));
             $query->bindValue(":session_token", base64_decode($token_data->session));
-        } else if(isset($token_data->reset) && $reset) {
+        } elseif (isset($token_data->reset) && $reset) {
             $query = $this->c->queries['user']['get_by_reset_token'];
             $query = $this->c->queries['user']['get_by_reset_token'];
             $query->bindValue(":reset_token", base64_decode($token_data->reset));
             $query->bindValue(":reset_token", base64_decode($token_data->reset));
         } else {
         } else {
@@ -161,12 +177,13 @@ class Utils
 
 
         $query->execute();
         $query->execute();
 
 
-        $currentStatus = $this->error_reponse_if_query_bad(false, $response, $query);
-        $currentStatus = $this->error_reponse_if_query_no_results($currentStatus, $response, $query, 'Nonexistent token submitted');
-        if($currentStatus) return true;
+        $currentStatus = $this->errorResponseIfQueryBad(false, $response, $query);
+        $currentStatus = $this->errorResponseIfQueryNoResults($currentStatus, $response, $query, 'Nonexistent token submitted');
+        if ($currentStatus) {
+            return true;
+        }
 
 
         $user = $query->fetchAll()[0];
         $user = $query->fetchAll()[0];
         return false;
         return false;
-
     }
     }
 }
 }

+ 15 - 11
src/dependencies.php

@@ -48,22 +48,22 @@ $container['queries'] = function ($c) {
 
 
 // mail
 // mail
 $container['mail'] = function ($c) {
 $container['mail'] = function ($c) {
-    return function() use ($c) {
+    return function () use ($c) {
         $settings = $c->get('settings')['mail'];
         $settings = $c->get('settings')['mail'];
         $mail = new PHPMailer;
         $mail = new PHPMailer;
         $mail->setFrom($settings['from']);
         $mail->setFrom($settings['from']);
-        if(isset($settings['replyTo'])) {
+        if (isset($settings['replyTo'])) {
             $mail->addReplyTo($settings['replyTo']);
             $mail->addReplyTo($settings['replyTo']);
         }
         }
-        if(isset($settings['smtp'])) {
+        if (isset($settings['smtp'])) {
             $mail->isSMTP();
             $mail->isSMTP();
             $mail->Host = $settings['smtp']['host'];
             $mail->Host = $settings['smtp']['host'];
             $mail->Port = $settings['smtp']['port'];
             $mail->Port = $settings['smtp']['port'];
-            if(isset($settings['smtp']['auth'])) {
+            if (isset($settings['smtp']['auth'])) {
                 $mail->SMTPAuth = true;
                 $mail->SMTPAuth = true;
                 $mail->Username = $settings['smtp']['auth']['user'];
                 $mail->Username = $settings['smtp']['auth']['user'];
                 $mail->Password = $settings['smtp']['auth']['pass'];
                 $mail->Password = $settings['smtp']['auth']['pass'];
-                if($settings['smtp']['secure']) {
+                if ($settings['smtp']['secure']) {
                     $mail->SMTPSecure = $settings['smtp']['secure'];
                     $mail->SMTPSecure = $settings['smtp']['secure'];
                 }
                 }
             } else {
             } else {
@@ -84,8 +84,12 @@ $container['csrf'] = function ($c) {
 // cookies
 // cookies
 $container['cookies'] = function ($c) {
 $container['cookies'] = function ($c) {
     return [
     return [
-        'cookie' => function($name, $value) {return Dflydev\FigCookies\Cookie::create($name, $value);},
-        'setCookie' => function($name) {return Dflydev\FigCookies\SetCookie::create($name);},
+        'cookie' => function ($name, $value) {
+            return Dflydev\FigCookies\Cookie::create($name, $value);
+        },
+        'setCookie' => function ($name) {
+            return Dflydev\FigCookies\SetCookie::create($name);
+        },
         'requestCookies' => new Dflydev\FigCookies\FigRequestCookies,
         'requestCookies' => new Dflydev\FigCookies\FigRequestCookies,
         'responseCookies' => new Dflydev\FigCookies\FigResponseCookies,
         'responseCookies' => new Dflydev\FigCookies\FigResponseCookies,
     ];
     ];
@@ -93,12 +97,12 @@ $container['cookies'] = function ($c) {
 
 
 // tokens
 // tokens
 $container['tokens'] = function ($c) {
 $container['tokens'] = function ($c) {
-    require_once __DIR__ . '/helpers/tokens.php';
-    return new Tokens($c);
+    require_once __DIR__ . '/Helpers/Tokens.php';
+    return new Godot\AssetLibrary\Helpers\Tokens($c);
 };
 };
 
 
 // utils
 // utils
 $container['utils'] = function ($c) {
 $container['utils'] = function ($c) {
-    require_once __DIR__ . '/helpers/utils.php';
-    return new Utils($c);
+    require_once __DIR__ . '/Helpers/Utils.php';
+    return new Godot\AssetLibrary\Helpers\Utils($c);
 };
 };

+ 22 - 22
src/middleware.php

@@ -1,6 +1,6 @@
 <?php
 <?php
 
 
-if(FRONTEND) {
+if (FRONTEND) {
     $container = $app->getContainer();
     $container = $app->getContainer();
 
 
     $app->get('/', function ($request, $response) {
     $app->get('/', function ($request, $response) {
@@ -10,14 +10,14 @@ if(FRONTEND) {
     $app->add(function ($request, $response, $next) {
     $app->add(function ($request, $response, $next) {
         $cookie = $this->cookies['requestCookies']->get($request, 'token');
         $cookie = $this->cookies['requestCookies']->get($request, 'token');
         $body = $request->getParsedBody();
         $body = $request->getParsedBody();
-        if($cookie->getValue() !== null && !isset($body['token'])) {
+        if ($cookie->getValue() !== null && !isset($body['token'])) {
             $cookieValue = (string) $cookie->getValue();
             $cookieValue = (string) $cookie->getValue();
             $body['token'] = $cookieValue;
             $body['token'] = $cookieValue;
             $request = $request->withParsedBody($body);
             $request = $request->withParsedBody($body);
         }
         }
         $response->getBody()->rewind();
         $response->getBody()->rewind();
         $preresult = json_decode($response->getBody()->getContents(), true);
         $preresult = json_decode($response->getBody()->getContents(), true);
-        if(!isset($preresult['error'])) {
+        if (!isset($preresult['error'])) {
             $response = $next($request, $response);
             $response = $next($request, $response);
         }
         }
 
 
@@ -33,13 +33,13 @@ if(FRONTEND) {
         $route = $request->getAttribute('route');
         $route = $request->getAttribute('route');
         $path = $request->getUri()->getPath();
         $path = $request->getUri()->getPath();
 
 
-        if(substr($path, 0, 8) == 'frontend') {
+        if (substr($path, 0, 8) == 'frontend') {
             $response = $response->withHeader('Location', $request->getUri()->getBasePath() . substr($path, 8) . '?' . $request->getUri()->getQuery());
             $response = $response->withHeader('Location', $request->getUri()->getBasePath() . substr($path, 8) . '?' . $request->getUri()->getQuery());
         }
         }
 
 
-        if(isset($static_routes['/' . $path])) {
+        if (isset($static_routes['/' . $path])) {
             $queryUri = '/' . $path;
             $queryUri = '/' . $path;
-        } elseif($route) {
+        } elseif ($route) {
             $queryUri = $route->getPattern();
             $queryUri = $route->getPattern();
         } else {
         } else {
             return $response;
             return $response;
@@ -47,10 +47,10 @@ if(FRONTEND) {
 
 
         $queryUri = $request->getMethod() . ' ' . $queryUri;
         $queryUri = $request->getMethod() . ' ' . $queryUri;
 
 
-        if($route) {
+        if ($route) {
             $response->getBody()->rewind();
             $response->getBody()->rewind();
             $result = json_decode($response->getBody()->getContents(), true);
             $result = json_decode($response->getBody()->getContents(), true);
-            if($result === null) {
+            if ($result === null) {
                 return $response;
                 return $response;
                 //$result = ['error' => 'Can\'t decode api response - ' . $response->getBody()->getContents()];
                 //$result = ['error' => 'Can\'t decode api response - ' . $response->getBody()->getContents()];
             }
             }
@@ -59,11 +59,11 @@ if(FRONTEND) {
         }
         }
 
 
 
 
-        if(isset($result['url'])) {
+        if (isset($result['url'])) {
             $response = new \Slim\Http\Response(303);
             $response = new \Slim\Http\Response(303);
             $response = $response->withHeader('Location', $request->getUri()->getBasePath() . '/' . $result['url']);
             $response = $response->withHeader('Location', $request->getUri()->getBasePath() . '/' . $result['url']);
         } else {
         } else {
-            if(isset($result['token'])) {
+            if (isset($result['token'])) {
                 $body['token'] = $result['token'];
                 $body['token'] = $result['token'];
             }
             }
             $template_names = [
             $template_names = [
@@ -91,15 +91,15 @@ if(FRONTEND) {
                 'ERROR' => 'error',
                 'ERROR' => 'error',
             ];
             ];
 
 
-            if(isset($result['error'])) {
-                if(isset($template_names['ERROR ' . $queryUri])) {
+            if (isset($result['error'])) {
+                if (isset($template_names['ERROR ' . $queryUri])) {
                     $queryUri = 'ERROR ' . $queryUri;
                     $queryUri = 'ERROR ' . $queryUri;
                 } else {
                 } else {
                     $queryUri = 'ERROR';
                     $queryUri = 'ERROR';
                 }
                 }
             }
             }
 
 
-            if(isset($template_names[$queryUri])) {
+            if (isset($template_names[$queryUri])) {
                 $response = new \Slim\Http\Response();
                 $response = new \Slim\Http\Response();
                 $errorResponse = new \Slim\Http\Response();
                 $errorResponse = new \Slim\Http\Response();
                 $params = [
                 $params = [
@@ -118,14 +118,14 @@ if(FRONTEND) {
                     //'body' => $request->getParsedBody(),
                     //'body' => $request->getParsedBody(),
                 ];
                 ];
 
 
-                if(isset($body['token'])) {
+                if (isset($body['token'])) {
                     $token = $this->tokens->validate($body['token']);
                     $token = $this->tokens->validate($body['token']);
-                    $error = $this->utils->get_user_from_token_data(false, $errorResponse, $token, $user);
-                    if(!$error) {
+                    $error = $this->utils->getUserFromTokenData(false, $errorResponse, $token, $user);
+                    if (!$error) {
                         $params['user'] = $user;
                         $params['user'] = $user;
                     } else {
                     } else {
-                        $error = $this->utils->get_user_from_token_data(false, $errorResponse, $token, $reset_user, true);
-                        if(!$error) {
+                        $error = $this->utils->getUserFromTokenData(false, $errorResponse, $token, $reset_user, true);
+                        if (!$error) {
                             $params['reset_user'] = $reset_user;
                             $params['reset_user'] = $reset_user;
                         }
                         }
                     }
                     }
@@ -135,9 +135,9 @@ if(FRONTEND) {
                 $query_categories->bindValue(':category_type', '%');
                 $query_categories->bindValue(':category_type', '%');
                 $query_categories->execute();
                 $query_categories->execute();
 
 
-                $error = $this->utils->error_reponse_if_query_bad(false, $errorResponse, $query_categories);
-                $error = $this->utils->error_reponse_if_query_no_results($error, $errorResponse, $query_categories);
-                if(!$error) {
+                $error = $this->utils->errorResponseIfQueryBad(false, $errorResponse, $query_categories);
+                $error = $this->utils->errorResponseIfQueryNoResults($error, $errorResponse, $query_categories);
+                if (!$error) {
                     $categories = $query_categories->fetchAll();
                     $categories = $query_categories->fetchAll();
                     foreach ($categories as $key => $value) {
                     foreach ($categories as $key => $value) {
                         $params['categories'][$value['id']] = $value;
                         $params['categories'][$value['id']] = $value;
@@ -148,7 +148,7 @@ if(FRONTEND) {
             }
             }
         }
         }
 
 
-        if(isset($result['token'])) {
+        if (isset($result['token'])) {
             $response = $this->cookies['responseCookies']->set($response, $this->cookies['setCookie']('token')
             $response = $this->cookies['responseCookies']->set($response, $this->cookies['setCookie']('token')
                 ->withValue($result['token'])
                 ->withValue($result['token'])
                 ->withDomain($_SERVER['HTTP_HOST'])
                 ->withDomain($_SERVER['HTTP_HOST'])

+ 58 - 48
src/routes/asset.php

@@ -6,7 +6,7 @@ $app->get('/asset', function ($request, $response, $args) {
     $params = $request->getQueryParams();
     $params = $request->getQueryParams();
 
 
     $category = '%';
     $category = '%';
-    if(FRONTEND) {
+    if (FRONTEND) {
         $category_type = $this->constants['category_type']['any'];
         $category_type = $this->constants['category_type']['any'];
     } else {
     } else {
         $category_type = $this->constants['category_type']['addon'];
         $category_type = $this->constants['category_type']['addon'];
@@ -19,43 +19,43 @@ $app->get('/asset', function ($request, $response, $args) {
     $page_size = 10;
     $page_size = 10;
     $max_page_size = 500;
     $max_page_size = 500;
     $page_offset = 0;
     $page_offset = 0;
-    if(isset($params['category']) && $params['category'] != "") {
+    if (isset($params['category']) && $params['category'] != "") {
         $category = (int) $params['category'];
         $category = (int) $params['category'];
     }
     }
-    if(isset($params['type']) && isset($this->constants['category_type'][$params['type']])) {
+    if (isset($params['type']) && isset($this->constants['category_type'][$params['type']])) {
         $category_type = $this->constants['category_type'][$params['type']];
         $category_type = $this->constants['category_type'][$params['type']];
     }
     }
-    if(isset($params['support'])) { // Expects the param like `support=community+testing` or `support[community]=1&support[testing]=1&...`
+    if (isset($params['support'])) { // Expects the param like `support=community+testing` or `support[community]=1&support[testing]=1&...`
         $support_levels = [];
         $support_levels = [];
-        if(is_array($params['support'])) {
-            foreach($params['support'] as $key => $value) {
-                if($value && isset($this->constants['support_level'][$key])) {
+        if (is_array($params['support'])) {
+            foreach ($params['support'] as $key => $value) {
+                if ($value && isset($this->constants['support_level'][$key])) {
                     array_push($support_levels, (int) $this->constants['support_level'][$key]);
                     array_push($support_levels, (int) $this->constants['support_level'][$key]);
                 }
                 }
             }
             }
         } else {
         } else {
-            foreach(explode(' ', $params['support']) as $key => $value) { // `+` is changed to ` ` automatically
-                if(isset($this->constants['support_level'][$value])) {
+            foreach (explode(' ', $params['support']) as $key => $value) { // `+` is changed to ` ` automatically
+                if (isset($this->constants['support_level'][$value])) {
                     array_push($support_levels, (int) $this->constants['support_level'][$value]);
                     array_push($support_levels, (int) $this->constants['support_level'][$value]);
                 }
                 }
             }
             }
         }
         }
     }
     }
-    if(isset($params['filter'])) {
+    if (isset($params['filter'])) {
         $filter = '%'.preg_replace('/[[:punct:]]+/', '%', $params['filter']).'%';
         $filter = '%'.preg_replace('/[[:punct:]]+/', '%', $params['filter']).'%';
     }
     }
-    if(isset($params['user'])) {
+    if (isset($params['user'])) {
         $username = $params['user'];
         $username = $params['user'];
     }
     }
-    if(isset($params['max_results'])) {
+    if (isset($params['max_results'])) {
         $page_size = min(abs((int) $params['max_results']), $max_page_size);
         $page_size = min(abs((int) $params['max_results']), $max_page_size);
     }
     }
-    if(isset($params['page'])) {
+    if (isset($params['page'])) {
         $page_offset = abs((int) $params['page']) * $page_size;
         $page_offset = abs((int) $params['page']) * $page_size;
-    } elseif(isset($params['offset'])) {
+    } elseif (isset($params['offset'])) {
         $page_offset = abs((int) $params['offset']);
         $page_offset = abs((int) $params['offset']);
     }
     }
-    if(isset($params['sort'])) {
+    if (isset($params['sort'])) {
         $column_mapping = [
         $column_mapping = [
             'rating' => 'rating',
             'rating' => 'rating',
             'cost' => 'cost',
             'cost' => 'cost',
@@ -63,15 +63,15 @@ $app->get('/asset', function ($request, $response, $args) {
             'updated' => 'modify_date'
             'updated' => 'modify_date'
             // TODO: downloads
             // TODO: downloads
         ];
         ];
-        if(isset($column_mapping[$params['sort']])) {
+        if (isset($column_mapping[$params['sort']])) {
             $order_column = $column_mapping[$params['sort']];
             $order_column = $column_mapping[$params['sort']];
         }
         }
     }
     }
-    if(isset($params['reverse'])) {
+    if (isset($params['reverse'])) {
         $order_direction = 'asc';
         $order_direction = 'asc';
     }
     }
 
 
-    if(count($support_levels) === 0) {
+    if (count($support_levels) === 0) {
         $support_levels = [0, 1, 2]; // Testing + Community + Official
         $support_levels = [0, 1, 2]; // Testing + Community + Official
     }
     }
     $support_levels = implode('|', $support_levels);
     $support_levels = implode('|', $support_levels);
@@ -88,8 +88,10 @@ $app->get('/asset', function ($request, $response, $args) {
     $query->bindValue(':skip_count', $page_offset, PDO::PARAM_INT);
     $query->bindValue(':skip_count', $page_offset, PDO::PARAM_INT);
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     $query_count = $this->queries['asset']['search_count'];
     $query_count = $this->queries['asset']['search_count'];
     $query_count->bindValue(':category', $category, PDO::PARAM_INT);
     $query_count->bindValue(':category', $category, PDO::PARAM_INT);
@@ -99,15 +101,17 @@ $app->get('/asset', function ($request, $response, $args) {
     $query_count->bindValue(':username', $username);
     $query_count->bindValue(':username', $username);
     $query_count->execute();
     $query_count->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_count);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_count);
+    if ($error) {
+        return $response;
+    }
 
 
     $total_count = $query_count->fetchAll()[0]['count'];
     $total_count = $query_count->fetchAll()[0]['count'];
 
 
     $assets = $query->fetchAll();
     $assets = $query->fetchAll();
 
 
     $context = $this;
     $context = $this;
-    $assets = array_map(function($asset) use($context) {
+    $assets = array_map(function ($asset) use ($context) {
         $asset["support_level"] = $context->constants['support_level'][(int) $asset['support_level']];
         $asset["support_level"] = $context->constants['support_level'][(int) $asset['support_level']];
         return $asset;
         return $asset;
     }, $assets);
     }, $assets);
@@ -128,10 +132,12 @@ $get_asset = function ($request, $response, $args) {
     $query->bindValue(':id', (int) $args['id'], PDO::PARAM_INT);
     $query->bindValue(':id', (int) $args['id'], PDO::PARAM_INT);
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
-    if($query->rowCount() <= 0) {
+    if ($query->rowCount() <= 0) {
         return $response->withJson([
         return $response->withJson([
             'error' => 'Couldn\'t find asset with id '.$args['id'].'!'
             'error' => 'Couldn\'t find asset with id '.$args['id'].'!'
         ], 404);
         ], 404);
@@ -143,16 +149,16 @@ $get_asset = function ($request, $response, $args) {
 
 
     foreach ($output as $row) {
     foreach ($output as $row) {
         foreach ($row as $column => $value) {
         foreach ($row as $column => $value) {
-            if($value!==null) {
-                if($column==='preview_id') {
+            if ($value!==null) {
+                if ($column==='preview_id') {
                     $previews[] = ['preview_id' => $value];
                     $previews[] = ['preview_id' => $value];
-                } elseif($column==="type" || $column==="link" || $column==="thumbnail") {
-                        $previews[count($previews) - 1][$column] = $value;
-                } elseif($column==="category_type") {
+                } elseif ($column==="type" || $column==="link" || $column==="thumbnail") {
+                    $previews[count($previews) - 1][$column] = $value;
+                } elseif ($column==="category_type") {
                     $asset_info["type"] = $this->constants['category_type'][(int) $value];
                     $asset_info["type"] = $this->constants['category_type'][(int) $value];
-                } elseif($column==="support_level") {
+                } elseif ($column==="support_level") {
                     $asset_info["support_level"] = $this->constants['support_level'][(int) $value];
                     $asset_info["support_level"] = $this->constants['support_level'][(int) $value];
-                } elseif($column==="download_provider") {
+                } elseif ($column==="download_provider") {
                     $asset_info["download_provider"] = $this->constants['download_provider'][(int) $value];
                     $asset_info["download_provider"] = $this->constants['download_provider'][(int) $value];
                 } else {
                 } else {
                     $asset_info[$column] = $value;
                     $asset_info[$column] = $value;
@@ -161,17 +167,17 @@ $get_asset = function ($request, $response, $args) {
         }
         }
     }
     }
 
 
-    $asset_info['download_url'] = $this->utils->get_computed_download_url($asset_info['browse_url'], $asset_info['download_provider'], $asset_info['download_commit']);
-    if($asset_info['issues_url'] == '') {
-        $asset_info['issues_url'] = $this->utils->get_default_issues_url($asset_info['browse_url'], $asset_info['download_provider']);
+    $asset_info['download_url'] = $this->utils->getComputedDownloadUrl($asset_info['browse_url'], $asset_info['download_provider'], $asset_info['download_commit']);
+    if ($asset_info['issues_url'] == '') {
+        $asset_info['issues_url'] = $this->utils->getDefaultIssuesUrl($asset_info['browse_url'], $asset_info['download_provider']);
     }
     }
 
 
 
 
     foreach ($previews as $i => $_) {
     foreach ($previews as $i => $_) {
-        if(!isset($previews[$i]['thumbnail']) || $previews[$i]['thumbnail'] == '') {
-            if($previews[$i]['type'] == 'video') {
+        if (!isset($previews[$i]['thumbnail']) || $previews[$i]['thumbnail'] == '') {
+            if ($previews[$i]['type'] == 'video') {
                 $matches = [];
                 $matches = [];
-                if(preg_match('|youtube.com/watch\\?v=([^&]+)|', $previews[$i]['link'], $matches)) {
+                if (preg_match('|youtube.com/watch\\?v=([^&]+)|', $previews[$i]['link'], $matches)) {
                     $previews[$i]['thumbnail'] = 'http://img.youtube.com/vi/'.$matches[1].'/default.jpg';
                     $previews[$i]['thumbnail'] = 'http://img.youtube.com/vi/'.$matches[1].'/default.jpg';
                 }
                 }
             } else {
             } else {
@@ -186,7 +192,7 @@ $get_asset = function ($request, $response, $args) {
 };
 };
 // Binding to multiple routes
 // Binding to multiple routes
 $app->get('/asset/{id:[0-9]+}', $get_asset);
 $app->get('/asset/{id:[0-9]+}', $get_asset);
-if(FRONTEND) {
+if (FRONTEND) {
     $app->get('/asset/{id:[0-9]+}/edit', $get_asset);
     $app->get('/asset/{id:[0-9]+}/edit', $get_asset);
 }
 }
 
 
@@ -194,14 +200,16 @@ if(FRONTEND) {
 $app->post('/asset/{id:[0-9]+}/support_level', function ($request, $response, $args) {
 $app->post('/asset/{id:[0-9]+}/support_level', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    $error = $this->utils->error_reponse_if_not_user_has_level($error, $response, $user, 'moderator');
-    $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'support_level');
-    if($error) return $response;
-    if(!isset($this->constants['support_level'][$body['support_level']])) {
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    $error = $this->utils->errorResponseIfNotUserHasLevel($error, $response, $user, 'moderator');
+    $error = $this->utils->errorResponseIfMissingOrNotString($error, $response, $body, 'support_level');
+    if ($error) {
+        return $response;
+    }
+    if (!isset($this->constants['support_level'][$body['support_level']])) {
         $numeric_value_keys = [];
         $numeric_value_keys = [];
         foreach ($this->constants['support_level'] as $key => $value) {
         foreach ($this->constants['support_level'] as $key => $value) {
-            if((int) $value === $value) {
+            if ((int) $value === $value) {
                 array_push($numeric_value_keys, $key);
                 array_push($numeric_value_keys, $key);
             }
             }
         }
         }
@@ -217,8 +225,10 @@ $app->post('/asset/{id:[0-9]+}/support_level', function ($request, $response, $a
 
 
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     return $response->withJson([
     return $response->withJson([
         'changed' => true,
         'changed' => true,

+ 271 - 191
src/routes/asset_edit.php

@@ -1,42 +1,49 @@
 <?php
 <?php
 // Asset editing routes
 // Asset editing routes
 
 
-function _submit_asset_edit($c, $response, $body, $user_id, $asset_id=-1) {
+function _submit_asset_edit($c, $response, $body, $user_id, $asset_id=-1)
+{
     $query = $c->queries['asset_edit']['submit'];
     $query = $c->queries['asset_edit']['submit'];
     $query->bindValue(':user_id', $user_id, PDO::PARAM_INT);
     $query->bindValue(':user_id', $user_id, PDO::PARAM_INT);
     $query->bindValue(':asset_id', $asset_id, PDO::PARAM_INT);
     $query->bindValue(':asset_id', $asset_id, PDO::PARAM_INT);
-    if($asset_id == -1) {
+    if ($asset_id == -1) {
         $error = _insert_asset_edit_fields($c, false, $response, $query, $body, true);
         $error = _insert_asset_edit_fields($c, false, $response, $query, $body, true);
-        if($error) return $response;
+        if ($error) {
+            return $response;
+        }
     } else {
     } else {
         $query_asset = $c->queries['asset']['get_one_bare'];
         $query_asset = $c->queries['asset']['get_one_bare'];
         $query_asset->bindValue(':asset_id', (int) $asset_id, PDO::PARAM_INT);
         $query_asset->bindValue(':asset_id', (int) $asset_id, PDO::PARAM_INT);
         $query_asset->execute();
         $query_asset->execute();
 
 
-        $error = $c->utils->error_reponse_if_query_bad(false, $response, $query_asset);
-        if($error) return $response;
+        $error = $c->utils->errorResponseIfQueryBad(false, $response, $query_asset);
+        if ($error) {
+            return $response;
+        }
 
 
         $asset = $query_asset->fetchAll()[0];
         $asset = $query_asset->fetchAll()[0];
 
 
         $error = _insert_asset_edit_fields($c, false, $response, $query, $body, false, $asset);
         $error = _insert_asset_edit_fields($c, false, $response, $query, $body, false, $asset);
-        if($error) return $response;
+        if ($error) {
+            return $response;
+        }
     }
     }
 
 
     // Make a transaction, so we can roll back failed submissions
     // Make a transaction, so we can roll back failed submissions
     $c->db->beginTransaction();
     $c->db->beginTransaction();
 
 
     $query->execute();
     $query->execute();
-    $error = $c->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) {
+    $error = $c->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
         $c->db->rollback();
         $c->db->rollback();
         return $response;
         return $response;
     }
     }
 
 
     $id = $c->db->lastInsertId();
     $id = $c->db->lastInsertId();
 
 
-    if(isset($body['previews'])) {
+    if (isset($body['previews'])) {
         $error = _add_previews_to_edit($c, $error, $response, $id, $body['previews'], null, $asset_id==-1);
         $error = _add_previews_to_edit($c, $error, $response, $id, $body['previews'], null, $asset_id==-1);
-        if($error) {
+        if ($error) {
             $c->db->rollback();
             $c->db->rollback();
             return $response;
             return $response;
         }
         }
@@ -50,105 +57,119 @@ function _submit_asset_edit($c, $response, $body, $user_id, $asset_id=-1) {
     ], 200);
     ], 200);
 }
 }
 
 
-function _insert_asset_edit_fields($c, $error, &$response, $query, $body, $required=false, $bare_asset=null) {
-    if($error) return true;
+function _insert_asset_edit_fields($c, $error, &$response, $query, $body, $required=false, $bare_asset=null)
+{
+    if ($error) {
+        return true;
+    }
 
 
-    if(isset($body['download_provider'])) {
-        if(isset($c->constants['download_provider'][$body['download_provider']])) {
+    if (isset($body['download_provider'])) {
+        if (isset($c->constants['download_provider'][$body['download_provider']])) {
             $body['download_provider'] = (string) ((int) $c->constants['download_provider'][$body['download_provider']]);
             $body['download_provider'] = (string) ((int) $c->constants['download_provider'][$body['download_provider']]);
         } else {
         } else {
             $body['download_provider'] = 0;
             $body['download_provider'] = 0;
         }
         }
     }
     }
-    if(isset($body['issues_url'])) {
+    if (isset($body['issues_url'])) {
         $default_issues_url = null;
         $default_issues_url = null;
-        if(isset($body['browse_url']) && isset($body['download_provider'])) {
-            $default_issues_url = $c->utils->get_default_issues_url(
+        if (isset($body['browse_url']) && isset($body['download_provider'])) {
+            $default_issues_url = $c->utils->getDefaultIssuesUrl(
                 $body['browse_url'],
                 $body['browse_url'],
                 intval($body['download_provider'])
                 intval($body['download_provider'])
             );
             );
-        } else if($bare_asset !== null) {
-            $default_issues_url = $c->utils->get_default_issues_url(
+        } elseif ($bare_asset !== null) {
+            $default_issues_url = $c->utils->getDefaultIssuesUrl(
                 $body['browse_url'] ?: $bare_asset['browse_url'],
                 $body['browse_url'] ?: $bare_asset['browse_url'],
                 intval($body['download_provider'] ?: $bare_asset['download_provider'])
                 intval($body['download_provider'] ?: $bare_asset['download_provider'])
             );
             );
         }
         }
-        if($default_issues_url !== null && $default_issues_url == $body['issues_url']) {
+        if ($default_issues_url !== null && $default_issues_url == $body['issues_url']) {
             unset($body['issues_url']);
             unset($body['issues_url']);
         }
         }
     }
     }
 
 
     foreach ($c->constants['asset_edit_fields'] as $i => $field) {
     foreach ($c->constants['asset_edit_fields'] as $i => $field) {
-        if(!$required) {
-            if(isset($body[$field]) && ($bare_asset === null || $bare_asset[$field] != $body[$field])) {
+        if (!$required) {
+            if (isset($body[$field]) && ($bare_asset === null || $bare_asset[$field] != $body[$field])) {
                 $query->bindValue(':' . $field, $body[$field]);
                 $query->bindValue(':' . $field, $body[$field]);
             } else {
             } else {
                 $query->bindValue(':' . $field, null, PDO::PARAM_NULL);
                 $query->bindValue(':' . $field, null, PDO::PARAM_NULL);
             }
             }
         } else {
         } else {
-            if($bare_asset === null) {
-                if($field == 'issues_url') { // Default value present, so, no need to error out
-                    if(isset($body[$field])) {
+            if ($bare_asset === null) {
+                if ($field == 'issues_url') { // Default value present, so, no need to error out
+                    if (isset($body[$field])) {
                         $query->bindValue(':' . $field, $body[$field]);
                         $query->bindValue(':' . $field, $body[$field]);
                     } else {
                     } else {
                         $query->bindValue(':' . $field, '', PDO::PARAM_NULL);
                         $query->bindValue(':' . $field, '', PDO::PARAM_NULL);
                     }
                     }
                 } else {
                 } else {
-                    $error = $c->utils->error_reponse_if_missing_or_not_string($error, $response, $body, $field);
-                    if(!$error) $query->bindValue(':' . $field, $body[$field]);
+                    $error = $c->utils->errorResponseIfMissingOrNotString($error, $response, $body, $field);
+                    if (!$error) {
+                        $query->bindValue(':' . $field, $body[$field]);
+                    }
                 }
                 }
             } else { // "Required" (so, non-null), but there is a base asset, so we can support incremental changes
             } else { // "Required" (so, non-null), but there is a base asset, so we can support incremental changes
-                if(isset($body[$field])) {
+                if (isset($body[$field])) {
                     $query->bindValue(':' . $field, $body[$field]);
                     $query->bindValue(':' . $field, $body[$field]);
                 } else {
                 } else {
                     $query->bindValue(':' . $field, $bare_asset[$field]);
                     $query->bindValue(':' . $field, $bare_asset[$field]);
                 }
                 }
-
             }
             }
         }
         }
 
 
-        if($error) {
+        if ($error) {
             return $error;
             return $error;
         }
         }
     }
     }
     return $error;
     return $error;
 }
 }
 
 
-function _add_previews_to_edit($c, $error, &$response, $edit_id, $previews, $asset=null, $required=false) {
-    if($error) return true;
+function _add_previews_to_edit($c, $error, &$response, $edit_id, $previews, $asset=null, $required=false)
+{
+    if ($error) {
+        return true;
+    }
 
 
     foreach ($previews as $i => $preview) {
     foreach ($previews as $i => $preview) {
-        if(!isset($preview['enabled']) || !$preview['enabled']) continue;
-        if($required || !isset($preview['edit_preview_id'])) {
-
+        if (!isset($preview['enabled']) || !$preview['enabled']) {
+            continue;
+        }
+        if ($required || !isset($preview['edit_preview_id'])) {
             $query = $c->queries['asset_edit']['add_preview'];
             $query = $c->queries['asset_edit']['add_preview'];
 
 
-            $error = $c->utils->error_reponse_if_missing_or_not_string($error, $response, $preview, 'operation');
-            if($error) return $error;
+            $error = $c->utils->errorResponseIfMissingOrNotString($error, $response, $preview, 'operation');
+            if ($error) {
+                return $error;
+            }
 
 
             $operation = $c->constants['edit_preview_operation']['insert'];
             $operation = $c->constants['edit_preview_operation']['insert'];
 
 
-            if(!$required && $asset !== null && isset($c->constants['edit_preview_operation'][$preview['operation']])) {
+            if (!$required && $asset !== null && isset($c->constants['edit_preview_operation'][$preview['operation']])) {
                 $operation = $c->constants['edit_preview_operation'][$preview['operation']];
                 $operation = $c->constants['edit_preview_operation'][$preview['operation']];
             }
             }
-            $query->bindValue(':operation',(int) $operation, PDO::PARAM_INT);
+            $query->bindValue(':operation', (int) $operation, PDO::PARAM_INT);
 
 
-            if($operation == $c->constants['edit_preview_operation']['insert']) {
+            if ($operation == $c->constants['edit_preview_operation']['insert']) {
                 $query->bindValue(':preview_id', -1, PDO::PARAM_INT);
                 $query->bindValue(':preview_id', -1, PDO::PARAM_INT);
             } else {
             } else {
-                $error = $c->utils->error_reponse_if_missing_or_not_string($error, $response, $preview, 'preview_id');
-                if($error) return $error;
+                $error = $c->utils->errorResponseIfMissingOrNotString($error, $response, $preview, 'preview_id');
+                if ($error) {
+                    return $error;
+                }
 
 
-                if($asset !== null) {
+                if ($asset !== null) {
                     $query_check = $c->queries['asset']['get_one_preview_bare'];
                     $query_check = $c->queries['asset']['get_one_preview_bare'];
                     $query_check->bindValue(':preview_id', (int) $preview['preview_id'], PDO::PARAM_INT);
                     $query_check->bindValue(':preview_id', (int) $preview['preview_id'], PDO::PARAM_INT);
                     $query_check->execute();
                     $query_check->execute();
-                    $error = $c->utils->error_reponse_if_query_bad(false, $response, $query_check);
-                    $error = $c->utils->error_reponse_if_query_no_results($error, $response, $query_check);
-                    if($error) return $error;
+                    $error = $c->utils->errorResponseIfQueryBad(false, $response, $query_check);
+                    $error = $c->utils->errorResponseIfQueryNoResults($error, $response, $query_check);
+                    if ($error) {
+                        return $error;
+                    }
 
 
                     $original_preview = $query_check->fetchAll()[0];
                     $original_preview = $query_check->fetchAll()[0];
-                    if($original_preview['asset_id'] != $asset['asset_id']) {
+                    if ($original_preview['asset_id'] != $asset['asset_id']) {
                         $response = $response->withJson(['error' => 'Invalid preview id.'], 400);
                         $response = $response->withJson(['error' => 'Invalid preview id.'], 400);
                         return true;
                         return true;
                     }
                     }
@@ -159,44 +180,52 @@ function _add_previews_to_edit($c, $error, &$response, $edit_id, $previews, $ass
 
 
                 $query->bindValue(':preview_id', (int) $preview['preview_id'], PDO::PARAM_INT);
                 $query->bindValue(':preview_id', (int) $preview['preview_id'], PDO::PARAM_INT);
             }
             }
-
-        } elseif(isset($preview['remove']) && $preview['remove']) {
-
+        } elseif (isset($preview['remove']) && $preview['remove']) {
             $query = $c->queries['asset_edit']['remove_preview'];
             $query = $c->queries['asset_edit']['remove_preview'];
             $query->bindValue(':edit_preview_id', (int) $preview['edit_preview_id'], PDO::PARAM_INT);
             $query->bindValue(':edit_preview_id', (int) $preview['edit_preview_id'], PDO::PARAM_INT);
             $query->bindValue(':edit_id', (int) $edit_id, PDO::PARAM_INT);
             $query->bindValue(':edit_id', (int) $edit_id, PDO::PARAM_INT);
             $query->execute();
             $query->execute();
-            $error = $c->utils->error_reponse_if_query_bad(false, $response, $query);
-            if($error) return $error;
+            $error = $c->utils->errorResponseIfQueryBad(false, $response, $query);
+            if ($error) {
+                return $error;
+            }
 
 
             continue;
             continue;
         } else {
         } else {
             $query = $c->queries['asset_edit']['update_preview'];
             $query = $c->queries['asset_edit']['update_preview'];
-            $error = $c->utils->error_reponse_if_missing_or_not_string($error, $response, $preview, 'edit_preview_id');
-            if($error) return $error;
+            $error = $c->utils->errorResponseIfMissingOrNotString($error, $response, $preview, 'edit_preview_id');
+            if ($error) {
+                return $error;
+            }
             $query->bindValue(':edit_preview_id', (int) $preview['edit_preview_id'], PDO::PARAM_INT);
             $query->bindValue(':edit_preview_id', (int) $preview['edit_preview_id'], PDO::PARAM_INT);
         }
         }
         $query->bindValue(':edit_id', (int) $edit_id, PDO::PARAM_INT);
         $query->bindValue(':edit_id', (int) $edit_id, PDO::PARAM_INT);
 
 
         foreach ($c->constants['asset_edit_preview_fields'] as $i => $field) {
         foreach ($c->constants['asset_edit_preview_fields'] as $i => $field) {
-            if(!$required) {
-                if(isset($preview[$field]) && !(isset($original_preview) && $original_preview[$field] == $preview[$field])) {
+            if (!$required) {
+                if (isset($preview[$field]) && !(isset($original_preview) && $original_preview[$field] == $preview[$field])) {
                     $query->bindValue(':' . $field, $preview[$field]);
                     $query->bindValue(':' . $field, $preview[$field]);
-                } elseif(!isset($preview[$field]) && !isset($original_preview)) {
+                } elseif (!isset($preview[$field]) && !isset($original_preview)) {
                     $query->bindValue(':' . $field, $preview[$field]);
                     $query->bindValue(':' . $field, $preview[$field]);
                 } else {
                 } else {
                     $query->bindValue(':' . $field, null, PDO::PARAM_NULL);
                     $query->bindValue(':' . $field, null, PDO::PARAM_NULL);
                 }
                 }
             } else {
             } else {
-                $error = $c->utils->error_reponse_if_missing_or_not_string($error, $response, $preview, $field);
-                if(!$error) $query->bindValue(':' . $field, $preview[$field]);
+                $error = $c->utils->errorResponseIfMissingOrNotString($error, $response, $preview, $field);
+                if (!$error) {
+                    $query->bindValue(':' . $field, $preview[$field]);
+                }
             }
             }
         }
         }
-        if($error) return $error;
+        if ($error) {
+            return $error;
+        }
 
 
         $query->execute();
         $query->execute();
-        $error = $c->utils->error_reponse_if_query_bad(false, $response, $query);
-        if($error) return $error;
+        $error = $c->utils->errorResponseIfQueryBad(false, $response, $query);
+        if ($error) {
+            return $error;
+        }
     }
     }
 
 
     return $error;
     return $error;
@@ -205,9 +234,11 @@ function _add_previews_to_edit($c, $error, &$response, $edit_id, $previews, $ass
 $app->get('/asset/edit', function ($request, $response, $args) {
 $app->get('/asset/edit', function ($request, $response, $args) {
 
 
     // Enable if needed (for now, transparent to all) [Also change request to post]
     // Enable if needed (for now, transparent to all) [Also change request to post]
-    // $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    // $error = $this->utils->error_reponse_if_not_user_has_level($error, $response, $user, 'moderator');
-    // if($error) return $response;
+    // $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    // $error = $this->utils->errorResponseIfNotUserHasLevel($error, $response, $user, 'moderator');
+    // if ($error) {
+    //     return $response;
+    // }
 
 
     $params = $request->getQueryParams();
     $params = $request->getQueryParams();
 
 
@@ -218,40 +249,40 @@ $app->get('/asset/edit', function ($request, $response, $args) {
     $page_size = 10;
     $page_size = 10;
     $max_page_size = 500;
     $max_page_size = 500;
     $page_offset = 0;
     $page_offset = 0;
-    if(isset($params['asset'])) {
+    if (isset($params['asset'])) {
         $asset_id = (int) $params['asset'];
         $asset_id = (int) $params['asset'];
     }
     }
-    if(isset($params['status'])) { // Expects the param like `new+in_review`
-        if(is_array($params['status'])) {
-            foreach($params['status'] as $key => $value) {
-                if($value && isset($this->constants['edit_status'][$key])) {
+    if (isset($params['status'])) { // Expects the param like `new+in_review`
+        if (is_array($params['status'])) {
+            foreach ($params['status'] as $key => $value) {
+                if ($value && isset($this->constants['edit_status'][$key])) {
                     array_push($statuses, (int) $this->constants['edit_status'][$key]);
                     array_push($statuses, (int) $this->constants['edit_status'][$key]);
                 }
                 }
             }
             }
         } else {
         } else {
-            foreach(explode(' ', $params['status']) as $key => $value) { // `+` is changed to ` ` automatically
-                if(isset($this->constants['edit_status'][$value])) {
+            foreach (explode(' ', $params['status']) as $key => $value) { // `+` is changed to ` ` automatically
+                if (isset($this->constants['edit_status'][$value])) {
                     array_push($statuses, (int) $this->constants['edit_status'][$value]);
                     array_push($statuses, (int) $this->constants['edit_status'][$value]);
                 }
                 }
             }
             }
         }
         }
     }
     }
-    if(isset($params['filter'])) {
+    if (isset($params['filter'])) {
         $filter = '%'.preg_replace('/[[:punct:]]+/', '%', $params['filter']).'%';
         $filter = '%'.preg_replace('/[[:punct:]]+/', '%', $params['filter']).'%';
     }
     }
-    if(isset($params['user'])) {
+    if (isset($params['user'])) {
         $username = $params['user'];
         $username = $params['user'];
     }
     }
-    if(isset($params['max_results'])) {
+    if (isset($params['max_results'])) {
         $page_size = min(abs((int) $params['max_results']), $max_page_size);
         $page_size = min(abs((int) $params['max_results']), $max_page_size);
     }
     }
-    if(isset($params['page'])) {
+    if (isset($params['page'])) {
         $page_offset = abs((int) $params['page']) * $page_size;
         $page_offset = abs((int) $params['page']) * $page_size;
-    } elseif(isset($params['offset'])) {
+    } elseif (isset($params['offset'])) {
         $page_offset = abs((int) $params['offset']);
         $page_offset = abs((int) $params['offset']);
     }
     }
 
 
-    if(count($statuses) === 0) {
+    if (count($statuses) === 0) {
         $statuses = [0, 1]; // New + In Review
         $statuses = [0, 1]; // New + In Review
     }
     }
     $statuses = implode('|', $statuses);
     $statuses = implode('|', $statuses);
@@ -265,8 +296,10 @@ $app->get('/asset/edit', function ($request, $response, $args) {
     $query->bindValue(':skip_count', $page_offset, PDO::PARAM_INT);
     $query->bindValue(':skip_count', $page_offset, PDO::PARAM_INT);
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     $query_count = $this->queries['asset_edit']['search_count'];
     $query_count = $this->queries['asset_edit']['search_count'];
     $query_count->bindValue(':filter', $filter);
     $query_count->bindValue(':filter', $filter);
@@ -275,15 +308,17 @@ $app->get('/asset/edit', function ($request, $response, $args) {
     $query_count->bindValue(':statuses_regex', $statuses);
     $query_count->bindValue(':statuses_regex', $statuses);
     $query_count->execute();
     $query_count->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_count);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_count);
+    if ($error) {
+        return $response;
+    }
 
 
     $total_count = $query_count->fetchAll()[0]['count'];
     $total_count = $query_count->fetchAll()[0]['count'];
 
 
     $asset_edits = $query->fetchAll();
     $asset_edits = $query->fetchAll();
 
 
     $context = $this;
     $context = $this;
-    $asset_edits = array_map(function($asset_edit) use($context) {
+    $asset_edits = array_map(function ($asset_edit) use ($context) {
         $asset_edit['status'] = $context->constants['edit_status'][(int) $asset_edit['status']];
         $asset_edit['status'] = $context->constants['edit_status'][(int) $asset_edit['status']];
         $asset_edit['support_level'] = $context->constants['support_level'][(int) $asset_edit['support_level']];
         $asset_edit['support_level'] = $context->constants['support_level'][(int) $asset_edit['support_level']];
         return $asset_edit;
         return $asset_edit;
@@ -304,9 +339,11 @@ $get_edit = function ($request, $response, $args) {
     $query->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    $error = $this->utils->error_reponse_if_query_no_results($error, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    $error = $this->utils->errorResponseIfQueryNoResults($error, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     $output = $query->fetchAll();
     $output = $query->fetchAll();
 
 
@@ -317,41 +354,40 @@ $get_edit = function ($request, $response, $args) {
     $asset_edit = [];
     $asset_edit = [];
 
 
     foreach ($output as $row) {
     foreach ($output as $row) {
-
         foreach ($row as $column => $value) {
         foreach ($row as $column => $value) {
-            if($previews_last_i !== null && ($column==='preview_id' || $column==='type' || $column==='link' || $column==='thumbnail')) {
+            if ($previews_last_i !== null && ($column==='preview_id' || $column==='type' || $column==='link' || $column==='thumbnail')) {
                 $previews[$previews_last_i][$column] = $value;
                 $previews[$previews_last_i][$column] = $value;
-            } elseif($previews_last_i !== null && $column==='operation') {
+            } elseif ($previews_last_i !== null && $column==='operation') {
                 $previews[$previews_last_i][$column] = $this->constants['edit_preview_operation'][(int) $value];
                 $previews[$previews_last_i][$column] = $this->constants['edit_preview_operation'][(int) $value];
-            } elseif($unedited_previews_last_i !== null && ($column==='unedited_type' || $column==='unedited_link' || $column==='unedited_thumbnail')) {
+            } elseif ($unedited_previews_last_i !== null && ($column==='unedited_type' || $column==='unedited_link' || $column==='unedited_thumbnail')) {
                 $unedited_previews[$unedited_previews_last_i][substr($column, strlen('unedited_'))] = $value;
                 $unedited_previews[$unedited_previews_last_i][substr($column, strlen('unedited_'))] = $value;
-            }  elseif($column==='orig_type' || $column==='orig_link' || $column==='orig_thumbnail') {
-                if($value != null && $previews_last_i !== null) {
+            } elseif ($column==='orig_type' || $column==='orig_link' || $column==='orig_thumbnail') {
+                if ($value != null && $previews_last_i !== null) {
                     $previews[$previews_last_i]['original'][substr($column, strlen('orig_'))] = $value;
                     $previews[$previews_last_i]['original'][substr($column, strlen('orig_'))] = $value;
                 }
                 }
-            } elseif($value!==null) {
-                if($column==='edit_preview_id') {
+            } elseif ($value!==null) {
+                if ($column==='edit_preview_id') {
                     $previews[$value] = ['edit_preview_id' => $value];
                     $previews[$value] = ['edit_preview_id' => $value];
                     $previews_last_i = $value;
                     $previews_last_i = $value;
-                } elseif($column==='unedited_preview_id') {
+                } elseif ($column==='unedited_preview_id') {
                     $unedited_previews[$value] = ['preview_id' => $value];
                     $unedited_previews[$value] = ['preview_id' => $value];
                     $unedited_previews_last_i = $value;
                     $unedited_previews_last_i = $value;
-                } elseif($column==='status') {
+                } elseif ($column==='status') {
                     $asset_edit['status'] = $this->constants['edit_status'][(int) $value];
                     $asset_edit['status'] = $this->constants['edit_status'][(int) $value];
-                } elseif($column==='download_provider') {
+                } elseif ($column==='download_provider') {
                     $asset_edit['download_provider'] = $this->constants['download_provider'][(int) $value];
                     $asset_edit['download_provider'] = $this->constants['download_provider'][(int) $value];
                 } else {
                 } else {
                     $asset_edit[$column] = $value;
                     $asset_edit[$column] = $value;
                 }
                 }
-            } elseif($column!=='edit_preview_id' && $column!=='preview_id') {
+            } elseif ($column!=='edit_preview_id' && $column!=='preview_id') {
                 $asset_edit[$column] = $value;
                 $asset_edit[$column] = $value;
             }
             }
         }
         }
     }
     }
 
 
-    if($asset_edit['asset_id'] != -1) {
-        foreach($previews as $preview) {
-            if(isset($preview['preview_id']) && isset($unedited_previews[$preview['preview_id']])) {
+    if ($asset_edit['asset_id'] != -1) {
+        foreach ($previews as $preview) {
+            if (isset($preview['preview_id']) && isset($unedited_previews[$preview['preview_id']])) {
                 unset($unedited_previews[$preview['preview_id']]);
                 unset($unedited_previews[$preview['preview_id']]);
             }
             }
         }
         }
@@ -360,29 +396,31 @@ $get_edit = function ($request, $response, $args) {
         $asset_edit['previews'] = array_values($previews);
         $asset_edit['previews'] = array_values($previews);
     }
     }
 
 
-    if($asset_edit['asset_id'] != -1) {
+    if ($asset_edit['asset_id'] != -1) {
         $query_asset = $this->queries['asset']['get_one_bare'];
         $query_asset = $this->queries['asset']['get_one_bare'];
         $query_asset->bindValue(':asset_id', (int) $asset_edit['asset_id'], PDO::PARAM_INT);
         $query_asset->bindValue(':asset_id', (int) $asset_edit['asset_id'], PDO::PARAM_INT);
         $query_asset->execute();
         $query_asset->execute();
 
 
-        $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_asset);
-        $error = $this->utils->error_reponse_if_query_no_results($error, $response, $query_asset);
-        if($error) return $response;
+        $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_asset);
+        $error = $this->utils->errorResponseIfQueryNoResults($error, $response, $query_asset);
+        if ($error) {
+            return $response;
+        }
 
 
         $asset = $query_asset->fetchAll()[0];
         $asset = $query_asset->fetchAll()[0];
 
 
         $asset_edit['original'] = $asset;
         $asset_edit['original'] = $asset;
         $asset_edit['original']['download_provider'] = $this->constants['download_provider'][$asset['download_provider']];
         $asset_edit['original']['download_provider'] = $this->constants['download_provider'][$asset['download_provider']];
 
 
-        if($asset_edit['browse_url'] || $asset_edit['download_provider'] || $asset_edit['download_commit']) {
-            $asset_edit['download_url'] = $this->utils->get_computed_download_url(
+        if ($asset_edit['browse_url'] || $asset_edit['download_provider'] || $asset_edit['download_commit']) {
+            $asset_edit['download_url'] = $this->utils->getComputedDownloadUrl(
                 $asset_edit['browse_url'] ?: $asset_edit['original']['browse_url'],
                 $asset_edit['browse_url'] ?: $asset_edit['original']['browse_url'],
                 $asset_edit['download_provider'] ?: $asset_edit['original']['download_provider'],
                 $asset_edit['download_provider'] ?: $asset_edit['original']['download_provider'],
                 $asset_edit['download_commit'] ?: $asset_edit['original']['download_commit'],
                 $asset_edit['download_commit'] ?: $asset_edit['original']['download_commit'],
                 $warning
                 $warning
             );
             );
-            if($asset_edit['issues_url'] == '') {
-                $asset_edit['issues_url'] = $this->utils->get_default_issues_url(
+            if ($asset_edit['issues_url'] == '') {
+                $asset_edit['issues_url'] = $this->utils->getDefaultIssuesUrl(
                     $asset_edit['browse_url'] ?: $asset_edit['original']['browse_url'],
                     $asset_edit['browse_url'] ?: $asset_edit['original']['browse_url'],
                     $asset_edit['download_provider'] ?: $asset_edit['original']['download_provider']
                     $asset_edit['download_provider'] ?: $asset_edit['original']['download_provider']
                 );
                 );
@@ -391,32 +429,32 @@ $get_edit = function ($request, $response, $args) {
             $asset_edit['download_url'] = null;
             $asset_edit['download_url'] = null;
         }
         }
 
 
-        $asset_edit['original']['download_url'] = $this->utils->get_computed_download_url($asset_edit['original']['browse_url'], $asset_edit['original']['download_provider'], $asset_edit['original']['download_commit'], $warning);
+        $asset_edit['original']['download_url'] = $this->utils->getComputedDownloadUrl($asset_edit['original']['browse_url'], $asset_edit['original']['download_provider'], $asset_edit['original']['download_commit'], $warning);
 
 
-        if($asset_edit['original']['issues_url'] == '') {
-            $asset_edit['original']['issues_url'] = $this->utils->get_default_issues_url($asset_edit['original']['browse_url'], $asset_edit['original']['download_provider']);
+        if ($asset_edit['original']['issues_url'] == '') {
+            $asset_edit['original']['issues_url'] = $this->utils->getDefaultIssuesUrl($asset_edit['original']['browse_url'], $asset_edit['original']['download_provider']);
         }
         }
     } else {
     } else {
-        $asset_edit['download_url'] = $this->utils->get_computed_download_url($asset_edit['browse_url'], $asset_edit['download_provider'], $asset_edit['download_commit'], $warning);
+        $asset_edit['download_url'] = $this->utils->getComputedDownloadUrl($asset_edit['browse_url'], $asset_edit['download_provider'], $asset_edit['download_commit'], $warning);
 
 
-        if($asset_edit['issues_url'] == '') {
-            $asset_edit['issues_url'] = $this->utils->get_default_issues_url($asset_edit['browse_url'], $asset_edit['download_provider']);
+        if ($asset_edit['issues_url'] == '') {
+            $asset_edit['issues_url'] = $this->utils->getDefaultIssuesUrl($asset_edit['browse_url'], $asset_edit['download_provider']);
         }
         }
     }
     }
 
 
-    if($warning != null) {
+    if ($warning != null) {
         $asset_edit['warning'] = $warning;
         $asset_edit['warning'] = $warning;
     }
     }
-    if($asset_edit['download_commit'] == 'master') {
-        if(isset($asset_edit['warning'])) {
+    if ($asset_edit['download_commit'] == 'master') {
+        if (isset($asset_edit['warning'])) {
             $asset_edit['warning'] .= "\n\n";
             $asset_edit['warning'] .= "\n\n";
         } else {
         } else {
             $asset_edit['warning'] = '';
             $asset_edit['warning'] = '';
         }
         }
         $asset_edit['warning'] .= "Giving 'master' (or any other branch name) as the commit to be downloaded is not recommended, since it would invalidate the asset when you push a new version (since we ensure the version is kept the same via a sha256 hash of the zip). You can try using tags instead.";
         $asset_edit['warning'] .= "Giving 'master' (or any other branch name) as the commit to be downloaded is not recommended, since it would invalidate the asset when you push a new version (since we ensure the version is kept the same via a sha256 hash of the zip). You can try using tags instead.";
     }
     }
-    if(sizeof(preg_grep('/\/|\\|\:|^\.|\ |\^|\~|\?|\*|\[|^\@$|\@\{/', [$asset_edit['download_commit']])) != 0) {
-        if(isset($asset_edit['warning'])) {
+    if (sizeof(preg_grep('/\/|\\|\:|^\.|\ |\^|\~|\?|\*|\[|^\@$|\@\{/', [$asset_edit['download_commit']])) != 0) {
+        if (isset($asset_edit['warning'])) {
             $asset_edit['warning'] .= "\n\n";
             $asset_edit['warning'] .= "\n\n";
         } else {
         } else {
             $asset_edit['warning'] = '';
             $asset_edit['warning'] = '';
@@ -430,7 +468,7 @@ $get_edit = function ($request, $response, $args) {
 
 
 // Binding to multiple routes
 // Binding to multiple routes
 $app->get('/asset/edit/{id:[0-9]+}', $get_edit);
 $app->get('/asset/edit/{id:[0-9]+}', $get_edit);
-if(FRONTEND) {
+if (FRONTEND) {
     $app->get('/asset/edit/{id:[0-9]+}/edit', $get_edit);
     $app->get('/asset/edit/{id:[0-9]+}/edit', $get_edit);
 }
 }
 
 
@@ -438,8 +476,10 @@ if(FRONTEND) {
 $app->post('/asset', function ($request, $response, $args) {
 $app->post('/asset', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    if ($error) {
+        return $response;
+    }
 
 
     return _submit_asset_edit($this, $response, $body, $user['user_id'], -1);
     return _submit_asset_edit($this, $response, $body, $user['user_id'], -1);
 });
 });
@@ -449,22 +489,28 @@ $app->post('/asset', function ($request, $response, $args) {
 $app->post('/asset/{id:[0-9]+}', function ($request, $response, $args) {
 $app->post('/asset/{id:[0-9]+}', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    if ($error) {
+        return $response;
+    }
 
 
     // Ensure the author is editing the asset
     // Ensure the author is editing the asset
     $query_asset = $this->queries['asset']['get_one_bare'];
     $query_asset = $this->queries['asset']['get_one_bare'];
     $query_asset->bindValue(':asset_id', (int) $args['id'], PDO::PARAM_INT);
     $query_asset->bindValue(':asset_id', (int) $args['id'], PDO::PARAM_INT);
     $query_asset->execute();
     $query_asset->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_asset);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_asset);
+    if ($error) {
+        return $response;
+    }
 
 
     $asset = $query_asset->fetchAll()[0];
     $asset = $query_asset->fetchAll()[0];
 
 
-    if((int) $asset['user_id'] !== (int) $user['user_id']) {
-        $error = $this->utils->error_reponse_if_not_user_has_level($error, $response, $user, 'editor', 'You are not authorized to update this asset');
-        if($error) return $response;
+    if ((int) $asset['user_id'] !== (int) $user['user_id']) {
+        $error = $this->utils->errorResponseIfNotUserHasLevel($error, $response, $user, 'editor', 'You are not authorized to update this asset');
+        if ($error) {
+            return $response;
+        }
     }
     }
 
 
     return _submit_asset_edit($this, $response, $body, $user['user_id'], (int) $args['id']);
     return _submit_asset_edit($this, $response, $body, $user['user_id'], (int) $args['id']);
@@ -475,27 +521,31 @@ $app->post('/asset/{id:[0-9]+}', function ($request, $response, $args) {
 $app->post('/asset/edit/{id:[0-9]+}', function ($request, $response, $args) {
 $app->post('/asset/edit/{id:[0-9]+}', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    if ($error) {
+        return $response;
+    }
 
 
     // Fetch the edit to check the user id
     // Fetch the edit to check the user id
     $query_edit = $this->queries['asset_edit']['get_one_bare'];
     $query_edit = $this->queries['asset_edit']['get_one_bare'];
     $query_edit->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query_edit->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query_edit->execute();
     $query_edit->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_edit);
-    $error = $this->utils->error_reponse_if_query_no_results($error, $response, $query_edit);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_edit);
+    $error = $this->utils->errorResponseIfQueryNoResults($error, $response, $query_edit);
+    if ($error) {
+        return $response;
+    }
 
 
     $asset_edit = $query_edit->fetchAll()[0];
     $asset_edit = $query_edit->fetchAll()[0];
 
 
-    if((int) $asset_edit['user_id'] !== (int) $user['user_id']) {
+    if ((int) $asset_edit['user_id'] !== (int) $user['user_id']) {
         return $response->withJson([
         return $response->withJson([
             'error' => 'You are not authorized to update this asset edit',
             'error' => 'You are not authorized to update this asset edit',
         ], 403);
         ], 403);
     }
     }
 
 
-    if((int) $asset_edit['status'] !== $this->constants['edit_status']['new']) {
+    if ((int) $asset_edit['status'] !== $this->constants['edit_status']['new']) {
         return $response->withJson([
         return $response->withJson([
             'error' => 'You are no longer allowed to update this asset edit, please make a new one',
             'error' => 'You are no longer allowed to update this asset edit, please make a new one',
         ], 403);
         ], 403);
@@ -507,21 +557,27 @@ $app->post('/asset/edit/{id:[0-9]+}', function ($request, $response, $args) {
 
 
 
 
     $asset = null;
     $asset = null;
-    if($asset_edit['asset_id'] != -1) {
+    if ($asset_edit['asset_id'] != -1) {
         $query_asset = $this->queries['asset']['get_one_bare'];
         $query_asset = $this->queries['asset']['get_one_bare'];
         $query_asset->bindValue(':asset_id', (int) $asset_edit['asset_id'], PDO::PARAM_INT);
         $query_asset->bindValue(':asset_id', (int) $asset_edit['asset_id'], PDO::PARAM_INT);
         $query_asset->execute();
         $query_asset->execute();
 
 
-        $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_asset);
-        if($error) return $response;
+        $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_asset);
+        if ($error) {
+            return $response;
+        }
 
 
         $asset = $query_asset->fetchAll()[0];
         $asset = $query_asset->fetchAll()[0];
 
 
         $error = _insert_asset_edit_fields($this, false, $response, $query, $body, false, $asset);
         $error = _insert_asset_edit_fields($this, false, $response, $query, $body, false, $asset);
-        if($error) return $response;
+        if ($error) {
+            return $response;
+        }
     } else {
     } else {
         $error = _insert_asset_edit_fields($this, false, $response, $query, $body, true, $asset_edit); // Edit of new asset, everything must be non-null
         $error = _insert_asset_edit_fields($this, false, $response, $query, $body, true, $asset_edit); // Edit of new asset, everything must be non-null
-        if($error) return $response;
+        if ($error) {
+            return $response;
+        }
     }
     }
 
 
 
 
@@ -529,15 +585,15 @@ $app->post('/asset/edit/{id:[0-9]+}', function ($request, $response, $args) {
     $this->db->beginTransaction();
     $this->db->beginTransaction();
 
 
     $query->execute();
     $query->execute();
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) {
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
         $this->db->rollback();
         $this->db->rollback();
         return $response;
         return $response;
     }
     }
 
 
-    if(isset($body['previews'])) {
+    if (isset($body['previews'])) {
         $error = _add_previews_to_edit($this, $error, $response, $args['id'], $body['previews'], $asset, false);
         $error = _add_previews_to_edit($this, $error, $response, $args['id'], $body['previews'], $asset, false);
-        if($error) {
+        if ($error) {
             $this->db->rollback();
             $this->db->rollback();
             return $response;
             return $response;
         }
         }
@@ -556,22 +612,26 @@ $app->post('/asset/edit/{id:[0-9]+}', function ($request, $response, $args) {
 $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $args) {
 $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    $error = $this->utils->error_reponse_if_not_user_has_level($error, $response, $user, 'moderator', 'You are not authorized to accept this asset edit');
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    $error = $this->utils->errorResponseIfNotUserHasLevel($error, $response, $user, 'moderator', 'You are not authorized to accept this asset edit');
+    if ($error) {
+        return $response;
+    }
 
 
     // Get the edit
     // Get the edit
     $query_edit = $this->queries['asset_edit']['get_one'];
     $query_edit = $this->queries['asset_edit']['get_one'];
     $query_edit->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query_edit->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query_edit->execute();
     $query_edit->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_edit);
-    $error = $this->utils->error_reponse_if_query_no_results($error, $response, $query_edit);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_edit);
+    $error = $this->utils->errorResponseIfQueryNoResults($error, $response, $query_edit);
+    if ($error) {
+        return $response;
+    }
 
 
     $asset_edit_previews = $query_edit->fetchAll();
     $asset_edit_previews = $query_edit->fetchAll();
     $asset_edit = $asset_edit_previews[0];
     $asset_edit = $asset_edit_previews[0];
-    if((int) $asset_edit['status'] !== $this->constants['edit_status']['in_review']) {
+    if ((int) $asset_edit['status'] !== $this->constants['edit_status']['in_review']) {
         return $response->withJson([
         return $response->withJson([
             'error' => 'The edit should be in review in order to be accepted',
             'error' => 'The edit should be in review in order to be accepted',
         ], 403);
         ], 403);
@@ -580,7 +640,7 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
     // Start building the query
     // Start building the query
     $query = null;
     $query = null;
 
 
-    if((int) $asset_edit['asset_id'] === -1) {
+    if ((int) $asset_edit['asset_id'] === -1) {
         $query = $this->queries['asset']['apply_creational_edit'];
         $query = $this->queries['asset']['apply_creational_edit'];
         $query->bindValue(':user_id', (int) $asset_edit['user_id'], PDO::PARAM_INT);
         $query->bindValue(':user_id', (int) $asset_edit['user_id'], PDO::PARAM_INT);
     } else {
     } else {
@@ -591,7 +651,7 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
     // Params
     // Params
     $update_version = false;
     $update_version = false;
     foreach ($this->constants['asset_edit_fields'] as $i => $field) {
     foreach ($this->constants['asset_edit_fields'] as $i => $field) {
-        if(isset($asset_edit[$field]) && $asset_edit[$field] !== null) {
+        if (isset($asset_edit[$field]) && $asset_edit[$field] !== null) {
             $query->bindValue(':' . $field, $asset_edit[$field]);
             $query->bindValue(':' . $field, $asset_edit[$field]);
             $update_version = $update_version || ($field === 'browse_url' || $field === 'download_provider' || $field === 'download_commit' || $field === 'version_string');
             $update_version = $update_version || ($field === 'browse_url' || $field === 'download_provider' || $field === 'download_commit' || $field === 'version_string');
         } else {
         } else {
@@ -599,12 +659,14 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
         }
         }
     }
     }
 
 
-    if($update_version) {
-        $error = $this->utils->error_reponse_if_missing_or_not_string(false, $response, $body, 'hash');
-        if($error) return $response;
+    if ($update_version) {
+        $error = $this->utils->errorResponseIfMissingOrNotString(false, $response, $body, 'hash');
+        if ($error) {
+            return $response;
+        }
 
 
         $body['hash'] = trim($body['hash']);
         $body['hash'] = trim($body['hash']);
-        if(sizeof(preg_grep('/^[a-f0-9]{64}$/', [$body['hash']])) == 0) {
+        if (sizeof(preg_grep('/^[a-f0-9]{64}$/', [$body['hash']])) == 0) {
             return $response->withJson([
             return $response->withJson([
                 'error' => 'Invalid hash given. Expected 64 lowercase hexadecimal digits.',
                 'error' => 'Invalid hash given. Expected 64 lowercase hexadecimal digits.',
             ]);
             ]);
@@ -613,9 +675,9 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
         $query->bindValue(':update_version', 1, PDO::PARAM_INT);
         $query->bindValue(':update_version', 1, PDO::PARAM_INT);
         $query->bindValue(':download_hash', $body['hash']);
         $query->bindValue(':download_hash', $body['hash']);
     } else {
     } else {
-        if(isset($body['hash']) && trim($body['hash']) != '') {
+        if (isset($body['hash']) && trim($body['hash']) != '') {
             $body['hash'] = trim($body['hash']);
             $body['hash'] = trim($body['hash']);
-            if(sizeof(preg_grep('/^[a-f0-9]{64}$/', [$body['hash']])) == 0) {
+            if (sizeof(preg_grep('/^[a-f0-9]{64}$/', [$body['hash']])) == 0) {
                 return $response->withJson([
                 return $response->withJson([
                     'error' => 'Invalid hash given. Expected either nothing or 64 lowercase hexadecimal digits.',
                     'error' => 'Invalid hash given. Expected either nothing or 64 lowercase hexadecimal digits.',
                 ]);
                 ]);
@@ -636,17 +698,21 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
     $query_status->bindValue(':reason', '');
     $query_status->bindValue(':reason', '');
 
 
     $query_status->execute();
     $query_status->execute();
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_status);
-    $error = $this->utils->error_reponse_if_query_no_results(false, $response, $query_status); // Important: Ensure that something was actually changed
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_status);
+    $error = $this->utils->errorResponseIfQueryNoResults(false, $response, $query_status); // Important: Ensure that something was actually changed
+    if ($error) {
+        return $response;
+    }
 
 
     // Run
     // Run
     $query->execute();
     $query->execute();
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     // Update the id in case it was newly-created
     // Update the id in case it was newly-created
-    if((int) $asset_edit['asset_id'] === -1) {
+    if ((int) $asset_edit['asset_id'] === -1) {
         $asset_edit['asset_id'] = $this->db->lastInsertId();
         $asset_edit['asset_id'] = $this->db->lastInsertId();
 
 
         $query_update_id = $this->queries['asset_edit']['set_asset_id'];
         $query_update_id = $this->queries['asset_edit']['set_asset_id'];
@@ -656,14 +722,16 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
 
 
         $query_update_id->execute();
         $query_update_id->execute();
 
 
-        $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_update_id);
-        if($error) return $response;
+        $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_update_id);
+        if ($error) {
+            return $response;
+        }
         $query_update_id->closeCursor();
         $query_update_id->closeCursor();
     }
     }
 
 
     $previews_processed = [];
     $previews_processed = [];
-    foreach($asset_edit_previews as $i => $preview) {
-        if(!isset($preview['edit_preview_id']) || $preview['edit_preview_id'] == null || isset($previews_processed[$preview['edit_preview_id']])) {
+    foreach ($asset_edit_previews as $i => $preview) {
+        if (!isset($preview['edit_preview_id']) || $preview['edit_preview_id'] == null || isset($previews_processed[$preview['edit_preview_id']])) {
             continue;
             continue;
         }
         }
         $previews_processed[$preview['edit_preview_id']] = true;
         $previews_processed[$preview['edit_preview_id']] = true;
@@ -672,13 +740,13 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
 
 
         $query_apply_preview->bindValue(':asset_id', (int) $asset_edit['asset_id']);
         $query_apply_preview->bindValue(':asset_id', (int) $asset_edit['asset_id']);
 
 
-        if($operation == 'remove' || $operation == 'update') {
+        if ($operation == 'remove' || $operation == 'update') {
             $query_apply_preview->bindValue(':preview_id', (int) $preview['preview_id']);
             $query_apply_preview->bindValue(':preview_id', (int) $preview['preview_id']);
         }
         }
 
 
-        if($operation == 'insert' || $operation == 'update') {
+        if ($operation == 'insert' || $operation == 'update') {
             foreach ($this->constants['asset_edit_preview_fields'] as $i => $field) {
             foreach ($this->constants['asset_edit_preview_fields'] as $i => $field) {
-                if(isset($preview[$field])) {
+                if (isset($preview[$field])) {
                     $query_apply_preview->bindValue(':' . $field, $preview[$field]);
                     $query_apply_preview->bindValue(':' . $field, $preview[$field]);
                 } else {
                 } else {
                     $query_apply_preview->bindValue(':' . $field, null, PDO::PARAM_NULL);
                     $query_apply_preview->bindValue(':' . $field, null, PDO::PARAM_NULL);
@@ -687,8 +755,10 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
         }
         }
 
 
         $query_apply_preview->execute();
         $query_apply_preview->execute();
-        $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_apply_preview);
-        if($error) return $response;
+        $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_apply_preview);
+        if ($error) {
+            return $response;
+        }
     }
     }
 
 
     return $response->withJson([
     return $response->withJson([
@@ -701,21 +771,25 @@ $app->post('/asset/edit/{id:[0-9]+}/accept', function ($request, $response, $arg
 $app->post('/asset/edit/{id:[0-9]+}/review', function ($request, $response, $args) {
 $app->post('/asset/edit/{id:[0-9]+}/review', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    $error = $this->utils->error_reponse_if_not_user_has_level($error, $response, $user, 'moderator', 'You are not authorized to put in review this asset edit');
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    $error = $this->utils->errorResponseIfNotUserHasLevel($error, $response, $user, 'moderator', 'You are not authorized to put in review this asset edit');
+    if ($error) {
+        return $response;
+    }
 
 
     // Get the edit
     // Get the edit
     $query_edit = $this->queries['asset_edit']['get_one_bare'];
     $query_edit = $this->queries['asset_edit']['get_one_bare'];
     $query_edit->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query_edit->bindValue(':edit_id', (int) $args['id'], PDO::PARAM_INT);
     $query_edit->execute();
     $query_edit->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_edit);
-    $error = $this->utils->error_reponse_if_query_no_results($error, $response, $query_edit);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_edit);
+    $error = $this->utils->errorResponseIfQueryNoResults($error, $response, $query_edit);
+    if ($error) {
+        return $response;
+    }
 
 
     $asset_edit = $query_edit->fetchAll()[0];
     $asset_edit = $query_edit->fetchAll()[0];
-    if((int) $asset_edit['status'] > $this->constants['edit_status']['in_review']) {
+    if ((int) $asset_edit['status'] > $this->constants['edit_status']['in_review']) {
         return $response->withJson([
         return $response->withJson([
             'error' => 'The edit should be new in order to be put in review',
             'error' => 'The edit should be new in order to be put in review',
         ], 403);
         ], 403);
@@ -730,8 +804,10 @@ $app->post('/asset/edit/{id:[0-9]+}/review', function ($request, $response, $arg
 
 
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     $asset_edit['status'] = 'in_review'; // Prepare to send
     $asset_edit['status'] = 'in_review'; // Prepare to send
     $asset_edit['url'] = 'asset/edit/' . $args['id'];
     $asset_edit['url'] = 'asset/edit/' . $args['id'];
@@ -744,10 +820,12 @@ $app->post('/asset/edit/{id:[0-9]+}/review', function ($request, $response, $arg
 $app->post('/asset/edit/{id:[0-9]+}/reject', function ($request, $response, $args) {
 $app->post('/asset/edit/{id:[0-9]+}/reject', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    $error = $this->utils->error_reponse_if_not_user_has_level($error, $response, $user, 'moderator', 'You are not authorized to reject this asset edit');
-    $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'reason');
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    $error = $this->utils->errorResponseIfNotUserHasLevel($error, $response, $user, 'moderator', 'You are not authorized to reject this asset edit');
+    $error = $this->utils->errorResponseIfMissingOrNotString($error, $response, $body, 'reason');
+    if ($error) {
+        return $response;
+    }
 
 
     $query = $this->queries['asset_edit']['set_status_and_reason'];
     $query = $this->queries['asset_edit']['set_status_and_reason'];
 
 
@@ -757,8 +835,10 @@ $app->post('/asset/edit/{id:[0-9]+}/reject', function ($request, $response, $arg
 
 
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     return $response->withJson([
     return $response->withJson([
         'rejected' => true,
         'rejected' => true,

+ 82 - 51
src/routes/auth.php

@@ -8,7 +8,7 @@ $app->get('/configure', function ($request, $response, $args) {
 
 
     $category_type = $this->constants['category_type']['addon'];
     $category_type = $this->constants['category_type']['addon'];
 
 
-    if(isset($params['type']) && isset($this->constants['category_type'][$params['type']])) {
+    if (isset($params['type']) && isset($this->constants['category_type'][$params['type']])) {
         $category_type = $this->constants['category_type'][$params['type']];
         $category_type = $this->constants['category_type'][$params['type']];
     }
     }
 
 
@@ -16,10 +16,12 @@ $app->get('/configure', function ($request, $response, $args) {
     $query->bindValue(':category_type', $category_type);
     $query->bindValue(':category_type', $category_type);
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
-    if(isset($request->getQueryParams()['session'])) {
+    if (isset($request->getQueryParams()['session'])) {
         $id = openssl_random_pseudo_bytes($this->settings['auth']['tokenSessionBytesLength']);
         $id = openssl_random_pseudo_bytes($this->settings['auth']['tokenSessionBytesLength']);
         $token = $this->tokens->generate([
         $token = $this->tokens->generate([
             'session' => base64_encode($id),
             'session' => base64_encode($id),
@@ -33,7 +35,6 @@ $app->get('/configure', function ($request, $response, $args) {
                 '/login#' . urlencode($token),
                 '/login#' . urlencode($token),
             // ^ TODO: Make those routes actually work
             // ^ TODO: Make those routes actually work
         ], 200);
         ], 200);
-
     } else {
     } else {
         return $response->withJson([
         return $response->withJson([
             'categories' => $query->fetchAll(),
             'categories' => $query->fetchAll(),
@@ -46,18 +47,22 @@ $app->post('/register', function ($request, $response, $args) {
     $query = $this->queries['user']['register'];
     $query = $this->queries['user']['register'];
     $query_check = $this->queries['user']['get_by_username'];
     $query_check = $this->queries['user']['get_by_username'];
 
 
-    $error = $this->utils->error_reponse_if_missing_or_not_string(false, $response, $body, 'username');
-    $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'email');
-    $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'password');
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfMissingOrNotString(false, $response, $body, 'username');
+    $error = $this->utils->errorResponseIfMissingOrNotString($error, $response, $body, 'email');
+    $error = $this->utils->errorResponseIfMissingOrNotString($error, $response, $body, 'password');
+    if ($error) {
+        return $response;
+    }
 
 
     $query_check->bindValue(':username', $body['username']);
     $query_check->bindValue(':username', $body['username']);
     $query_check->execute();
     $query_check->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_check);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_check);
+    if ($error) {
+        return $response;
+    }
 
 
-    if($query_check->rowCount() > 0) {
+    if ($query_check->rowCount() > 0) {
         return $response->withJson([
         return $response->withJson([
             'error' => 'Username already taken.',
             'error' => 'Username already taken.',
         ], 409);
         ], 409);
@@ -71,8 +76,10 @@ $app->post('/register', function ($request, $response, $args) {
 
 
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     return $response->withJson([
     return $response->withJson([
         'username' => $body['username'],
         'username' => $body['username'],
@@ -85,24 +92,28 @@ $app->post('/login', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
     $query = $this->queries['user']['get_by_username'];
     $query = $this->queries['user']['get_by_username'];
 
 
-    $error = $this->utils->error_reponse_if_missing_or_not_string(false, $response, $body, 'username');
-    $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'password');
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfMissingOrNotString(false, $response, $body, 'username');
+    $error = $this->utils->errorResponseIfMissingOrNotString($error, $response, $body, 'password');
+    if ($error) {
+        return $response;
+    }
 
 
     $query->bindValue(':username', $body['username']);
     $query->bindValue(':username', $body['username']);
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    $error = $this->utils->error_reponse_if_query_no_results(false, $response, $query, 'No such username: ' . $body['username']);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    $error = $this->utils->errorResponseIfQueryNoResults(false, $response, $query, 'No such username: ' . $body['username']);
+    if ($error) {
+        return $response;
+    }
 
 
     $user = $query->fetchAll()[0];
     $user = $query->fetchAll()[0];
 
 
-    if(password_verify($body['password'], $user['password_hash'])) {
-        if(isset($body['authorize_token'])) {
+    if (password_verify($body['password'], $user['password_hash'])) {
+        if (isset($body['authorize_token'])) {
             $token_data = $this->tokens->validate($body['authorize_token']);
             $token_data = $this->tokens->validate($body['authorize_token']);
 
 
-            if(!$token_data || !isset($token_data->session)) {
+            if (!$token_data || !isset($token_data->session)) {
                 return $response->withJson([
                 return $response->withJson([
                     'error' => 'Invalid token supplied'
                     'error' => 'Invalid token supplied'
                 ], 400);
                 ], 400);
@@ -121,8 +132,10 @@ $app->post('/login', function ($request, $response, $args) {
         $query_session->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
         $query_session->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
         $query_session->bindValue(':session_token', $session_id);
         $query_session->bindValue(':session_token', $session_id);
         $query_session->execute();
         $query_session->execute();
-        $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_session);
-        if($error) return $response;
+        $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_session);
+        if ($error) {
+            return $response;
+        }
 
 
         return $response->withJson([
         return $response->withJson([
             'username' => $body['username'],
             'username' => $body['username'],
@@ -140,7 +153,7 @@ $app->post('/login', function ($request, $response, $args) {
 
 
 $logout = function ($request, $response, $args) {
 $logout = function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
 
 
     $query = $this->queries['user']['set_session_token'];
     $query = $this->queries['user']['set_session_token'];
     $query->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
     $query->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
@@ -154,7 +167,7 @@ $logout = function ($request, $response, $args) {
     ], 200);
     ], 200);
 };
 };
 
 
-if(FRONTEND) {
+if (FRONTEND) {
     $app->get('/logout', $logout); // Cookies would allow us to logout without post body.
     $app->get('/logout', $logout); // Cookies would allow us to logout without post body.
 } else {
 } else {
     $app->post('/logout', $logout);
     $app->post('/logout', $logout);
@@ -163,18 +176,22 @@ if(FRONTEND) {
 $app->post('/forgot_password', function ($request, $response, $args) {
 $app->post('/forgot_password', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->error_reponse_if_missing_or_not_string(false, $response, $body, 'email');
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfMissingOrNotString(false, $response, $body, 'email');
+    if ($error) {
+        return $response;
+    }
 
 
     $query_user = $this->queries['user']['get_by_email'];
     $query_user = $this->queries['user']['get_by_email'];
     $query_user->bindValue(':email', $body['email']);
     $query_user->bindValue(':email', $body['email']);
     $query_user->execute();
     $query_user->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_user);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_user);
+    if ($error) {
+        return $response;
+    }
 
 
 
 
-    if($query_user->rowCount() != 0) {
+    if ($query_user->rowCount() != 0) {
         $user = $query_user->fetchAll()[0];
         $user = $query_user->fetchAll()[0];
 
 
         $reset_id = openssl_random_pseudo_bytes($this->settings['auth']['tokenResetBytesLength']);
         $reset_id = openssl_random_pseudo_bytes($this->settings['auth']['tokenResetBytesLength']);
@@ -186,8 +203,10 @@ $app->post('/forgot_password', function ($request, $response, $args) {
         $query->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
         $query->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
         $query->bindValue(':reset_token', $reset_id);
         $query->bindValue(':reset_token', $reset_id);
         $query->execute();
         $query->execute();
-        $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-        if($error) return $response;
+        $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+        if ($error) {
+            return $response;
+        }
 
 
         $reset_link = $request->getUri()->getScheme() . '://' . $_SERVER['HTTP_HOST'] .
         $reset_link = $request->getUri()->getScheme() . '://' . $_SERVER['HTTP_HOST'] .
             (FRONTEND ? $request->getUri()->getBasePath() : dirname($request->getUri()->getBasePath())) .
             (FRONTEND ? $request->getUri()->getBasePath() : dirname($request->getUri()->getBasePath())) .
@@ -202,7 +221,7 @@ $app->post('/forgot_password', function ($request, $response, $args) {
             'link' => $reset_link,
             'link' => $reset_link,
         ]);
         ]);
         $mail->AltBody = "Reset your ($user[username]'s) password: $reset_link\n";
         $mail->AltBody = "Reset your ($user[username]'s) password: $reset_link\n";
-        if(!$mail->send()) {
+        if (!$mail->send()) {
             $this->logger->error('mailSendFail', [$mail->ErrorInfo]);
             $this->logger->error('mailSendFail', [$mail->ErrorInfo]);
         }
         }
         // $this->logger->info('mailLinkDebug', [$reset_link]);
         // $this->logger->info('mailLinkDebug', [$reset_link]);
@@ -217,8 +236,10 @@ $app->get('/reset_password', function ($request, $response, $args) {
     $params = $request->getQueryParams();
     $params = $request->getQueryParams();
     $body = null !== $request->getParsedBody()? $request->getParsedBody() : [];
     $body = null !== $request->getParsedBody()? $request->getParsedBody() : [];
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $params + $body, $user, $token_data, true);
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $params + $body, $user, $token_data, true);
+    if ($error) {
+        return $response;
+    }
 
 
     $combined_body = $params + $body;
     $combined_body = $params + $body;
 
 
@@ -230,9 +251,11 @@ $app->get('/reset_password', function ($request, $response, $args) {
 $app->post('/reset_password', function ($request, $response, $args) {
 $app->post('/reset_password', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user, $token_data, true);
-    $error = $this->utils->error_reponse_if_missing_or_not_string(false, $response, $body, 'password');
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user, $token_data, true);
+    $error = $this->utils->errorResponseIfMissingOrNotString(false, $response, $body, 'password');
+    if ($error) {
+        return $response;
+    }
 
 
     $password_hash = password_hash($body['password'], PASSWORD_BCRYPT, $this->get('settings')['auth']['bcryptOptions']);
     $password_hash = password_hash($body['password'], PASSWORD_BCRYPT, $this->get('settings')['auth']['bcryptOptions']);
 
 
@@ -240,15 +263,19 @@ $app->post('/reset_password', function ($request, $response, $args) {
     $query_password->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
     $query_password->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
     $query_password->bindValue(':password_hash', $password_hash);
     $query_password->bindValue(':password_hash', $password_hash);
     $query_password->execute();
     $query_password->execute();
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_password);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_password);
+    if ($error) {
+        return $response;
+    }
 
 
     $query = $this->queries['user']['set_reset_token'];
     $query = $this->queries['user']['set_reset_token'];
     $query->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
     $query->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
     $query->bindValue(':reset_token', null, PDO::PARAM_NULL);
     $query->bindValue(':reset_token', null, PDO::PARAM_NULL);
     $query->execute();
     $query->execute();
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     return $response->withJson([
     return $response->withJson([
         'token' => null,
         'token' => null,
@@ -259,12 +286,14 @@ $app->post('/reset_password', function ($request, $response, $args) {
 $app->post('/change_password', function ($request, $response, $args) {
 $app->post('/change_password', function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user, $token_data);
-    $error = $this->utils->error_reponse_if_missing_or_not_string(false, $response, $body, 'new_password');
-    $error = $this->utils->error_reponse_if_missing_or_not_string($error, $response, $body, 'old_password');
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user, $token_data);
+    $error = $this->utils->errorResponseIfMissingOrNotString(false, $response, $body, 'new_password');
+    $error = $this->utils->errorResponseIfMissingOrNotString($error, $response, $body, 'old_password');
+    if ($error) {
+        return $response;
+    }
 
 
-    if(!password_verify($body['old_password'], $user['password_hash'])) {
+    if (!password_verify($body['old_password'], $user['password_hash'])) {
         return $response->withJson([
         return $response->withJson([
             'error' => 'Wrong old password supplied!',
             'error' => 'Wrong old password supplied!',
         ], 403);
         ], 403);
@@ -276,8 +305,10 @@ $app->post('/change_password', function ($request, $response, $args) {
     $query_password->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
     $query_password->bindValue(':id', (int) $user['user_id'], PDO::PARAM_INT);
     $query_password->bindValue(':password_hash', $password_hash);
     $query_password->bindValue(':password_hash', $password_hash);
     $query_password->execute();
     $query_password->execute();
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query_password);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query_password);
+    if ($error) {
+        return $response;
+    }
 
 
     return $response->withJson([
     return $response->withJson([
         'token' => null,
         'token' => null,

+ 13 - 9
src/routes/user.php

@@ -4,18 +4,20 @@
 $get_feed = function ($request, $response, $args) {
 $get_feed = function ($request, $response, $args) {
     $body = $request->getParsedBody();
     $body = $request->getParsedBody();
 
 
-    $error = $this->utils->ensure_logged_in(false, $response, $body, $user);
-    if($error) return $response;
+    $error = $this->utils->ensureLoggedIn(false, $response, $body, $user);
+    if ($error) {
+        return $response;
+    }
 
 
     $page_size = 40;
     $page_size = 40;
     $max_page_size = 500;
     $max_page_size = 500;
     $page_offset = 0;
     $page_offset = 0;
-    if(isset($params['max_results'])) {
+    if (isset($params['max_results'])) {
         $page_size = min(abs((int) $params['max_results']), $max_page_size);
         $page_size = min(abs((int) $params['max_results']), $max_page_size);
     }
     }
-    if(isset($params['page'])) {
+    if (isset($params['page'])) {
         $page_offset = abs((int) $params['page']) * $page_size;
         $page_offset = abs((int) $params['page']) * $page_size;
-    } elseif(isset($params['offset'])) {
+    } elseif (isset($params['offset'])) {
         $page_offset = abs((int) $params['offset']);
         $page_offset = abs((int) $params['offset']);
     }
     }
 
 
@@ -25,13 +27,15 @@ $get_feed = function ($request, $response, $args) {
     $query->bindValue(':skip_count', $page_offset, PDO::PARAM_INT);
     $query->bindValue(':skip_count', $page_offset, PDO::PARAM_INT);
     $query->execute();
     $query->execute();
 
 
-    $error = $this->utils->error_reponse_if_query_bad(false, $response, $query);
-    if($error) return $response;
+    $error = $this->utils->errorResponseIfQueryBad(false, $response, $query);
+    if ($error) {
+        return $response;
+    }
 
 
     $events = $query->fetchAll();
     $events = $query->fetchAll();
 
 
     $context = $this;
     $context = $this;
-    $events = array_map(function($event) use($context) {
+    $events = array_map(function ($event) use ($context) {
         $event['status'] = $context->constants['edit_status'][(int) $event['status']];
         $event['status'] = $context->constants['edit_status'][(int) $event['status']];
         return $event;
         return $event;
     }, $events);
     }, $events);
@@ -43,6 +47,6 @@ $get_feed = function ($request, $response, $args) {
 
 
 // Binding to multiple routes
 // Binding to multiple routes
 $app->post('/user/feed', $get_feed);
 $app->post('/user/feed', $get_feed);
-if(FRONTEND) {
+if (FRONTEND) {
     $app->get('/user/feed', $get_feed);
     $app->get('/user/feed', $get_feed);
 }
 }