From 17b5fb797f4bc142fee8cd72ade3890b4dfdbd82 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Fri, 21 Mar 2008 07:45:46 -0400
Subject: [PATCH] Add configurable default charset for message decoding

---
 CHANGELOG                         |    5 ++
 program/include/main.inc          |    3 +
 program/steps/mail/compose.inc    |    6 ++
 program/steps/mail/viewsource.inc |   26 +++++++------
 program/steps/mail/show.inc       |    4 ++
 config/main.inc.php.dist          |    3 +
 program/include/rcube_imap.inc    |   27 ++++++++++---
 7 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 1a0f687..6b62006 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,11 @@
 CHANGELOG RoundCube Webmail
 ---------------------------
 
+2008/03/21 (thomasb)
+----------
+- Add configurable default charset for message decoding
+
+
 2008/02/28 (thomasb)
 ----------
 - Fix folder adding/renaming inspired by #1484800
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 8475c81..ab98da8 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -180,6 +180,9 @@
 // 0 = ask the user, 1 = send automatically, 2 = ignore (never send or ask)
 $rcmail_config['mdn_requests'] = 0;
 
+// Use this as charset as fallback for message decoding
+$rcmail_config['default_charset'] = 'ISO-8859-1';
+
 // Make use of the built-in spell checker. It is based on GoogieSpell.
 // Since Google only accepts connections over https your PHP installatation
 // requires to be compiled with Open SSL support
diff --git a/program/include/main.inc b/program/include/main.inc
index 247cdf1..e2005e7 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -290,6 +290,9 @@
 function rcmail_set_imap_prop()
   {
   global $CONFIG, $IMAP;
+  
+  if (!empty($CONFIG['default_charset']))
+    $IMAP->set_charset($CONFIG['default_charset']);
 
   // set root dir from config
   if (!empty($CONFIG['imap_root']))
diff --git a/program/include/rcube_imap.inc b/program/include/rcube_imap.inc
index dce7789..bec429f 100644
--- a/program/include/rcube_imap.inc
+++ b/program/include/rcube_imap.inc
@@ -51,6 +51,7 @@
   var $sort_order = 'DESC';
   var $delimiter = NULL;
   var $caching_enabled = FALSE;
+  var $default_charset = 'ISO-8859-1';
   var $default_folders = array('INBOX');
   var $default_folders_lc = array('inbox');
   var $cache = array();
@@ -200,6 +201,20 @@
     
     if (empty($this->delimiter))
       $this->get_hierarchy_delimiter();
+    }
+
+
+  /**
+   * Set default message charset
+   *
+   * This will be used for message decoding if a charset specification is not available
+   *
+   * @param  string   Charset string
+   * @access public
+   */
+  function set_charset($cs)
+    {
+    $this->default_charset = $ch;
     }
 
 
@@ -1146,7 +1161,7 @@
       
     // normalize filename property
     if ($filename_mime = $struct->d_parameters['filename'] ? $struct->d_parameters['filename'] : $struct->ctype_parameters['name'])
-      $struct->filename = $this->decode_mime_string($filename_mime);
+      $struct->filename = rcube_imap::decode_mime_string($filename_mime, $this->default_charset);
     else if ($filename_encoded = $struct->d_parameters['filename*'] ? $struct->d_parameters['filename*'] : $struct->ctype_parameters['name*'])
     {
       // decode filename according to RFC 2231, Section 4
@@ -1154,7 +1169,7 @@
       $struct->filename = rcube_charset_convert(urldecode($filename_urlencoded), $filename_charset);
     }
     else if (!empty($struct->headers['content-description']))
-      $struct->filename = $this->decode_mime_string($struct->headers['content-description']);
+      $struct->filename = rcube_imap::decode_mime_string($struct->headers['content-description'], $this->default_charset);
       
     return $struct;
     }
@@ -1241,9 +1256,9 @@
       // convert charset (if text or message part)
       if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message')
         {
-        // assume ISO-8859-1 if no charset specified
+        // assume default if no charset specified
         if (empty($o_part->charset))
-          $o_part->charset = 'ISO-8859-1';
+          $o_part->charset = $this->default_charset;
 
         $body = rcube_charset_convert($body, $o_part->charset);
         }
@@ -2297,7 +2312,7 @@
    */
   function decode_header($input, $remove_quotes=FALSE)
     {
-    $str = $this->decode_mime_string((string)$input);
+    $str = rcube_imap::decode_mime_string((string)$input, $this->default_charset);
     if ($str{0}=='"' && $remove_quotes)
       $str = str_replace('"', '', $str);
     
@@ -2414,7 +2429,7 @@
       return rcube_charset_convert($body, $ctype_param['charset']);
 
     // defaults to what is specified in the class header
-    return rcube_charset_convert($body,  'ISO-8859-1');
+    return rcube_charset_convert($body,  $this->default_charset);
     }
 
 
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 71d2197..6b08d85 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -90,7 +90,11 @@
   // similar as in program/steps/mail/show.inc
   $MESSAGE = array('UID' => $msg_uid);
   $MESSAGE['headers'] = &$IMAP->get_headers($msg_uid);
-  $MESSAGE['structure'] = &$IMAP->get_structure($msg_uid);  
+  $MESSAGE['structure'] = &$IMAP->get_structure($msg_uid);
+  
+  if (!empty($MESSAGE['headers']->charset))
+    $IMAP->set_charset($MESSAGE['headers']->charset);
+    
   $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
   $MESSAGE['parts'] = $IMAP->get_mime_numbers($MESSAGE['structure']);
   
diff --git a/program/steps/mail/show.inc b/program/steps/mail/show.inc
index 5cd5e83..27d2660 100644
--- a/program/steps/mail/show.inc
+++ b/program/steps/mail/show.inc
@@ -30,6 +30,10 @@
   $MESSAGE = array('UID' => get_input_value('_uid', RCUBE_INPUT_GET));
   $MESSAGE['headers'] = $IMAP->get_headers($MESSAGE['UID']);
   
+  // set message charset as default
+  if (!empty($MESSAGE['headers']->charset))
+    $IMAP->set_charset($MESSAGE['headers']->charset);
+  
   // go back to list if message not found (wrong UID)
   if (!$MESSAGE['headers'])
     {
diff --git a/program/steps/mail/viewsource.inc b/program/steps/mail/viewsource.inc
index c88b165..7c32b89 100644
--- a/program/steps/mail/viewsource.inc
+++ b/program/steps/mail/viewsource.inc
@@ -5,7 +5,7 @@
  | program/steps/mail/viewsource.inc                                     |
  |                                                                       |
  | This file is part of the RoundCube Webmail client                     |
- | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
+ | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland                 |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
@@ -22,19 +22,21 @@
 
 // similar code as in program/steps/mail/get.inc
 if ($uid = get_input_value('_uid', RCUBE_INPUT_GET))
-  {
-  header('Content-Type: text/plain');
-  //@ob_end_clean();
+{
+  $headers = $IMAP->get_headers($uid);
+  $charset = $headers->charset ? $headers->charset : $IMAP->default_charset;
+  header("Content-Type: text/plain; charset={$charset}");
   $IMAP->print_raw_body($uid);
-  }
+}
 else
-  {
-  raise_error(array('code' => 500,
-                    'type' => 'php',
-                    'message' => 'Message UID '.$uid.' not found'),
-              TRUE,
-              TRUE);
-  }
+{
+  raise_error(array(
+      'code' => 500,
+      'type' => 'php',
+      'message' => 'Message UID '.$uid.' not found'),
+    true,
+    true);
+}
 
 exit;
 ?>

--
Gitblit v1.9.1