source: contrib/z-push/include/mimeDecode.php @ 4898

Revision 4898, 28.5 KB checked in by thiagoaos, 13 years ago (diff)

Ticket #2180 - Adicionado código fonte completo do zpush

  • Property svn:executable set to *
RevLine 
[3637]1<?php
2/**
3 * The Mail_mimeDecode class is used to decode mail/mime messages
4 *
5 * This class will parse a raw mime email and return
6 * the structure. Returned structure is similar to
7 * that returned by imap_fetchstructure().
8 *
9 *  +----------------------------- IMPORTANT ------------------------------+
10 *  | Usage of this class compared to native php extensions such as        |
11 *  | mailparse or imap, is slow and may be feature deficient. If available|
12 *  | you are STRONGLY recommended to use the php extensions.              |
13 *  +----------------------------------------------------------------------+
14 *
15 * Compatible with PHP versions 4 and 5
16 *
17 * LICENSE: This LICENSE is in the BSD license style.
18 * Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org>
19 * Copyright (c) 2003-2006, PEAR <pear-group@php.net>
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or
23 * without modification, are permitted provided that the following
24 * conditions are met:
25 *
26 * - Redistributions of source code must retain the above copyright
27 *   notice, this list of conditions and the following disclaimer.
28 * - Redistributions in binary form must reproduce the above copyright
29 *   notice, this list of conditions and the following disclaimer in the
30 *   documentation and/or other materials provided with the distribution.
[3754]31 * - Neither the name of the authors, nor the names of its contributors
32 *   may be used to endorse or promote products derived from this
[3637]33 *   software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
39 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
45 * THE POSSIBILITY OF SUCH DAMAGE.
46 *
47 * @category   Mail
48 * @package    Mail_Mime
49 * @author     Richard Heyes  <richard@phpguru.org>
50 * @author     George Schlossnagle <george@omniti.com>
51 * @author     Cipriano Groenendal <cipri@php.net>
52 * @author     Sean Coates <sean@php.net>
53 * @copyright  2003-2006 PEAR <pear-group@php.net>
54 * @license    http://www.opensource.org/licenses/bsd-license.php BSD License
55 * @version    CVS: $Id: mimeDecode.php 288500 2009-09-21 05:32:32Z alan_k $
56 * @link       http://pear.php.net/package/Mail_mime
57 */
58
59
60/**
61 * Z-Push changes
62 *
63 * removed PEAR dependency by implementing own raiseError()
64 * removed deprecated referencing: $obj = &new Mail_mimeDecode($body); in _decode()
65 * implemented automated decoding of strings from mail charset
[3754]66 *
67 * Reference implementation used:
[3637]68 * http://download.pear.php.net/package/Mail_mimeDecode-1.5.1.tgz
[3754]69 *
[3637]70 */
71
72/**
73 * require PEAR
74 *
75 * This package depends on PEAR to raise errors.
76 */
77//require_once 'PEAR.php';
78
79/**
80 * The Mail_mimeDecode class is used to decode mail/mime messages
81 *
82 * This class will parse a raw mime email and return the structure.
83 * Returned structure is similar to that returned by imap_fetchstructure().
84 *
85 *  +----------------------------- IMPORTANT ------------------------------+
86 *  | Usage of this class compared to native php extensions such as        |
87 *  | mailparse or imap, is slow and may be feature deficient. If available|
88 *  | you are STRONGLY recommended to use the php extensions.              |
89 *  +----------------------------------------------------------------------+
90 *
91 * @category   Mail
92 * @package    Mail_Mime
93 * @author     Richard Heyes  <richard@phpguru.org>
94 * @author     George Schlossnagle <george@omniti.com>
95 * @author     Cipriano Groenendal <cipri@php.net>
96 * @author     Sean Coates <sean@php.net>
97 * @copyright  2003-2006 PEAR <pear-group@php.net>
98 * @license    http://www.opensource.org/licenses/bsd-license.php BSD License
99 * @version    Release: @package_version@
100 * @link       http://pear.php.net/package/Mail_mime
101 */
102class Mail_mimeDecode
103{
[4898]104        /**
105         * The raw email to decode
106         *
107         * @var    string
108         * @access private
109         */
110        var $_input;
[3637]111
[4898]112        /**
113         * The header part of the input
114         *
115         * @var    string
116         * @access private
117         */
118        var $_header;
[3637]119
[4898]120        /**
121         * The body part of the input
122         *
123         * @var    string
124         * @access private
125         */
126        var $_body;
[3637]127
[4898]128        /**
129         * If an error occurs, this is used to store the message
130         *
131         * @var    string
132         * @access private
133         */
134        var $_error;
[3637]135
[4898]136        /**
137         * Flag to determine whether to include bodies in the
138         * returned object.
139         *
140         * @var    boolean
141         * @access private
142         */
143        var $_include_bodies;
[3637]144
[4898]145        /**
146         * Flag to determine whether to decode bodies
147         *
148         * @var    boolean
149         * @access private
150         */
151        var $_decode_bodies;
[3637]152
[4898]153        /**
154         * Flag to determine whether to decode headers
155         *
156         * @var    boolean
157         * @access private
158         */
159        var $_decode_headers;
[3637]160
[4898]161        /**
162         * Flag to determine whether to include attached messages
163         * as body in the returned object. Depends on $_include_bodies
164         *
165         * @var    boolean
166         * @access private
167         */
168        var $_rfc822_bodies;
[3637]169
[4898]170        /**
171         * Constructor.
172         *
173         * Sets up the object, initialise the variables, and splits and
174         * stores the header and body of the input.
175         *
176         * @param string The input to decode
177         * @access public
178         */
179        function Mail_mimeDecode($input, $deprecated_linefeed = '')
180        {
181                list($header, $body)   = $this->_splitBodyHeader($input);
[3637]182
[4898]183                $this->_input          = $input;
184                $this->_header         = $header;
185                $this->_body           = $body;
186                $this->_decode_bodies  = false;
187                $this->_include_bodies = true;
188                $this->_rfc822_bodies  = false;
189        }
[3637]190
[4898]191        /**
192         * Begins the decoding process. If called statically
193         * it will create an object and call the decode() method
194         * of it.
195         *
196         * @param array An array of various parameters that determine
197         *              various things:
198         *              include_bodies - Whether to include the body in the returned
199         *                               object.
200         *              decode_bodies  - Whether to decode the bodies
201         *                               of the parts. (Transfer encoding)
202         *              decode_headers - Whether to decode headers
203         *              input          - If called statically, this will be treated
204         *                               as the input
205         *              charset        - convert all data to this charset
206         * @return object Decoded results
207         * @access public
208         */
209        function decode($params = null)
210        {
211                // determine if this method has been called statically
212                $isStatic = !(isset($this) && get_class($this) == __CLASS__);
[3637]213
[4898]214                // Have we been called statically?
215                // If so, create an object and pass details to that.
216                if ($isStatic AND isset($params['input'])) {
[3637]217
[4898]218                        $obj = new Mail_mimeDecode($params['input']);
219                        $structure = $obj->decode($params);
[3637]220
[4898]221                        // Called statically but no input
222                } elseif ($isStatic) {
223                        return $this->raiseError('Called statically and no input given');
[3637]224
[4898]225                        // Called via an object
226                } else {
227                        $this->_include_bodies = isset($params['include_bodies']) ?
228                                $params['include_bodies'] : false;
229                        $this->_decode_bodies  = isset($params['decode_bodies']) ?
230                                $params['decode_bodies']  : false;
231                        $this->_decode_headers = isset($params['decode_headers']) ?
232                                $params['decode_headers'] : false;
233                        $this->_rfc822_bodies  = isset($params['rfc_822bodies']) ?
234                                $params['rfc_822bodies']  : false;
235                        $this->_charset = isset($params['charset']) ?
236                                strtolower($params['charset']) : 'utf-8';
237                         
238                        $structure = $this->_decode($this->_header, $this->_body);
239                        if ($structure === false) {
240                                $structure = $this->raiseError($this->_error);
241                        }
242                }
[3637]243
[4898]244                return $structure;
245        }
[3637]246
[4898]247        /**
248         * Performs the decoding. Decodes the body string passed to it
249         * If it finds certain content-types it will call itself in a
250         * recursive fashion
251         *
252         * @param string Header section
253         * @param string Body section
254         * @return object Results of decoding process
255         * @access private
256         */
257        function _decode($headers, $body, $default_ctype = 'text/plain')
258        {
259                $return = new stdClass;
260                $return->headers = array();
261                $headers = $this->_parseHeaders($headers);
[3637]262
[4898]263                foreach ($headers as $value) {
264                        if (isset($return->headers[strtolower($value['name'])]) AND !is_array($return->headers[strtolower($value['name'])])) {
265                                $return->headers[strtolower($value['name'])]   = array($return->headers[strtolower($value['name'])]);
266                                $return->headers[strtolower($value['name'])][] = $value['value'];
[3637]267
[4898]268                        } elseif (isset($return->headers[strtolower($value['name'])])) {
269                                $return->headers[strtolower($value['name'])][] = $value['value'];
[3637]270
[4898]271                        } else {
272                                $return->headers[strtolower($value['name'])] = $value['value'];
273                        }
274                }
[3637]275
[4898]276                reset($headers);
277                while (list($key, $value) = each($headers)) {
278                        $headers[$key]['name'] = strtolower($headers[$key]['name']);
279                        switch ($headers[$key]['name']) {
[3637]280
[4898]281                                case 'content-type':
282                                        $content_type = $this->_parseHeaderValue($headers[$key]['value']);
[3637]283
[4898]284                                        if (preg_match('/([0-9a-z+.-]+)\/([0-9a-z+.-]+)/i', $content_type['value'], $regs)) {
285                                                $return->ctype_primary   = $regs[1];
286                                                $return->ctype_secondary = $regs[2];
287                                        }
[3637]288
[4898]289                                        if (isset($content_type['other'])) {
290                                                while (list($p_name, $p_value) = each($content_type['other'])) {
291                                                        $return->ctype_parameters[$p_name] = $p_value;
292                                                }
293                                        }
294                                        break;
[3637]295
[4898]296                                case 'content-disposition':
297                                        $content_disposition = $this->_parseHeaderValue($headers[$key]['value']);
298                                        $return->disposition   = $content_disposition['value'];
299                                        if (isset($content_disposition['other'])) {
300                                                while (list($p_name, $p_value) = each($content_disposition['other'])) {
301                                                        $return->d_parameters[$p_name] = $p_value;
302                                                }
303                                        }
304                                        break;
[3637]305
[4898]306                                case 'content-transfer-encoding':
307                                        $content_transfer_encoding = $this->_parseHeaderValue($headers[$key]['value']);
308                                        break;
309                        }
310                }
[3637]311
[4898]312                if (isset($content_type)) {
313                        switch (strtolower($content_type['value'])) {
314                                case 'text/plain':
315                                        $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
316                                        $charset = isset($return->ctype_parameters['charset']) ? $return->ctype_parameters['charset'] : $this->_charset;
317                                        $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding, $charset) : $body) : null;
318                                        break;
[3637]319
[4898]320                                case 'text/html':
321                                        $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
322                                        $charset = isset($return->ctype_parameters['charset']) ? $return->ctype_parameters['charset'] : $this->_charset;
323                                        $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding, $charset) : $body) : null;
324                                        break;
[3637]325
[4898]326                                case 'multipart/parallel':
327                                case 'multipart/appledouble': // Appledouble mail
328                                case 'multipart/report': // RFC1892
329                                case 'multipart/signed': // PGP
330                                case 'multipart/digest':
331                                case 'multipart/alternative':
332                                case 'multipart/related':
333                                case 'multipart/mixed':
334                                        if(!isset($content_type['other']['boundary'])){
335                                                $this->_error = 'No boundary found for ' . $content_type['value'] . ' part';
336                                                return false;
337                                        }
[3637]338
[4898]339                                        $default_ctype = (strtolower($content_type['value']) === 'multipart/digest') ? 'message/rfc822' : 'text/plain';
[3637]340
[4898]341                                        $parts = $this->_boundarySplit($body, $content_type['other']['boundary']);
342                                        for ($i = 0; $i < count($parts); $i++) {
343                                                list($part_header, $part_body) = $this->_splitBodyHeader($parts[$i]);
344                                                $part = $this->_decode($part_header, $part_body, $default_ctype);
345                                                if($part === false)
346                                                $part = $this->raiseError($this->_error);
347                                                $return->parts[] = $part;
348                                        }
349                                        break;
[3637]350
[4898]351                                case 'message/rfc822':
352                                        if ($this->_rfc822_bodies) {
353                                                $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
354                                                $charset = isset($return->ctype_parameters['charset']) ? $return->ctype_parameters['charset'] : $this->_charset;
355                                                $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding, $charset) : $body);
356                                        }
[3637]357
[4898]358                                        $obj = new Mail_mimeDecode($body);
359                                        $return->parts[] = $obj->decode(array('include_bodies' => $this->_include_bodies,
360                                                'decode_bodies'  => $this->_decode_bodies,
361                                                'decode_headers' => $this->_decode_headers));
362                                        unset($obj);
363                                        break;
[4613]364
[4898]365                                default:
366                                        if(!isset($content_transfer_encoding['value']))
367                                                $content_transfer_encoding['value'] = '7bit';
368                                       
369                                        // if there is no explicit charset, then don't try to convert to default charset, and make sure that only text mimetypes are converted
370                                        $charset = (isset($return->ctype_parameters['charset']) && ((isset($return->ctype_primary) && $return->ctype_primary == 'text') || !isset($return->ctype_primary)) ) ? $return->ctype_parameters['charset']: '';
371                                        $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $content_transfer_encoding['value'], $charset) : $body) : null;
372                                        break;
373                        }
[3637]374
[4898]375                } else {
376                        $ctype = explode('/', $default_ctype);
377                        $return->ctype_primary   = $ctype[0];
378                        $return->ctype_secondary = $ctype[1];
379                        $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body) : $body) : null;
380                }
[3637]381
[4898]382                return $return;
383        }
[3637]384
[4898]385        /**
386         * Given the output of the above function, this will return an
387         * array of references to the parts, indexed by mime number.
388         *
389         * @param  object $structure   The structure to go through
390         * @param  string $mime_number Internal use only.
391         * @return array               Mime numbers
392         */
393        function &getMimeNumbers(&$structure, $no_refs = false, $mime_number = '', $prepend = '')
394        {
395                $return = array();
396                if (!empty($structure->parts)) {
397                        if ($mime_number != '') {
398                                $structure->mime_id = $prepend . $mime_number;
399                                $return[$prepend . $mime_number] = &$structure;
400                        }
401                        for ($i = 0; $i < count($structure->parts); $i++) {
402                                if (!empty($structure->headers['content-type']) AND substr(strtolower($structure->headers['content-type']), 0, 8) == 'message/') {
403                                        $prepend      = $prepend . $mime_number . '.';
404                                        $_mime_number = '';
405                                } else {
406                                        $_mime_number = ($mime_number == '' ? $i + 1 : sprintf('%s.%s', $mime_number, $i + 1));
407                                }
[3637]408
[4898]409                                $arr = &Mail_mimeDecode::getMimeNumbers($structure->parts[$i], $no_refs, $_mime_number, $prepend);
410                                foreach ($arr as $key => $val) {
411                                        $no_refs ? $return[$key] = '' : $return[$key] = &$arr[$key];
412                                }
413                        }
414                } else {
415                        if ($mime_number == '') {
416                                $mime_number = '1';
417                        }
418                        $structure->mime_id = $prepend . $mime_number;
419                        $no_refs ? $return[$prepend . $mime_number] = '' : $return[$prepend . $mime_number] = &$structure;
420                }
[3637]421
[4898]422                return $return;
423        }
[3637]424
[4898]425        /**
426         * Given a string containing a header and body
427         * section, this function will split them (at the first
428         * blank line) and return them.
429         *
430         * @param string Input to split apart
431         * @return array Contains header and body section
432         * @access private
433         */
434        function _splitBodyHeader($input)
435        {
436                if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $match)) {
437                        return array($match[1], $match[2]);
438                }
439                $this->_error = 'Could not split header and body';
440                return false;
441        }
[3637]442
[4898]443        /**
444         * Parse headers given in $input and return
445         * as assoc array.
446         *
447         * @param string Headers to parse
448         * @return array Contains parsed headers
449         * @access private
450         */
451        function _parseHeaders($input)
452        {
[3637]453
[4898]454                if ($input !== '') {
455                        // Unfold the input
456                        $input   = preg_replace("/\r?\n/", "\r\n", $input);
457                        $input   = preg_replace("/\r\n(\t| )+/", ' ', $input);
458                        $headers = explode("\r\n", trim($input));
[3637]459
[4898]460                        foreach ($headers as $value) {
461                                $hdr_name = substr($value, 0, $pos = strpos($value, ':'));
462                                $hdr_value = substr($value, $pos+1);
463                                if($hdr_value[0] == ' ')
464                                        $hdr_value = substr($hdr_value, 1);
[3637]465
[4898]466                                $return[] = array(
467                                        'name'  => $hdr_name,
468                                        'value' => $this->_decode_headers ? $this->_decodeHeader($hdr_value) : $hdr_value
469                                );
470                        }
471                } else {
472                        $return = array();
473                }
[3637]474
[4898]475                return $return;
476        }
[3754]477
[4898]478        /**
479         * Function to parse a header value,
480         * extract first part, and any secondary
481         * parts (after ;) This function is not as
482         * robust as it could be. Eg. header comments
483         * in the wrong place will probably break it.
484         *
485         * @param string Header value to parse
486         * @return array Contains parsed result
487         * @access private
488         */
489        function _parseHeaderValue($input)
490        {
491                if (($pos = strpos($input, ';')) !== false) {
492                        $return['value'] = trim(substr($input, 0, $pos));
493                        $input = trim(substr($input, $pos+1));
[3754]494
[4898]495                        if (strlen($input) > 0) {
496                                // This splits on a semi-colon, if there's no preceeding backslash
497                                // Now works with quoted values; had to glue the \; breaks in PHP
498                                // the regex is already bordering on incomprehensible
499                                //$splitRegex = '/([^;\'"]*[\'"]([^\'"]*([^\'"]*)*)[\'"][^;\'"]*|([^;]+))(;|$)/';
500                                // simplyfied RegEx - Nokia Mail2 sends boundaries containing ' which break the above regex
501                                $splitRegex = '/([^;\'"]*[\'"]([^\'"]*)[\'"][^;\'"]*|([^;]+))(;|$)/';
502                                preg_match_all($splitRegex, $input, $matches);
[3637]503
[4898]504                                $parameters = array();
505                                for ($i=0; $i<count($matches[0]); $i++) {
506                                        $param = $matches[0][$i];
507                                        while (substr($param, -2) == '\;') {
508                                                $param .= $matches[0][++$i];
509                                        }
510                                        $parameters[] = $param;
511                                }
[3637]512
[4898]513                                for ($i = 0; $i < count($parameters); $i++) {
514                                        $param_name  = trim(substr($parameters[$i], 0, $pos = strpos($parameters[$i], '=')), "'\";\t\\ ");
515                                        $param_value = trim(str_replace('\;', ';', substr($parameters[$i], $pos + 1)), "'\";\t\\ ");
516                                        if (!empty($param_value[0]) && $param_value[0] == '"') {
517                                                $param_value = substr($param_value, 1, -1);
518                                        }
519                                        $return['other'][$param_name] = $param_value;
520                                        $return['other'][strtolower($param_name)] = $param_value;
521                                }
522                        }
523                } else {
524                        $return['value'] = trim($input);
525                }
[3637]526
[4898]527                return $return;
528        }
[3637]529
[4898]530        /**
531         * This function splits the input based
532         * on the given boundary
533         *
534         * @param string Input to parse
535         * @return array Contains array of resulting mime parts
536         * @access private
537         */
538        function _boundarySplit($input, $boundary)
539        {
540                $parts = array();
[3637]541
[4898]542                $bs_possible = substr($boundary, 2, -2);
543                $bs_check = '\"' . $bs_possible . '\"';
[3637]544
[4898]545                if ($boundary == $bs_check) {
546                        $boundary = $bs_possible;
547                }
[3637]548
[4898]549                $tmp = explode('--' . $boundary, $input);
[3637]550
[4898]551                for ($i = 1; $i < count($tmp) - 1; $i++) {
552                        $parts[] = $tmp[$i];
553                }
[3637]554
[4898]555                return $parts;
556        }
[3637]557
[4898]558        /**
559         * Given a header, this function will decode it
560         * according to RFC2047. Probably not *exactly*
561         * conformant, but it does pass all the given
562         * examples (in RFC2047).
563         *
564         * @param string Input header value to decode
565         * @return string Decoded header value
566         * @access private
567         */
568        function _decodeHeader($input)
569        {
570                // Remove white space between encoded-words
571                $input = preg_replace('/(=\?[^?]+\?(q|b)\?[^?]*\?=)(\s)+=\?/i', '\1=?', $input);
[3637]572
[4898]573                // For each encoded-word...
574                while (preg_match('/(=\?([^?]+)\?(q|b)\?([^?]*)\?=)/i', $input, $matches)) {
575                        $encoded  = $matches[1];
576                        $charset  = $matches[2];
577                        $encoding = $matches[3];
578                        $text     = $matches[4];
[3637]579
[4898]580                        switch (strtolower($encoding)) {
581                                case 'b':
582                                        $text = base64_decode($text);
583                                        break;
[3637]584
[4898]585                                case 'q':
586                                        $text = str_replace('_', ' ', $text);
587                                        preg_match_all('/=([a-f0-9]{2})/i', $text, $matches);
588                                        foreach($matches[1] as $value)
589                                        $text = str_replace('='.$value, chr(hexdec($value)), $text);
590                                        break;
591                        }
[3637]592
[4898]593                        $input = str_replace($encoded, $this->_fromCharset($charset, $text), $input);
594                }
[3637]595
[4898]596                return $input;
597        }
[3637]598
[4898]599        /**
600         * Given a body string and an encoding type,
601         * this function will decode and return it.
602         *
603         * @param  string Input body to decode
604         * @param  string Encoding type to use.
605         * @return string Decoded body
606         * @access private
607         */
608        function _decodeBody($input, $encoding = '7bit', $charset = '')
609        {
610                switch (strtolower($encoding)) {
611                        case '7bit':
612                                return $this->_fromCharset($charset, $input);;
613                                break;
[3637]614
[4898]615                        case '8bit':
616                                return $this->_fromCharset($charset, $input);
617                                break;
[3637]618
[4898]619                        case 'quoted-printable':
620                                return $this->_fromCharset($charset, $this->_quotedPrintableDecode($input));
621                                break;
[3637]622
[4898]623                        case 'base64':
624                                return $this->_fromCharset($charset, base64_decode($input));
625                                break;
[3637]626
[4898]627                        default:
628                                return $input;
629                }
630        }
[3637]631
[4898]632        /**
633         * Given a quoted-printable string, this
634         * function will decode and return it.
635         *
636         * @param  string Input body to decode
637         * @return string Decoded body
638         * @access private
639         */
640        function _quotedPrintableDecode($input)
641        {
642                // Remove soft line breaks
643                $input = preg_replace("/=\r?\n/", '', $input);
[3637]644
[4898]645                // Replace encoded characters
646                $input = preg_replace('/=([a-f0-9]{2})/ie', "chr(hexdec('\\1'))", $input);
[3637]647
[4898]648                return $input;
649        }
[3637]650
[4898]651        /**
652         * Checks the input for uuencoded files and returns
653         * an array of them. Can be called statically, eg:
654         *
655         * $files =& Mail_mimeDecode::uudecode($some_text);
656         *
657         * It will check for the begin 666 ... end syntax
658         * however and won't just blindly decode whatever you
659         * pass it.
660         *
661         * @param  string Input body to look for attahcments in
662         * @return array  Decoded bodies, filenames and permissions
663         * @access public
664         * @author Unknown
665         */
666        function &uudecode($input)
667        {
668                // Find all uuencoded sections
669                preg_match_all("/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us", $input, $matches);
[3637]670
[4898]671                for ($j = 0; $j < count($matches[3]); $j++) {
672                        $str      = $matches[3][$j];
673                        $filename = $matches[2][$j];
674                        $fileperm = $matches[1][$j];
[3637]675
[4898]676                        $file = '';
677                        $str = preg_split("/\r?\n/", trim($str));
678                        $strlen = count($str);
[3637]679
[4898]680                        for ($i = 0; $i < $strlen; $i++) {
681                                $pos = 1;
682                                $d = 0;
683                                $len=(int)(((ord(substr($str[$i],0,1)) -32) - ' ') & 077);
[3637]684
[4898]685                                while (($d + 3 <= $len) AND ($pos + 4 <= strlen($str[$i]))) {
686                                        $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
687                                        $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
688                                        $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
689                                        $c3 = (ord(substr($str[$i],$pos+3,1)) ^ 0x20);
690                                        $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
[3637]691
[4898]692                                        $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
[3637]693
[4898]694                                        $file .= chr(((($c2 - ' ') & 077) << 6) |  (($c3 - ' ') & 077));
[3637]695
[4898]696                                        $pos += 4;
697                                        $d += 3;
698                                }
[3637]699
[4898]700                                if (($d + 2 <= $len) && ($pos + 3 <= strlen($str[$i]))) {
701                                        $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
702                                        $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
703                                        $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
704                                        $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
[3637]705
[4898]706                                        $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
[3637]707
[4898]708                                        $pos += 3;
709                                        $d += 2;
710                                }
[3637]711
[4898]712                                if (($d + 1 <= $len) && ($pos + 2 <= strlen($str[$i]))) {
713                                        $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
714                                        $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
715                                        $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
716                                }
717                        }
718                        $files[] = array('filename' => $filename, 'fileperm' => $fileperm, 'filedata' => $file);
719                }
[3637]720
[4898]721                return $files;
722        }
[3637]723
[4898]724        /**
725         * getSendArray() returns the arguments required for Mail::send()
726         * used to build the arguments for a mail::send() call
727         *
728         * Usage:
729         * $mailtext = Full email (for example generated by a template)
730         * $decoder = new Mail_mimeDecode($mailtext);
731         * $parts =  $decoder->getSendArray();
732         * if (!PEAR::isError($parts) {
733         *     list($recipents,$headers,$body) = $parts;
734         *     $mail = Mail::factory('smtp');
735         *     $mail->send($recipents,$headers,$body);
736         * } else {
737         *     echo $parts->message;
738         * }
739         * @return mixed   array of recipeint, headers,body or Pear_Error
740         * @access public
741         * @author Alan Knowles <alan@akbkhome.com>
742         */
743        function getSendArray()
744        {
745                // prevent warning if this is not set
746                $this->_decode_headers = FALSE;
747                $headerlist =$this->_parseHeaders($this->_header);
748                $to = "";
749                if (!$headerlist) {
750                        return $this->raiseError("Message did not contain headers");
751                }
752                foreach($headerlist as $item) {
753                        $header[$item['name']] = $item['value'];
754                        switch (strtolower($item['name'])) {
755                                case "to":
756                                case "cc":
757                                case "bcc":
758                                        $to .= ",".$item['value'];
759                                default:
760                                        break;
761                        }
762                }
763                if ($to == "") {
764                        return $this->raiseError("Message did not contain any recipents");
765                }
766                $to = substr($to,1);
767                return array($to,$header,$this->_body);
768        }
[3637]769
[4898]770        /**
771         * Returns a xml copy of the output of
772         * Mail_mimeDecode::decode. Pass the output in as the
773         * argument. This function can be called statically. Eg:
774         *
775         * $output = $obj->decode();
776         * $xml    = Mail_mimeDecode::getXML($output);
777         *
778         * The DTD used for this should have been in the package. Or
779         * alternatively you can get it from cvs, or here:
780         * http://www.phpguru.org/xmail/xmail.dtd.
781         *
782         * @param  object Input to convert to xml. This should be the
783         *                output of the Mail_mimeDecode::decode function
784         * @return string XML version of input
785         * @access public
786         */
787        function getXML($input)
788        {
789                $crlf    =  "\r\n";
790                $output  = '<?xml version=\'1.0\'?>' . $crlf .
[3637]791                   '<!DOCTYPE email SYSTEM "http://www.phpguru.org/xmail/xmail.dtd">' . $crlf .
792                   '<email>' . $crlf .
[4898]793                Mail_mimeDecode::_getXML($input) .
[3637]794                   '</email>';
795
[4898]796                return $output;
797        }
[3637]798
[4898]799        /**
800         * Function that does the actual conversion to xml. Does a single
801         * mimepart at a time.
802         *
803         * @param  object  Input to convert to xml. This is a mimepart object.
804         *                 It may or may not contain subparts.
805         * @param  integer Number of tabs to indent
806         * @return string  XML version of input
807         * @access private
808         */
809        function _getXML($input, $indent = 1)
810        {
811                $htab    =  "\t";
812                $crlf    =  "\r\n";
813                $output  =  '';
814                $headers = @(array)$input->headers;
[3637]815
[4898]816                foreach ($headers as $hdr_name => $hdr_value) {
817                        // Multiple headers with this name
818                        if (is_array($headers[$hdr_name])) {
819                                for ($i = 0; $i < count($hdr_value); $i++) {
820                                        $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value[$i], $indent);
821                                }
[3637]822
[4898]823                                // Only one header of this sort
824                        } else {
825                                $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value, $indent);
826                        }
827                }
[3637]828
[4898]829                if (!empty($input->parts)) {
830                        for ($i = 0; $i < count($input->parts); $i++) {
831                                $output .= $crlf . str_repeat($htab, $indent) . '<mimepart>' . $crlf .
832                                Mail_mimeDecode::_getXML($input->parts[$i], $indent+1) .
833                                str_repeat($htab, $indent) . '</mimepart>' . $crlf;
834                        }
835                } elseif (isset($input->body)) {
836                        $output .= $crlf . str_repeat($htab, $indent) . '<body><![CDATA[' .
837                        $input->body . ']]></body>' . $crlf;
838                }
[3637]839
[4898]840                return $output;
841        }
[3637]842
[4898]843        /**
844         * Helper function to _getXML(). Returns xml of a header.
845         *
846         * @param  string  Name of header
847         * @param  string  Value of header
848         * @param  integer Number of tabs to indent
849         * @return string  XML version of input
850         * @access private
851         */
852        function _getXML_helper($hdr_name, $hdr_value, $indent)
853        {
854                $htab   = "\t";
855                $crlf   = "\r\n";
856                $return = '';
[3637]857
[4898]858                $new_hdr_value = ($hdr_name != 'received') ? Mail_mimeDecode::_parseHeaderValue($hdr_value) : array('value' => $hdr_value);
859                $new_hdr_name  = str_replace(' ', '-', ucwords(str_replace('-', ' ', $hdr_name)));
[3637]860
[4898]861                // Sort out any parameters
862                if (!empty($new_hdr_value['other'])) {
863                        foreach ($new_hdr_value['other'] as $paramname => $paramvalue) {
864                                $params[] = str_repeat($htab, $indent) . $htab . '<parameter>' . $crlf .
865                                str_repeat($htab, $indent) . $htab . $htab . '<paramname>' . htmlspecialchars($paramname) . '</paramname>' . $crlf .
866                                str_repeat($htab, $indent) . $htab . $htab . '<paramvalue>' . htmlspecialchars($paramvalue) . '</paramvalue>' . $crlf .
867                                str_repeat($htab, $indent) . $htab . '</parameter>' . $crlf;
868                        }
[3637]869
[4898]870                        $params = implode('', $params);
871                } else {
872                        $params = '';
873                }
[3637]874
[4898]875                $return = str_repeat($htab, $indent) . '<header>' . $crlf .
876                str_repeat($htab, $indent) . $htab . '<headername>' . htmlspecialchars($new_hdr_name) . '</headername>' . $crlf .
877                str_repeat($htab, $indent) . $htab . '<headervalue>' . htmlspecialchars($new_hdr_value['value']) . '</headervalue>' . $crlf .
878                $params .
879                str_repeat($htab, $indent) . '</header>' . $crlf;
[3637]880
[4898]881                return $return;
882        }
[3637]883
[4898]884        /**
885         * Z-Push helper to decode text
886         *
887         * @param  string  current charset of input
888         * @param  string  input
889         * @return string  XML version of input
890         * @access private
891         */
892        function _fromCharset($charset, $input) {
893                if($charset == '' || (strtolower($charset) == $this->_charset))
894                        return $input;
[3637]895
[4898]896                return @iconv($charset, $this->_charset. "//TRANSLIT", $input);
897        }
[3637]898
[4898]899        /**
900         * Z-Push helper for error logging
901         * removing PEAR dependency
902         *
903         * @param  string  debug message
904         * @return boolean always false as there was an error
905         * @access private
906         */
907        function raiseError($message) {
908                debugLog("mimeDecode error: ". $message);
909                return false;
910        }
[3637]911} // End of class
Note: See TracBrowser for help on using the repository browser.