PageRenderTime 64ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/library/mime/mimeDecode.php

https://github.com/muchael/expressolivre
PHP | 1014 lines | 544 code | 119 blank | 351 comment | 90 complexity | e5a3c41cfd95e2594553ae6c5e0d13f7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-2-Clause, BSD-3-Clause, AGPL-3.0
  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.
  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
  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 305875 2010-12-01 07:17:10Z alan_k $
  56. * @link http://pear.php.net/package/Mail_mime
  57. */
  58. /**
  59. * require PEAR
  60. *
  61. * This package depends on PEAR to raise errors.
  62. */
  63. //require_once 'PEAR.php';
  64. /**
  65. * The Mail_mimeDecode class is used to decode mail/mime messages
  66. *
  67. * This class will parse a raw mime email and return the structure.
  68. * Returned structure is similar to that returned by imap_fetchstructure().
  69. *
  70. * +----------------------------- IMPORTANT ------------------------------+
  71. * | Usage of this class compared to native php extensions such as |
  72. * | mailparse or imap, is slow and may be feature deficient. If available|
  73. * | you are STRONGLY recommended to use the php extensions. |
  74. * +----------------------------------------------------------------------+
  75. *
  76. * @category Mail
  77. * @package Mail_Mime
  78. * @author Richard Heyes <richard@phpguru.org>
  79. * @author George Schlossnagle <george@omniti.com>
  80. * @author Cipriano Groenendal <cipri@php.net>
  81. * @author Sean Coates <sean@php.net>
  82. * @copyright 2003-2006 PEAR <pear-group@php.net>
  83. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  84. * @version Release: @package_version@
  85. * @link http://pear.php.net/package/Mail_mime
  86. */
  87. class Mail_mimeDecode
  88. {
  89. /**
  90. * The raw email to decode
  91. *
  92. * @var string
  93. * @access private
  94. */
  95. var $_input;
  96. /**
  97. * The header part of the input
  98. *
  99. * @var string
  100. * @access private
  101. */
  102. var $_header;
  103. /**
  104. * The body part of the input
  105. *
  106. * @var string
  107. * @access private
  108. */
  109. var $_body;
  110. /**
  111. * If an error occurs, this is used to store the message
  112. *
  113. * @var string
  114. * @access private
  115. */
  116. var $_error;
  117. /**
  118. * Flag to determine whether to include bodies in the
  119. * returned object.
  120. *
  121. * @var boolean
  122. * @access private
  123. */
  124. var $_include_bodies;
  125. /**
  126. * Flag to determine whether to decode bodies
  127. *
  128. * @var boolean
  129. * @access private
  130. */
  131. var $_decode_bodies;
  132. /**
  133. * Flag to determine whether to decode headers
  134. *
  135. * @var boolean
  136. * @access private
  137. */
  138. var $_decode_headers;
  139. /**
  140. * Flag to determine whether to include attached messages
  141. * as body in the returned object. Depends on $_include_bodies
  142. *
  143. * @var boolean
  144. * @access private
  145. */
  146. var $_rfc822_bodies;
  147. /**
  148. * Constructor.
  149. *
  150. * Sets up the object, initialise the variables, and splits and
  151. * stores the header and body of the input.
  152. *
  153. * @param string The input to decode
  154. * @access public
  155. */
  156. function Mail_mimeDecode($input)
  157. {
  158. list($header, $body) = $this->_splitBodyHeader($input);
  159. $this->_input = $input;
  160. $this->_header = $header;
  161. $this->_body = $body;
  162. $this->_decode_bodies = false;
  163. $this->_include_bodies = true;
  164. $this->_rfc822_bodies = false;
  165. }
  166. /**
  167. * Begins the decoding process. If called statically
  168. * it will create an object and call the decode() method
  169. * of it.
  170. *
  171. * @param array An array of various parameters that determine
  172. * various things:
  173. * include_bodies - Whether to include the body in the returned
  174. * object.
  175. * decode_bodies - Whether to decode the bodies
  176. * of the parts. (Transfer encoding)
  177. * decode_headers - Whether to decode headers
  178. * input - If called statically, this will be treated
  179. * as the input
  180. * @return object Decoded results
  181. * @access public
  182. */
  183. function decode($params = null)
  184. {
  185. // determine if this method has been called statically
  186. $isStatic = empty($this) || !is_a($this, __CLASS__);
  187. // Have we been called statically?
  188. // If so, create an object and pass details to that.
  189. if ($isStatic AND isset($params['input'])) {
  190. $obj = new Mail_mimeDecode($params['input']);
  191. $structure = $obj->decode($params);
  192. // Called statically but no input
  193. } elseif ($isStatic) {
  194. return PEAR::raiseError('Called statically and no input given');
  195. // Called via an object
  196. } else {
  197. $this->_include_bodies = isset($params['include_bodies']) ?
  198. $params['include_bodies'] : false;
  199. $this->_decode_bodies = isset($params['decode_bodies']) ?
  200. $params['decode_bodies'] : false;
  201. $this->_decode_headers = isset($params['decode_headers']) ?
  202. $params['decode_headers'] : false;
  203. $this->_rfc822_bodies = isset($params['rfc_822bodies']) ?
  204. $params['rfc_822bodies'] : false;
  205. $structure = $this->_decode($this->_header, $this->_body);
  206. if ($structure === false) {
  207. $structure = $this->raiseError($this->_error);
  208. }
  209. }
  210. return $structure;
  211. }
  212. /**
  213. * Performs the decoding. Decodes the body string passed to it
  214. * If it finds certain content-types it will call itself in a
  215. * recursive fashion
  216. *
  217. * @param string Header section
  218. * @param string Body section
  219. * @return object Results of decoding process
  220. * @access private
  221. */
  222. function _decode($headers, $body, $default_ctype = 'text/plain')
  223. {
  224. $return = new stdClass;
  225. $return->headers = array();
  226. $headers = $this->_parseHeaders($headers);
  227. foreach ($headers as $value) {
  228. $value['value'] = $this->_decode_headers ? $this->_decodeHeader($value['value']) : $value['value'];
  229. if (isset($return->headers[strtolower($value['name'])]) AND !is_array($return->headers[strtolower($value['name'])])) {
  230. $return->headers[strtolower($value['name'])] = array($return->headers[strtolower($value['name'])]);
  231. $return->headers[strtolower($value['name'])][] = $value['value'];
  232. } elseif (isset($return->headers[strtolower($value['name'])])) {
  233. $return->headers[strtolower($value['name'])][] = $value['value'];
  234. } else {
  235. $return->headers[strtolower($value['name'])] = $value['value'];
  236. }
  237. }
  238. foreach ($headers as $key => $value) {
  239. $headers[$key]['name'] = strtolower($headers[$key]['name']);
  240. switch ($headers[$key]['name']) {
  241. case 'content-type':
  242. $content_type = $this->_parseHeaderValue($headers[$key]['value']);
  243. if (preg_match('/([0-9a-z+.-]+)\/([0-9a-z+.-]+)/i', $content_type['value'], $regs)) {
  244. $return->ctype_primary = $regs[1];
  245. $return->ctype_secondary = $regs[2];
  246. }
  247. if (isset($content_type['other'])) {
  248. foreach($content_type['other'] as $p_name => $p_value) {
  249. $return->ctype_parameters[$p_name] = $p_value;
  250. }
  251. }
  252. break;
  253. case 'content-disposition':
  254. $content_disposition = $this->_parseHeaderValue($headers[$key]['value']);
  255. $return->disposition = $content_disposition['value'];
  256. if (isset($content_disposition['other'])) {
  257. foreach($content_disposition['other'] as $p_name => $p_value) {
  258. $return->d_parameters[$p_name] = $p_value;
  259. }
  260. }
  261. break;
  262. case 'content-transfer-encoding':
  263. $content_transfer_encoding = $this->_parseHeaderValue($headers[$key]['value']);
  264. break;
  265. }
  266. }
  267. if (isset($content_type)) {
  268. switch (strtolower($content_type['value'])) {
  269. case 'text/plain':
  270. $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
  271. $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null;
  272. break;
  273. case 'text/html':
  274. $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
  275. $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null;
  276. break;
  277. case 'multipart/parallel':
  278. case 'multipart/appledouble': // Appledouble mail
  279. case 'multipart/report': // RFC1892
  280. case 'multipart/signed': // PGP
  281. case 'multipart/digest':
  282. case 'multipart/alternative':
  283. case 'multipart/related':
  284. case 'multipart/mixed':
  285. case 'application/vnd.wap.multipart.related':
  286. if(!isset($content_type['other']['boundary'])){
  287. $this->_error = 'No boundary found for ' . $content_type['value'] . ' part';
  288. return false;
  289. }
  290. $default_ctype = (strtolower($content_type['value']) === 'multipart/digest') ? 'message/rfc822' : 'text/plain';
  291. $parts = $this->_boundarySplit($body, $content_type['other']['boundary']);
  292. $parts_count = count($parts);
  293. for ($i = 0; $i < $parts_count; ++$i) {
  294. list($part_header, $part_body) = $this->_splitBodyHeader($parts[$i]);
  295. $part = $this->_decode($part_header, $part_body, $default_ctype);
  296. if($part === false)
  297. $part = $this->raiseError($this->_error);
  298. $return->parts[] = $part;
  299. }
  300. break;
  301. case 'message/rfc822':
  302. if ($this->_rfc822_bodies) {
  303. $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
  304. $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body);
  305. }
  306. $obj = new Mail_mimeDecode($body);
  307. $return->parts[] = $obj->decode(array('include_bodies' => $this->_include_bodies,
  308. 'decode_bodies' => $this->_decode_bodies,
  309. 'decode_headers' => $this->_decode_headers));
  310. unset($obj);
  311. break;
  312. case 'message/delivery-status':
  313. if(!isset($content_transfer_encoding['value']))
  314. $content_transfer_encoding['value'] = '7bit';
  315. $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $content_transfer_encoding['value']) : $body) : null;
  316. break;
  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. } else {
  324. $ctype = explode('/', $default_ctype);
  325. $return->ctype_primary = $ctype[0];
  326. $return->ctype_secondary = $ctype[1];
  327. $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body) : $body) : null;
  328. }
  329. return $return;
  330. }
  331. /**
  332. * Given the output of the above function, this will return an
  333. * array of references to the parts, indexed by mime number.
  334. *
  335. * @param object $structure The structure to go through
  336. * @param string $mime_number Internal use only.
  337. * @return array Mime numbers
  338. */
  339. function &getMimeNumbers(&$structure, $no_refs = false, $mime_number = '', $prepend = '')
  340. {
  341. $return = array();
  342. if (!empty($structure->parts)) {
  343. if ($mime_number != '') {
  344. $structure->mime_id = $prepend . $mime_number;
  345. $return[$prepend . $mime_number] = &$structure;
  346. }
  347. $structure_parts_count = count($structure->parts);
  348. for ($i = 0; $i < $structure_parts_count; ++$i) {
  349. if (!empty($structure->headers['content-type']) AND substr(strtolower($structure->headers['content-type']), 0, 8) == 'message/') {
  350. $prepend = $prepend . $mime_number . '.';
  351. $_mime_number = '';
  352. } else {
  353. $_mime_number = ($mime_number == '' ? $i + 1 : sprintf('%s.%s', $mime_number, $i + 1));
  354. }
  355. $arr = &Mail_mimeDecode::getMimeNumbers($structure->parts[$i], $no_refs, $_mime_number, $prepend);
  356. foreach ($arr as $key => $val) {
  357. $no_refs ? $return[$key] = '' : $return[$key] = &$arr[$key];
  358. }
  359. }
  360. } else {
  361. if ($mime_number == '') {
  362. $mime_number = '1';
  363. }
  364. $structure->mime_id = $prepend . $mime_number;
  365. $no_refs ? $return[$prepend . $mime_number] = '' : $return[$prepend . $mime_number] = &$structure;
  366. }
  367. return $return;
  368. }
  369. /**
  370. * Given a string containing a header and body
  371. * section, this function will split them (at the first
  372. * blank line) and return them.
  373. *
  374. * @param string Input to split apart
  375. * @return array Contains header and body section
  376. * @access private
  377. */
  378. function _splitBodyHeader($input)
  379. {
  380. if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $match)) {
  381. return array($match[1], $match[2]);
  382. }
  383. // bug #17325 - empty bodies are allowed. - we just check that at least one line
  384. // of headers exist..
  385. if (count(explode("\n",$input))) {
  386. return array($input, '');
  387. }
  388. $this->_error = 'Could not split header and body';
  389. return false;
  390. }
  391. /**
  392. * Parse headers given in $input and return
  393. * as assoc array.
  394. *
  395. * @param string Headers to parse
  396. * @return array Contains parsed headers
  397. * @access private
  398. */
  399. function _parseHeaders($input)
  400. {
  401. if ($input !== '') {
  402. // Unfold the input
  403. $input = preg_replace("/\r?\n/", "\r\n", $input);
  404. //#7065 - wrapping.. with encoded stuff.. - probably not needed,
  405. // wrapping space should only get removed if the trailing item on previous line is a
  406. // encoded character
  407. $input = preg_replace("/=\r\n(\t| )+/", '=', $input);
  408. $input = preg_replace("/\r\n(\t| )+/", ' ', $input);
  409. $headers = explode("\r\n", trim($input));
  410. foreach ($headers as $value) {
  411. $hdr_name = substr($value, 0, $pos = strpos($value, ':'));
  412. $hdr_value = substr($value, $pos+1);
  413. if($hdr_value[0] == ' ')
  414. $hdr_value = substr($hdr_value, 1);
  415. $return[] = array(
  416. 'name' => $hdr_name,
  417. 'value' => $hdr_value
  418. );
  419. }
  420. } else {
  421. $return = array();
  422. }
  423. return $return;
  424. }
  425. /**
  426. * Function to parse a header value,
  427. * extract first part, and any secondary
  428. * parts (after ;) This function is not as
  429. * robust as it could be. Eg. header comments
  430. * in the wrong place will probably break it.
  431. *
  432. * @param string Header value to parse
  433. * @return array Contains parsed result
  434. * @access private
  435. */
  436. function _parseHeaderValue($input)
  437. {
  438. if (($pos = strpos($input, ';')) === false) {
  439. $input = $this->_decode_headers ? $this->_decodeHeader($input) : $input;
  440. $return['value'] = trim($input);
  441. return $return;
  442. }
  443. $value = substr($input, 0, $pos);
  444. $value = $this->_decode_headers ? $this->_decodeHeader($value) : $value;
  445. $return['value'] = trim($value);
  446. $input = trim(substr($input, $pos+1));
  447. if (!strlen($input) > 0) {
  448. return $return;
  449. }
  450. // at this point input contains xxxx=".....";zzzz="...."
  451. // since we are dealing with quoted strings, we need to handle this properly..
  452. $i = 0;
  453. $l = strlen($input);
  454. $key = '';
  455. $val = false; // our string - including quotes..
  456. $q = false; // in quote..
  457. $lq = ''; // last quote..
  458. while ($i < $l) {
  459. $c = $input[$i];
  460. //var_dump(array('i'=>$i,'c'=>$c,'q'=>$q, 'lq'=>$lq, 'key'=>$key, 'val' =>$val));
  461. $escaped = false;
  462. if ($c == '\\') {
  463. ++$i;
  464. if ($i == $l-1) { // end of string.
  465. break;
  466. }
  467. $escaped = true;
  468. $c = $input[$i];
  469. }
  470. // state - in key..
  471. if ($val === false) {
  472. if (!$escaped && $c == '=') {
  473. $val = '';
  474. $key = trim($key);
  475. ++$i;
  476. continue;
  477. }
  478. if (!$escaped && $c == ';') {
  479. if ($key) { // a key without a value..
  480. $key= trim($key);
  481. $return['other'][$key] = '';
  482. $return['other'][strtolower($key)] = '';
  483. }
  484. $key = '';
  485. }
  486. $key .= $c;
  487. ++$i;
  488. continue;
  489. }
  490. // state - in value.. (as $val is set..)
  491. if ($q === false) {
  492. // not in quote yet.
  493. if ((!strlen($val) || $lq !== false) && $c == ' ' || $c == "\t") {
  494. ++$i;
  495. continue; // skip leading spaces after '=' or after '"'
  496. }
  497. if (!$escaped && ($c == '"' || $c == "'")) {
  498. // start quoted area..
  499. $q = $c;
  500. // in theory should not happen raw text in value part..
  501. // but we will handle it as a merged part of the string..
  502. $val = !strlen(trim($val)) ? '' : trim($val);
  503. ++$i;
  504. continue;
  505. }
  506. // got end....
  507. if (!$escaped && $c == ';') {
  508. $val = trim($val);
  509. $added = false;
  510. if (preg_match('/\*[0-9]+$/', $key)) {
  511. // this is the extended aaa*0=...;aaa*1=.... code
  512. // it assumes the pieces arrive in order, and are valid...
  513. $key = preg_replace('/\*[0-9]+$/', '', $key);
  514. if (isset($return['other'][$key])) {
  515. $return['other'][$key] .= $val;
  516. if (strtolower($key) != $key) {
  517. $return['other'][strtolower($key)] .= $val;
  518. }
  519. $added = true;
  520. }
  521. // continue and use standard setters..
  522. }
  523. if (!$added) {
  524. $return['other'][$key] = $val;
  525. $return['other'][strtolower($key)] = $val;
  526. }
  527. $val = false;
  528. $key = '';
  529. $lq = false;
  530. ++$i;
  531. continue;
  532. }
  533. $val .= $c;
  534. ++$i;
  535. continue;
  536. }
  537. // state - in quote..
  538. if (!$escaped && $c == $q) { // potential exit state..
  539. // end of quoted string..
  540. $lq = $q;
  541. $q = false;
  542. ++$i;
  543. continue;
  544. }
  545. // normal char inside of quoted string..
  546. $val.= $c;
  547. ++$i;
  548. }
  549. // do we have anything left..
  550. if (strlen(trim($key)) || $val !== false) {
  551. $val = trim($val);
  552. $added = false;
  553. if ($val !== false && preg_match('/\*[0-9]+$/', $key)) {
  554. // no dupes due to our crazy regexp.
  555. $key = preg_replace('/\*[0-9]+$/', '', $key);
  556. if (isset($return['other'][$key])) {
  557. $return['other'][$key] .= $val;
  558. if (strtolower($key) != $key) {
  559. $return['other'][strtolower($key)] .= $val;
  560. }
  561. $added = true;
  562. }
  563. // continue and use standard setters..
  564. }
  565. if (!$added) {
  566. $return['other'][$key] = $val;
  567. $return['other'][strtolower($key)] = $val;
  568. }
  569. }
  570. // decode values.
  571. foreach($return['other'] as $key =>$val) {
  572. $return['other'][$key] = $this->_decode_headers ? $this->_decodeHeader($val) : $val;
  573. }
  574. //print_r($return);
  575. return $return;
  576. }
  577. /**
  578. * This function splits the input based
  579. * on the given boundary
  580. *
  581. * @param string Input to parse
  582. * @return array Contains array of resulting mime parts
  583. * @access private
  584. */
  585. function _boundarySplit($input, $boundary)
  586. {
  587. $parts = array();
  588. $bs_possible = substr($boundary, 2, -2);
  589. $bs_check = '\"' . $bs_possible . '\"';
  590. if ($boundary == $bs_check) {
  591. $boundary = $bs_possible;
  592. }
  593. $tmp = preg_split("/--".preg_quote($boundary, '/')."((?=\s)|--)/", $input);
  594. $len = count($tmp) -1;
  595. for ($i = 1; $i < $len; ++$i) {
  596. if (strlen(trim($tmp[$i]))) {
  597. $parts[] = $tmp[$i];
  598. }
  599. }
  600. // add the last part on if it does not end with the 'closing indicator'
  601. if (!empty($tmp[$len]) && strlen(trim($tmp[$len])) && $tmp[$len][0] != '-') {
  602. $parts[] = $tmp[$len];
  603. }
  604. return $parts;
  605. }
  606. /**
  607. * Given a header, this function will decode it
  608. * according to RFC2047. Probably not *exactly*
  609. * conformant, but it does pass all the given
  610. * examples (in RFC2047).
  611. *
  612. * @param string Input header value to decode
  613. * @return string Decoded header value
  614. * @access private
  615. */
  616. function _decodeHeader($input)
  617. {
  618. // Remove white space between encoded-words
  619. $input = preg_replace('/(=\?[^?]+\?(q|b)\?[^?]*\?=)(\s)+=\?/i', '\1=?', $input);
  620. // For each encoded-word...
  621. while (preg_match('/(=\?([^?]+)\?(q|b)\?([^?]*)\?=)/i', $input, $matches)) {
  622. $encoded = $matches[1];
  623. $charset = $matches[2];
  624. $encoding = $matches[3];
  625. $text = $matches[4];
  626. switch (strtolower($encoding)) {
  627. case 'b':
  628. $text = base64_decode($text);
  629. break;
  630. case 'q':
  631. $text = str_replace('_', ' ', $text);
  632. preg_match_all('/=([a-f0-9]{2})/i', $text, $matches);
  633. foreach($matches[1] as $value)
  634. $text = str_replace('='.$value, chr(hexdec($value)), $text);
  635. break;
  636. }
  637. $input = str_replace($encoded, $text, $input);
  638. }
  639. return $input;
  640. }
  641. /**
  642. * Given a body string and an encoding type,
  643. * this function will decode and return it.
  644. *
  645. * @param string Input body to decode
  646. * @param string Encoding type to use.
  647. * @return string Decoded body
  648. * @access private
  649. */
  650. function _decodeBody($input, $encoding = '7bit')
  651. {
  652. switch (strtolower($encoding)) {
  653. case '7bit':
  654. return $input;
  655. break;
  656. case 'quoted-printable':
  657. return $this->_quotedPrintableDecode($input);
  658. break;
  659. case 'base64':
  660. return base64_decode($input);
  661. break;
  662. default:
  663. return $input;
  664. }
  665. }
  666. /**
  667. * Given a quoted-printable string, this
  668. * function will decode and return it.
  669. *
  670. * @param string Input body to decode
  671. * @return string Decoded body
  672. * @access private
  673. */
  674. function _quotedPrintableDecode($input)
  675. {
  676. // Remove soft line breaks
  677. $input = preg_replace("/=\r?\n/", '', $input);
  678. // Replace encoded characters
  679. $input = preg_replace('/=([a-f0-9]{2})/ie', "chr(hexdec('\\1'))", $input);
  680. return $input;
  681. }
  682. /**
  683. * Checks the input for uuencoded files and returns
  684. * an array of them. Can be called statically, eg:
  685. *
  686. * $files =& Mail_mimeDecode::uudecode($some_text);
  687. *
  688. * It will check for the begin 666 ... end syntax
  689. * however and won't just blindly decode whatever you
  690. * pass it.
  691. *
  692. * @param string Input body to look for attahcments in
  693. * @return array Decoded bodies, filenames and permissions
  694. * @access public
  695. * @author Unknown
  696. */
  697. function &uudecode($input)
  698. {
  699. // Find all uuencoded sections
  700. preg_match_all("/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us", $input, $matches);
  701. $matches_count = count($matches[3]);
  702. for ($j = 0; $j < $matches_count; ++$j) {
  703. $str = $matches[3][$j];
  704. $filename = $matches[2][$j];
  705. $fileperm = $matches[1][$j];
  706. $file = '';
  707. $str = preg_split('/\r?\n/', trim($str));
  708. $strlen = count($str);
  709. for ($i = 0; $i < $strlen; ++$i) {
  710. $pos = 1;
  711. $d = 0;
  712. $len=(int)(((ord(substr($str[$i],0,1)) -32) - ' ') & 077);
  713. while (($d + 3 <= $len) AND ($pos + 4 <= strlen($str[$i]))) {
  714. $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
  715. $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
  716. $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
  717. $c3 = (ord(substr($str[$i],$pos+3,1)) ^ 0x20);
  718. $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
  719. $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
  720. $file .= chr(((($c2 - ' ') & 077) << 6) | (($c3 - ' ') & 077));
  721. $pos += 4;
  722. $d += 3;
  723. }
  724. if (($d + 2 <= $len) && ($pos + 3 <= strlen($str[$i]))) {
  725. $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
  726. $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
  727. $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
  728. $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
  729. $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
  730. $pos += 3;
  731. $d += 2;
  732. }
  733. if (($d + 1 <= $len) && ($pos + 2 <= strlen($str[$i]))) {
  734. $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
  735. $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
  736. $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
  737. }
  738. }
  739. $files[] = array('filename' => $filename, 'fileperm' => $fileperm, 'filedata' => $file);
  740. }
  741. return $files;
  742. }
  743. /**
  744. * getSendArray() returns the arguments required for Mail::send()
  745. * used to build the arguments for a mail::send() call
  746. *
  747. * Usage:
  748. * $mailtext = Full email (for example generated by a template)
  749. * $decoder = new Mail_mimeDecode($mailtext);
  750. * $parts = $decoder->getSendArray();
  751. * if (!PEAR::isError($parts) {
  752. * list($recipents,$headers,$body) = $parts;
  753. * $mail = Mail::factory('smtp');
  754. * $mail->send($recipents,$headers,$body);
  755. * } else {
  756. * echo $parts->message;
  757. * }
  758. * @return mixed array of recipeint, headers,body or Pear_Error
  759. * @access public
  760. * @author Alan Knowles <alan@akbkhome.com>
  761. */
  762. function getSendArray()
  763. {
  764. // prevent warning if this is not set
  765. $this->_decode_headers = FALSE;
  766. $headerlist =$this->_parseHeaders($this->_header);
  767. $to = "";
  768. if (!$headerlist) {
  769. return $this->raiseError("Message did not contain headers");
  770. }
  771. foreach($headerlist as $item) {
  772. $header[$item['name']] = $item['value'];
  773. switch (strtolower($item['name'])) {
  774. case "to":
  775. case "cc":
  776. case "bcc":
  777. $to .= ",".$item['value'];
  778. default:
  779. break;
  780. }
  781. }
  782. if ($to == "") {
  783. return $this->raiseError("Message did not contain any recipents");
  784. }
  785. $to = substr($to,1);
  786. return array($to,$header,$this->_body);
  787. }
  788. /**
  789. * Returns a xml copy of the output of
  790. * Mail_mimeDecode::decode. Pass the output in as the
  791. * argument. This function can be called statically. Eg:
  792. *
  793. * $output = $obj->decode();
  794. * $xml = Mail_mimeDecode::getXML($output);
  795. *
  796. * The DTD used for this should have been in the package. Or
  797. * alternatively you can get it from cvs, or here:
  798. * http://www.phpguru.org/xmail/xmail.dtd.
  799. *
  800. * @param object Input to convert to xml. This should be the
  801. * output of the Mail_mimeDecode::decode function
  802. * @return string XML version of input
  803. * @access public
  804. */
  805. function getXML($input)
  806. {
  807. $crlf = "\r\n";
  808. $output = '<?xml version=\'1.0\'?>' . $crlf .
  809. '<!DOCTYPE email SYSTEM "http://www.phpguru.org/xmail/xmail.dtd">' . $crlf .
  810. '<email>' . $crlf .
  811. Mail_mimeDecode::_getXML($input) .
  812. '</email>';
  813. return $output;
  814. }
  815. /**
  816. * Function that does the actual conversion to xml. Does a single
  817. * mimepart at a time.
  818. *
  819. * @param object Input to convert to xml. This is a mimepart object.
  820. * It may or may not contain subparts.
  821. * @param integer Number of tabs to indent
  822. * @return string XML version of input
  823. * @access private
  824. */
  825. function _getXML($input, $indent = 1)
  826. {
  827. $htab = "\t";
  828. $crlf = "\r\n";
  829. $output = '';
  830. $headers = @(array)$input->headers;
  831. foreach ($headers as $hdr_name => $hdr_value) {
  832. // Multiple headers with this name
  833. if (is_array($headers[$hdr_name])) {
  834. $hdr_value_count = count($hdr_value);
  835. for ($i = 0; $i < $hdr_value_count; ++$i) {
  836. $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value[$i], $indent);
  837. }
  838. // Only one header of this sort
  839. } else {
  840. $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value, $indent);
  841. }
  842. }
  843. if (!empty($input->parts)) {
  844. $parts_count = count($input->parts);
  845. for ($i = 0; $i < $parts_count; ++$i) {
  846. $output .= $crlf . str_repeat($htab, $indent) . '<mimepart>' . $crlf .
  847. Mail_mimeDecode::_getXML($input->parts[$i], $indent+1) .
  848. str_repeat($htab, $indent) . '</mimepart>' . $crlf;
  849. }
  850. } elseif (isset($input->body)) {
  851. $output .= $crlf . str_repeat($htab, $indent) . '<body><![CDATA[' .
  852. $input->body . ']]></body>' . $crlf;
  853. }
  854. return $output;
  855. }
  856. /**
  857. * Helper function to _getXML(). Returns xml of a header.
  858. *
  859. * @param string Name of header
  860. * @param string Value of header
  861. * @param integer Number of tabs to indent
  862. * @return string XML version of input
  863. * @access private
  864. */
  865. function _getXML_helper($hdr_name, $hdr_value, $indent)
  866. {
  867. $htab = "\t";
  868. $crlf = "\r\n";
  869. $return = '';
  870. $new_hdr_value = ($hdr_name != 'received') ? Mail_mimeDecode::_parseHeaderValue($hdr_value) : array('value' => $hdr_value);
  871. $new_hdr_name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $hdr_name)));
  872. // Sort out any parameters
  873. if (!empty($new_hdr_value['other'])) {
  874. foreach ($new_hdr_value['other'] as $paramname => $paramvalue) {
  875. $params[] = str_repeat($htab, $indent) . $htab . '<parameter>' . $crlf .
  876. str_repeat($htab, $indent) . $htab . $htab . '<paramname>' . htmlspecialchars($paramname) . '</paramname>' . $crlf .
  877. str_repeat($htab, $indent) . $htab . $htab . '<paramvalue>' . htmlspecialchars($paramvalue) . '</paramvalue>' . $crlf .
  878. str_repeat($htab, $indent) . $htab . '</parameter>' . $crlf;
  879. }
  880. $params = implode('', $params);
  881. } else {
  882. $params = '';
  883. }
  884. $return = str_repeat($htab, $indent) . '<header>' . $crlf .
  885. str_repeat($htab, $indent) . $htab . '<headername>' . htmlspecialchars($new_hdr_name) . '</headername>' . $crlf .
  886. str_repeat($htab, $indent) . $htab . '<headervalue>' . htmlspecialchars($new_hdr_value['value']) . '</headervalue>' . $crlf .
  887. $params .
  888. str_repeat($htab, $indent) . '</header>' . $crlf;
  889. return $return;
  890. }
  891. } // End of class