From a294026e3799eefa195b89f5833e44ddeb7eb503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 3 Aug 2016 10:54:40 +0200 Subject: [PATCH] Simplified getting error from preg_match MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's enough to use error_get_last to obtain the message without need to play with PHP configuration and error handling. The only tricky thing is to know that there is new error. With PHP 7, there is error_clear_last() which ensures this, on older PHP we need to trigger *another* error to be able to detect that new error comes. Signed-off-by: Michal Čihař --- libraries/config/Validator.php | 63 ++++++++-------------------------- 1 file changed, 14 insertions(+), 49 deletions(-) diff --git a/libraries/config/Validator.php b/libraries/config/Validator.php index b7973ce9a7..eb8e6aa2b5 100644 --- a/libraries/config/Validator.php +++ b/libraries/config/Validator.php @@ -159,50 +159,6 @@ class Validator return empty($new_result) ? true : $new_result; } - /** - * Empty error handler, used to temporarily restore PHP internal error handler - * - * @return bool - */ - public static function nullErrorHandler() - { - return false; - } - - /** - * Ensures that $php_errormsg variable will be registered in case of an error - * and enables output buffering (when $start = true). - * Called with $start = false disables output buffering end restores - * html_errors and track_errors. - * - * @param boolean $start Whether to start buffering - * - * @return void - */ - public static function testPHPErrorMsg($start = true) - { - static $old_html_errors, $old_track_errors, $old_error_reporting; - static $old_display_errors; - if ($start) { - $old_html_errors = ini_get('html_errors'); - $old_track_errors = ini_get('track_errors'); - $old_display_errors = ini_get('display_errors'); - $old_error_reporting = error_reporting(E_ALL); - ini_set('html_errors', 'false'); - ini_set('track_errors', 'true'); - ini_set('display_errors', 'true'); - set_error_handler(array('PMA\libraries\config\Validator', "nullErrorHandler")); - ob_start(); - } else { - ob_end_clean(); - restore_error_handler(); - error_reporting($old_error_reporting); - ini_set('html_errors', $old_html_errors); - ini_set('track_errors', $old_track_errors); - ini_set('display_errors', $old_display_errors); - } - } - /** * Test database connection * @@ -404,17 +360,26 @@ class Validator return $result; } - static::testPHPErrorMsg(); + if (function_exists('error_clear_last')) { + /* PHP 7 only code */ + error_clear_last(); + $last_error = null; + } else { + // As fallback we trigger another error to ensure + // that preg error will be different + @strpos(); + $last_error = error_get_last(); + } $matches = array(); // in libraries/ListDatabase.php _checkHideDatabase(), // a '/' is used as the delimiter for hide_db - preg_match('/' . Util::requestString($values[$path]) . '/', '', $matches); + @preg_match('/' . Util::requestString($values[$path]) . '/', '', $matches); - static::testPHPErrorMsg(false); + $current_error = error_get_last(); - if (isset($php_errormsg)) { - $error = preg_replace('/^preg_match\(\): /', '', $php_errormsg); + if ($current_error !== $last_error) { + $error = preg_replace('/^preg_match\(\): /', '', $current_error['message']); return array($path => $error); }