convert character set in html messages
authortokul <tokul@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Sun, 21 Aug 2005 11:00:20 +0000 (11:00 +0000)
committertokul <tokul@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Sun, 21 Aug 2005 11:00:20 +0000 (11:00 +0000)
git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@9978 7612ce4b-ef26-0410-bec9-ea0150e637f0

ChangeLog
functions/i18n.php
functions/mime.php

index bdf142146dc315a5ef55dfb844895eaec877a47d..e8ddf48e5757f3c0bd9611d31cfb90b440680566 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -427,6 +427,10 @@ Version 1.5.1 -- CVS
     function caused E_NOTICE warnings in php 4.4.0. Reported by Cor Bosman.
   - Test to ensure folder exists before attempting to delete it, otherwise
     IMAP server will return an error.
+  - Added $save_html argument to charset_decode() function in order to be
+    able to convert html formated mails to different character set. Initial
+    patch by Peter Draganov (#1195232). Fixed display of html formated emails
+    in formatBody() function (#1258925).
 
 Version 1.5.0 - 2 February 2004
 -------------------------------
index 5b33204d07591edfe91aeacebcd1c23f4b327db4..d4799d268b0eb8af7edd291a1668390372537950 100644 (file)
@@ -109,9 +109,11 @@ function sq_setlocale($category,$locale) {
  * @param string $string Text to be decoded
  * @param boolean $force_decode converts string to html without $charset!=$default_charset check.
  * Argument is available since 1.5.1 and 1.4.5.
+ * @param boolean $save_html disables htmlspecialchars() in order to preserve 
+ *  html formating. Use with care. Available since 1.5.1
  * @return string decoded string
  */
-function charset_decode ($charset, $string, $force_decode=false) {
+function charset_decode ($charset, $string, $force_decode=false, $save_html=false) {
     global $languages, $squirrelmail_language, $default_charset;
     global $use_php_recode, $use_php_iconv, $aggressive_decoding;
 
@@ -132,7 +134,7 @@ function charset_decode ($charset, $string, $force_decode=false) {
 
     // Don't do conversion if charset is the same.
     if ( ! $force_decode && $charset == strtolower($default_charset) )
-        return htmlspecialchars($string);
+        return ($save_html ? $strings : htmlspecialchars($string));
 
     // catch iso-8859-8-i thing
     if ( $charset == "iso-8859-8-i" )
@@ -148,11 +150,15 @@ function charset_decode ($charset, $string, $force_decode=false) {
             // other charsets can be converted to utf-8 without loss.
             // and output string is smaller
             $string = recode_string($charset . "..utf-8",$string);
-            return htmlspecialchars($string);
+            return ($save_html ? $strings : htmlspecialchars($string));
         } else {
             $string = recode_string($charset . "..html",$string);
             // recode does not convert single quote, htmlspecialchars does.
             $string = str_replace("'", '&#039;', $string);
+            // undo html specialchars
+            if ($save_html) 
+                $string=str_replace(array('&amp;','&quot;','&lt;','&gt;'),
+                                    array('&','"','<','>'),$string);
             return $string;
         }
     }
@@ -160,14 +166,13 @@ function charset_decode ($charset, $string, $force_decode=false) {
     // iconv functions does not have html target and can be used only with utf-8
     if ( $use_php_iconv && $default_charset=='utf-8') {
         $string = iconv($charset,$default_charset,$string);
-        return htmlspecialchars($string);
+        return ($save_html ? $strings : htmlspecialchars($string));
     }
 
     // If we don't use recode and iconv, we'll do it old way.
 
     /* All HTML special characters are 7 bit and can be replaced first */
-
-    $string = htmlspecialchars ($string);
+    if (! $save_html) $string = htmlspecialchars ($string);
 
     /* controls cpu and memory intensive decoding cycles */
     if (! isset($aggressive_decoding) || $aggressive_decoding=="" ) {
@@ -177,7 +182,8 @@ function charset_decode ($charset, $string, $force_decode=false) {
     $decodefile=SM_PATH . 'functions/decode/' . $decode . '.php';
     if (file_exists($decodefile)) {
         include_once($decodefile);
-        $ret = call_user_func('charset_decode_'.$decode, $string);
+        // send $save_html argument to decoding function. needed for iso-2022-xx decoding.
+        $ret = call_user_func('charset_decode_'.$decode, $string, $save_html);
     } else {
         $ret = $string;
     }
index a7e6cbf30ecaa4e68037fac0de4b0ea8c9b6dc37..a7fe59087bcb1ca40369f87151bd375bb5d2a1ea 100644 (file)
@@ -388,7 +388,9 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $ma
                  * If we don't add html message between iframe tags,
                  * we must detect unsafe images and modify $has_unsafe_images.
                  */
-                $html_body =  magicHTML($body, $id, $message, $mailbox);
+                $html_body = magicHTML($body, $id, $message, $mailbox); 
+                // Convert character set in order to display html mails in different character set
+                $html_body = charset_decode($body_message->header->getParameter('charset'),$html_body,false,true);
 
                 // creating iframe url
                 $iframeurl=sqm_baseuri().'src/view_html.php?'
@@ -427,6 +429,12 @@ function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $ma
             } else {
                 // old way of html rendering
                 $body = magicHTML($body, $id, $message, $mailbox);
+                /**
+                 * convert character set. charset_decode does not remove html special chars 
+                 * applied by magicHTML functions and does not sanitize them second time if
+                 * fourth argument is true. 
+                 */ 
+                $body = charset_decode($body_message->header->getParameter('charset'),$body,false,true);
             }
         } else {
             translateText($body, $wrap_at,