From 42a4a8c24b8e0717894accb23a431eff13448435 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 | 46 ++++++++++++++++++++++++++++++++ libraries/display_tbl.lib.php | 7 +---- libraries/tbl_properties.inc.php | 15 ++++++----- tbl_change.php | 12 ++++----- tbl_structure.php | 11 +++++++- 6 files changed, 73 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2df28f246..de3e0ac4f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -66,6 +66,7 @@ danbarry 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 163b8b2c9..4eed8bf87 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -2442,4 +2442,50 @@ function PMA_cacheSet($var, $val = null, $server = 0) $_SESSION['cache']['server_' . $server][$var] = $val; } +/** + * 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 e291f0ad5..c92bdc342 100644 --- a/libraries/display_tbl.lib.php +++ b/libraries/display_tbl.lib.php @@ -1339,12 +1339,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 8d5449391..d9e275376 100644 --- a/libraries/tbl_properties.inc.php +++ b/libraries/tbl_properties.inc.php @@ -191,6 +191,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 @@ -227,12 +232,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 d7d436b18..169a946e3 100644 --- a/tbl_change.php +++ b/tbl_change.php @@ -424,11 +424,7 @@ foreach ($rows as $row_id => $vrow) { $special_chars = ''; $data = $vrow[$field['Field']]; } elseif ($field['True_Type'] == 'bit') { - $special_chars = ''; - for ($j = 0; $j < ceil($field['len'] / 8); $j++) { - $special_chars .= sprintf('%08d', decbin(ord(substr($vrow[$field['Field']], $j, 1)))); - } - $special_chars = substr($special_chars, -$field['len']); + $special_chars = PMA_printable_bit_value($vrow[$field], $len); } else { // loic1: special binary "characters" if ($field['is_binary'] || $field['is_blob']) { @@ -457,7 +453,11 @@ foreach ($rows as $row_id => $vrow) { } else { $data = $field['Default']; } - $special_chars = htmlspecialchars($field['Default']); + if ($field['True_Type'] == 'bit') { + $special_chars = PMA_printable_bit_value($field['Default'], $len); + } else { + $special_chars = htmlspecialchars($field['Default']); + } $backup_field = ''; } diff --git a/tbl_structure.php b/tbl_structure.php index fe5340b6a..963d6ff57 100644 --- a/tbl_structure.php +++ b/tbl_structure.php @@ -212,6 +212,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)) { @@ -335,7 +337,14 @@ while ($row = PMA_DBI_fetch_assoc($fields_rs)) { ' . $field_charset . ''); ?> - +