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