Simplified getting error from preg_match

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ř <michal@cihar.com>
This commit is contained in:
Michal Čihař 2016-08-03 10:54:40 +02:00
parent 91ff2a97e2
commit a294026e37

View File

@ -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);
}