From 869ece5d542664e592ec632eb2612a92dca90c9a Mon Sep 17 00:00:00 2001 From: centaurix Date: Thu, 16 May 2002 12:17:15 +0000 Subject: [PATCH 1/1] patch #551871 for feature request #545070 from Stefan Tietke (stfn) regarding utf7 imap encoding git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@2833 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/imap_mailbox.php | 15 +++-- functions/imap_utf7_decode_local.php | 86 ++++++++++++++++++++++++++++ functions/imap_utf7_encode_local.php | 68 ++++++++++++++++++++++ functions/mailbox_display.php | 4 +- functions/page_header.php | 4 +- 5 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 functions/imap_utf7_decode_local.php create mode 100644 functions/imap_utf7_encode_local.php diff --git a/functions/imap_mailbox.php b/functions/imap_mailbox.php index 3b7ca6dd..7dfab0fe 100755 --- a/functions/imap_mailbox.php +++ b/functions/imap_mailbox.php @@ -11,6 +11,9 @@ * $Id$ */ +require_once('../functions/imap_utf7_encode_local.php'); +require_once('../functions/imap_utf7_decode_local.php'); + global $boxesnew; class mailboxes { @@ -214,6 +217,9 @@ function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { if (strtolower($type) == 'noselect') { $mailbox .= $delimiter; } + + $mailbox = imap_utf7_encode_local($mailbox); + $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"", true, $response, $message); sqimap_subscribe ($imap_stream, $mailbox); @@ -343,10 +349,11 @@ function sqimap_mailbox_parse ($line, $line_lsub) { else { $boxesall[$g]['formatted'] = ''; } - $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); + $boxesall[$g]['formatted'] .= imap_utf7_decode_local( + readShortMailboxName($mailbox, $delimiter)); } else { - $boxesall[$g]['formatted'] = $mailbox; + $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox); } $boxesall[$g]['unformatted-dm'] = $mailbox; @@ -635,10 +642,10 @@ function sqimap_mailbox_list_all($imap_stream) { else { $boxes[$g]['formatted'] = ''; } - $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); + $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); } else { - $boxes[$g]['formatted'] = $mailbox; + $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox); } $boxes[$g]['unformatted-dm'] = $mailbox; diff --git a/functions/imap_utf7_decode_local.php b/functions/imap_utf7_decode_local.php new file mode 100644 index 00000000..044b5781 --- /dev/null +++ b/functions/imap_utf7_decode_local.php @@ -0,0 +1,86 @@ + 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; $i < strlen($s); $i++) { + $c = $s[$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; +} + +function imap_utf7_decode_local($s) { + $b64_s = ''; + $iso_8859_1_s = ''; + for ($i = 0; $i < strlen($s); $i++) { + $c = $s[$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; +} + +?> diff --git a/functions/imap_utf7_encode_local.php b/functions/imap_utf7_encode_local.php new file mode 100644 index 00000000..020fcda7 --- /dev/null +++ b/functions/imap_utf7_encode_local.php @@ -0,0 +1,68 @@ +> 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; +} + +function imap_utf7_encode_local($s) { + $b64_s = ''; // buffer for substring to be base64-encoded + $utf7_s = ''; // imap-utf7-encoded string + for ($i = 0; $i < strlen($s); $i++) { + $c = $s[$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; +} + +?> diff --git a/functions/mailbox_display.php b/functions/mailbox_display.php index 1ef91de8..3c412693 100644 --- a/functions/mailbox_display.php +++ b/functions/mailbox_display.php @@ -13,6 +13,7 @@ */ require_once('../functions/strings.php'); +require_once('../functions/imap_utf7_decode_local.php'); /* Default value for page_selector_max. */ define('PG_SEL_MAX', 10); @@ -673,7 +674,8 @@ function mail_message_listing_beginning ($imapConnection, $moveURL, foreach ($boxes as $boxes_part) { if (!in_array('noselect', $boxes_part['flags'])) { $box = $boxes_part['unformatted']; - $box2 = str_replace(' ', ' ', $boxes_part['unformatted-disp']); + $box2 = imap_utf7_decode_local( + str_replace(' ', ' ', $boxes_part['unformatted-disp'])); if( $box2 == 'INBOX' ) { $box2 = _("INBOX"); } diff --git a/functions/page_header.php b/functions/page_header.php index 03a71fe7..fe30f23c 100644 --- a/functions/page_header.php +++ b/functions/page_header.php @@ -12,6 +12,7 @@ */ require_once('../functions/strings.php'); +require_once('../functions/imap_utf7_decode_local.php'); /* Always set up the language before calling these functions */ function displayHtmlHeader( $title = 'SquirrelMail', $xtra = '', $do_hook = TRUE ) { @@ -130,7 +131,8 @@ function displayPageHeader($color, $mailbox, $xtra='') { echo "\n\n"; /** Here is the header and wrapping table **/ - $shortBoxName = readShortMailboxName($mailbox, $delimiter); + $shortBoxName = imap_utf7_decode_local( + readShortMailboxName($mailbox, $delimiter)); if ( $shortBoxName == 'INBOX' ) { $shortBoxName = _("INBOX"); } -- 2.25.1