mimeDecode.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 extends PEAR
  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. for ($i = 0; $i < count($parts); $i++) {
  293. list($part_header, $part_body) = $this->_splitBodyHeader($parts[$i]);
  294. $part = $this->_decode($part_header, $part_body, $default_ctype);
  295. if($part === false)
  296. $part = $this->raiseError($this->_error);
  297. $return->parts[] = $part;
  298. }
  299. break;
  300. case 'message/rfc822':
  301. if ($this->_rfc822_bodies) {
  302. $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
  303. $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body);
  304. }
  305. $obj = new Mail_mimeDecode($body);
  306. $return->parts[] = $obj->decode(array('include_bodies' => $this->_include_bodies,
  307. 'decode_bodies' => $this->_decode_bodies,
  308. 'decode_headers' => $this->_decode_headers));
  309. unset($obj);
  310. break;
  311. default:
  312. if(!isset($content_transfer_encoding['value']))
  313. $content_transfer_encoding['value'] = '7bit';
  314. $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $content_transfer_encoding['value']) : $body) : null;
  315. break;
  316. }
  317. } else {
  318. $ctype = explode('/', $default_ctype);
  319. $return->ctype_primary = $ctype[0];
  320. $return->ctype_secondary = $ctype[1];
  321. $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body) : $body) : null;
  322. }
  323. return $return;
  324. }
  325. /**
  326. * Given the output of the above function, this will return an
  327. * array of references to the parts, indexed by mime number.
  328. *
  329. * @param object $structure The structure to go through
  330. * @param string $mime_number Internal use only.
  331. * @return array Mime numbers
  332. */
  333. function &getMimeNumbers(&$structure, $no_refs = false, $mime_number = '', $prepend = '')
  334. {
  335. $return = array();
  336. if (!empty($structure->parts)) {
  337. if ($mime_number != '') {
  338. $structure->mime_id = $prepend . $mime_number;
  339. $return[$prepend . $mime_number] = &$structure;
  340. }
  341. for ($i = 0; $i < count($structure->parts); $i++) {
  342. if (!empty($structure->headers['content-type']) AND substr(strtolower($structure->headers['content-type']), 0, 8) == 'message/') {
  343. $prepend = $prepend . $mime_number . '.';
  344. $_mime_number = '';
  345. } else {
  346. $_mime_number = ($mime_number == '' ? $i + 1 : sprintf('%s.%s', $mime_number, $i + 1));
  347. }
  348. $arr = &Mail_mimeDecode::getMimeNumbers($structure->parts[$i], $no_refs, $_mime_number, $prepend);
  349. foreach ($arr as $key => $val) {
  350. $no_refs ? $return[$key] = '' : $return[$key] = &$arr[$key];
  351. }
  352. }
  353. } else {
  354. if ($mime_number == '') {
  355. $mime_number = '1';
  356. }
  357. $structure->mime_id = $prepend . $mime_number;
  358. $no_refs ? $return[$prepend . $mime_number] = '' : $return[$prepend . $mime_number] = &$structure;
  359. }
  360. return $return;
  361. }
  362. /**
  363. * Given a string containing a header and body
  364. * section, this function will split them (at the first
  365. * blank line) and return them.
  366. *
  367. * @param string Input to split apart
  368. * @return array Contains header and body section
  369. * @access private
  370. */
  371. function _splitBodyHeader($input)
  372. {
  373. if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $match)) {
  374. return array($match[1], $match[2]);
  375. }
  376. // bug #17325 - empty bodies are allowed. - we just check that at least one line
  377. // of headers exist..
  378. if (count(explode("\n",$input))) {
  379. return array($input, '');
  380. }
  381. $this->_error = 'Could not split header and body';
  382. return false;
  383. }
  384. /**
  385. * Parse headers given in $input and return
  386. * as assoc array.
  387. *
  388. * @param string Headers to parse
  389. * @return array Contains parsed headers
  390. * @access private
  391. */
  392. function _parseHeaders($input)
  393. {
  394. if ($input !== '') {
  395. // Unfold the input
  396. $input = preg_replace("/\r?\n/", "\r\n", $input);
  397. //#7065 - wrapping.. with encoded stuff.. - probably not needed,
  398. // wrapping space should only get removed if the trailing item on previous line is a
  399. // encoded character
  400. $input = preg_replace("/=\r\n(\t| )+/", '=', $input);
  401. $input = preg_replace("/\r\n(\t| )+/", ' ', $input);
  402. $headers = explode("\r\n", trim($input));
  403. foreach ($headers as $value) {
  404. $hdr_name = substr($value, 0, $pos = strpos($value, ':'));
  405. $hdr_value = substr($value, $pos+1);
  406. if($hdr_value[0] == ' ')
  407. $hdr_value = substr($hdr_value, 1);
  408. $return[] = array(
  409. 'name' => $hdr_name,
  410. 'value' => $hdr_value
  411. );
  412. }
  413. } else {
  414. $return = array();
  415. }
  416. return $return;
  417. }
  418. /**
  419. * Function to parse a header value,
  420. * extract first part, and any secondary
  421. * parts (after ;) This function is not as
  422. * robust as it could be. Eg. header comments
  423. * in the wrong place will probably break it.
  424. *
  425. * @param string Header value to parse
  426. * @return array Contains parsed result
  427. * @access private
  428. */
  429. function _parseHeaderValue($input)
  430. {
  431. if (($pos = strpos($input, ';')) === false) {
  432. $input = $this->_decode_headers ? $this->_decodeHeader($input) : $input;
  433. $return['value'] = trim($input);
  434. return $return;
  435. }
  436. $value = substr($input, 0, $pos);
  437. $value = $this->_decode_headers ? $this->_decodeHeader($value) : $value;
  438. $return['value'] = trim($value);
  439. $input = trim(substr($input, $pos+1));
  440. if (!strlen($input) > 0) {
  441. return $return;
  442. }
  443. // at this point input contains xxxx=".....";zzzz="...."
  444. // since we are dealing with quoted strings, we need to handle this properly..
  445. $i = 0;
  446. $l = strlen($input);
  447. $key = '';
  448. $val = false; // our string - including quotes..
  449. $q = false; // in quote..
  450. $lq = ''; // last quote..
  451. while ($i < $l) {
  452. $c = $input[$i];
  453. //var_dump(array('i'=>$i,'c'=>$c,'q'=>$q, 'lq'=>$lq, 'key'=>$key, 'val' =>$val));
  454. $escaped = false;
  455. if ($c == '\\') {
  456. $i++;
  457. if ($i == $l-1) { // end of string.
  458. break;
  459. }
  460. $escaped = true;
  461. $c = $input[$i];
  462. }
  463. // state - in key..
  464. if ($val === false) {
  465. if (!$escaped && $c == '=') {
  466. $val = '';
  467. $key = trim($key);
  468. $i++;
  469. continue;
  470. }
  471. if (!$escaped && $c == ';') {
  472. if ($key) { // a key without a value..
  473. $key= trim($key);
  474. $return['other'][$key] = '';
  475. $return['other'][strtolower($key)] = '';
  476. }
  477. $key = '';
  478. }
  479. $key .= $c;
  480. $i++;
  481. continue;
  482. }
  483. // state - in value.. (as $val is set..)
  484. if ($q === false) {
  485. // not in quote yet.
  486. if ((!strlen($val) || $lq !== false) && $c == ' ' || $c == "\t") {
  487. $i++;
  488. continue; // skip leading spaces after '=' or after '"'
  489. }
  490. if (!$escaped && ($c == '"' || $c == "'")) {
  491. // start quoted area..
  492. $q = $c;
  493. // in theory should not happen raw text in value part..
  494. // but we will handle it as a merged part of the string..
  495. $val = !strlen(trim($val)) ? '' : trim($val);
  496. $i++;
  497. continue;
  498. }
  499. // got end....
  500. if (!$escaped && $c == ';') {
  501. $val = trim($val);
  502. $added = false;
  503. if (preg_match('/\*[0-9]+$/', $key)) {
  504. // this is the extended aaa*0=...;aaa*1=.... code
  505. // it assumes the pieces arrive in order, and are valid...
  506. $key = preg_replace('/\*[0-9]+$/', '', $key);
  507. if (isset($return['other'][$key])) {
  508. $return['other'][$key] .= $val;
  509. if (strtolower($key) != $key) {
  510. $return['other'][strtolower($key)] .= $val;
  511. }
  512. $added = true;
  513. }
  514. // continue and use standard setters..
  515. }
  516. if (!$added) {
  517. $return['other'][$key] = $val;
  518. $return['other'][strtolower($key)] = $val;
  519. }
  520. $val = false;
  521. $key = '';
  522. $lq = false;
  523. $i++;
  524. continue;
  525. }
  526. $val .= $c;
  527. $i++;
  528. continue;
  529. }
  530. // state - in quote..
  531. if (!$escaped && $c == $q) { // potential exit state..
  532. // end of quoted string..
  533. $lq = $q;
  534. $q = false;
  535. $i++;
  536. continue;
  537. }
  538. // normal char inside of quoted string..
  539. $val.= $c;
  540. $i++;
  541. }
  542. // do we have anything left..
  543. if (strlen(trim($key)) || $val !== false) {
  544. $val = trim($val);
  545. $added = false;
  546. if ($val !== false && preg_match('/\*[0-9]+$/', $key)) {
  547. // no dupes due to our crazy regexp.
  548. $key = preg_replace('/\*[0-9]+$/', '', $key);
  549. if (isset($return['other'][$key])) {
  550. $return['other'][$key] .= $val;
  551. if (strtolower($key) != $key) {
  552. $return['other'][strtolower($key)] .= $val;
  553. }
  554. $added = true;
  555. }
  556. // continue and use standard setters..
  557. }
  558. if (!$added) {
  559. $return['other'][$key] = $val;
  560. $return['other'][strtolower($key)] = $val;
  561. }
  562. }
  563. // decode values.
  564. foreach($return['other'] as $key =>$val) {
  565. $return['other'][$key] = $this->_decode_headers ? $this->_decodeHeader($val) : $val;
  566. }
  567. //print_r($return);
  568. return $return;
  569. }
  570. /**
  571. * This function splits the input based
  572. * on the given boundary
  573. *
  574. * @param string Input to parse
  575. * @return array Contains array of resulting mime parts
  576. * @access private
  577. */
  578. function _boundarySplit($input, $boundary)
  579. {
  580. $parts = array();
  581. $bs_possible = substr($boundary, 2, -2);
  582. $bs_check = '\"' . $bs_possible . '\"';
  583. if ($boundary == $bs_check) {
  584. $boundary = $bs_possible;
  585. }
  586. $tmp = preg_split("/--".preg_quote($boundary, '/')."((?=\s)|--)/", $input);
  587. $len = count($tmp) -1;
  588. for ($i = 1; $i < $len; $i++) {
  589. if (strlen(trim($tmp[$i]))) {
  590. $parts[] = $tmp[$i];
  591. }
  592. }
  593. // add the last part on if it does not end with the 'closing indicator'
  594. if (!empty($tmp[$len]) && strlen(trim($tmp[$len])) && $tmp[$len][0] != '-') {
  595. $parts[] = $tmp[$len];
  596. }
  597. return $parts;
  598. }
  599. /**
  600. * Given a header, this function will decode it
  601. * according to RFC2047. Probably not *exactly*
  602. * conformant, but it does pass all the given
  603. * examples (in RFC2047).
  604. *
  605. * @param string Input header value to decode
  606. * @return string Decoded header value
  607. * @access private
  608. */
  609. function _decodeHeader($input)
  610. {
  611. // Remove white space between encoded-words
  612. $input = preg_replace('/(=\?[^?]+\?(q|b)\?[^?]*\?=)(\s)+=\?/i', '\1=?', $input);
  613. // For each encoded-word...
  614. while (preg_match('/(=\?([^?]+)\?(q|b)\?([^?]*)\?=)/i', $input, $matches)) {
  615. $encoded = $matches[1];
  616. $charset = $matches[2];
  617. $encoding = $matches[3];
  618. $text = $matches[4];
  619. switch (strtolower($encoding)) {
  620. case 'b':
  621. $text = base64_decode($text);
  622. break;
  623. case 'q':
  624. $text = str_replace('_', ' ', $text);
  625. preg_match_all('/=([a-f0-9]{2})/i', $text, $matches);
  626. foreach($matches[1] as $value)
  627. $text = str_replace('='.$value, chr(hexdec($value)), $text);
  628. break;
  629. }
  630. $input = str_replace($encoded, $text, $input);
  631. }
  632. return $input;
  633. }
  634. /**
  635. * Given a body string and an encoding type,
  636. * this function will decode and return it.
  637. *
  638. * @param string Input body to decode
  639. * @param string Encoding type to use.
  640. * @return string Decoded body
  641. * @access private
  642. */
  643. function _decodeBody($input, $encoding = '7bit')
  644. {
  645. switch (strtolower($encoding)) {
  646. case '7bit':
  647. return $input;
  648. break;
  649. case 'quoted-printable':
  650. return $this->_quotedPrintableDecode($input);
  651. break;
  652. case 'base64':
  653. return base64_decode($input);
  654. break;
  655. default:
  656. return $input;
  657. }
  658. }
  659. /**
  660. * Given a quoted-printable string, this
  661. * function will decode and return it.
  662. *
  663. * @param string Input body to decode
  664. * @return string Decoded body
  665. * @access private
  666. */
  667. function _quotedPrintableDecode($input)
  668. {
  669. // Remove soft line breaks
  670. $input = preg_replace("/=\r?\n/", '', $input);
  671. // Replace encoded characters
  672. $input = preg_replace('/=([a-f0-9]{2})/ie', "chr(hexdec('\\1'))", $input);
  673. return $input;
  674. }
  675. /**
  676. * Checks the input for uuencoded files and returns
  677. * an array of them. Can be called statically, eg:
  678. *
  679. * $files =& Mail_mimeDecode::uudecode($some_text);
  680. *
  681. * It will check for the begin 666 ... end syntax
  682. * however and won't just blindly decode whatever you
  683. * pass it.
  684. *
  685. * @param string Input body to look for attahcments in
  686. * @return array Decoded bodies, filenames and permissions
  687. * @access public
  688. * @author Unknown
  689. */
  690. function &uudecode($input)
  691. {
  692. // Find all uuencoded sections
  693. preg_match_all("/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us", $input, $matches);
  694. for ($j = 0; $j < count($matches[3]); $j++) {
  695. $str = $matches[3][$j];
  696. $filename = $matches[2][$j];
  697. $fileperm = $matches[1][$j];
  698. $file = '';
  699. $str = preg_split("/\r?\n/", trim($str));
  700. $strlen = count($str);
  701. for ($i = 0; $i < $strlen; $i++) {
  702. $pos = 1;
  703. $d = 0;
  704. $len=(int)(((ord(substr($str[$i],0,1)) -32) - ' ') & 077);
  705. while (($d + 3 <= $len) AND ($pos + 4 <= strlen($str[$i]))) {
  706. $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
  707. $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
  708. $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
  709. $c3 = (ord(substr($str[$i],$pos+3,1)) ^ 0x20);
  710. $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
  711. $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
  712. $file .= chr(((($c2 - ' ') & 077) << 6) | (($c3 - ' ') & 077));
  713. $pos += 4;
  714. $d += 3;
  715. }
  716. if (($d + 2 <= $len) && ($pos + 3 <= strlen($str[$i]))) {
  717. $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
  718. $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
  719. $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
  720. $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
  721. $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
  722. $pos += 3;
  723. $d += 2;
  724. }
  725. if (($d + 1 <= $len) && ($pos + 2 <= strlen($str[$i]))) {
  726. $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
  727. $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
  728. $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
  729. }
  730. }
  731. $files[] = array('filename' => $filename, 'fileperm' => $fileperm, 'filedata' => $file);
  732. }
  733. return $files;
  734. }
  735. /**
  736. * getSendArray() returns the arguments required for Mail::send()
  737. * used to build the arguments for a mail::send() call
  738. *
  739. * Usage:
  740. * $mailtext = Full email (for example generated by a template)
  741. * $decoder = new Mail_mimeDecode($mailtext);
  742. * $parts = $decoder->getSendArray();
  743. * if (!PEAR::isError($parts) {
  744. * list($recipents,$headers,$body) = $parts;
  745. * $mail = Mail::factory('smtp');
  746. * $mail->send($recipents,$headers,$body);
  747. * } else {
  748. * echo $parts->message;
  749. * }
  750. * @return mixed array of recipeint, headers,body or Pear_Error
  751. * @access public
  752. * @author Alan Knowles <alan@akbkhome.com>
  753. */
  754. function getSendArray()
  755. {
  756. // prevent warning if this is not set
  757. $this->_decode_headers = FALSE;
  758. $headerlist =$this->_parseHeaders($this->_header);
  759. $to = "";
  760. if (!$headerlist) {
  761. return $this->raiseError("Message did not contain headers");
  762. }
  763. foreach($headerlist as $item) {
  764. $header[$item['name']] = $item['value'];
  765. switch (strtolower($item['name'])) {
  766. case "to":
  767. case "cc":
  768. case "bcc":
  769. $to .= ",".$item['value'];
  770. default:
  771. break;
  772. }
  773. }
  774. if ($to == "") {
  775. return $this->raiseError("Message did not contain any recipents");
  776. }
  777. $to = substr($to,1);
  778. return array($to,$header,$this->_body);
  779. }
  780. /**
  781. * Returns a xml copy of the output of
  782. * Mail_mimeDecode::decode. Pass the output in as the
  783. * argument. This function can be called statically. Eg:
  784. *
  785. * $output = $obj->decode();
  786. * $xml = Mail_mimeDecode::getXML($output);
  787. *
  788. * The DTD used for this should have been in the package. Or
  789. * alternatively you can get it from cvs, or here:
  790. * http://www.phpguru.org/xmail/xmail.dtd.
  791. *
  792. * @param object Input to convert to xml. This should be the
  793. * output of the Mail_mimeDecode::decode function
  794. * @return string XML version of input
  795. * @access public
  796. */
  797. function getXML($input)
  798. {
  799. $crlf = "\r\n";
  800. $output = '<?xml version=\'1.0\'?>' . $crlf .
  801. '<!DOCTYPE email SYSTEM "http://www.phpguru.org/xmail/xmail.dtd">' . $crlf .
  802. '<email>' . $crlf .
  803. Mail_mimeDecode::_getXML($input) .
  804. '</email>';
  805. return $output;
  806. }
  807. /**
  808. * Function that does the actual conversion to xml. Does a single
  809. * mimepart at a time.
  810. *
  811. * @param object Input to convert to xml. This is a mimepart object.
  812. * It may or may not contain subparts.
  813. * @param integer Number of tabs to indent
  814. * @return string XML version of input
  815. * @access private
  816. */
  817. function _getXML($input, $indent = 1)
  818. {
  819. $htab = "\t";
  820. $crlf = "\r\n";
  821. $output = '';
  822. $headers = @(array)$input->headers;
  823. foreach ($headers as $hdr_name => $hdr_value) {
  824. // Multiple headers with this name
  825. if (is_array($headers[$hdr_name])) {
  826. for ($i = 0; $i < count($hdr_value); $i++) {
  827. $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value[$i], $indent);
  828. }
  829. // Only one header of this sort
  830. } else {
  831. $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value, $indent);
  832. }
  833. }
  834. if (!empty($input->parts)) {
  835. for ($i = 0; $i < count($input->parts); $i++) {
  836. $output .= $crlf . str_repeat($htab, $indent) . '<mimepart>' . $crlf .
  837. Mail_mimeDecode::_getXML($input->parts[$i], $indent+1) .
  838. str_repeat($htab, $indent) . '</mimepart>' . $crlf;
  839. }
  840. } elseif (isset($input->body)) {
  841. $output .= $crlf . str_repeat($htab, $indent) . '<body><![CDATA[' .
  842. $input->body . ']]></body>' . $crlf;
  843. }
  844. return $output;
  845. }
  846. /**
  847. * Helper function to _getXML(). Returns xml of a header.
  848. *
  849. * @param string Name of header
  850. * @param string Value of header
  851. * @param integer Number of tabs to indent
  852. * @return string XML version of input
  853. * @access private
  854. */
  855. function _getXML_helper($hdr_name, $hdr_value, $indent)
  856. {
  857. $htab = "\t";
  858. $crlf = "\r\n";
  859. $return = '';
  860. $new_hdr_value = ($hdr_name != 'received') ? Mail_mimeDecode::_parseHeaderValue($hdr_value) : array('value' => $hdr_value);
  861. $new_hdr_name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $hdr_name)));
  862. // Sort out any parameters
  863. if (!empty($new_hdr_value['other'])) {
  864. foreach ($new_hdr_value['other'] as $paramname => $paramvalue) {
  865. $params[] = str_repeat($htab, $indent) . $htab . '<parameter>' . $crlf .
  866. str_repeat($htab, $indent) . $htab . $htab . '<paramname>' . htmlspecialchars($paramname) . '</paramname>' . $crlf .
  867. str_repeat($htab, $indent) . $htab . $htab . '<paramvalue>' . htmlspecialchars($paramvalue) . '</paramvalue>' . $crlf .
  868. str_repeat($htab, $indent) . $htab . '</parameter>' . $crlf;
  869. }
  870. $params = implode('', $params);
  871. } else {
  872. $params = '';
  873. }
  874. $return = str_repeat($htab, $indent) . '<header>' . $crlf .
  875. str_repeat($htab, $indent) . $htab . '<headername>' . htmlspecialchars($new_hdr_name) . '</headername>' . $crlf .
  876. str_repeat($htab, $indent) . $htab . '<headervalue>' . htmlspecialchars($new_hdr_value['value']) . '</headervalue>' . $crlf .
  877. $params .
  878. str_repeat($htab, $indent) . '</header>' . $crlf;
  879. return $return;
  880. }
  881. } // End of class