Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / login / email / mimePart.php
blob800335226af2717402cb2dd793953474b97b70bc
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Richard Heyes <richard@phpguru.org> |
17 // +----------------------------------------------------------------------+
19 /**
21 * Raw mime encoding class
23 * What is it?
24 * This class enables you to manipulate and build
25 * a mime email from the ground up.
27 * Why use this instead of mime.php?
28 * mime.php is a userfriendly api to this class for
29 * people who aren't interested in the internals of
30 * mime mail. This class however allows full control
31 * over the email.
33 * Eg.
35 * // Since multipart/mixed has no real body, (the body is
36 * // the subpart), we set the body argument to blank.
38 * $params['content_type'] = 'multipart/mixed';
39 * $email = new Mail_mimePart('', $params);
41 * // Here we add a text part to the multipart we have
42 * // already. Assume $body contains plain text.
44 * $params['content_type'] = 'text/plain';
45 * $params['encoding'] = '7bit';
46 * $text = $email->addSubPart($body, $params);
48 * // Now add an attachment. Assume $attach is
49 * the contents of the attachment
51 * $params['content_type'] = 'application/zip';
52 * $params['encoding'] = 'base64';
53 * $params['disposition'] = 'attachment';
54 * $params['dfilename'] = 'example.zip';
55 * $attach =& $email->addSubPart($body, $params);
57 * // Now build the email. Note that the encode
58 * // function returns an associative array containing two
59 * // elements, body and headers. You will need to add extra
60 * // headers, (eg. Mime-Version) before sending.
62 * $email = $message->encode();
63 * $email['headers'][] = 'Mime-Version: 1.0';
66 * Further examples are available at http://www.phpguru.org
68 * TODO:
69 * - Set encode() to return the $obj->encoded if encode()
70 * has already been run. Unless a flag is passed to specifically
71 * re-build the message.
73 * @author Richard Heyes <richard@phpguru.org>
74 * @version $Revision: 1.1 $
75 * @package Mail
78 class Mail_mimePart {
80 /**
81 * The encoding type of this part
82 * @var string
84 var $_encoding;
86 /**
87 * An array of subparts
88 * @var array
90 var $_subparts;
92 /**
93 * The output of this part after being built
94 * @var string
96 var $_encoded;
98 /**
99 * Headers for this part
100 * @var array
102 var $_headers;
105 * The body of this part (not encoded)
106 * @var string
108 var $_body;
111 * Constructor.
113 * Sets up the object.
115 * @param $body - The body of the mime part if any.
116 * @param $params - An associative array of parameters:
117 * content_type - The content type for this part eg multipart/mixed
118 * encoding - The encoding to use, 7bit, 8bit, base64, or quoted-printable
119 * cid - Content ID to apply
120 * disposition - Content disposition, inline or attachment
121 * dfilename - Optional filename parameter for content disposition
122 * description - Content description
123 * charset - Character set to use
124 * @access public
126 function Mail_mimePart($body = '', $params = array())
128 if (!defined('MAIL_MIMEPART_CRLF')) {
129 define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF : "\r\n", TRUE);
132 foreach ($params as $key => $value) {
133 switch ($key) {
134 case 'content_type':
135 $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
136 break;
138 case 'encoding':
139 $this->_encoding = $value;
140 $headers['Content-Transfer-Encoding'] = $value;
141 break;
143 case 'cid':
144 $headers['Content-ID'] = '<' . $value . '>';
145 break;
147 case 'disposition':
148 $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
149 break;
151 case 'dfilename':
152 if (isset($headers['Content-Disposition'])) {
153 $headers['Content-Disposition'] .= '; filename="' . $value . '"';
154 } else {
155 $dfilename = $value;
157 break;
159 case 'description':
160 $headers['Content-Description'] = $value;
161 break;
163 case 'charset':
164 if (isset($headers['Content-Type'])) {
165 $headers['Content-Type'] .= '; charset="' . $value . '"';
166 } else {
167 $charset = $value;
169 break;
173 // Default content-type
174 if (!isset($headers['Content-Type'])) {
175 $headers['Content-Type'] = 'text/plain';
178 //Default encoding
179 if (!isset($this->_encoding)) {
180 $this->_encoding = '7bit';
183 // Assign stuff to member variables
184 $this->_encoded = array();
185 $this->_headers = $headers;
186 $this->_body = $body;
190 * encode()
192 * Encodes and returns the email. Also stores
193 * it in the encoded member variable
195 * @return An associative array containing two elements,
196 * body and headers. The headers element is itself
197 * an indexed array.
198 * @access public
200 function encode()
202 $encoded =& $this->_encoded;
204 if (!empty($this->_subparts)) {
205 srand((double)microtime()*1000000);
206 $boundary = '=_' . md5(uniqid(rand()) . microtime());
207 $this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"';
209 // Add body parts to $subparts
210 for ($i = 0; $i < count($this->_subparts); $i++) {
211 $headers = array();
212 $tmp = $this->_subparts[$i]->encode();
213 foreach ($tmp['headers'] as $key => $value) {
214 $headers[] = $key . ': ' . $value;
216 $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'];
219 $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF .
220 implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) .
221 '--' . $boundary.'--' . MAIL_MIMEPART_CRLF;
223 } else {
224 $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF;
227 // Add headers to $encoded
228 $encoded['headers'] =& $this->_headers;
230 return $encoded;
234 * &addSubPart()
236 * Adds a subpart to current mime part and returns
237 * a reference to it
239 * @param $body The body of the subpart, if any.
240 * @param $params The parameters for the subpart, same
241 * as the $params argument for constructor.
242 * @return A reference to the part you just added. It is
243 * crucial if using multipart/* in your subparts that
244 * you use =& in your script when calling this function,
245 * otherwise you will not be able to add further subparts.
246 * @access public
248 function &addSubPart($body, $params)
250 $this->_subparts[] = new Mail_mimePart($body, $params);
251 return $this->_subparts[count($this->_subparts) - 1];
255 * _getEncodedData()
257 * Returns encoded data based upon encoding passed to it
259 * @param $data The data to encode.
260 * @param $encoding The encoding type to use, 7bit, base64,
261 * or quoted-printable.
262 * @access private
264 function _getEncodedData($data, $encoding)
266 switch ($encoding) {
267 case '8bit':
268 case '7bit':
269 return $data;
270 break;
272 case 'quoted-printable':
273 return $this->_quotedPrintableEncode($data);
274 break;
276 case 'base64':
277 return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF));
278 break;
280 default:
281 return $data;
286 * quoteadPrintableEncode()
288 * Encodes data to quoted-printable standard.
290 * @param $input The data to encode
291 * @param $line_max Optional max line length. Should
292 * not be more than 76 chars
294 * @access private
296 function _quotedPrintableEncode($input , $line_max = 76)
298 $lines = preg_split("/\r?\n/", $input);
299 $eol = MAIL_MIMEPART_CRLF;
300 $escape = '=';
301 $output = '';
303 while(list(, $line) = each($lines)){
305 $linlen = strlen($line);
306 $newline = '';
308 for ($i = 0; $i < $linlen; $i++) {
309 $char = substr($line, $i, 1);
310 $dec = ord($char);
312 if (($dec == 32) AND ($i == ($linlen - 1))){ // convert space at eol only
313 $char = '=20';
315 } elseif($dec == 9) {
316 ; // Do nothing if a tab.
317 } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) {
318 $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
321 if ((strlen($newline) + strlen($char)) >= $line_max) { // MAIL_MIMEPART_CRLF is not counted
322 $output .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay
323 $newline = '';
325 $newline .= $char;
326 } // end of for
327 $output .= $newline . $eol;
329 $output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf
330 return $output;
332 } // End of class