From b9b38d39f922839de88c6b8042d240b5f0909558 Mon Sep 17 00:00:00 2001 From: Marc Delisle Date: Wed, 2 Apr 2008 17:19:59 +0000 Subject: [PATCH] bug #1926357 [data] BIT defaults displayed incorrectly --- ChangeLog | 1 + libraries/common.lib.php | 47 ++++++++++++++++++++++++++++++++ libraries/display_tbl.lib.php | 7 +---- libraries/tbl_properties.inc.php | 15 ++++++---- tbl_change.php | 12 ++++---- tbl_structure.php | 11 +++++++- 6 files changed, 74 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 05bf4ee46..c8d9e17b8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,6 +20,7 @@ $HeadURL: https://phpmyadmin.svn.sourceforge.net/svnroot/phpmyadmin/trunk/phpMyA thanks to Tony Marston - tonymarston - bug #1918531 [compatibility] Navigation isn't w3.org valid thanks to Michael Keck - mkkeck +- bug #1926357 [data] BIT defaults displayed incorrectly (todo: export?) 2.11.5.1 (2008-03-29) - bug #1909711 [security] Sensitive data in session files diff --git a/libraries/common.lib.php b/libraries/common.lib.php index 25c006495..dc9eb05f5 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -2310,4 +2310,51 @@ function PMA_externalBug($functionality, $component, $minimum_version, $bugref) echo PMA_showHint(sprintf($GLOBALS['strKnownExternalBug'], $functionality, 'http://bugs.mysql.com/' . $bugref)); } } + +/** + * Converts a bit value to printable format; + * in MySQL a BIT field can be from 1 to 64 bits so we need this + * function because in PHP, decbin() supports only 32 bits + * + * @uses ceil() + * @uses decbin() + * @uses ord() + * @uses substr() + * @uses sprintf() + * @param numeric $value coming from a BIT field + * @param integer $length + * @return string the printable value + */ +function PMA_printable_bit_value($value, $length) { + $printable = ''; + for ($i = 0; $i < ceil($length / 8); $i++) { + $printable .= sprintf('%08d', decbin(ord(substr($value, $i, 1)))); + } + $printable = substr($printable, -$length); + return $printable; +} + +/** + * Extracts the true field type and length from a field type spec + * + * @uses strpos() + * @uses chop() + * @uses substr() + * @param string $fieldspec + * @return array associative array containing the type and length + */ +function PMA_extract_type_length($fieldspec) { + $first_bracket_pos = strpos($fieldspec, '('); + if ($first_bracket_pos) { + $length = chop(substr($fieldspec, $first_bracket_pos + 1, (strpos($fieldspec, ')') - $first_bracket_pos - 1))); + $type = chop(substr($fieldspec, 0, $first_bracket_pos)); + } else { + $type = $fieldspec; + $length = ''; + } + return array( + 'type' => $type, + 'length' => $length + ); +} ?> diff --git a/libraries/display_tbl.lib.php b/libraries/display_tbl.lib.php index de8d46acd..0921a2823 100644 --- a/libraries/display_tbl.lib.php +++ b/libraries/display_tbl.lib.php @@ -1362,12 +1362,7 @@ function PMA_displayTableBody(&$dt_result, &$is_display, $map, $analyzed_sql) { // loic1: displays special characters from binaries $field_flags = PMA_DBI_field_flags($dt_result, $i); if (isset($meta->_type) && $meta->_type === MYSQLI_TYPE_BIT) { - $db_value = $row[$i]; - $row[$i] = ''; - for ($j = 0; $j < ceil($meta->length / 8); $j++) { - $row[$i] .= sprintf('%08d', decbin(ord(substr($db_value, $j, 1)))); - } - $row[$i] = substr($row[$i], -$meta->length); + $row[$i] = PMA_printable_bit_value($row[$i], $meta->length); } elseif (stristr($field_flags, 'BINARY')) { $row[$i] = str_replace("\x00", '\0', $row[$i]); $row[$i] = str_replace("\x08", '\b', $row[$i]); diff --git a/libraries/tbl_properties.inc.php b/libraries/tbl_properties.inc.php index 14e4d0562..58ce8c425 100644 --- a/libraries/tbl_properties.inc.php +++ b/libraries/tbl_properties.inc.php @@ -234,6 +234,11 @@ for ($i = 0 ; $i <= $num_fields; $i++) { $row = $fields_meta[$i]; } + $type_and_length = PMA_extract_type_length($row['Type']); + if ($type_and_length['type'] == 'bit') { + $row['Default'] = PMA_printable_bit_value($row['Default'], $type_and_length['length']); + } + // Cell index: If certain fields get left out, the counter shouldn't chage. $ci = 0; // Everytime a cell shall be left out the STRG-jumping feature, $ci_offset @@ -272,12 +277,10 @@ for ($i = 0 ; $i <= $num_fields; $i++) { $type = preg_replace('@ZEROFILL@i', '', $type); $type = preg_replace('@UNSIGNED@i', '', $type); - if (strpos($type, '(')) { - $length = chop(substr($type, (strpos($type, '(') + 1), (strpos($type, ')') - strpos($type, '(') - 1))); - $type = chop(substr($type, 0, strpos($type, '('))); - } else { - $length = ''; - } + $type_and_length = PMA_extract_type_length($type); + $type = $type_and_length['type']; + $length = $type_and_length['length']; + unset($type_and_length); } // end if else // some types, for example longtext, are reported as diff --git a/tbl_change.php b/tbl_change.php index 4608ca55b..960a13d39 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -413,11 +413,7 @@ foreach ($loop_array as $vrowcount => $vrow) { $special_chars = ''; $data = $vrow[$field]; } elseif ($row_table_def['True_Type'] == 'bit') { - $special_chars = ''; - for ($j = 0; $j < ceil($len / 8); $j++) { - $special_chars .= sprintf('%08d', decbin(ord(substr($vrow[$field], $j, 1)))); - } - $special_chars = substr($special_chars, -$len); + $special_chars = PMA_printable_bit_value($vrow[$field], $len); } else { // loic1: special binary "characters" if ($is_binary || $is_blob) { @@ -446,7 +442,11 @@ foreach ($loop_array as $vrowcount => $vrow) { } else { $data = $row_table_def['Default']; } - $special_chars = htmlspecialchars($row_table_def['Default']); + if ($row_table_def['True_Type'] == 'bit') { + $special_chars = PMA_printable_bit_value($row_table_def['Default'], $len); + } else { + $special_chars = htmlspecialchars($row_table_def['Default']); + } $backup_field = ''; } diff --git a/tbl_structure.php b/tbl_structure.php index 0f7ac85ba..5cd22e7f6 100644 --- a/tbl_structure.php +++ b/tbl_structure.php @@ -252,6 +252,8 @@ while ($row = PMA_DBI_fetch_assoc($fields_rs)) { $aryFields[] = $row['Field']; $type = $row['Type']; + $type_and_length = PMA_extract_type_length($row['Type']); + // reformat mysql query output - staybyte - 9. June 2001 // loic1: set or enum types: slashes single quotes inside options if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) { @@ -378,7 +380,14 @@ while ($row = PMA_DBI_fetch_assoc($fields_rs)) { = 40100 ? ' ' . (empty($field_charset) ? '' : '' . $field_charset . '') . '' . "\n" : '' ?> - +