]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_mail_mime.php
Imported Upstream version 0.2~alpha
[roundcube.git] / program / include / rcube_mail_mime.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_mail_mime.php                                   |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2007-2008, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Extend PEAR:Mail_mime class and override encodeHeaders method       |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: sendmail.inc 506 2007-03-14 00:39:51Z thomasb $
19
20 */
21
22
23 /**
24  * Replacement PEAR:Mail_mime with some additional or overloaded methods
25  *
26  * @package Mail
27  */
28 class rcube_mail_mime extends Mail_mime
29 {
30   /**
31    * Set build parameters
32    */
33   function setParam($param)
34   {
35     if (is_array($param)) {
36       $this->_build_params = array_merge($this->_build_params, $param);
37     }
38   }
39   
40   /**
41    * Adds an image to the list of embedded images.
42    *
43    * @param  string  $file       The image file name OR image data itself
44    * @param  string  $c_type     The content type
45    * @param  string  $name       The filename of the image.
46    *                             Only use if $file is the image data
47    * @param  bool    $isfilename Whether $file is a filename or not
48    *                             Defaults to true
49    * @param  string  $contentid  Desired Content-ID of MIME part
50    *                             Defaults to generated unique ID
51    * @return mixed   true on success or PEAR_Error object
52    * @access public
53    */
54   function addHTMLImage($file, $c_type='application/octet-stream', $name = '', $isfilename = true, $contentid = '')
55   {
56     $filedata = ($isfilename === true) ? $this->_file2str($file) : $file;
57     if ($isfilename === true) {
58       $filename = ($name == '' ? $file : $name);
59     }
60     else {
61       $filename = $name;
62     }
63
64     if (PEAR::isError($filedata)) {
65         return $filedata;
66     }
67
68     if ($contentid == '') {
69        $contentid = md5(uniqid(time()));
70     }
71
72     $this->_html_images[] = array(
73       'body'   => $filedata,
74       'name'   => $filename,
75       'c_type' => $c_type,
76       'cid'    => $contentid
77     );
78
79     return true;
80   }
81   
82   
83   /**
84   * returns the HTML body portion of the message
85   * @return string HTML body of the message
86   * @access public
87   */
88   function getHTMLBody()
89   {
90      return $this->_htmlbody;
91   }
92   
93   
94   /**
95    * Creates a new mimePart object, using multipart/mixed as
96    * the initial content-type and returns it during the
97    * build process.
98    *
99    * @return object  The multipart/mixed mimePart object
100    * @access private
101    */
102   function &_addMixedPart()
103   {
104     $params['content_type'] = $this->_headers['Content-Type'] ? $this->_headers['Content-Type'] : 'multipart/mixed';
105     $ret = new Mail_mimePart('', $params);
106     return $ret;
107   }
108   
109   
110   /**
111    * Encodes a header as per RFC2047
112    *
113    * @param  array $input The header data to encode
114    * @param  array $params Extra build parameters
115    * @return array Encoded data
116    * @access private
117    * @override
118    */
119   function _encodeHeaders($input, $params = array())
120   {
121     $maxlen = 73;
122     $params += $this->_build_params;
123     
124     foreach ($input as $hdr_name => $hdr_value)
125     {
126       // if header contains e-mail addresses
127       if (preg_match('/\s<.+@[a-z0-9\-\.]+\.[a-z]+>/U', $hdr_value)) {
128         $chunks = $this->_explode_quoted_string(',', $hdr_value);
129       }
130       else {
131         $chunks = array($hdr_value);
132       }
133
134       $hdr_value = '';
135       $line_len = 0;
136
137       foreach ($chunks as $i => $value) {
138         $value = trim($value);
139
140         //This header contains non ASCII chars and should be encoded.
141         if (preg_match('#[\x80-\xFF]{1}#', $value)) {
142           $suffix = '';
143           // Don't encode e-mail address
144           if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches)) {
145             $value = $matches[1];
146             $suffix = ' '.$matches[2];
147           }
148
149           switch ($params['head_encoding']) {
150             case 'base64':
151             // Base64 encoding has been selected.
152             $mode = 'B';
153             $encoded = base64_encode($value);
154             break;
155
156             case 'quoted-printable':
157             default:
158             // quoted-printable encoding has been selected
159             $mode = 'Q';
160             $encoded = preg_replace('/([\x2C\x3F\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value);
161             // replace spaces with _
162             $encoded = str_replace(' ', '_', $encoded);
163           }
164
165           $value = '=?' . $params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix;
166         }
167
168         // add chunk to output string by regarding the header maxlen
169         $len = strlen($value);
170         if ($i == 0 || $line_len + $len < $maxlen) {
171           $hdr_value .= ($i>0?', ':'') . $value;
172           $line_len += $len + ($i>0?2:0);
173         }
174         else {
175           $hdr_value .= ($i>0?', ':'') . "\n " . $value;
176           $line_len = $len;
177         }
178       }
179
180       $input[$hdr_name] = $hdr_value;
181     }
182
183     return $input;
184   }
185
186
187   function _explode_quoted_string($delimiter, $string)
188   {
189     $result = array();
190     $strlen = strlen($string);
191     for ($q=$p=$i=0; $i < $strlen; $i++) {
192       if ($string{$i} == "\"" && $string{$i-1} != "\\") {
193         $q = $q ? false : true;
194       }
195       else if (!$q && $string{$i} == $delimiter) {
196         $result[] = substr($string, $p, $i - $p);
197         $p = $i + 1;
198       }
199     }
200     
201     $result[] = substr($string, $p);
202     return $result;
203   }
204
205 }
206