diff --git a/core/classes/Endpoints/Endpoints.php b/core/classes/Endpoints/Endpoints.php index e2bc1f09f7..b3615ccf3b 100644 --- a/core/classes/Endpoints/Endpoints.php +++ b/core/classes/Endpoints/Endpoints.php @@ -1,4 +1,5 @@ throwError(Nameless2API::ERROR_INVALID_API_METHOD, "The $route endpoint only accepts " . implode(', ', $available_methods) . ", $method was used.", 405); + $api->throwError(Nameless2API::ERROR_INVALID_API_METHOD, "The $route endpoint only accepts " . implode(', ', $available_methods) . ", $method was used.", Response::HTTP_METHOD_NOT_ALLOWED); } - $api->throwError(Nameless2API::ERROR_INVALID_API_METHOD, 'If you are seeing this while in a browser, this means your API is functioning!', 404); + $api->throwError(Nameless2API::ERROR_INVALID_API_METHOD, 'If you are seeing this while in a browser, this means your API is functioning!', Response::HTTP_NOT_FOUND); } /** diff --git a/core/classes/Endpoints/KeyAuthEndpoint.php b/core/classes/Endpoints/KeyAuthEndpoint.php index 8ea22bcc9a..436b73bbfa 100644 --- a/core/classes/Endpoints/KeyAuthEndpoint.php +++ b/core/classes/Endpoints/KeyAuthEndpoint.php @@ -1,4 +1,5 @@ throwError(Nameless2API::ERROR_MISSING_API_KEY, 'Missing authorization header'); + $api->throwError(Nameless2API::ERROR_MISSING_API_KEY, 'Missing authorization header', Response::HTTP_UNAUTHORIZED); } $api_key = $api_key_header; } - return $this->validateKey($api, $api_key); + return $this->validateKey($api_key); } /** * Validate provided API key to make sure it matches. * - * @param Nameless2API $api Instance of API to use for database connection. * @param string $api_key API key to check. * @return bool Whether it matches or not. */ - private function validateKey(Nameless2API $api, string $api_key): bool { + private function validateKey(string $api_key): bool { $correct_key = Settings::get('mc_api_key'); if ($correct_key === null) { die('API key is null'); diff --git a/core/includes/image_upload.php b/core/includes/image_upload.php index 5351d29295..2fa6cb10f8 100644 --- a/core/includes/image_upload.php +++ b/core/includes/image_upload.php @@ -119,7 +119,7 @@ Redirect::to(URL::build('/profile/' . urlencode($user->data()->username))); } - http_response_code(500); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); $error = $image->getError() ?: 'Unknown error, check logs for more details'; ErrorHandler::logWarning('Image upload error: ' . $error); die($error); @@ -155,7 +155,7 @@ die('OK'); } catch (Exception $e) { - http_response_code(500); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); $error = $e->getMessage() ?: 'Unknown error, check logs for more details'; ErrorHandler::logWarning('Image upload exception: ' . $error); die($error); diff --git a/modules/Core/classes/Misc/Nameless2API.php b/modules/Core/classes/Misc/Nameless2API.php index f3dfb3b1fb..a83fc79ad2 100644 --- a/modules/Core/classes/Misc/Nameless2API.php +++ b/modules/Core/classes/Misc/Nameless2API.php @@ -1,4 +1,5 @@ throwError(self::ERROR_UNKNOWN_ERROR, $e->getMessage()); + $this->throwError(self::ERROR_UNKNOWN_ERROR, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } } @@ -67,7 +67,7 @@ public function __construct(string $route, Language $api_language, Endpoints $en * @param int $status HTTP status code * @return never */ - public function throwError(string $error, $meta = null, int $status = 400): void { + public function throwError(string $error, $meta = null, int $status = Response::HTTP_BAD_REQUEST): void { $this->returnArray( array_merge(['error' => $error], $meta ? ['meta' => $meta] : []), $status @@ -112,7 +112,7 @@ public function getLanguage(): Language { * @param int $status HTTP status code * @return never */ - public function returnArray(array $array, int $status = 200): void { + public function returnArray(array $array, int $status = Response::HTTP_OK): void { http_response_code($status); die(self::encodeJson($array)); diff --git a/modules/Core/includes/endpoints/RegisterEndpoint.php b/modules/Core/includes/endpoints/RegisterEndpoint.php index 3f5a30698d..063b22ac9b 100644 --- a/modules/Core/includes/endpoints/RegisterEndpoint.php +++ b/modules/Core/includes/endpoints/RegisterEndpoint.php @@ -1,4 +1,5 @@ $user_id]; } catch (Exception $e) { - $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_CREATE_ACCOUNT, $e->getMessage()); + $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_CREATE_ACCOUNT, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } } @@ -210,7 +211,7 @@ private function sendRegistrationEmail(Nameless2API $api, string $username, stri 'user_id' => $user_id ]); - $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_SEND_REGISTRATION_EMAIL); + $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_SEND_REGISTRATION_EMAIL, null, Response::HTTP_INTERNAL_SERVER_ERROR); } $api->returnArray(['message' => $api->getLanguage()->get('api', 'finish_registration_email')]); diff --git a/modules/Core/includes/endpoints/ServerInfoEndpoint.php b/modules/Core/includes/endpoints/ServerInfoEndpoint.php index 7785aeecf2..779e56c206 100644 --- a/modules/Core/includes/endpoints/ServerInfoEndpoint.php +++ b/modules/Core/includes/endpoints/ServerInfoEndpoint.php @@ -1,4 +1,5 @@ throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_SERVER_INFO, $e->getMessage(), 500); + $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_SERVER_INFO, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } $cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']); @@ -83,7 +84,7 @@ public function execute(Nameless2API $api): void { } } } catch (Exception $e) { - $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_SERVER_INFO, $e->getMessage(), 500); + $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_SERVER_INFO, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } } @@ -105,7 +106,7 @@ public function execute(Nameless2API $api): void { ], intval($_POST['interval_seconds'] ?? 10) * 2); } } catch (Exception $e) { - $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_SERVER_INFO, $e->getMessage(), 500); + $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_SERVER_INFO, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } $api->returnArray(array_merge(['message' => $api->getLanguage()->get('api', 'server_info_updated')])); diff --git a/modules/Core/includes/endpoints/UpdateUsernameEndpoint.php b/modules/Core/includes/endpoints/UpdateUsernameEndpoint.php index 97ddd925e7..474a9b662e 100644 --- a/modules/Core/includes/endpoints/UpdateUsernameEndpoint.php +++ b/modules/Core/includes/endpoints/UpdateUsernameEndpoint.php @@ -1,4 +1,5 @@ getDb()->update('users', $user->data()->id, $fields); } catch (Exception $e) { - $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_USERNAME, null, 500); + $api->throwError(CoreApiErrors::ERROR_UNABLE_TO_UPDATE_USERNAME, null, Response::HTTP_INTERNAL_SERVER_ERROR); } $api->returnArray(['message' => $api->getLanguage()->get('api', 'username_updated')]); diff --git a/modules/Core/queries/reactions.php b/modules/Core/queries/reactions.php index ed465bf28d..6e6fac8a9a 100644 --- a/modules/Core/queries/reactions.php +++ b/modules/Core/queries/reactions.php @@ -5,7 +5,7 @@ // Validate form input if ($_SERVER['REQUEST_METHOD'] === 'GET') { if (!isset($_GET['reactable_id']) || !is_numeric($_GET['reactable_id'])) { - http_response_code(400); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); die('Invalid input'); } $reactable_id = $_GET['reactable_id']; @@ -13,12 +13,12 @@ } else { // User must be logged in to proceed if (!$user->isLoggedIn()) { - http_response_code(401); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_UNAUTHORIZED); die('Not logged in'); } if (!isset($_POST['reactable_id'], $_POST['reaction_id']) || !is_numeric($_POST['reactable_id']) || !is_numeric($_POST['reaction_id'])) { - http_response_code(400); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); die('Invalid input'); } $reactable_id = $_POST['reactable_id']; @@ -28,14 +28,14 @@ $reaction_context = ReactionContextsManager::getInstance()->getContext($context); if (!$reaction_context->isEnabled()) { - http_response_code(400); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); die('Reactions disabled in this context'); } // Ensure exists $reactable = $reaction_context->validateReactable($reactable_id, $user); if (!$reactable) { - http_response_code(400); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); die('Invalid reactable'); } @@ -131,7 +131,7 @@ // add reaction if (!Token::check()) { - http_response_code(400); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); die('Invalid token'); } @@ -145,7 +145,7 @@ $reaction_context->name(), )); - http_response_code(200); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_OK); die('Reaction deleted'); } @@ -158,5 +158,5 @@ $reaction_context->name(), )); -http_response_code(200); +http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_OK); die('Reaction added'); diff --git a/modules/Core/queries/tinymce_image_upload.php b/modules/Core/queries/tinymce_image_upload.php index ac68eb0053..ea1e951f6e 100644 --- a/modules/Core/queries/tinymce_image_upload.php +++ b/modules/Core/queries/tinymce_image_upload.php @@ -1,12 +1,12 @@ isLoggedIn()) { - http_response_code(400); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_UNAUTHORIZED); die('Not logged in'); } if (!Token::check()) { - http_response_code(400); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); die('Invalid token'); } @@ -18,7 +18,7 @@ if ($image['file']) { if (!$image->upload()) { - http_response_code(500); + http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR); $error = $image->getError() ?: 'Unknown error, check logs for more details'; ErrorHandler::logWarning('TinyMCE image upload error: ' . $error); die($error); @@ -29,5 +29,5 @@ ])); } -http_response_code(400); +http_response_code(\Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST); die('No file uploaded'); diff --git a/modules/Discord Integration/includes/endpoints/SubmitDiscordRoleListEndpoint.php b/modules/Discord Integration/includes/endpoints/SubmitDiscordRoleListEndpoint.php index b8a5f5bd46..b960c71d9f 100644 --- a/modules/Discord Integration/includes/endpoints/SubmitDiscordRoleListEndpoint.php +++ b/modules/Discord Integration/includes/endpoints/SubmitDiscordRoleListEndpoint.php @@ -1,4 +1,5 @@ throwError(DiscordApiErrors::ERROR_UNABLE_TO_UPDATE_DISCORD_ROLES, $e->getMessage(), 500); + $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_UPDATE_DISCORD_ROLES, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } $api->returnArray(['message' => Discord::getLanguageTerm('discord_settings_updated')]); diff --git a/modules/Discord Integration/includes/endpoints/UpdateDiscordBotSettingsEndpoint.php b/modules/Discord Integration/includes/endpoints/UpdateDiscordBotSettingsEndpoint.php index 640b1cbdb0..5ad2d1e682 100644 --- a/modules/Discord Integration/includes/endpoints/UpdateDiscordBotSettingsEndpoint.php +++ b/modules/Discord Integration/includes/endpoints/UpdateDiscordBotSettingsEndpoint.php @@ -1,4 +1,5 @@ throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_BOT_URL, $e->getMessage(), 500); + $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_BOT_URL, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } } @@ -28,7 +29,7 @@ public function execute(Nameless2API $api): void { try { Settings::set('discord', $_POST['guild_id']); } catch (Exception $e) { - $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_GUILD_ID, $e->getMessage(), 500); + $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_GUILD_ID, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } } @@ -36,7 +37,7 @@ public function execute(Nameless2API $api): void { try { Settings::set('discord_bot_username', $_POST['bot_username']); } catch (Exception $e) { - $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_BOT_USERNAME, $e->getMessage(), 500); + $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_BOT_USERNAME, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } } diff --git a/modules/Discord Integration/includes/endpoints/UpdateDiscordUsernames.php b/modules/Discord Integration/includes/endpoints/UpdateDiscordUsernames.php index 502530b7a1..c3470d89e3 100644 --- a/modules/Discord Integration/includes/endpoints/UpdateDiscordUsernames.php +++ b/modules/Discord Integration/includes/endpoints/UpdateDiscordUsernames.php @@ -1,4 +1,5 @@ Discord username to update @@ -35,7 +36,7 @@ public function execute(Nameless2API $api): void { } } } catch (Exception $e) { - $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_BOT_USERNAME, $e->getMessage(), 500); + $api->throwError(DiscordApiErrors::ERROR_UNABLE_TO_SET_DISCORD_BOT_USERNAME, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); } $api->returnArray(['message' => Discord::getLanguageTerm('discord_usernames_updated'), 'updated_users' => $updated]);