#32586 [Opn]: imap_utf7_encode doesn't work as expected

From: Date: Wed, 06 Apr 2005 13:25:18 +0000
Subject: #32586 [Opn]: imap_utf7_encode doesn't work as expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-77278@lists.php.net to get a copy of this message
ID: 32586 User updated by: mschering at intermesh dot nl Reported By: mschering at intermesh dot nl Status: Open Bug Type: IMAP related Operating System: Linux 2.6 PHP Version: 4.3.10 New Comment: It seems to be related to base64_encode and base64_decode because these functions work but only work with the custom base64 functions: /** * encode folder name to utf7-imap * * If mbstring functions do not support charset used by translation, falls back to iso-8859-1 * @param string $string folder name * @return string utf7-imap encoded folder name */ function imap_utf7_encode($string) { // works only for ISO-8859-1 $b64_s = ''; // buffer for substring to be base64-encoded $utf7_s = ''; // imap-utf7-encoded string for ($i = 0; $i < strlen($string); $i++) { $c = $string[$i]; $ord_c = ord($c); if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or (($ord_c >= 0x27) and ($ord_c <= 0x7e))) { if ($b64_s) { $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-'; $b64_s = ''; } $utf7_s = $utf7_s . $c; } elseif ($ord_c == 0x26) { if ($b64_s) { $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; $b64_s = ''; } $utf7_s = $utf7_s . '&-'; } else { $b64_s = $b64_s . chr(0) . $c; } } // // flush buffer // if ($b64_s) { $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; $b64_s = ''; } return $utf7_s; } /** * converts folder name from utf7-imap to charset used by translation * * If mbstring functions do not support charset used by translation, falls back to iso-8859-1 * @param string $string folder name in utf7-imap * @return string folder name in charset used by translation */ function imap_utf7_decode($string) { // works only for ISO-8859-1 $b64_s = ''; $iso_8859_1_s = ''; for ($i = 0, $len = strlen($string); $i < $len; $i++) { $c = $string[$i]; if (strlen($b64_s) > 0) { if ($c == '-') { if ($b64_s == '&') { $iso_8859_1_s = $iso_8859_1_s . '&'; } else { $iso_8859_1_s = $iso_8859_1_s . decodeBASE64(substr($b64_s, 1)); } $b64_s = ''; } else { $b64_s = $b64_s . $c; } } else { if ($c == '&') { $b64_s = '&'; } else { $iso_8859_1_s = $iso_8859_1_s . $c; } } } return $iso_8859_1_s; } /** * Converts string to base64 * @param string $s string * @return string base64 encoded string */ function encodeBASE64($string) { $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,'; $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3... $e = ''; // base64-encoded string //foreach($s as $c) { for ($i = 0; $i < strlen($string); $i++) { $c = $string[$i]; if ($p == 0) { $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1); $t = (ord($c) & 3); $p = 1; } elseif ($p == 1) { $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)]; $t = (ord($c) & 15); $p = 2; } elseif ($p == 2) { $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)]; $e = $e . $B64Chars[ord($c) & 63]; $p = 0; } } // // flush buffer // if ($p == 1) { $e = $e . $B64Chars[$t << 4]; } elseif ($p == 2) { $e = $e . $B64Chars[$t << 2]; } return $e; } /** * Converts string from base64 * @param string $s base64 encoded string * @return string decoded string */ function decodeBASE64($string) { $B64Values = array( 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25, 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37, 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49, 'y' => 50, 'z' => 51, '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57, '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63 ); $p = 0; $d = ''; $unicodeNullByteToggle = 0; for ($i = 0, $len = strlen($string); $i < $len; $i++) { $c = $string[$i]; if ($p == 0) { $t = $B64Values[$c]; $p = 1; } elseif ($p == 1) { if ($unicodeNullByteToggle) { $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4)); $unicodeNullByteToggle = 0; } else { $unicodeNullByteToggle = 1; } $t = ($B64Values[$c] & 15); $p = 2; } elseif ($p == 2) { if ($unicodeNullByteToggle) { $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2)); $unicodeNullByteToggle = 0; } else { $unicodeNullByteToggle = 1; } $t = ($B64Values[$c] & 3); $p = 3; } elseif ($p == 3) { if ($unicodeNullByteToggle) { $d = $d . chr(($t << 6) + $B64Values[$c]); $unicodeNullByteToggle = 0; } else { $unicodeNullByteToggle = 1; } $t = ($B64Values[$c] & 3); $p = 0; } } return $d; } Previous Comments: ------------------------------------------------------------------------ [2005-04-05 12:54:15] mschering at intermesh dot nl Description: ------------ These functions do not work: /** * Replacement for $this->utf7_encode because it expects the text to be in * ISO-8859-1 format while GO uses UTF-8. * * @access public * @param $text The UTF-8 encoded text to encode in UTF-7 * @return string UTF-7 encoded text */ function utf7_encode($text) { return imap_utf7_encode(utf8_decode($text)); } /** * Replacement for $this->utf7_decode becuase it returns the text in * ISO-8859-1 format while GO uses UTF-8. * * @access public * @param $text The UTF-7 text to encode to UTF-8 * @return string UTF-8 encoded text */ function utf7_decode($text) { return utf8_encode(imap_utf7_decode($text)); } } Reproduce code: --------------- Use these functions and encode UTF-8 strings with characters like ëäö etc in it and it will produce garbage. I use these in my mail client and it works but other mail clients do not see the folders correctly. Expected result: ---------------- Other mail clients should see the folder names correctly Actual result: -------------- The folder names have strange characters in other mail clients ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=32586&edit=1

« previous php.bugs (#77278) next »