From 6fbd125bd6a9b93f4426ded8ec23b4d6986c81bf Mon Sep 17 00:00:00 2001 From: philippe_mingo Date: Fri, 23 Aug 2002 15:46:58 +0000 Subject: [PATCH] Masato: Change into the extra encode philosophy to allow other languages to use it. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@3430 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/i18n.php | 75 +++++++++++++++++++++++++---------- functions/mailbox_display.php | 6 +-- functions/mime.php | 21 +++++----- functions/smtp.php | 11 +++-- src/compose.php | 8 ++-- src/download.php | 12 ++---- src/read_body.php | 6 ++- 7 files changed, 89 insertions(+), 50 deletions(-) diff --git a/functions/i18n.php b/functions/i18n.php index 93217f1c..e1f8a595 100644 --- a/functions/i18n.php +++ b/functions/i18n.php @@ -17,8 +17,11 @@ /* Decodes a string to the internal encoding from the given charset */ function charset_decode ($charset, $string) { + global $languages, $squirrelmail_language; - $string = charset_decode_japanese($string); + if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { + $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string); + } /* All HTML special characters are 7 bit and can be replaced first */ $string = htmlspecialchars ($string); @@ -675,32 +678,63 @@ function charset_decode_koi8r ($string) { return $string; } + /* - * for japanese + * Japanese charset extra function + * */ -function charset_decode_japanese($string) -{ - global $squirrelmail_language; - if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding')) { - $detect_encoding = mb_detect_encoding($string); - if ($detect_encoding == 'JIS' || $detect_encoding == 'EUC-JP' || $detect_encoding == 'SJIS') { - $string = mb_convert_encoding($string, 'EUC-JP', 'AUTO'); - } - } - return $string; +function japanese_charset_xtra() { + $ret = func_get_arg(1); /* default return value */ + if (function_exists('mb_detect_encoding')) { + switch (func_get_arg(0)) { /* action */ + case 'decode': + $detect_encoding = mb_detect_encoding($ret); + if ($detect_encoding == 'JIS' || + $detect_encoding == 'EUC-JP' || + $detect_encoding == 'SJIS') { + + $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO'); + } + break; + case 'encode': + $detect_encoding = mb_detect_encoding($ret); + if ($detect_encoding == 'JIS' || + $detect_encoding == 'EUC-JP' || + $detect_encoding == 'SJIS') { + + $ret = mb_convert_encoding($ret, 'JIS', 'AUTO'); + } + break; + case 'strimwidth': + $width = func_get_arg(2); + $ret = mb_strimwidth($ret, 0, $width, '...'); + break; + case 'encodeheader': + $ret = mb_encode_mimeheader($ret); + break; + case 'decodeheader': + $ret = str_replace("\t", "", $ret); + if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret)) + $ret = mb_decode_mimeheader($ret); + $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO'); + break; + case 'downloadfilename': + $useragent = func_get_arg(2); + if (strstr($useragent, 'Windows') !== false || + strstr($useragent, 'Mac_') !== false) { + $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO'); + } else { + $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO'); } -function charset_encode_japanese($string) -{ - global $squirrelmail_language; - if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding')) { - $detect_encoding = mb_detect_encoding($string); - if ($detect_encoding == 'JIS' || $detect_encoding == 'EUC-JP' || $detect_encoding == 'SJIS') { - $string = mb_convert_encoding($string, 'JIS', 'AUTO'); + break; + default: + break; } } - return $string; + return $ret; } + /* * Set up the language to be output * if $do_search is true, then scan the browser information @@ -859,6 +893,7 @@ $languages['it']['ALIAS'] = 'it_IT'; $languages['ja_JP']['NAME'] = 'Japanese'; $languages['ja_JP']['CHARSET'] = 'iso-2022-jp'; +$languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra'; $languages['ja']['ALIAS'] = 'ja_JP'; $languages['ko_KR']['NAME'] = 'Korean'; diff --git a/functions/mailbox_display.php b/functions/mailbox_display.php index ed6e2c92..0ef018fc 100644 --- a/functions/mailbox_display.php +++ b/functions/mailbox_display.php @@ -1178,7 +1178,7 @@ function get_paginator_str($box, $start_msg, $end_msg, $num_msgs, } function processSubject($subject) { - global $squirrelmail_language; + global $languages, $squirrelmail_language; /* Shouldn't ever happen -- caught too many times in the IMAP functions */ if ($subject == '') return _("(no subject)"); @@ -1206,8 +1206,8 @@ function processSubject($subject) { return $subject; } - if ($squirrelmail_language == 'ja_JP' && function_exists('mb_strimwidth')) { - return mb_strimwidth($subject, 0, $trim_val, '...'); + if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { + return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val); } return substr($subject, 0, $trim_val) . '...'; diff --git a/functions/mime.php b/functions/mime.php index b307e560..1390dcfc 100644 --- a/functions/mime.php +++ b/functions/mime.php @@ -348,7 +348,6 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $ma // If there are other types that shouldn't be formatted, add // them here if ($body_message->header->type1 == 'html') { - $body = charset_decode_japanese($body); if ( $show_html_default <> 1 ) { $body = strip_tags( $body ); translateText($body, $wrap_at, @@ -491,6 +490,7 @@ function formatAttachments($message, $exclude_id, $mailbox, $id) { /** this function decodes the body depending on the encoding type. **/ function decodeBody($body, $encoding) { + global $languages, $squirrelmail_language; $body = str_replace("\r\n", "\n", $body); $encoding = strtolower($encoding); @@ -508,6 +508,10 @@ function decodeBody($body, $encoding) { $body = base64_decode($body); } + if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { + $body = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $body); + } + // All other encodings are returned raw. return $body; } @@ -518,16 +522,13 @@ function decodeBody($body, $encoding) { * Patched by Christian Schmidt 23/03/2002 */ function decodeHeader ($string, $utfencode=true) { - global $squirrelmail_language; + global $languages, $squirrelmail_language; if (is_array($string)) { $string = implode("\n", $string); } - if ($squirrelmail_language == 'ja_JP' && function_exists('mb_convert_encoding')) { - $string = str_replace("\t", "", $string); - if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $string)) - $string = mb_decode_mimeheader($string); - return @mb_convert_encoding($string, 'EUC-JP', 'AUTO'); + if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { + $string = $languages[$squirrelmail_language]['XTRA_CODE']('decodeheader', $string); } $i = 0; @@ -563,10 +564,10 @@ function decodeHeader ($string, $utfencode=true) { * be encoded. */ function encodeHeader ($string) { - global $default_charset, $squirrelmail_language; + global $default_charset, $languages, $squirrelmail_language; - if ($squirrelmail_language == 'ja_JP' && function_exists('mb_encode_mimeheader')) { - return mb_encode_mimeheader($string); + if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { + return $languages[$squirrelmail_language]['XTRA_CODE']('encodeheader', $string); } // Encode only if the string contains 8-bit characters or =? diff --git a/functions/smtp.php b/functions/smtp.php index 022bacb9..a2551ed4 100644 --- a/functions/smtp.php +++ b/functions/smtp.php @@ -111,7 +111,7 @@ function expandRcptAddrs ($array) { /* Attach the files that are due to be attached */ function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) { - global $attachments, $attachment_dir, $username; + global $attachments, $attachment_dir, $username, $languages, $squirrelmail_language; $length = 0; @@ -129,10 +129,15 @@ function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) { $header = '--' . mimeBoundary() . "$rn"; if ( isset($info['remotefilename']) && $info['remotefilename'] != '') { + $remotefilename = $info['remotefilename']; + if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { + $remotefilename = + $languages[$squirrelmail_language]['XTRA_CODE']('encode', $remotefilename); + } $header .= "Content-Type: $filetype; name=\"" . - encodeHeader(charset_encode_japanese($info['remotefilename'])) . "\"$rn"; + encodeHeader($remotefilename) . "\"$rn"; $header .= "Content-Disposition: attachment; filename=\"" - . encodeHeader(charset_encode_japanese($info['remotefilename'])) . "\"$rn"; + . encodeHeader($remotefilename) . "\"$rn"; } else { $header .= "Content-Type: $filetype$rn"; } diff --git a/src/compose.php b/src/compose.php index a9bab89a..9ffb6633 100644 --- a/src/compose.php +++ b/src/compose.php @@ -516,7 +516,7 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se $send_to = $orig_header->getAddr_s('to'); $send_to_cc = $orig_header->getAddr_s('cc'); $send_to_bcc = $orig_header->getAddr_s('bcc'); - $subject = $orig_header->subject; + $subject = decodeHeader($orig_header->subject); $body_ary = explode("\n", $body); $cnt = count($body_ary) ; @@ -535,7 +535,7 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se $send_to = $orig_header->getAddr_s('to'); $send_to_cc = $orig_header->getAddr_s('cc'); $send_to_bcc = $orig_header->getAddr_s('bcc'); - $subject = $orig_header->subject; + $subject = decodeHeader($orig_header->subject); $mailprio = $orig_header->priority; $orig_from = ''; getAttachments($message, $session, $passed_id, $entities, $imapConnection); @@ -543,13 +543,13 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se break; case ('forward'): $send_to = ''; - $subject = $orig_header->subject; + $subject = decodeHeader($orig_header->subject); if ((substr(strtolower($subject), 0, 4) != 'fwd:') && (substr(strtolower($subject), 0, 5) != '[fwd:') && (substr(strtolower($subject), 0, 6) != '[ fwd:')) { $subject = '[Fwd: ' . $subject . ']'; } - $body = getforwardHeader($orig_header) . charset_decode_japanese($body); + $body = getforwardHeader($orig_header) . $body; sqUnWordWrap($body); getAttachments($message, $session, $passed_id, $entities, $imapConnection); break; diff --git a/src/download.php b/src/download.php index 60f388bb..ebf4e91b 100644 --- a/src/download.php +++ b/src/download.php @@ -151,7 +151,7 @@ mime_print_body_lines ($imapConnection, $passed_id, $ent_id, $encoding); * version of IE. I don't know if it works with Opera, but it should now. */ function DumpHeaders($type0, $type1, $filename, $force) { - global $HTTP_USER_AGENT, $squirrelmail_language; + global $HTTP_USER_AGENT, $languages, $squirrelmail_language; $isIE = 0; if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false && @@ -164,13 +164,9 @@ function DumpHeaders($type0, $type1, $filename, $force) { $isIE6 = 1; } - if ($squirrelmail_language == 'ja_JP') { - if (strstr($HTTP_USER_AGENT, 'Windows') !== false || - strstr($HTTP_USER_AGENT, 'Mac_') !== false) { - $filename = mb_convert_encoding($filename, 'SJIS', 'AUTO'); - } else { - $filename = mb_convert_encoding($filename, 'EUC-JP', 'AUTO'); - } + if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { + $filename = + $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT); } else { $filename = ereg_replace('[^-a-zA-Z0-9\.]', '_', $filename); } diff --git a/src/read_body.php b/src/read_body.php index 47763504..97ae4574 100644 --- a/src/read_body.php +++ b/src/read_body.php @@ -157,7 +157,7 @@ function ServerMDNSupport( $read ) { function SendMDN ( $mailbox, $passed_id, $sender, $message) { global $username, $attachment_dir, $SERVER_NAME, - $version, $attachments; + $version, $attachments, $squirrelmail_language; $header = $message->rfc822_header; $hashed_attachment_dir = getHashedDir($username, $attachment_dir); @@ -186,7 +186,9 @@ function SendMDN ( $mailbox, $passed_id, $sender, $message) { "\r\n" . sprintf( _("Was displayed on %s"), $now ); - $body = charset_encode_japanese($body); + if (function_exists($languages[$squrrelmail_language]['XTRA_CODE'])) { + $body = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $body); + } // part2 (RFC2298) $original_recipient = $to; -- 2.25.1