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