From 4fd715c937914fd87703c851f841cec30b2944aa Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 23 Jun 2014 14:47:08 +0000 Subject: [PATCH] Introduce a binary-safe wrapper for strlen() and use it in seems_utf8(), utf8_uri_encode(), and wp_read_image_metadata(). Use binary-safe POMO_Reader::strlen() in MO::export_to_file_handle(). fixes #28162. git-svn-id: https://develop.svn.wordpress.org/trunk@28806 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/image.php | 4 ++-- src/wp-includes/formatting.php | 4 ++-- src/wp-includes/functions.php | 18 ++++++++++++++++++ src/wp-includes/pomo/mo.php | 8 +++++--- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 7203baecd6..4236249fca 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -290,7 +290,7 @@ function wp_read_image_metadata( $file ) { $caption = trim( $iptc['2#120'][0] ); if ( empty( $meta['title'] ) ) { // Assume the title is stored in 2:120 if it's short. - if ( strlen( $caption ) < 80 ) + if ( mbstring_binary_safe_strlen( $caption ) < 80 ) $meta['title'] = $caption; else $meta['caption'] = $caption; @@ -327,7 +327,7 @@ function wp_read_image_metadata( $file ) { } if ( ! empty( $exif['ImageDescription'] ) ) { - if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) { + if ( empty( $meta['title'] ) && mbstring_binary_safe_strlen( $exif['ImageDescription'] ) < 80 ) { // Assume the title is stored in ImageDescription $meta['title'] = trim( $exif['ImageDescription'] ); if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) { diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 9d93817e64..3b213931cf 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -481,7 +481,7 @@ function shortcode_unautop( $pee ) { * @return bool True if $str fits a UTF-8 model, false otherwise. */ function seems_utf8($str) { - $length = strlen($str); + $length = mbstring_binary_safe_strlen($str); for ($i=0; $i < $length; $i++) { $c = ord($str[$i]); if ($c < 0x80) $n = 0; # 0bbbbbbb @@ -705,7 +705,7 @@ function utf8_uri_encode( $utf8_string, $length = 0 ) { $num_octets = 1; $unicode_length = 0; - $string_length = strlen( $utf8_string ); + $string_length = mbstring_binary_safe_strlen( $utf8_string ); for ($i = 0; $i < $string_length; $i++ ) { $value = ord( $utf8_string[ $i ] ); diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 33ace4283b..a4ad1fe567 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -4404,6 +4404,24 @@ function reset_mbstring_encoding() { mbstring_binary_safe_encoding( true ); } +/** + * Uses a binary-safe encoding to get the length of a string in bytes if func_overload is enabled. + * + * @see mbstring_binary_safe_encoding() + * + * @since 4.0.0 + * + * @param string $string The string to get the length of. + * @return int The length of the string in bytes. + */ +function mbstring_binary_safe_strlen( $string ) { + mbstring_binary_safe_encoding(); + $length = strlen( $string ); + reset_mbstring_encoding(); + + return $length; +} + /** * Alternative to filter_var( $var, FILTER_VALIDATE_BOOLEAN ) * diff --git a/src/wp-includes/pomo/mo.php b/src/wp-includes/pomo/mo.php index 3f76289b89..407c5a83e6 100644 --- a/src/wp-includes/pomo/mo.php +++ b/src/wp-includes/pomo/mo.php @@ -75,21 +75,23 @@ class MO extends Gettext_Translations { $current_addr++; $originals_table = chr(0); + $reader = new POMO_Reader(); + foreach($entries as $entry) { $originals_table .= $this->export_original($entry) . chr(0); - $length = strlen($this->export_original($entry)); + $length = $reader->strlen($this->export_original($entry)); fwrite($fh, pack('VV', $length, $current_addr)); $current_addr += $length + 1; // account for the NULL byte after } $exported_headers = $this->export_headers(); - fwrite($fh, pack('VV', strlen($exported_headers), $current_addr)); + fwrite($fh, pack('VV', $reader->strlen($exported_headers), $current_addr)); $current_addr += strlen($exported_headers) + 1; $translations_table = $exported_headers . chr(0); foreach($entries as $entry) { $translations_table .= $this->export_translations($entry) . chr(0); - $length = strlen($this->export_translations($entry)); + $length = $reader->strlen($this->export_translations($entry)); fwrite($fh, pack('VV', $length, $current_addr)); $current_addr += $length + 1; }