source: subversion/branches/release-0.7/program/lib/Mail/mimePart.php @ 5991

Last change on this file since 5991 was 5991, checked in by alec, 15 months ago
  • Merge fix r5990 from trunk
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.2 KB
Line 
1<?php
2/**
3 * The Mail_mimePart class is used to create MIME E-mail messages
4 *
5 * This class enables you to manipulate and build a mime email
6 * from the ground up. The Mail_Mime class is a userfriendly api
7 * to this class for people who aren't interested in the internals
8 * of mime mail.
9 * This class however allows full control over the email.
10 *
11 * Compatible with PHP versions 4 and 5
12 *
13 * LICENSE: This LICENSE is in the BSD license style.
14 * Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org>
15 * Copyright (c) 2003-2006, PEAR <pear-group@php.net>
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or
19 * without modification, are permitted provided that the following
20 * conditions are met:
21 *
22 * - Redistributions of source code must retain the above copyright
23 *   notice, this list of conditions and the following disclaimer.
24 * - Redistributions in binary form must reproduce the above copyright
25 *   notice, this list of conditions and the following disclaimer in the
26 *   documentation and/or other materials provided with the distribution.
27 * - Neither the name of the authors, nor the names of its contributors
28 *   may be used to endorse or promote products derived from this
29 *   software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
35 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
41 * THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 * @category  Mail
44 * @package   Mail_Mime
45 * @author    Richard Heyes  <richard@phpguru.org>
46 * @author    Cipriano Groenendal <cipri@php.net>
47 * @author    Sean Coates <sean@php.net>
48 * @author    Aleksander Machniak <alec@php.net>
49 * @copyright 2003-2006 PEAR <pear-group@php.net>
50 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
51 * @version   CVS: $Id$
52 * @link      http://pear.php.net/package/Mail_mime
53 */
54
55
56/**
57 * The Mail_mimePart class is used to create MIME E-mail messages
58 *
59 * This class enables you to manipulate and build a mime email
60 * from the ground up. The Mail_Mime class is a userfriendly api
61 * to this class for people who aren't interested in the internals
62 * of mime mail.
63 * This class however allows full control over the email.
64 *
65 * @category  Mail
66 * @package   Mail_Mime
67 * @author    Richard Heyes  <richard@phpguru.org>
68 * @author    Cipriano Groenendal <cipri@php.net>
69 * @author    Sean Coates <sean@php.net>
70 * @author    Aleksander Machniak <alec@php.net>
71 * @copyright 2003-2006 PEAR <pear-group@php.net>
72 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
73 * @version   Release: @package_version@
74 * @link      http://pear.php.net/package/Mail_mime
75 */
76class Mail_mimePart
77{
78    /**
79    * The encoding type of this part
80    *
81    * @var string
82    * @access private
83    */
84    var $_encoding;
85
86    /**
87    * An array of subparts
88    *
89    * @var array
90    * @access private
91    */
92    var $_subparts;
93
94    /**
95    * The output of this part after being built
96    *
97    * @var string
98    * @access private
99    */
100    var $_encoded;
101
102    /**
103    * Headers for this part
104    *
105    * @var array
106    * @access private
107    */
108    var $_headers;
109
110    /**
111    * The body of this part (not encoded)
112    *
113    * @var string
114    * @access private
115    */
116    var $_body;
117
118    /**
119    * The location of file with body of this part (not encoded)
120    *
121    * @var string
122    * @access private
123    */
124    var $_body_file;
125
126    /**
127    * The end-of-line sequence
128    *
129    * @var string
130    * @access private
131    */
132    var $_eol = "\r\n";
133
134
135    /**
136    * Constructor.
137    *
138    * Sets up the object.
139    *
140    * @param string $body   The body of the mime part if any.
141    * @param array  $params An associative array of optional parameters:
142    *     content_type      - The content type for this part eg multipart/mixed
143    *     encoding          - The encoding to use, 7bit, 8bit,
144    *                         base64, or quoted-printable
145    *     charset           - Content character set
146    *     cid               - Content ID to apply
147    *     disposition       - Content disposition, inline or attachment
148    *     filename          - Filename parameter for content disposition
149    *     description       - Content description
150    *     name_encoding     - Encoding of the attachment name (Content-Type)
151    *                         By default filenames are encoded using RFC2231
152    *                         Here you can set RFC2047 encoding (quoted-printable
153    *                         or base64) instead
154    *     filename_encoding - Encoding of the attachment filename (Content-Disposition)
155    *                         See 'name_encoding'
156    *     headers_charset   - Charset of the headers e.g. filename, description.
157    *                         If not set, 'charset' will be used
158    *     eol               - End of line sequence. Default: "\r\n"
159    *     body_file         - Location of file with part's body (instead of $body)
160    *
161    * @access public
162    */
163    function Mail_mimePart($body = '', $params = array())
164    {
165        if (!empty($params['eol'])) {
166            $this->_eol = $params['eol'];
167        } else if (defined('MAIL_MIMEPART_CRLF')) { // backward-copat.
168            $this->_eol = MAIL_MIMEPART_CRLF;
169        }
170
171        foreach ($params as $key => $value) {
172            switch ($key) {
173            case 'encoding':
174                $this->_encoding = $value;
175                $headers['Content-Transfer-Encoding'] = $value;
176                break;
177
178            case 'cid':
179                $headers['Content-ID'] = '<' . $value . '>';
180                break;
181
182            case 'location':
183                $headers['Content-Location'] = $value;
184                break;
185
186            case 'body_file':
187                $this->_body_file = $value;
188                break;
189
190            // for backward compatibility
191            case 'dfilename':
192                $params['filename'] = $value;
193                break;
194            }
195        }
196
197        // Default content-type
198        if (empty($params['content_type'])) {
199            $params['content_type'] = 'text/plain';
200        }
201
202        // Content-Type
203        $headers['Content-Type'] = $params['content_type'];
204        if (!empty($params['charset'])) {
205            $charset = "charset={$params['charset']}";
206            // place charset parameter in the same line, if possible
207            if ((strlen($headers['Content-Type']) + strlen($charset) + 16) <= 76) {
208                $headers['Content-Type'] .= '; ';
209            } else {
210                $headers['Content-Type'] .= ';' . $this->_eol . ' ';
211            }
212            $headers['Content-Type'] .= $charset;
213
214            // Default headers charset
215            if (!isset($params['headers_charset'])) {
216                $params['headers_charset'] = $params['charset'];
217            }
218        }
219        if (!empty($params['filename'])) {
220            $headers['Content-Type'] .= ';' . $this->_eol;
221            $headers['Content-Type'] .= $this->_buildHeaderParam(
222                'name', $params['filename'],
223                !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII',
224                !empty($params['language']) ? $params['language'] : null,
225                !empty($params['name_encoding']) ? $params['name_encoding'] : null
226            );
227        }
228
229        // Content-Disposition
230        if (!empty($params['disposition'])) {
231            $headers['Content-Disposition'] = $params['disposition'];
232            if (!empty($params['filename'])) {
233                $headers['Content-Disposition'] .= ';' . $this->_eol;
234                $headers['Content-Disposition'] .= $this->_buildHeaderParam(
235                    'filename', $params['filename'],
236                    !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII',
237                    !empty($params['language']) ? $params['language'] : null,
238                    !empty($params['filename_encoding']) ? $params['filename_encoding'] : null
239                );
240            }
241        }
242
243        if (!empty($params['description'])) {
244            $headers['Content-Description'] = $this->encodeHeader(
245                'Content-Description', $params['description'],
246                !empty($params['headers_charset']) ? $params['headers_charset'] : 'US-ASCII',
247                !empty($params['name_encoding']) ? $params['name_encoding'] : 'quoted-printable',
248                $this->_eol
249            );
250        }
251
252        // Default encoding
253        if (!isset($this->_encoding)) {
254            $this->_encoding = '7bit';
255        }
256
257        // Assign stuff to member variables
258        $this->_encoded  = array();
259        $this->_headers  = $headers;
260        $this->_body     = $body;
261    }
262
263    /**
264     * Encodes and returns the email. Also stores
265     * it in the encoded member variable
266     *
267     * @param string $boundary Pre-defined boundary string
268     *
269     * @return An associative array containing two elements,
270     *         body and headers. The headers element is itself
271     *         an indexed array. On error returns PEAR error object.
272     * @access public
273     */
274    function encode($boundary=null)
275    {
276        $encoded =& $this->_encoded;
277
278        if (count($this->_subparts)) {
279            $boundary = $boundary ? $boundary : '=_' . md5(rand() . microtime());
280            $eol = $this->_eol;
281
282            $this->_headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
283
284            $encoded['body'] = ''; 
285
286            for ($i = 0; $i < count($this->_subparts); $i++) {
287                $encoded['body'] .= '--' . $boundary . $eol;
288                $tmp = $this->_subparts[$i]->encode();
289                if (PEAR::isError($tmp)) {
290                    return $tmp;
291                }
292                foreach ($tmp['headers'] as $key => $value) {
293                    $encoded['body'] .= $key . ': ' . $value . $eol;
294                }
295                $encoded['body'] .= $eol . $tmp['body'] . $eol;
296            }
297
298            $encoded['body'] .= '--' . $boundary . '--' . $eol;
299
300        } else if ($this->_body) {
301            $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding);
302        } else if ($this->_body_file) {
303            // Temporarily reset magic_quotes_runtime for file reads and writes
304            if ($magic_quote_setting = get_magic_quotes_runtime()) {
305                @ini_set('magic_quotes_runtime', 0);
306            }
307            $body = $this->_getEncodedDataFromFile($this->_body_file, $this->_encoding);
308            if ($magic_quote_setting) {
309                @ini_set('magic_quotes_runtime', $magic_quote_setting);
310            }
311
312            if (PEAR::isError($body)) {
313                return $body;
314            }
315            $encoded['body'] = $body;
316        } else {
317            $encoded['body'] = '';
318        }
319
320        // Add headers to $encoded
321        $encoded['headers'] =& $this->_headers;
322
323        return $encoded;
324    }
325
326    /**
327     * Encodes and saves the email into file. File must exist.
328     * Data will be appended to the file.
329     *
330     * @param string  $filename  Output file location
331     * @param string  $boundary  Pre-defined boundary string
332     * @param boolean $skip_head True if you don't want to save headers
333     *
334     * @return array An associative array containing message headers
335     *               or PEAR error object
336     * @access public
337     * @since 1.6.0
338     */
339    function encodeToFile($filename, $boundary=null, $skip_head=false)
340    {
341        if (file_exists($filename) && !is_writable($filename)) {
342            $err = PEAR::raiseError('File is not writeable: ' . $filename);
343            return $err;
344        }
345
346        if (!($fh = fopen($filename, 'ab'))) {
347            $err = PEAR::raiseError('Unable to open file: ' . $filename);
348            return $err;
349        }
350
351        // Temporarily reset magic_quotes_runtime for file reads and writes
352        if ($magic_quote_setting = get_magic_quotes_runtime()) {
353            @ini_set('magic_quotes_runtime', 0);
354        }
355
356        $res = $this->_encodePartToFile($fh, $boundary, $skip_head);
357
358        fclose($fh);
359
360        if ($magic_quote_setting) {
361            @ini_set('magic_quotes_runtime', $magic_quote_setting);
362        }
363
364        return PEAR::isError($res) ? $res : $this->_headers;
365    }
366
367    /**
368     * Encodes given email part into file
369     *
370     * @param string  $fh        Output file handle
371     * @param string  $boundary  Pre-defined boundary string
372     * @param boolean $skip_head True if you don't want to save headers
373     *
374     * @return array True on sucess or PEAR error object
375     * @access private
376     */
377    function _encodePartToFile($fh, $boundary=null, $skip_head=false)
378    {
379        $eol = $this->_eol;
380
381        if (count($this->_subparts)) {
382            $boundary = $boundary ? $boundary : '=_' . md5(rand() . microtime());
383            $this->_headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
384        }
385
386        if (!$skip_head) {
387            foreach ($this->_headers as $key => $value) {
388                fwrite($fh, $key . ': ' . $value . $eol);
389            }
390            $f_eol = $eol;
391        } else {
392            $f_eol = '';
393        }
394
395        if (count($this->_subparts)) {
396            for ($i = 0; $i < count($this->_subparts); $i++) {
397                fwrite($fh, $f_eol . '--' . $boundary . $eol);
398                $res = $this->_subparts[$i]->_encodePartToFile($fh);
399                if (PEAR::isError($res)) {
400                    return $res;
401                }
402                $f_eol = $eol;
403            }
404
405            fwrite($fh, $eol . '--' . $boundary . '--' . $eol);
406
407        } else if ($this->_body) {
408            fwrite($fh, $f_eol . $this->_getEncodedData($this->_body, $this->_encoding));
409        } else if ($this->_body_file) {
410            fwrite($fh, $f_eol);
411            $res = $this->_getEncodedDataFromFile(
412                $this->_body_file, $this->_encoding, $fh
413            );
414            if (PEAR::isError($res)) {
415                return $res;
416            }
417        }
418
419        return true;
420    }
421
422    /**
423     * Adds a subpart to current mime part and returns
424     * a reference to it
425     *
426     * @param string $body   The body of the subpart, if any.
427     * @param array  $params The parameters for the subpart, same
428     *                       as the $params argument for constructor.
429     *
430     * @return Mail_mimePart A reference to the part you just added. It is
431     *                       crucial if using multipart/* in your subparts that
432     *                       you use =& in your script when calling this function,
433     *                       otherwise you will not be able to add further subparts.
434     * @access public
435     */
436    function &addSubpart($body, $params)
437    {
438        $this->_subparts[] = new Mail_mimePart($body, $params);
439        return $this->_subparts[count($this->_subparts) - 1];
440    }
441
442    /**
443     * Returns encoded data based upon encoding passed to it
444     *
445     * @param string $data     The data to encode.
446     * @param string $encoding The encoding type to use, 7bit, base64,
447     *                         or quoted-printable.
448     *
449     * @return string
450     * @access private
451     */
452    function _getEncodedData($data, $encoding)
453    {
454        switch ($encoding) {
455        case 'quoted-printable':
456            return $this->_quotedPrintableEncode($data);
457            break;
458
459        case 'base64':
460            return rtrim(chunk_split(base64_encode($data), 76, $this->_eol));
461            break;
462
463        case '8bit':
464        case '7bit':
465        default:
466            return $data;
467        }
468    }
469
470    /**
471     * Returns encoded data based upon encoding passed to it
472     *
473     * @param string   $filename Data file location
474     * @param string   $encoding The encoding type to use, 7bit, base64,
475     *                           or quoted-printable.
476     * @param resource $fh       Output file handle. If set, data will be
477     *                           stored into it instead of returning it
478     *
479     * @return string Encoded data or PEAR error object
480     * @access private
481     */
482    function _getEncodedDataFromFile($filename, $encoding, $fh=null)
483    {
484        if (!is_readable($filename)) {
485            $err = PEAR::raiseError('Unable to read file: ' . $filename);
486            return $err;
487        }
488
489        if (!($fd = fopen($filename, 'rb'))) {
490            $err = PEAR::raiseError('Could not open file: ' . $filename);
491            return $err;
492        }
493
494        $data = '';
495
496        switch ($encoding) {
497        case 'quoted-printable':
498            while (!feof($fd)) {
499                $buffer = $this->_quotedPrintableEncode(fgets($fd));
500                if ($fh) {
501                    fwrite($fh, $buffer);
502                } else {
503                    $data .= $buffer;
504                }
505            }
506            break;
507
508        case 'base64':
509            while (!feof($fd)) {
510                // Should read in a multiple of 57 bytes so that
511                // the output is 76 bytes per line. Don't use big chunks
512                // because base64 encoding is memory expensive
513                $buffer = fread($fd, 57 * 9198); // ca. 0.5 MB
514                $buffer = base64_encode($buffer);
515                $buffer = chunk_split($buffer, 76, $this->_eol);
516                if (feof($fd)) {
517                    $buffer = rtrim($buffer);
518                }
519
520                if ($fh) {
521                    fwrite($fh, $buffer);
522                } else {
523                    $data .= $buffer;
524                }
525            }
526            break;
527
528        case '8bit':
529        case '7bit':
530        default:
531            while (!feof($fd)) {
532                $buffer = fread($fd, 1048576); // 1 MB
533                if ($fh) {
534                    fwrite($fh, $buffer);
535                } else {
536                    $data .= $buffer;
537                }
538            }
539        }
540
541        fclose($fd);
542
543        if (!$fh) {
544            return $data;
545        }
546    }
547
548    /**
549     * Encodes data to quoted-printable standard.
550     *
551     * @param string $input    The data to encode
552     * @param int    $line_max Optional max line length. Should
553     *                         not be more than 76 chars
554     *
555     * @return string Encoded data
556     *
557     * @access private
558     */
559    function _quotedPrintableEncode($input , $line_max = 76)
560    {
561        $eol = $this->_eol;
562        /*
563        // imap_8bit() is extremely fast, but doesn't handle properly some characters
564        if (function_exists('imap_8bit') && $line_max == 76) {
565            $input = preg_replace('/\r?\n/', "\r\n", $input);
566            $input = imap_8bit($input);
567            if ($eol != "\r\n") {
568                $input = str_replace("\r\n", $eol, $input);
569            }
570            return $input;
571        }
572        */
573        $lines  = preg_split("/\r?\n/", $input);
574        $escape = '=';
575        $output = '';
576
577        while (list($idx, $line) = each($lines)) {
578            $newline = '';
579            $i = 0;
580
581            while (isset($line[$i])) {
582                $char = $line[$i];
583                $dec  = ord($char);
584                $i++;
585
586                if (($dec == 32) && (!isset($line[$i]))) {
587                    // convert space at eol only
588                    $char = '=20';
589                } elseif ($dec == 9 && isset($line[$i])) {
590                    ; // Do nothing if a TAB is not on eol
591                } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) {
592                    $char = $escape . sprintf('%02X', $dec);
593                } elseif (($dec == 46) && (($newline == '')
594                    || ((strlen($newline) + strlen("=2E")) >= $line_max))
595                ) {
596                    // Bug #9722: convert full-stop at bol,
597                    // some Windows servers need this, won't break anything (cipri)
598                    // Bug #11731: full-stop at bol also needs to be encoded
599                    // if this line would push us over the line_max limit.
600                    $char = '=2E';
601                }
602
603                // Note, when changing this line, also change the ($dec == 46)
604                // check line, as it mimics this line due to Bug #11731
605                // EOL is not counted
606                if ((strlen($newline) + strlen($char)) >= $line_max) {
607                    // soft line break; " =\r\n" is okay
608                    $output  .= $newline . $escape . $eol;
609                    $newline  = '';
610                }
611                $newline .= $char;
612            } // end of for
613            $output .= $newline . $eol;
614            unset($lines[$idx]);
615        }
616        // Don't want last crlf
617        $output = substr($output, 0, -1 * strlen($eol));
618        return $output;
619    }
620
621    /**
622     * Encodes the paramater of a header.
623     *
624     * @param string $name      The name of the header-parameter
625     * @param string $value     The value of the paramter
626     * @param string $charset   The characterset of $value
627     * @param string $language  The language used in $value
628     * @param string $encoding  Parameter encoding. If not set, parameter value
629     *                          is encoded according to RFC2231
630     * @param int    $maxLength The maximum length of a line. Defauls to 75
631     *
632     * @return string
633     *
634     * @access private
635     */
636    function _buildHeaderParam($name, $value, $charset=null, $language=null,
637        $encoding=null, $maxLength=75
638    ) {
639        // RFC 2045:
640        // value needs encoding if contains non-ASCII chars or is longer than 78 chars
641        if (!preg_match('#[^\x20-\x7E]#', $value)) {
642            $token_regexp = '#([^\x21\x23-\x27\x2A\x2B\x2D'
643                . '\x2E\x30-\x39\x41-\x5A\x5E-\x7E])#';
644            if (!preg_match($token_regexp, $value)) {
645                // token
646                if (strlen($name) + strlen($value) + 3 <= $maxLength) {
647                    return " {$name}={$value}";
648                }
649            } else {
650                // quoted-string
651                $quoted = addcslashes($value, '\\"');
652                if (strlen($name) + strlen($quoted) + 5 <= $maxLength) {
653                    return " {$name}=\"{$quoted}\"";
654                }
655            }
656        }
657
658        // RFC2047: use quoted-printable/base64 encoding
659        if ($encoding == 'quoted-printable' || $encoding == 'base64') {
660            return $this->_buildRFC2047Param($name, $value, $charset, $encoding);
661        }
662
663        // RFC2231:
664        $encValue = preg_replace_callback(
665            '/([^\x21\x23\x24\x26\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E])/',
666            array($this, '_encodeReplaceCallback'), $value
667        );
668        $value = "$charset'$language'$encValue";
669
670        $header = " {$name}*={$value}";
671        if (strlen($header) <= $maxLength) {
672            return $header;
673        }
674
675        $preLength = strlen(" {$name}*0*=");
676        $maxLength = max(16, $maxLength - $preLength - 3);
677        $maxLengthReg = "|(.{0,$maxLength}[^\%][^\%])|";
678
679        $headers = array();
680        $headCount = 0;
681        while ($value) {
682            $matches = array();
683            $found = preg_match($maxLengthReg, $value, $matches);
684            if ($found) {
685                $headers[] = " {$name}*{$headCount}*={$matches[0]}";
686                $value = substr($value, strlen($matches[0]));
687            } else {
688                $headers[] = " {$name}*{$headCount}*={$value}";
689                $value = '';
690            }
691            $headCount++;
692        }
693
694        $headers = implode(';' . $this->_eol, $headers);
695        return $headers;
696    }
697
698    /**
699     * Encodes header parameter as per RFC2047 if needed
700     *
701     * @param string $name      The parameter name
702     * @param string $value     The parameter value
703     * @param string $charset   The parameter charset
704     * @param string $encoding  Encoding type (quoted-printable or base64)
705     * @param int    $maxLength Encoded parameter max length. Default: 76
706     *
707     * @return string Parameter line
708     * @access private
709     */
710    function _buildRFC2047Param($name, $value, $charset,
711        $encoding='quoted-printable', $maxLength=76
712    ) {
713        // WARNING: RFC 2047 says: "An 'encoded-word' MUST NOT be used in
714        // parameter of a MIME Content-Type or Content-Disposition field",
715        // but... it's supported by many clients/servers
716        $quoted = '';
717
718        if ($encoding == 'base64') {
719            $value = base64_encode($value);
720            $prefix = '=?' . $charset . '?B?';
721            $suffix = '?=';
722
723            // 2 x SPACE, 2 x '"', '=', ';'
724            $add_len = strlen($prefix . $suffix) + strlen($name) + 6;
725            $len = $add_len + strlen($value);
726
727            while ($len > $maxLength) { 
728                // We can cut base64-encoded string every 4 characters
729                $real_len = floor(($maxLength - $add_len) / 4) * 4;
730                $_quote = substr($value, 0, $real_len);
731                $value = substr($value, $real_len);
732
733                $quoted .= $prefix . $_quote . $suffix . $this->_eol . ' ';
734                $add_len = strlen($prefix . $suffix) + 4; // 2 x SPACE, '"', ';'
735                $len = strlen($value) + $add_len;
736            }
737            $quoted .= $prefix . $value . $suffix;
738
739        } else {
740            // quoted-printable
741            $value = $this->encodeQP($value);
742            $prefix = '=?' . $charset . '?Q?';
743            $suffix = '?=';
744
745            // 2 x SPACE, 2 x '"', '=', ';'
746            $add_len = strlen($prefix . $suffix) + strlen($name) + 6;
747            $len = $add_len + strlen($value);
748
749            while ($len > $maxLength) {
750                $length = $maxLength - $add_len;
751                // don't break any encoded letters
752                if (preg_match("/^(.{0,$length}[^\=][^\=])/", $value, $matches)) {
753                    $_quote = $matches[1];
754                }
755
756                $quoted .= $prefix . $_quote . $suffix . $this->_eol . ' ';
757                $value = substr($value, strlen($_quote));
758                $add_len = strlen($prefix . $suffix) + 4; // 2 x SPACE, '"', ';'
759                $len = strlen($value) + $add_len;
760            }
761
762            $quoted .= $prefix . $value . $suffix;
763        }
764
765        return " {$name}=\"{$quoted}\"";
766    }
767
768    /**
769     * Encodes a header as per RFC2047
770     *
771     * @param string $name     The header name
772     * @param string $value    The header data to encode
773     * @param string $charset  Character set name
774     * @param string $encoding Encoding name (base64 or quoted-printable)
775     * @param string $eol      End-of-line sequence. Default: "\r\n"
776     *
777     * @return string          Encoded header data (without a name)
778     * @access public
779     * @since 1.6.1
780     */
781    function encodeHeader($name, $value, $charset='ISO-8859-1',
782        $encoding='quoted-printable', $eol="\r\n"
783    ) {
784        // Structured headers
785        $comma_headers = array(
786            'from', 'to', 'cc', 'bcc', 'sender', 'reply-to',
787            'resent-from', 'resent-to', 'resent-cc', 'resent-bcc',
788            'resent-sender', 'resent-reply-to',
789            'return-receipt-to', 'disposition-notification-to',
790        );
791        $other_headers = array(
792            'references', 'in-reply-to', 'message-id', 'resent-message-id',
793        );
794
795        $name = strtolower($name);
796
797        if (in_array($name, $comma_headers)) {
798            $separator = ',';
799        } else if (in_array($name, $other_headers)) {
800            $separator = ' ';
801        }
802
803        if (!$charset) {
804            $charset = 'ISO-8859-1';
805        }
806
807        // Structured header (make sure addr-spec inside is not encoded)
808        if (!empty($separator)) {
809            // Simple e-mail address regexp
810            $email_regexp = '(\S+|("[^\r\n"]+"))@\S+';
811
812            $parts = Mail_mimePart::_explodeQuotedString($separator, $value);
813            $value = '';
814
815            foreach ($parts as $part) {
816                $part = preg_replace('/\r?\n[\s\t]*/', $eol . ' ', $part);
817                $part = trim($part);
818
819                if (!$part) {
820                    continue;
821                }
822                if ($value) {
823                    $value .= $separator==',' ? $separator.' ' : ' ';
824                } else {
825                    $value = $name . ': ';
826                }
827
828                // let's find phrase (name) and/or addr-spec
829                if (preg_match('/^<' . $email_regexp . '>$/', $part)) {
830                    $value .= $part;
831                } else if (preg_match('/^' . $email_regexp . '$/', $part)) {
832                    // address without brackets and without name
833                    $value .= $part;
834                } else if (preg_match('/<*' . $email_regexp . '>*$/', $part, $matches)) {
835                    // address with name (handle name)
836                    $address = $matches[0];
837                    $word = str_replace($address, '', $part);
838                    $word = trim($word);
839                    // check if phrase requires quoting
840                    if ($word) {
841                        // non-ASCII: require encoding
842                        if (preg_match('#([\x80-\xFF]){1}#', $word)) {
843                            if ($word[0] == '"' && $word[strlen($word)-1] == '"') {
844                                // de-quote quoted-string, encoding changes
845                                // string to atom
846                                $search = array("\\\"", "\\\\");
847                                $replace = array("\"", "\\");
848                                $word = str_replace($search, $replace, $word);
849                                $word = substr($word, 1, -1);
850                            }
851                            // find length of last line
852                            if (($pos = strrpos($value, $eol)) !== false) {
853                                $last_len = strlen($value) - $pos;
854                            } else {
855                                $last_len = strlen($value);
856                            }
857                            $word = Mail_mimePart::encodeHeaderValue(
858                                $word, $charset, $encoding, $last_len, $eol
859                            );
860                        } else if (($word[0] != '"' || $word[strlen($word)-1] != '"')
861                            && preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $word)
862                        ) {
863                            // ASCII: quote string if needed
864                            $word = '"'.addcslashes($word, '\\"').'"';
865                        }
866                    }
867                    $value .= $word.' '.$address;
868                } else {
869                    // addr-spec not found, don't encode (?)
870                    $value .= $part;
871                }
872
873                // RFC2822 recommends 78 characters limit, use 76 from RFC2047
874                $value = wordwrap($value, 76, $eol . ' ');
875            }
876
877            // remove header name prefix (there could be EOL too)
878            $value = preg_replace(
879                '/^'.$name.':('.preg_quote($eol, '/').')* /', '', $value
880            );
881
882        } else {
883            // Unstructured header
884            // non-ASCII: require encoding
885            if (preg_match('#([\x80-\xFF]){1}#', $value)) {
886                if ($value[0] == '"' && $value[strlen($value)-1] == '"') {
887                    // de-quote quoted-string, encoding changes
888                    // string to atom
889                    $search = array("\\\"", "\\\\");
890                    $replace = array("\"", "\\");
891                    $value = str_replace($search, $replace, $value);
892                    $value = substr($value, 1, -1);
893                }
894                $value = Mail_mimePart::encodeHeaderValue(
895                    $value, $charset, $encoding, strlen($name) + 2, $eol
896                );
897            } else if (strlen($name.': '.$value) > 78) {
898                // ASCII: check if header line isn't too long and use folding
899                $value = preg_replace('/\r?\n[\s\t]*/', $eol . ' ', $value);
900                $tmp = wordwrap($name.': '.$value, 78, $eol . ' ');
901                $value = preg_replace('/^'.$name.':\s*/', '', $tmp);
902                // hard limit 998 (RFC2822)
903                $value = wordwrap($value, 998, $eol . ' ', true);
904            }
905        }
906
907        return $value;
908    }
909
910    /**
911     * Explode quoted string
912     *
913     * @param string $delimiter Delimiter expression string for preg_match()
914     * @param string $string    Input string
915     *
916     * @return array            String tokens array
917     * @access private
918     */
919    function _explodeQuotedString($delimiter, $string)
920    {
921        $result = array();
922        $strlen = strlen($string);
923
924        for ($q=$p=$i=0; $i < $strlen; $i++) {
925            if ($string[$i] == "\""
926                && (empty($string[$i-1]) || $string[$i-1] != "\\")
927            ) {
928                $q = $q ? false : true;
929            } else if (!$q && preg_match("/$delimiter/", $string[$i])) {
930                $result[] = substr($string, $p, $i - $p);
931                $p = $i + 1;
932            }
933        }
934
935        $result[] = substr($string, $p);
936        return $result;
937    }
938
939    /**
940     * Encodes a header value as per RFC2047
941     *
942     * @param string $value      The header data to encode
943     * @param string $charset    Character set name
944     * @param string $encoding   Encoding name (base64 or quoted-printable)
945     * @param int    $prefix_len Prefix length. Default: 0
946     * @param string $eol        End-of-line sequence. Default: "\r\n"
947     *
948     * @return string            Encoded header data
949     * @access public
950     * @since 1.6.1
951     */
952    function encodeHeaderValue($value, $charset, $encoding, $prefix_len=0, $eol="\r\n")
953    {
954        // #17311: Use multibyte aware method (requires mbstring extension)
955        if ($result = Mail_mimePart::encodeMB($value, $charset, $encoding, $prefix_len, $eol)) {
956            return $result;
957        }
958
959        // Generate the header using the specified params and dynamicly
960        // determine the maximum length of such strings.
961        // 75 is the value specified in the RFC.
962        $encoding = $encoding == 'base64' ? 'B' : 'Q';
963        $prefix = '=?' . $charset . '?' . $encoding .'?';
964        $suffix = '?=';
965        $maxLength = 75 - strlen($prefix . $suffix);
966        $maxLength1stLine = $maxLength - $prefix_len;
967
968        if ($encoding == 'B') {
969            // Base64 encode the entire string
970            $value = base64_encode($value);
971
972            // We can cut base64 every 4 characters, so the real max
973            // we can get must be rounded down.
974            $maxLength = $maxLength - ($maxLength % 4);
975            $maxLength1stLine = $maxLength1stLine - ($maxLength1stLine % 4);
976
977            $cutpoint = $maxLength1stLine;
978            $output = '';
979
980            while ($value) {
981                // Split translated string at every $maxLength
982                $part = substr($value, 0, $cutpoint);
983                $value = substr($value, $cutpoint);
984                $cutpoint = $maxLength;
985                // RFC 2047 specifies that any split header should
986                // be seperated by a CRLF SPACE.
987                if ($output) {
988                    $output .= $eol . ' ';
989                }
990                $output .= $prefix . $part . $suffix;
991            }
992            $value = $output;
993        } else {
994            // quoted-printable encoding has been selected
995            $value = Mail_mimePart::encodeQP($value);
996
997            // This regexp will break QP-encoded text at every $maxLength
998            // but will not break any encoded letters.
999            $reg1st = "|(.{0,$maxLength1stLine}[^\=][^\=])|";
1000            $reg2nd = "|(.{0,$maxLength}[^\=][^\=])|";
1001
1002            if (strlen($value) > $maxLength1stLine) {
1003                // Begin with the regexp for the first line.
1004                $reg = $reg1st;
1005                $output = '';
1006                while ($value) {
1007                    // Split translated string at every $maxLength
1008                    // But make sure not to break any translated chars.
1009                    $found = preg_match($reg, $value, $matches);
1010
1011                    // After this first line, we need to use a different
1012                    // regexp for the first line.
1013                    $reg = $reg2nd;
1014
1015                    // Save the found part and encapsulate it in the
1016                    // prefix & suffix. Then remove the part from the
1017                    // $value_out variable.
1018                    if ($found) {
1019                        $part = $matches[0];
1020                        $len = strlen($matches[0]);
1021                        $value = substr($value, $len);
1022                    } else {
1023                        $part = $value;
1024                        $value = '';
1025                    }
1026
1027                    // RFC 2047 specifies that any split header should
1028                    // be seperated by a CRLF SPACE
1029                    if ($output) {
1030                        $output .= $eol . ' ';
1031                    }
1032                    $output .= $prefix . $part . $suffix;
1033                }
1034                $value = $output;
1035            } else {
1036                $value = $prefix . $value . $suffix;
1037            }
1038        }
1039
1040        return $value;
1041    }
1042
1043    /**
1044     * Encodes the given string using quoted-printable
1045     *
1046     * @param string $str String to encode
1047     *
1048     * @return string     Encoded string
1049     * @access public
1050     * @since 1.6.0
1051     */
1052    function encodeQP($str)
1053    {
1054        // Bug #17226 RFC 2047 restricts some characters
1055        // if the word is inside a phrase, permitted chars are only:
1056        // ASCII letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
1057
1058        // "=",  "_",  "?" must be encoded
1059        $regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';
1060        $str = preg_replace_callback(
1061            $regexp, array('Mail_mimePart', '_qpReplaceCallback'), $str
1062        );
1063
1064        return str_replace(' ', '_', $str);
1065    }
1066
1067    /**
1068     * Encodes the given string using base64 or quoted-printable.
1069     * This method makes sure that encoded-word represents an integral
1070     * number of characters as per RFC2047.
1071     *
1072     * @param string $str        String to encode
1073     * @param string $charset    Character set name
1074     * @param string $encoding   Encoding name (base64 or quoted-printable)
1075     * @param int    $prefix_len Prefix length. Default: 0
1076     * @param string $eol        End-of-line sequence. Default: "\r\n"
1077     *
1078     * @return string     Encoded string
1079     * @access public
1080     * @since 1.8.0
1081     */
1082    function encodeMB($str, $charset, $encoding, $prefix_len=0, $eol="\r\n")
1083    {
1084        if (!function_exists('mb_substr') || !function_exists('mb_strlen')) {
1085            return;
1086        }
1087
1088        $encoding = $encoding == 'base64' ? 'B' : 'Q';
1089        // 75 is the value specified in the RFC
1090        $prefix = '=?' . $charset . '?'.$encoding.'?';
1091        $suffix = '?=';
1092        $maxLength = 75 - strlen($prefix . $suffix);
1093
1094        // A multi-octet character may not be split across adjacent encoded-words
1095        // So, we'll loop over each character
1096        // mb_stlen() with wrong charset will generate a warning here and return null
1097        $length      = mb_strlen($str, $charset);
1098        $result      = '';
1099        $line_length = $prefix_len;
1100
1101        if ($encoding == 'B') {
1102            // base64
1103            $start = 0;
1104            $prev  = '';
1105
1106            for ($i=1; $i<=$length; $i++) {
1107                // See #17311
1108                $chunk = mb_substr($str, $start, $i-$start, $charset);
1109                $chunk = base64_encode($chunk);
1110                $chunk_len = strlen($chunk);
1111
1112                if ($line_length + $chunk_len == $maxLength || $i == $length) {
1113                    if ($result) {
1114                        $result .= "\n";
1115                    }
1116                    $result .= $chunk;
1117                    $line_length = 0;
1118                    $start = $i;
1119                } else if ($line_length + $chunk_len > $maxLength) {
1120                    if ($result) {
1121                        $result .= "\n";
1122                    }
1123                    if ($prev) {
1124                        $result .= $prev;
1125                    }
1126                    $line_length = 0;
1127                    $start = $i - 1;
1128                } else {
1129                    $prev = $chunk;
1130                }
1131            }
1132        } else {
1133            // quoted-printable
1134            // see encodeQP()
1135            $regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';
1136
1137            for ($i=0; $i<=$length; $i++) {
1138                $char = mb_substr($str, $i, 1, $charset);
1139                // RFC recommends underline (instead of =20) in place of the space
1140                // that's one of the reasons why we're not using iconv_mime_encode()
1141                if ($char == ' ') {
1142                    $char = '_';
1143                    $char_len = 1;
1144                } else {
1145                    $char = preg_replace_callback(
1146                        $regexp, array('Mail_mimePart', '_qpReplaceCallback'), $char
1147                    );
1148                    $char_len = strlen($char);
1149                }
1150
1151                if ($line_length + $char_len > $maxLength) {
1152                    if ($result) {
1153                        $result .= "\n";
1154                    }
1155                    $line_length = 0;
1156                }
1157
1158                $result      .= $char;
1159                $line_length += $char_len;
1160            }
1161        }
1162
1163        if ($result) {
1164            $result = $prefix
1165                .str_replace("\n", $suffix.$eol.' '.$prefix, $result).$suffix;
1166        }
1167
1168        return $result;
1169    }
1170
1171    /**
1172     * Callback function to replace extended characters (\x80-xFF) with their
1173     * ASCII values (RFC2047: quoted-printable)
1174     *
1175     * @param array $matches Preg_replace's matches array
1176     *
1177     * @return string        Encoded character string
1178     * @access private
1179     */
1180    function _qpReplaceCallback($matches)
1181    {
1182        return sprintf('=%02X', ord($matches[1]));
1183    }
1184
1185    /**
1186     * Callback function to replace extended characters (\x80-xFF) with their
1187     * ASCII values (RFC2231)
1188     *
1189     * @param array $matches Preg_replace's matches array
1190     *
1191     * @return string        Encoded character string
1192     * @access private
1193     */
1194    function _encodeReplaceCallback($matches)
1195    {
1196        return sprintf('%%%02X', ord($matches[1]));
1197    }
1198
1199} // End of class
Note: See TracBrowser for help on using the repository browser.