📄 mimedecode.php
字号:
<?php/** * The Mail_mimeDecode class is used to decode mail/mime messages * * This class will parse a raw mime email and return * the structure. Returned structure is similar to * that returned by imap_fetchstructure(). * * +----------------------------- IMPORTANT ------------------------------+ * | Usage of this class compared to native php extensions such as | * | mailparse or imap, is slow and may be feature deficient. If available| * | you are STRONGLY recommended to use the php extensions. | * +----------------------------------------------------------------------+ * * Compatible with PHP versions 4 and 5 * * LICENSE: This LICENSE is in the BSD license style. * Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org> * Copyright (c) 2003-2006, PEAR <pear-group@php.net> * All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - Neither the name of the authors, nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. * * @category Mail * @package Mail_Mime * @author Richard Heyes <richard@phpguru.org> * @author George Schlossnagle <george@omniti.com> * @author Cipriano Groenendal <cipri@php.net> * @author Sean Coates <sean@php.net> * @copyright 2003-2006 PEAR <pear-group@php.net> * @license http://www.opensource.org/licenses/bsd-license.php BSD License * @version CVS: $Id: mimeDecode.php,v 1.48 2006/12/03 13:43:33 cipri Exp $ * @link http://pear.php.net/package/Mail_mime *//** * require PEAR * * This package depends on PEAR to raise errors. */require_once 'PEAR.php';/** * The Mail_mimeDecode class is used to decode mail/mime messages * * This class will parse a raw mime email and return the structure. * Returned structure is similar to that returned by imap_fetchstructure(). * * +----------------------------- IMPORTANT ------------------------------+ * | Usage of this class compared to native php extensions such as | * | mailparse or imap, is slow and may be feature deficient. If available| * | you are STRONGLY recommended to use the php extensions. | * +----------------------------------------------------------------------+ * * @category Mail * @package Mail_Mime * @author Richard Heyes <richard@phpguru.org> * @author George Schlossnagle <george@omniti.com> * @author Cipriano Groenendal <cipri@php.net> * @author Sean Coates <sean@php.net> * @copyright 2003-2006 PEAR <pear-group@php.net> * @license http://www.opensource.org/licenses/bsd-license.php BSD License * @version Release: @package_version@ * @link http://pear.php.net/package/Mail_mime */class Mail_mimeDecode extends PEAR{ /** * The raw email to decode * * @var string * @access private */ var $_input; /** * The header part of the input * * @var string * @access private */ var $_header; /** * The body part of the input * * @var string * @access private */ var $_body; /** * If an error occurs, this is used to store the message * * @var string * @access private */ var $_error; /** * Flag to determine whether to include bodies in the * returned object. * * @var boolean * @access private */ var $_include_bodies; /** * Flag to determine whether to decode bodies * * @var boolean * @access private */ var $_decode_bodies; /** * Flag to determine whether to decode headers * * @var boolean * @access private */ var $_decode_headers; /** * Constructor. * * Sets up the object, initialise the variables, and splits and * stores the header and body of the input. * * @param string The input to decode * @access public */ function Mail_mimeDecode($input) { list($header, $body) = $this->_splitBodyHeader($input); $this->_input = $input; $this->_header = $header; $this->_body = $body; $this->_decode_bodies = false; $this->_include_bodies = true; } /** * Begins the decoding process. If called statically * it will create an object and call the decode() method * of it. * * @param array An array of various parameters that determine * various things: * include_bodies - Whether to include the body in the returned * object. * decode_bodies - Whether to decode the bodies * of the parts. (Transfer encoding) * decode_headers - Whether to decode headers * input - If called statically, this will be treated * as the input * @return object Decoded results * @access public */ function decode($params = null) { // determine if this method has been called statically $isStatic = !(isset($this) && get_class($this) == __CLASS__); // Have we been called statically? // If so, create an object and pass details to that. if ($isStatic AND isset($params['input'])) { $obj = new Mail_mimeDecode($params['input']); $structure = $obj->decode($params); // Called statically but no input } elseif ($isStatic) { return PEAR::raiseError('Called statically and no input given'); // Called via an object } else { $this->_include_bodies = isset($params['include_bodies']) ? $params['include_bodies'] : false; $this->_decode_bodies = isset($params['decode_bodies']) ? $params['decode_bodies'] : false; $this->_decode_headers = isset($params['decode_headers']) ? $params['decode_headers'] : false; $structure = $this->_decode($this->_header, $this->_body); if ($structure === false) { $structure = $this->raiseError($this->_error); } } return $structure; } /** * Performs the decoding. Decodes the body string passed to it * If it finds certain content-types it will call itself in a * recursive fashion * * @param string Header section * @param string Body section * @return object Results of decoding process * @access private */ function _decode($headers, $body, $default_ctype = 'text/plain') { $return = new stdClass; $return->headers = array(); $headers = $this->_parseHeaders($headers); foreach ($headers as $value) { if (isset($return->headers[strtolower($value['name'])]) AND !is_array($return->headers[strtolower($value['name'])])) { $return->headers[strtolower($value['name'])] = array($return->headers[strtolower($value['name'])]); $return->headers[strtolower($value['name'])][] = $value['value']; } elseif (isset($return->headers[strtolower($value['name'])])) { $return->headers[strtolower($value['name'])][] = $value['value']; } else { $return->headers[strtolower($value['name'])] = $value['value']; } } reset($headers); while (list($key, $value) = each($headers)) { $headers[$key]['name'] = strtolower($headers[$key]['name']); switch ($headers[$key]['name']) { case 'content-type': $content_type = $this->_parseHeaderValue($headers[$key]['value']); if (preg_match('/([0-9a-z+.-]+)\/([0-9a-z+.-]+)/i', $content_type['value'], $regs)) { $return->ctype_primary = $regs[1]; $return->ctype_secondary = $regs[2]; } if (isset($content_type['other'])) { while (list($p_name, $p_value) = each($content_type['other'])) { $return->ctype_parameters[$p_name] = $p_value; } } break; case 'content-disposition': $content_disposition = $this->_parseHeaderValue($headers[$key]['value']); $return->disposition = $content_disposition['value']; if (isset($content_disposition['other'])) { while (list($p_name, $p_value) = each($content_disposition['other'])) { $return->d_parameters[$p_name] = $p_value; } } break; case 'content-transfer-encoding': $content_transfer_encoding = $this->_parseHeaderValue($headers[$key]['value']); break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -