From a687b114ccb48b1ccfca75fda2125bd23bb2803f Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 29 Jan 2025 18:27:20 +0000 Subject: [PATCH] Create addNotice and addUserError Signed-off-by: Kamil Tekiela --- src/Core.php | 3 +-- src/Dbal/DatabaseInterface.php | 15 +++----------- src/Error/ErrorHandler.php | 24 +++++++++++++++++++++++ src/Plugins/Auth/AuthenticationConfig.php | 10 ++-------- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/Core.php b/src/Core.php index e1666e55df..4c4f0c01e6 100644 --- a/src/Core.php +++ b/src/Core.php @@ -45,7 +45,6 @@ use function unserialize; use function urldecode; use const DATE_RFC1123; -use const E_USER_WARNING; use const FILTER_VALIDATE_IP; /** @@ -115,7 +114,7 @@ class Core throw new MissingExtensionException(Sanitize::convertBBCode($message)); } - ErrorHandler::getInstance()->addError($message, E_USER_WARNING, '', 0, false); + ErrorHandler::getInstance()->addUserError($message, false); } /** diff --git a/src/Dbal/DatabaseInterface.php b/src/Dbal/DatabaseInterface.php index 747d70c32d..79505f98e1 100644 --- a/src/Dbal/DatabaseInterface.php +++ b/src/Dbal/DatabaseInterface.php @@ -64,7 +64,6 @@ use function uasort; use function uksort; use function usort; -use const E_USER_WARNING; use const LOG_INFO; use const LOG_NDELAY; use const LOG_PID; @@ -1046,7 +1045,7 @@ class DatabaseInterface if (! $this->tryQuery($sqlQueryTz)) { $errorHandler = ErrorHandler::getInstance(); - $errorHandler->addError( + $errorHandler->addUserError( sprintf( __( 'Unable to use timezone "%1$s" for server %2$d. ' @@ -1059,9 +1058,6 @@ class DatabaseInterface Current::$server, Current::$server, ), - E_USER_WARNING, - __FILE__, - __LINE__, ); } } @@ -1093,12 +1089,7 @@ class DatabaseInterface if ($result === false) { $errorHandler = ErrorHandler::getInstance(); - $errorHandler->addError( - __('Failed to set configured collation connection!'), - E_USER_WARNING, - __FILE__, - __LINE__, - ); + $errorHandler->addUserError(__('Failed to set configured collation connection!')); return; } @@ -1609,7 +1600,7 @@ class DatabaseInterface try { $result = $this->extension->connect($server); } catch (ConnectionException $exception) { - $errorHandler->addError($exception->getMessage(), E_USER_WARNING, __FILE__, __LINE__); + $errorHandler->addUserError($exception->getMessage()); return null; } diff --git a/src/Error/ErrorHandler.php b/src/Error/ErrorHandler.php index 82bcb31329..ff8666a18e 100644 --- a/src/Error/ErrorHandler.php +++ b/src/Error/ErrorHandler.php @@ -314,6 +314,30 @@ class ErrorHandler } } + public function addUserError(string $message, bool $escape = true): void + { + // The file name and line number are not relevant for user errors + $error = new Error( + E_USER_WARNING, + $escape ? htmlspecialchars($message) : $message, + __FILE__, + __LINE__, + ); + $this->errors[$error->getHash()] = $error; + } + + public function addNotice(string $message, bool $escape = true): void + { + // The file name and line number are not relevant for user errors + $error = new Error( + E_USER_NOTICE, + $escape ? htmlspecialchars($message) : $message, + __FILE__, + __LINE__, + ); + $this->errors[$error->getHash()] = $error; + } + /** * display fatal error and exit * diff --git a/src/Plugins/Auth/AuthenticationConfig.php b/src/Plugins/Auth/AuthenticationConfig.php index b2c44f354e..7f48a6ec82 100644 --- a/src/Plugins/Auth/AuthenticationConfig.php +++ b/src/Plugins/Auth/AuthenticationConfig.php @@ -23,9 +23,6 @@ use function ob_get_clean; use function ob_start; use function sprintf; -use const E_USER_NOTICE; -use const E_USER_WARNING; - /** * Handles the config authentication method */ @@ -92,7 +89,7 @@ class AuthenticationConfig extends AuthenticationPlugin '; $config = Config::getInstance(); if ($failure->failureType === AuthenticationFailure::ALLOW_DENIED) { - $errorHandler->addError($failure->getMessage(), E_USER_NOTICE, __FILE__, __LINE__); + $errorHandler->addNotice($failure->getMessage()); } else { // Check whether user has configured something if ($config->sourceMtime == 0) { @@ -113,7 +110,7 @@ class AuthenticationConfig extends AuthenticationPlugin // rejected the connection, which is not really what happened) // 2002 is the error given by mysqli // 2003 is the error given by mysql - $errorHandler->addError( + $errorHandler->addUserError( __( 'phpMyAdmin tried to connect to the MySQL server, and the' . ' server rejected the connection. You should check the' @@ -121,9 +118,6 @@ class AuthenticationConfig extends AuthenticationPlugin . ' make sure that they correspond to the information given' . ' by the administrator of the MySQL server.', ), - E_USER_WARNING, - __FILE__, - __LINE__, ); } }