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