]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/include/rcube_imap.inc
Imported Upstream version 0.1.1
[roundcube.git] / program / include / rcube_imap.inc
index a41f231f1e1045eb49d477e024dece8a4f23812a..51163e4ed3125879d0766b26ee2e3c8b05615542 100644 (file)
@@ -16,7 +16,7 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  +-----------------------------------------------------------------------+
 
- $Id: rcube_imap.inc 1050 2008-02-12 14:06:26Z thomasb $
+ $Id: rcube_imap.inc 1255 2008-04-05 12:49:21Z thomasb $
 
 */
 
@@ -51,6 +51,7 @@ class rcube_imap
   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();
@@ -203,6 +204,20 @@ class rcube_imap
     }
 
 
+  /**
+   * 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;
+    }
+
+
   /**
    * This list of folders will be listed above all other folders
    *
@@ -1146,7 +1161,7 @@ class rcube_imap
       
     // 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 @@ class rcube_imap
       $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 @@ class rcube_imap
       // 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);
         }
@@ -1633,16 +1648,14 @@ class rcube_imap
   /**
    * Subscribe to a specific mailbox(es)
    *
-   * @param string Mailbox name(s)
+   * @param array Mailbox name(s)
    * @return boolean True on success
    */ 
-  function subscribe($mbox_name)
+  function subscribe($a_mboxes)
     {
-    if (is_array($mbox_name))
-      $a_mboxes = $mbox_name;
-    else if (is_string($mbox_name) && strlen($mbox_name))
-      $a_mboxes = explode(',', $mbox_name);
-    
+    if (!is_array($a_mboxes))
+      $a_mboxes = array($a_mboxes);
+
     // let this common function do the main work
     return $this->_change_subscription($a_mboxes, 'subscribe');
     }
@@ -1651,15 +1664,13 @@ class rcube_imap
   /**
    * Unsubscribe mailboxes
    *
-   * @param string Mailbox name(s)
+   * @param array Mailbox name(s)
    * @return boolean True on success
    */
-  function unsubscribe($mbox_name)
+  function unsubscribe($a_mboxes)
     {
-    if (is_array($mbox_name))
-      $a_mboxes = $mbox_name;
-    else if (is_string($mbox_name) && strlen($mbox_name))
-      $a_mboxes = explode(',', $mbox_name);
+    if (!is_array($a_mboxes))
+      $a_mboxes = array($a_mboxes);
 
     // let this common function do the main work
     return $this->_change_subscription($a_mboxes, 'unsubscribe');
@@ -2213,6 +2224,9 @@ class rcube_imap
    */
   function remove_message_cache($key, $index)
     {
+    if (!$this->caching_enabled)
+      return;
+    
     $this->db->query(
       "DELETE FROM ".get_table_name('messages')."
        WHERE  user_id=?
@@ -2228,6 +2242,9 @@ class rcube_imap
    */
   function clear_message_cache($key, $start_index=1)
     {
+    if (!$this->caching_enabled)
+      return;
+    
     $this->db->query(
       "DELETE FROM ".get_table_name('messages')."
        WHERE  user_id=?
@@ -2257,6 +2274,8 @@ class rcube_imap
     {
     $a = $this->_parse_address_list($input, $decode);
     $out = array();
+    // Special chars as defined by RFC 822 need to in quoted string (or escaped).
+    $special_chars = '[\(\)\<\>\\\.\[\]@,;:"]';
     
     if (!is_array($a))
       return $out;
@@ -2270,7 +2289,7 @@ class rcube_imap
       $address = $val['address'];
       $name = preg_replace(array('/^[\'"]/', '/[\'"]$/'), '', trim($val['name']));
       if ($name && $address && $name != $address)
-        $string = sprintf('%s <%s>', strpos($name, ',')!==FALSE ? '"'.$name.'"' : $name, $address);
+        $string = sprintf('%s <%s>', preg_match("/$special_chars/", $name) ? '"'.addcslashes($name, '"').'"' : $name, $address);
       else if ($address)
         $string = $address;
       else if ($name)
@@ -2297,7 +2316,7 @@ class rcube_imap
    */
   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);
     
@@ -2320,6 +2339,10 @@ class rcube_imap
     $pos = strpos($input, '=?');
     if ($pos !== false)
       {
+      // rfc: all line breaks or other characters not found in the Base64 Alphabet must be ignored by decoding software
+      // delete all blanks between MIME-lines, differently we can receive unnecessary blanks and broken utf-8 symbols
+      $input = preg_replace("/\?=\s+=\?/", '?==?', $input);
+
       $out = substr($input, 0, $pos);
   
       $end_cs_pos = strpos($input, "?", $pos+2);
@@ -2414,7 +2437,7 @@ class rcube_imap
       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);
     }