]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/include/rcube_mail_mime.php
Imported Upstream version 0.3.1
[roundcube.git] / program / include / rcube_mail_mime.php
index 866786b93aac78d57496a632f960d73adb748f04..ab93d3a708029cfecc2f8314b7eae451fb2fc7b4 100644 (file)
@@ -5,7 +5,7 @@
  | program/include/rcube_mail_mime.php                                   |
  |                                                                       |
  | This file is part of the RoundCube Webmail client                     |
- | Copyright (C) 2007-2008, RoundCube Dev. - Switzerland                 |
+ | Copyright (C) 2007-2009, RoundCube Dev. - Switzerland                 |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
@@ -27,6 +27,9 @@
  */
 class rcube_mail_mime extends Mail_mime
 {
+
+  protected $mime_content;
+
   /**
    * Set build parameters
    */
@@ -125,7 +128,7 @@ class rcube_mail_mime extends Mail_mime
     {
       // if header contains e-mail addresses
       if (preg_match('/\s<.+@[a-z0-9\-\.]+\.[a-z]+>/U', $hdr_value)) {
-        $chunks = $this->_explode_quoted_string(',', $hdr_value);
+        $chunks = rcube_explode_quoted_string(',', $hdr_value);
       }
       else {
         $chunks = array($hdr_value);
@@ -138,7 +141,7 @@ class rcube_mail_mime extends Mail_mime
         $value = trim($value);
 
         //This header contains non ASCII chars and should be encoded.
-        if (preg_match('#[\x80-\xFF]{1}#', $value)) {
+        if (preg_match('/[\x80-\xFF]{1}/', $value)) {
           $suffix = '';
           // Don't encode e-mail address
           if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches)) {
@@ -157,9 +160,9 @@ class rcube_mail_mime extends Mail_mime
             default:
             // quoted-printable encoding has been selected
             $mode = 'Q';
-            $encoded = preg_replace('/([\x2C\x3F\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value);
-            // replace spaces with _
-            $encoded = str_replace(' ', '_', $encoded);
+            // replace ?, =, _ and spaces
+            $encoded = str_replace(array('=','_','?',' '), array('=3D','=5F','=3F','_'), $value);
+            $encoded = preg_replace('/([\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $encoded);
           }
 
           $value = '=?' . $params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix;
@@ -177,29 +180,26 @@ class rcube_mail_mime extends Mail_mime
         }
       }
 
-      $input[$hdr_name] = $hdr_value;
+      $input[$hdr_name] = wordwrap($hdr_value, 990, "\n", true);  // hard limit header length
     }
 
     return $input;
   }
 
 
-  function _explode_quoted_string($delimiter, $string)
+  /**
+   * Provides caching of body of constructed MIME Message to avoid 
+   * duplicate construction of message and damage of MIME headers
+   *
+   * @return string The mime content
+   * @access public
+   * @override
+   */
+  public function &get($build_params = null)
   {
-    $result = array();
-    $strlen = strlen($string);
-    for ($q=$p=$i=0; $i < $strlen; $i++) {
-      if ($string{$i} == "\"" && $string{$i-1} != "\\") {
-        $q = $q ? false : true;
-      }
-      else if (!$q && $string{$i} == $delimiter) {
-        $result[] = substr($string, $p, $i - $p);
-        $p = $i + 1;
-      }
-    }
-    
-    $result[] = substr($string, $p);
-    return $result;
+    if(empty($this->mime_content))
+      $this->mime_content = parent::get($build_params);
+    return $this->mime_content;
   }
 
 }