From 88dc344cdbd115360f7b7e5574746d056d5034cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 9 Mar 2004 15:02:28 +0000 Subject: [PATCH] Use charset aware substr and strlen functions (bugs #854755 and #910575). --- ChangeLog | 5 ++ browse_foreigners.php | 2 +- libraries/common.lib.php | 2 - libraries/display_tbl.lib.php | 8 +-- libraries/relation.lib.php | 2 +- libraries/sqlparser.lib.php | 20 +++---- libraries/string.lib.php | 53 +++++++++++++++++-- .../text_plain__substr.inc.php | 4 +- 8 files changed, 71 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index d1a3b3525..17cb27040 100755 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,11 @@ $Source$ conversion. * libraries/dbi/mysql.dbi.lib.php, libraries/dbi/mysqli.dbi.lib.php: Fix PMA_mysql_fetch_array return value for last result (bug #905223). + * browse_foreigners.php, libraries/common.lib.php, + libraries/display_tbl.lib.php, libraries/relation.lib.php, + libraries/sqlparser.lib.php, libraries/string.lib.php, + libraries/transformations/text_plain__substr.inc.php: Use charset aware + substr and strlen functions (bugs #854755 and #910575). 2004-03-05 Marc Delisle * libraries/sqlparser.lib.php: bug 909752, floating point digit diff --git a/browse_foreigners.php b/browse_foreigners.php index 7d032a37b..a69dd2f02 100644 --- a/browse_foreigners.php +++ b/browse_foreigners.php @@ -186,7 +186,7 @@ if (isset($disp_row) && is_array($disp_row)) { $vtitle = ''; } else { $vtitle = htmlspecialchars($val); - $value = htmlspecialchars(substr($val, 0, $cfg['LimitChars']) . '...'); + $value = htmlspecialchars(PMA_substr($val, 0, $cfg['LimitChars']) . '...'); } $key_equals_data = isset($data) && $key == $data; diff --git a/libraries/common.lib.php b/libraries/common.lib.php index 73f036440..ab53942e5 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -251,9 +251,7 @@ if ($is_minimum_common == FALSE) { * Charset conversion. */ require_once('./libraries/charset_conversion.lib.php'); -} -if ($is_minimum_common == FALSE) { /** * String handling */ diff --git a/libraries/display_tbl.lib.php b/libraries/display_tbl.lib.php index 0b1d2e81e..911289013 100644 --- a/libraries/display_tbl.lib.php +++ b/libraries/display_tbl.lib.php @@ -1362,8 +1362,8 @@ function PMA_displayTableBody(&$dt_result, &$is_display, $map, $analyzed_sql) $vertical_display['data'][$row_no][$i] = ' NULL' . "\n"; } else if ($row[$pointer] != '') { // garvin: if a transform function for blob is set, none of these replacements will be made - if (strlen($row[$pointer]) > $GLOBALS['cfg']['LimitChars'] && ($dontlimitchars != 1)) { - $row[$pointer] = substr($row[$pointer], 0, $GLOBALS['cfg']['LimitChars']) . '...'; + if (PMA_strlen($row[$pointer]) > $GLOBALS['cfg']['LimitChars'] && ($dontlimitchars != 1)) { + $row[$pointer] = PMA_substr($row[$pointer], 0, $GLOBALS['cfg']['LimitChars']) . '...'; } // loic1: displays all space characters, 4 space // characters for tabulations and / @@ -1384,8 +1384,8 @@ function PMA_displayTableBody(&$dt_result, &$is_display, $map, $analyzed_sql) $relation_id = $row[$pointer]; // nijel: Cut all fields to $cfg['LimitChars'] - if (strlen($row[$pointer]) > $GLOBALS['cfg']['LimitChars'] && ($dontlimitchars != 1)) { - $row[$pointer] = substr($row[$pointer], 0, $GLOBALS['cfg']['LimitChars']) . '...'; + if (PMA_strlen($row[$pointer]) > $GLOBALS['cfg']['LimitChars'] && ($dontlimitchars != 1)) { + $row[$pointer] = PMA_substr($row[$pointer], 0, $GLOBALS['cfg']['LimitChars']) . '...'; } // loic1: displays special characters from binaries diff --git a/libraries/relation.lib.php b/libraries/relation.lib.php index de9162726..e7cd0f432 100644 --- a/libraries/relation.lib.php +++ b/libraries/relation.lib.php @@ -604,7 +604,7 @@ function PMA_foreignDropdown($disp, $foreign_field, $foreign_display, $data, $ma foreach($disp AS $disp_key => $relrow) { $key = $relrow[$foreign_field]; - if (strlen($relrow[$foreign_display]) <= $cfg['LimitChars']) { + if (PMA_strlen($relrow[$foreign_display]) <= $cfg['LimitChars']) { $value = (($foreign_display != FALSE) ? htmlspecialchars($relrow[$foreign_display]) : ''); $vtitle = ''; } else { diff --git a/libraries/sqlparser.lib.php b/libraries/sqlparser.lib.php index b47f10ac6..a120b96bb 100644 --- a/libraries/sqlparser.lib.php +++ b/libraries/sqlparser.lib.php @@ -211,7 +211,7 @@ if ($is_minimum_common == FALSE) { $sql = str_replace("\r\n", "\n", $sql); $sql = str_replace("\r", "\n", $sql); - $len = $GLOBALS['PMA_strlen']($sql); + $len = PMA_strlen($sql); if ($len == 0) { return array(); } @@ -288,7 +288,7 @@ if ($is_minimum_common == FALSE) { break; } // end switch $count2 = ($pos < $count2) ? $len : $pos; - $str = $GLOBALS['PMA_substr']($sql, $count1, $count2 - $count1); + $str = PMA_substr($sql, $count1, $count2 - $count1); PMA_SQP_arrayAdd($sql_array, 'comment_' . $type, $str, $arraysize); continue; } // end if @@ -348,7 +348,7 @@ if ($is_minimum_common == FALSE) { default: break; } // end switch - $data = $GLOBALS['PMA_substr']($sql, $count1, $count2 - $count1); + $data = PMA_substr($sql, $count1, $count2 - $count1); PMA_SQP_arrayAdd($sql_array, $type, $data, $arraysize); continue; } @@ -387,7 +387,7 @@ if ($is_minimum_common == FALSE) { if ($l == 1) { $punct_data = $c; } else { - $punct_data = $GLOBALS['PMA_substr']($sql, $count1, $l); + $punct_data = PMA_substr($sql, $count1, $l); } // Special case, sometimes, althought two characters are @@ -424,10 +424,10 @@ if ($is_minimum_common == FALSE) { $punct_data = $first; } else if (($last2 == '/*') || ($last2 == '--')) { $count2 -= 2; - $punct_data = $GLOBALS['PMA_substr']($sql, $count1, $count2 - $count1); + $punct_data = PMA_substr($sql, $count1, $count2 - $count1); } else if (($last == '-') || ($last == '+') || ($last == '!')) { $count2--; - $punct_data = $GLOBALS['PMA_substr']($sql, $count1, $count2 - $count1); + $punct_data = PMA_substr($sql, $count1, $count2 - $count1); // TODO: for negation operator, split in 2 tokens ? // "select x&~1 from t" // becomes "select x & ~ 1 from t" ? @@ -475,7 +475,7 @@ if ($is_minimum_common == FALSE) { continue; } else { $debugstr = $GLOBALS['strSQPBugInvalidIdentifer'] . ' @ ' . ($count1+1) . "\n" - . 'STR: ' . $GLOBALS['PMA_substr']($sql, $count1, $count2 - $count1); + . 'STR: ' . PMA_substr($sql, $count1, $count2 - $count1); PMA_SQP_throwError($debugstr, $sql); return $sql; } @@ -503,7 +503,7 @@ if ($is_minimum_common == FALSE) { } // end while $l = $count2 - $count1; - $str = $GLOBALS['PMA_substr']($sql, $count1, $l); + $str = PMA_substr($sql, $count1, $l); $type = ''; if ($is_digit) { @@ -532,7 +532,7 @@ if ($is_minimum_common == FALSE) { $count2++; $debugstr = 'C1 C2 LEN: ' . $count1 . ' ' . $count2 . ' ' . $len . "\n" - . 'STR: ' . $GLOBALS['PMA_substr']($sql, $count1, $count2 - $count1) . "\n"; + . 'STR: ' . PMA_substr($sql, $count1, $count2 - $count1) . "\n"; PMA_SQP_bug($debugstr, $sql); return $sql; @@ -1567,7 +1567,7 @@ if ($is_minimum_common == FALSE) { $i = $GLOBALS['PMA_strpos']($arr['type'], '_'); $class = ''; if ($i > 0) { - $class = 'syntax_' . $GLOBALS['PMA_substr']($arr['type'], 0, $i) . ' '; + $class = 'syntax_' . PMA_substr($arr['type'], 0, $i) . ' '; } $class .= 'syntax_' . $arr['type']; diff --git a/libraries/string.lib.php b/libraries/string.lib.php index f0a4e7ce1..d51b1c890 100644 --- a/libraries/string.lib.php +++ b/libraries/string.lib.php @@ -15,18 +15,61 @@ * The SQL Parser code relies heavily on these functions. */ - // This is for handling input better if (defined('PMA_MULTIBYTE_ENCODING')) { - $GLOBALS['PMA_strlen'] = 'mb_strlen'; $GLOBALS['PMA_strpos'] = 'mb_strpos'; $GLOBALS['PMA_strrpos'] = 'mb_strrpos'; - $GLOBALS['PMA_substr'] = 'mb_substr'; } else { - $GLOBALS['PMA_strlen'] = 'strlen'; $GLOBALS['PMA_strpos'] = 'strpos'; $GLOBALS['PMA_strrpos'] = 'strrpos'; - $GLOBALS['PMA_substr'] = 'substr'; +} + +$GLOBALS['PMA_has_mbstr'] = @function_exists('mb_strlen'); + +/** + * Returns length of string depending on current charset. + * + * @param string string to count + * + * @return int string length + * + * @access public + * + * @author nijel + */ +function PMA_strlen($string) +{ + if (defined('PMA_MULTIBYTE_ENCODING')) { + return mb_strlen($string); + } elseif($GLOBALS['PMA_has_mbstr']) { + return mb_strlen($string, $GLOBALS['charset']); + } else { + return strlen($string); + } +} + +/** + * Returns substring from string, works depending on current charset. + * + * @param string string to count + * @param int start of substring + * @param int length of substring + * + * @return int string length + * + * @access public + * + * @author nijel + */ +function PMA_substr($string, $start, $length = 2147483647) +{ + if (defined('PMA_MULTIBYTE_ENCODING')) { + return mb_substr($string, $start, $length); + } elseif($GLOBALS['PMA_has_mbstr']) { + return mb_substr($string, $start, $length, $GLOBALS['charset']); + } else { + return substr($string, $start, $length); + } } diff --git a/libraries/transformations/text_plain__substr.inc.php b/libraries/transformations/text_plain__substr.inc.php index 8d2946292..177c14873 100644 --- a/libraries/transformations/text_plain__substr.inc.php +++ b/libraries/transformations/text_plain__substr.inc.php @@ -21,9 +21,9 @@ function PMA_transformation_text_plain__substr($buffer, $options = array(), $met $newtext = ''; if ($options[1] != 'all') { - $newtext = substr($buffer, $options[0], $options[1]); + $newtext = PMA_substr($buffer, $options[0], $options[1]); } else { - $newtext = substr($buffer, $options[0]); + $newtext = PMA_substr($buffer, $options[0]); } $length = strlen($newtext);