From 4d7765c6c2f99e268721837df28d887d34e7d267 Mon Sep 17 00:00:00 2001 From: Sebastian Mendel Date: Sun, 6 Nov 2005 12:14:43 +0000 Subject: [PATCH] - added PMA_DBI_convert_message() - bug #1344654 Not converted mysql error message to chosen user language --- ChangeLog | 6 +++ libraries/database_interface.lib.php | 72 ++++++++++++++++++++++++++ libraries/dbi/mysql.dbi.lib.php | 71 +++++++++++++++---------- libraries/dbi/mysqli.dbi.lib.php | 77 +++++++++++++++++----------- 4 files changed, 170 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0c68db97a..472afc8fa 100755 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,12 @@ phpMyAdmin - Changelog $Id$ $Source$ +2005-11-06 Sebastian Mendel + * libraries/database_interface.lib.php, + libraries/dbi/*.dbi.lib.php: + - added PMA_DBI_convert_message() + - bug #1344654 Not converted mysql error message to chosen user language + 2005-11-05 Sebastian Mendel * themes/*/css/theme_right.css.php: dropped deprecated style classes diff --git a/libraries/database_interface.lib.php b/libraries/database_interface.lib.php index b1b4c0827..a6d0d0214 100644 --- a/libraries/database_interface.lib.php +++ b/libraries/database_interface.lib.php @@ -26,6 +26,78 @@ function PMA_DBI_query($query, $link = NULL, $options = 0) { return $res; } +/** + * converts charset in message, usally from mysql_error(), + * into PMA charset, usally UTF-8 + * + * @uses $GLOBALS['cfg']['IconvExtraParams'] + * @uses PMA_DBI_fetch_value() + * @uses preg_match() + * @uses in_array() + * @uses function_exists() + * @uses iconv() + * @uses libiconv() + * @uses recode_string() + * @uses mb_convert_encoding() + * @param string $message + * @return string $message + */ +function PMA_DBI_convert_message( $message ) { + // latin always last! + $encodings = array( + 'japanese' => 'EUC-JP', //'ujis', + 'japanese-sjis' => 'Shift-JIS', //'sjis', + 'korean' => 'EUC-KR', //'euckr', + 'russian' => 'KOI8-R', //'koi8r', + 'ukrainian' => 'KOI8-U', //'koi8u', + 'greek' => 'ISO-8859-7', //'greek', + 'serbian' => 'CP1250', //'cp1250', + 'estonian' => 'ISO-8859-13', //'latin7', + 'slovak' => 'ISO-8859-2', //'latin2', + 'czech' => 'ISO-8859-2', //'latin2', + 'hungarian' => 'ISO-8859-2', //'latin2', + 'polish' => 'ISO-8859-2', //'latin2', + 'romanian' => 'ISO-8859-2', //'latin2', + 'spanish' => 'CP1252', //'latin1', + 'swedish' => 'CP1252', //'latin1', + 'italian' => 'CP1252', //'latin1', + 'norwegian-ny' => 'CP1252', //'latin1', + 'norwegian' => 'CP1252', //'latin1', + 'portuguese' => 'CP1252', //'latin1', + 'danish' => 'CP1252', //'latin1', + 'dutch' => 'CP1252', //'latin1', + 'english' => 'CP1252', //'latin1', + 'french' => 'CP1252', //'latin1', + 'german' => 'CP1252', //'latin1', + ); + + if ( $lang = PMA_DBI_fetch_value( 'SHOW VARIABLES LIKE \'language\';', 0, 1 ) ) { + if ( preg_match( '&(?:\\\|\\/)([^\\\\\/]*)(?:\\\|\\/)$&i', $lang, $found = array() ) ) { + $lang = $found[1]; + } + } + + if ( ! empty( $lang ) && isset( $encodings[$lang] ) ) { + if ( function_exists( 'iconv' ) ) { + $message = iconv($encodings[$lang], 'UTF-8' . $GLOBALS['cfg']['IconvExtraParams'], $message); + } elseif ( function_exists( 'recode_string' ) ) { + $message = recode_string($encodings[$lang] . '..' . 'UTF-8', $message); + } elseif ( function_exists( 'libiconv' ) ) { + $message = libiconv($encodings[$lang], 'UTF-8', $message); + } elseif ( function_exists( 'mb_convert_encoding' ) ) { + // do not try unsupported charsets + if ( ! in_array( $lang, array( 'ukrainian', 'greek', 'serbian' ) ) ) { + $message = mb_convert_encoding( $message, 'UTF-8', $encodings[$lang] ); + } + } + } else { + // lang not found, try all + // what TODO ? + } + + return $message; +} + /** * returns array with database names * diff --git a/libraries/dbi/mysql.dbi.lib.php b/libraries/dbi/mysql.dbi.lib.php index 015af73c5..6ab623001 100644 --- a/libraries/dbi/mysql.dbi.lib.php +++ b/libraries/dbi/mysql.dbi.lib.php @@ -167,43 +167,60 @@ function PMA_DBI_free_result($result) { } } -function PMA_DBI_getError($link = NULL) { - unset($GLOBALS['errno']); - if (empty($link)) { - if (isset($GLOBALS['userlink'])) { - $link = $GLOBALS['userlink']; +/** + * returns last error message or false if no errors occured + * + * @uses PMA_MYSQL_INT_VERSION + * @uses PMA_convert_display_charset() + * @uses PMA_DBI_convert_message() + * @uses $GLOBALS['errno'] + * @uses $GLOBALS['userlink'] + * @uses $GLOBALS['strServerNotResponding'] + * @uses $GLOBALS['strSocketProblem'] + * @uses mysql_errno() + * @uses mysql_error() + * @uses defined() + * @param resource $link mysql link + * @return string|boolean $error or false + */ +function PMA_DBI_getError( $link = NULL ) { + unset( $GLOBALS['errno'] ); + if ( NULL === $link && isset( $GLOBALS['userlink'] ) ) { + $link =& $GLOBALS['userlink']; // Do not stop now. On the initial connection, we don't have a $link, // we don't have a $GLOBALS['userlink'], but we can catch the error code -// } else { +// } else { // return FALSE; - } } - - if (mysql_errno()) { - $error = mysql_errno(); + + if ( NULL !== $link ) { + $error_number = mysql_errno( $link ); + $error_message = mysql_error( $link ); + } else { + $error_number = mysql_errno(); $error_message = mysql_error(); - } elseif ($link) { - $error = mysql_errno($link); - $error_message = mysql_error($link); + } + if ( 0 == $error_number ) { + return false; } // keep the error number for further check after the call to PMA_DBI_getError() - if (isset($error) && $error) { - $GLOBALS['errno'] = $error; - } else { - return FALSE; - } + $GLOBALS['errno'] = $error_number; -// Some errors messages cannot be obtained by mysql_error() - if ($error && $error == 2002) { - $error = '#' . ((string) $error) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem']; - } elseif ($error && $error == 2003) { - $error = '#' . ((string) $error) . ' - ' . $GLOBALS['strServerNotResponding']; - } elseif ($error && defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) { - $error = '#' . ((string) $error) . ' - ' . $error_message; - } elseif ($error) { - $error = '#' . ((string) $error) . ' - ' . PMA_convert_display_charset($error_message); + if ( ! empty( $error_message ) ) { + $error_message = PMA_DBI_convert_message( $error_message ); + } + + // Some errors messages cannot be obtained by mysql_error() + if ( $error_number == 2002 ) { + $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem']; + } elseif ( $error_number == 2003 ) { + $error = '#' . ((string) $error_number ) . ' - ' . $GLOBALS['strServerNotResponding']; + } elseif ( defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100 ) { + $error = '#' . ((string) $error_number ) . ' - ' . $error_message; + } else { + $error = '#' . ((string) $error_number ) . ' - ' . PMA_convert_display_charset( $error_message ); } return $error; } diff --git a/libraries/dbi/mysqli.dbi.lib.php b/libraries/dbi/mysqli.dbi.lib.php index bdf00c32d..a1ec0b722 100644 --- a/libraries/dbi/mysqli.dbi.lib.php +++ b/libraries/dbi/mysqli.dbi.lib.php @@ -202,40 +202,59 @@ function PMA_DBI_free_result($result) { } } -function PMA_DBI_getError($link = NULL) { - unset($GLOBALS['errno']); - if (empty($link)) { - if (isset($GLOBALS['userlink'])) { - $link = $GLOBALS['userlink']; - // Do not stop now. We still can get the error code - // with mysqli_connect_errno() -// } else { -// return FALSE; - } +/** + * returns last error message or false if no errors occured + * + * @uses PMA_MYSQL_INT_VERSION + * @uses PMA_convert_display_charset() + * @uses PMA_DBI_convert_message() + * @uses $GLOBALS['errno'] + * @uses $GLOBALS['userlink'] + * @uses $GLOBALS['strServerNotResponding'] + * @uses $GLOBALS['strSocketProblem'] + * @uses mysqli_errno() + * @uses mysqli_error() + * @uses mysqli_connect_errno() + * @uses mysqli_connect_error() + * @uses defined() + * @param resource $link mysql link + * @return string|boolean $error or false + */ +function PMA_DBI_getError( $link = NULL ) { + unset( $GLOBALS['errno'] ); + + if ( NULL === $link && isset( $GLOBALS['userlink'] ) ) { + $link =& $GLOBALS['userlink']; + // Do not stop now. We still can get the error code + // with mysqli_connect_errno() +// } else { +// return false; } - if (mysqli_connect_errno()) { - $error = mysqli_connect_errno(); - $error_message = mysqli_connect_error(); - } elseif ( !empty($link) && mysqli_errno($link)) { - $error = mysqli_errno($link); - $error_message = mysqli_error($link); - } - - // keep the error number for further check after the call to PMA_DBI_getError() - if (!empty($error)) { - $GLOBALS['errno'] = $error; + if ( NULL !== $link ) { + $error_number = mysqli_errno( $link ); + $error_message = mysqli_error( $link ); } else { - return FALSE; + $error_number = mysqli_connect_errno(); + $error_message = mysqli_connect_error(); } + if ( 0 == $error_number ) { + return false; + } + + // keep the error number for further check after the call to PMA_DBI_getError() + $GLOBALS['errno'] = $error_number; - - if ($error && $error == 2002) { - $error = '#' . ((string) $error) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem']; - } elseif ($error && defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) { - $error = '#' . ((string) $error) . ' - ' . $error_message; - } elseif ($error) { - $error = '#' . ((string) $error) . ' - ' . PMA_convert_display_charset($error_message); + if ( ! empty( $error_message ) ) { + $error_message = PMA_DBI_convert_message( $error_message ); + } + + if ( $error_number == 2002 ) { + $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem']; + } elseif ( defined( 'PMA_MYSQL_INT_VERSION' ) && PMA_MYSQL_INT_VERSION >= 40100 ) { + $error = '#' . ((string) $error_number) . ' - ' . $error_message; + } else { + $error = '#' . ((string) $error_number) . ' - ' . PMA_convert_display_charset($error_message); } return $error; }