Rfc822Header.class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. <?php
  2. /**
  3. * Rfc822Header.class.php
  4. *
  5. * This file contains functions needed to handle headers in mime messages.
  6. *
  7. * @copyright &copy; 2003-2006 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage mime
  12. * @since 1.3.2
  13. */
  14. /**
  15. * MIME header class
  16. * input: header_string or array
  17. * You must call parseHeader() function after creating object in order to fill object's
  18. * parameters.
  19. * @todo FIXME: there is no constructor function and class should ignore all input args.
  20. * @package squirrelmail
  21. * @subpackage mime
  22. * @since 1.3.0
  23. */
  24. class Rfc822Header {
  25. /**
  26. * Date header
  27. * @var mixed
  28. */
  29. var $date = -1;
  30. /**
  31. * Subject header
  32. * @var string
  33. */
  34. var $subject = '';
  35. /**
  36. * From header
  37. * @var array
  38. */
  39. var $from = array();
  40. /**
  41. * @var mixed
  42. */
  43. var $sender = '';
  44. /**
  45. * Reply-To header
  46. * @var array
  47. */
  48. var $reply_to = array();
  49. /**
  50. * Mail-Followup-To header
  51. * @var array
  52. */
  53. var $mail_followup_to = array();
  54. /**
  55. * To header
  56. * @var array
  57. */
  58. var $to = array();
  59. /**
  60. * Cc header
  61. * @var array
  62. */
  63. var $cc = array();
  64. /**
  65. * Bcc header
  66. * @var array
  67. */
  68. var $bcc = array();
  69. /**
  70. * In-reply-to header
  71. * @var string
  72. */
  73. var $in_reply_to = '';
  74. /**
  75. * Message-ID header
  76. * @var string
  77. */
  78. var $message_id = '';
  79. /**
  80. * References header
  81. * @var string
  82. */
  83. var $references = '';
  84. /**
  85. * @var mixed
  86. */
  87. var $mime = false;
  88. /**
  89. * Content Type object
  90. * @var object
  91. */
  92. var $content_type = '';
  93. /**
  94. * @var mixed
  95. */
  96. var $disposition = '';
  97. /**
  98. * X-Mailer header
  99. * @var string
  100. */
  101. var $xmailer = '';
  102. /**
  103. * Priority header
  104. * @var integer
  105. */
  106. var $priority = 3;
  107. /**
  108. * Disposition notification for requesting message delivery notification (MDN)
  109. * @var mixed
  110. */
  111. var $dnt = '';
  112. /**
  113. * Delivery notification (DR)
  114. * @var mixed
  115. */
  116. var $drnt = '';
  117. /**
  118. * @var mixed
  119. */
  120. var $encoding = '';
  121. /**
  122. * @var mixed
  123. */
  124. var $content_id = '';
  125. /**
  126. * @var mixed
  127. */
  128. var $content_desc = '';
  129. /**
  130. * @var mixed
  131. */
  132. var $mlist = array();
  133. /**
  134. * Extra header
  135. * only needed for constructing headers in delivery class
  136. * @var array
  137. */
  138. var $more_headers = array();
  139. /**
  140. * @param mixed $hdr string or array with message headers
  141. */
  142. function parseHeader($hdr) {
  143. if (is_array($hdr)) {
  144. $hdr = implode('', $hdr);
  145. }
  146. /* First we replace \r\n by \n and unfold the header */
  147. /* FIXME: unfolding header with multiple spaces "\n( +)" */
  148. $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
  149. /* Now we can make a new header array with */
  150. /* each element representing a headerline */
  151. $hdr = explode("\n" , $hdr);
  152. foreach ($hdr as $line) {
  153. $pos = strpos($line, ':');
  154. if ($pos > 0) {
  155. $field = substr($line, 0, $pos);
  156. if (!strstr($field,' ')) { /* valid field */
  157. $value = trim(substr($line, $pos+1));
  158. $this->parseField($field, $value);
  159. }
  160. }
  161. }
  162. if (!is_object($this->content_type)) {
  163. $this->parseContentType('text/plain; charset=us-ascii');
  164. }
  165. }
  166. /**
  167. * @param string $value
  168. * @return string
  169. */
  170. function stripComments($value) {
  171. $result = '';
  172. $cnt = strlen($value);
  173. for ($i = 0; $i < $cnt; ++$i) {
  174. switch ($value{$i}) {
  175. case '"':
  176. $result .= '"';
  177. while ((++$i < $cnt) && ($value{$i} != '"')) {
  178. if ($value{$i} == '\\') {
  179. $result .= '\\';
  180. ++$i;
  181. }
  182. $result .= $value{$i};
  183. }
  184. if($i < $cnt) {
  185. $result .= $value{$i};
  186. }
  187. break;
  188. case '(':
  189. $depth = 1;
  190. while (($depth > 0) && (++$i < $cnt)) {
  191. switch($value{$i}) {
  192. case '\\':
  193. ++$i;
  194. break;
  195. case '(':
  196. ++$depth;
  197. break;
  198. case ')':
  199. --$depth;
  200. break;
  201. default:
  202. break;
  203. }
  204. }
  205. break;
  206. default:
  207. $result .= $value{$i};
  208. break;
  209. }
  210. }
  211. return $result;
  212. }
  213. /**
  214. * Parse header field according to field type
  215. * @param string $field field name
  216. * @param string $value field value
  217. */
  218. function parseField($field, $value) {
  219. $field = strtolower($field);
  220. switch($field) {
  221. case 'date':
  222. $value = $this->stripComments($value);
  223. $d = strtr($value, array(' ' => ' '));
  224. $d = explode(' ', $d);
  225. $this->date = getTimeStamp($d);
  226. break;
  227. case 'subject':
  228. $this->subject = $value;
  229. break;
  230. case 'from':
  231. $this->from = $this->parseAddress($value,true);
  232. break;
  233. case 'sender':
  234. $this->sender = $this->parseAddress($value);
  235. break;
  236. case 'reply-to':
  237. $this->reply_to = $this->parseAddress($value, true);
  238. break;
  239. case 'mail-followup-to':
  240. $this->mail_followup_to = $this->parseAddress($value, true);
  241. break;
  242. case 'to':
  243. $this->to = $this->parseAddress($value, true);
  244. break;
  245. case 'cc':
  246. $this->cc = $this->parseAddress($value, true);
  247. break;
  248. case 'bcc':
  249. $this->bcc = $this->parseAddress($value, true);
  250. break;
  251. case 'in-reply-to':
  252. $this->in_reply_to = $value;
  253. break;
  254. case 'message-id':
  255. $value = $this->stripComments($value);
  256. $this->message_id = $value;
  257. break;
  258. case 'references':
  259. $value = $this->stripComments($value);
  260. $this->references = $value;
  261. break;
  262. case 'x-confirm-reading-to':
  263. case 'disposition-notification-to':
  264. $value = $this->stripComments($value);
  265. $this->dnt = $this->parseAddress($value);
  266. break;
  267. case 'return-receipt-to':
  268. $value = $this->stripComments($value);
  269. $this->drnt = $this->parseAddress($value);
  270. break;
  271. case 'mime-version':
  272. $value = $this->stripComments($value);
  273. $value = str_replace(' ', '', $value);
  274. $this->mime = ($value == '1.0' ? true : $this->mime);
  275. break;
  276. case 'content-type':
  277. $value = $this->stripComments($value);
  278. $this->parseContentType($value);
  279. break;
  280. case 'content-disposition':
  281. $value = $this->stripComments($value);
  282. $this->parseDisposition($value);
  283. break;
  284. case 'content-transfer-encoding':
  285. $this->encoding = $value;
  286. break;
  287. case 'content-description':
  288. $this->content_desc = $value;
  289. break;
  290. case 'content-id':
  291. $value = $this->stripComments($value);
  292. $this->content_id = $value;
  293. break;
  294. case 'user-agent':
  295. case 'x-mailer':
  296. $this->xmailer = $value;
  297. break;
  298. case 'x-priority':
  299. case 'importance':
  300. case 'priority':
  301. $this->priority = $this->parsePriority($value);
  302. break;
  303. case 'list-post':
  304. $value = $this->stripComments($value);
  305. $this->mlist('post', $value);
  306. break;
  307. case 'list-reply':
  308. $value = $this->stripComments($value);
  309. $this->mlist('reply', $value);
  310. break;
  311. case 'list-subscribe':
  312. $value = $this->stripComments($value);
  313. $this->mlist('subscribe', $value);
  314. break;
  315. case 'list-unsubscribe':
  316. $value = $this->stripComments($value);
  317. $this->mlist('unsubscribe', $value);
  318. break;
  319. case 'list-archive':
  320. $value = $this->stripComments($value);
  321. $this->mlist('archive', $value);
  322. break;
  323. case 'list-owner':
  324. $value = $this->stripComments($value);
  325. $this->mlist('owner', $value);
  326. break;
  327. case 'list-help':
  328. $value = $this->stripComments($value);
  329. $this->mlist('help', $value);
  330. break;
  331. case 'list-id':
  332. $value = $this->stripComments($value);
  333. $this->mlist('id', $value);
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. /**
  340. * @param string $address
  341. * @return array
  342. */
  343. function getAddressTokens($address) {
  344. $aTokens = array();
  345. $aSpecials = array('(' ,'<' ,',' ,';' ,':');
  346. $aReplace = array(' (',' <',' ,',' ;',' :');
  347. $address = str_replace($aSpecials,$aReplace,$address);
  348. $iCnt = strlen($address);
  349. $i = 0;
  350. while ($i < $iCnt) {
  351. $cChar = $address{$i};
  352. switch($cChar)
  353. {
  354. case '<':
  355. $iEnd = strpos($address,'>',$i+1);
  356. if (!$iEnd) {
  357. $sToken = substr($address,$i);
  358. $i = $iCnt;
  359. } else {
  360. $sToken = substr($address,$i,$iEnd - $i +1);
  361. $i = $iEnd;
  362. }
  363. $sToken = str_replace($aReplace, $aSpecials,$sToken);
  364. if ($sToken) $aTokens[] = $sToken;
  365. break;
  366. case '"':
  367. $iEnd = strpos($address,$cChar,$i+1);
  368. if ($iEnd) {
  369. // skip escaped quotes
  370. $prev_char = $address{$iEnd-1};
  371. while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
  372. $iEnd = strpos($address,$cChar,$iEnd+1);
  373. if ($iEnd) {
  374. $prev_char = $address{$iEnd-1};
  375. } else {
  376. $prev_char = false;
  377. }
  378. }
  379. }
  380. if (!$iEnd) {
  381. $sToken = substr($address,$i);
  382. $i = $iCnt;
  383. } else {
  384. // also remove the surrounding quotes
  385. $sToken = substr($address,$i+1,$iEnd - $i -1);
  386. $i = $iEnd;
  387. }
  388. $sToken = str_replace($aReplace, $aSpecials,$sToken);
  389. if ($sToken) $aTokens[] = $sToken;
  390. break;
  391. case '(':
  392. array_pop($aTokens); //remove inserted space
  393. $iEnd = strpos($address,')',$i);
  394. if (!$iEnd) {
  395. $sToken = substr($address,$i);
  396. $i = $iCnt;
  397. } else {
  398. $iDepth = 1;
  399. $iComment = $i;
  400. while (($iDepth > 0) && (++$iComment < $iCnt)) {
  401. $cCharComment = $address{$iComment};
  402. switch($cCharComment) {
  403. case '\\':
  404. ++$iComment;
  405. break;
  406. case '(':
  407. ++$iDepth;
  408. break;
  409. case ')':
  410. --$iDepth;
  411. break;
  412. default:
  413. break;
  414. }
  415. }
  416. if ($iDepth == 0) {
  417. $sToken = substr($address,$i,$iComment - $i +1);
  418. $i = $iComment;
  419. } else {
  420. $sToken = substr($address,$i,$iEnd - $i + 1);
  421. $i = $iEnd;
  422. }
  423. }
  424. // check the next token in case comments appear in the middle of email addresses
  425. $prevToken = end($aTokens);
  426. if (!in_array($prevToken,$aSpecials,true)) {
  427. if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) {
  428. $iEnd = strpos($address,' ',$i+1);
  429. if ($iEnd) {
  430. $sNextToken = trim(substr($address,$i+1,$iEnd - $i -1));
  431. $i = $iEnd-1;
  432. } else {
  433. $sNextToken = trim(substr($address,$i+1));
  434. $i = $iCnt;
  435. }
  436. // remove the token
  437. array_pop($aTokens);
  438. // create token and add it again
  439. $sNewToken = $prevToken . $sNextToken;
  440. if($sNewToken) $aTokens[] = $sNewToken;
  441. }
  442. }
  443. $sToken = str_replace($aReplace, $aSpecials,$sToken);
  444. if ($sToken) $aTokens[] = $sToken;
  445. break;
  446. case ',':
  447. case ':':
  448. case ';':
  449. case ' ':
  450. $aTokens[] = $cChar;
  451. break;
  452. default:
  453. $iEnd = strpos($address,' ',$i+1);
  454. if ($iEnd) {
  455. $sToken = trim(substr($address,$i,$iEnd - $i));
  456. $i = $iEnd-1;
  457. } else {
  458. $sToken = trim(substr($address,$i));
  459. $i = $iCnt;
  460. }
  461. if ($sToken) $aTokens[] = $sToken;
  462. }
  463. ++$i;
  464. }
  465. return $aTokens;
  466. }
  467. /**
  468. * @param array $aStack
  469. * @param array $aComment
  470. * @param string $sEmail
  471. * @param string $sGroup
  472. * @return object AddressStructure object
  473. */
  474. function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
  475. //$aStack=explode(' ',implode('',$aStack));
  476. if (!$sEmail) {
  477. while (count($aStack) && !$sEmail) {
  478. $sEmail = trim(array_pop($aStack));
  479. }
  480. }
  481. if (count($aStack)) {
  482. $sPersonal = trim(implode('',$aStack));
  483. } else {
  484. $sPersonal = '';
  485. }
  486. if (!$sPersonal && count($aComment)) {
  487. $sComment = trim(implode(' ',$aComment));
  488. $sPersonal .= $sComment;
  489. }
  490. $oAddr =& new AddressStructure();
  491. if ($sPersonal && substr($sPersonal,0,2) == '=?') {
  492. $oAddr->personal = encodeHeader($sPersonal);
  493. } else {
  494. $oAddr->personal = $sPersonal;
  495. }
  496. // $oAddr->group = $sGroup;
  497. $iPosAt = strpos($sEmail,'@');
  498. if ($iPosAt) {
  499. $oAddr->mailbox = substr($sEmail, 0, $iPosAt);
  500. $oAddr->host = substr($sEmail, $iPosAt+1);
  501. } else {
  502. $oAddr->mailbox = $sEmail;
  503. $oAddr->host = false;
  504. }
  505. $sEmail = '';
  506. $aStack = $aComment = array();
  507. return $oAddr;
  508. }
  509. /**
  510. * recursive function for parsing address strings and storing them in an address stucture object.
  511. * personal name: encoded: =?charset?Q|B?string?=
  512. * quoted: "string"
  513. * normal: string
  514. * email : <mailbox@host>
  515. * : mailbox@host
  516. * This function is also used for validating addresses returned from compose
  517. * That's also the reason that the function became a little bit huge
  518. * @param string $address
  519. * @param boolean $ar return array instead of only the first element
  520. * @param array $addr_ar (obsolete) array with parsed addresses
  521. * @param string $group (obsolete)
  522. * @param string $host default domainname in case of addresses without a domainname
  523. * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
  524. * @return mixed array with AddressStructure objects or only one address_structure object.
  525. */
  526. function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
  527. $aTokens = $this->getAddressTokens($address);
  528. $sPersonal = $sEmail = $sGroup = '';
  529. $aStack = $aComment = array();
  530. foreach ($aTokens as $sToken) {
  531. $cChar = $sToken{0};
  532. switch ($cChar)
  533. {
  534. case '=':
  535. case '"':
  536. case ' ':
  537. $aStack[] = $sToken;
  538. break;
  539. case '(':
  540. $aComment[] = substr($sToken,1,-1);
  541. break;
  542. case ';':
  543. if ($sGroup) {
  544. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
  545. $oAddr = end($aAddress);
  546. if(!$oAddr || ((isset($oAddr)) && !$oAddr->mailbox && !$oAddr->personal)) {
  547. $sEmail = $sGroup . ':;';
  548. }
  549. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
  550. $sGroup = '';
  551. $aStack = $aComment = array();
  552. break;
  553. }
  554. case ',':
  555. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
  556. break;
  557. case ':':
  558. $sGroup = trim(implode(' ',$aStack));
  559. $sGroup = preg_replace('/\s+/',' ',$sGroup);
  560. $aStack = array();
  561. break;
  562. case '<':
  563. $sEmail = trim(substr($sToken,1,-1));
  564. break;
  565. case '>':
  566. /* skip */
  567. break;
  568. default: $aStack[] = $sToken; break;
  569. }
  570. }
  571. /* now do the action again for the last address */
  572. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
  573. /* try to lookup the addresses in case of invalid email addresses */
  574. $aProcessedAddress = array();
  575. foreach ($aAddress as $oAddr) {
  576. $aAddrBookAddress = array();
  577. if (!$oAddr->host) {
  578. $grouplookup = false;
  579. if ($lookup) {
  580. $aAddr = call_user_func_array($lookup,array($oAddr->mailbox));
  581. if (isset($aAddr['email'])) {
  582. if (strpos($aAddr['email'],',')) {
  583. $grouplookup = true;
  584. $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
  585. } else {
  586. $iPosAt = strpos($aAddr['email'], '@');
  587. $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
  588. $oAddr->host = substr($aAddr['email'], $iPosAt+1);
  589. if (isset($aAddr['name'])) {
  590. $oAddr->personal = $aAddr['name'];
  591. } else {
  592. $oAddr->personal = encodeHeader($sPersonal);
  593. }
  594. }
  595. }
  596. }
  597. if (!$grouplookup && !$oAddr->mailbox) {
  598. $oAddr->mailbox = trim($sEmail);
  599. if ($sHost && $oAddr->mailbox) {
  600. $oAddr->host = $sHost;
  601. }
  602. } else if (!$grouplookup && !$oAddr->host) {
  603. if ($sHost && $oAddr->mailbox) {
  604. $oAddr->host = $sHost;
  605. }
  606. }
  607. }
  608. if (!$aAddrBookAddress && $oAddr->mailbox) {
  609. $aProcessedAddress[] = $oAddr;
  610. } else {
  611. $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
  612. }
  613. }
  614. if ($ar) {
  615. return $aProcessedAddress;
  616. } else {
  617. return $aProcessedAddress[0];
  618. }
  619. }
  620. /**
  621. * Normalise the different Priority headers into a uniform value,
  622. * namely that of the X-Priority header (1, 3, 5). Supports:
  623. * Priority, X-Priority, Importance.
  624. * X-MS-Mail-Priority is not parsed because it always coincides
  625. * with one of the other headers.
  626. *
  627. * NOTE: this is actually a duplicate from the function in
  628. * functions/imap_messages. I'm not sure if it's ok here to call
  629. * that function?
  630. * @param string $sValue literal priority name
  631. * @return integer
  632. */
  633. function parsePriority($sValue) {
  634. // don't use function call inside array_shift.
  635. $aValue = split('/\w/',trim($sValue));
  636. $value = strtolower(array_shift($aValue));
  637. if ( is_numeric($value) ) {
  638. return $value;
  639. }
  640. if ( $value == 'urgent' || $value == 'high' ) {
  641. return 1;
  642. } elseif ( $value == 'non-urgent' || $value == 'low' ) {
  643. return 5;
  644. }
  645. // default is normal priority
  646. return 3;
  647. }
  648. /**
  649. * @param string $value content type header
  650. */
  651. function parseContentType($value) {
  652. $pos = strpos($value, ';');
  653. $props = '';
  654. if ($pos > 0) {
  655. $type = trim(substr($value, 0, $pos));
  656. $props = trim(substr($value, $pos+1));
  657. } else {
  658. $type = $value;
  659. }
  660. $content_type = new ContentType($type);
  661. if ($props) {
  662. $properties = $this->parseProperties($props);
  663. if (!isset($properties['charset'])) {
  664. $properties['charset'] = 'us-ascii';
  665. }
  666. $content_type->properties = $this->parseProperties($props);
  667. }
  668. $this->content_type = $content_type;
  669. }
  670. /**
  671. * RFC2184
  672. * @param array $aParameters
  673. * @return array
  674. */
  675. function processParameters($aParameters) {
  676. $aResults = array();
  677. $aCharset = array();
  678. // handle multiline parameters
  679. foreach($aParameters as $key => $value) {
  680. if ($iPos = strpos($key,'*')) {
  681. $sKey = substr($key,0,$iPos);
  682. if (!isset($aResults[$sKey])) {
  683. $aResults[$sKey] = $value;
  684. if (substr($key,-1) == '*') { // parameter contains language/charset info
  685. $aCharset[] = $sKey;
  686. }
  687. } else {
  688. $aResults[$sKey] .= $value;
  689. }
  690. } else {
  691. $aResults[$key] = $value;
  692. }
  693. }
  694. foreach ($aCharset as $key) {
  695. $value = $aResults[$key];
  696. // extract the charset & language
  697. $charset = substr($value,0,strpos($value,"'"));
  698. $value = substr($value,strlen($charset)+1);
  699. $language = substr($value,0,strpos($value,"'"));
  700. $value = substr($value,strlen($charset)+1);
  701. /* FIXME: What's the status of charset decode with language information ????
  702. * Maybe language information contains only ascii text and charset_decode()
  703. * only runs htmlspecialchars() on it. If it contains 8bit information, you
  704. * get html encoded text in charset used by selected translation.
  705. */
  706. $value = charset_decode($charset,$value);
  707. $aResults[$key] = $value;
  708. }
  709. return $aResults;
  710. }
  711. /**
  712. * @param string $value
  713. * @return array
  714. */
  715. function parseProperties($value) {
  716. $propArray = explode(';', $value);
  717. $propResultArray = array();
  718. foreach ($propArray as $prop) {
  719. $prop = trim($prop);
  720. $pos = strpos($prop, '=');
  721. if ($pos > 0) {
  722. $key = trim(substr($prop, 0, $pos));
  723. $val = trim(substr($prop, $pos+1));
  724. if (strlen($val) > 0 && $val{0} == '"') {
  725. $val = substr($val, 1, -1);
  726. }
  727. $propResultArray[$key] = $val;
  728. }
  729. }
  730. return $this->processParameters($propResultArray);
  731. }
  732. /**
  733. * Fills disposition object in rfc822Header object
  734. * @param string $value
  735. */
  736. function parseDisposition($value) {
  737. $pos = strpos($value, ';');
  738. $props = '';
  739. if ($pos > 0) {
  740. $name = trim(substr($value, 0, $pos));
  741. $props = trim(substr($value, $pos+1));
  742. } else {
  743. $name = $value;
  744. }
  745. $props_a = $this->parseProperties($props);
  746. $disp = new Disposition($name);
  747. $disp->properties = $props_a;
  748. $this->disposition = $disp;
  749. }
  750. /**
  751. * Fills mlist array keys in rfc822Header object
  752. * @param string $field
  753. * @param string $value
  754. */
  755. function mlist($field, $value) {
  756. $res_a = array();
  757. $value_a = explode(',', $value);
  758. foreach ($value_a as $val) {
  759. $val = trim($val);
  760. if ($val{0} == '<') {
  761. $val = substr($val, 1, -1);
  762. }
  763. if (substr($val, 0, 7) == 'mailto:') {
  764. $res_a['mailto'] = substr($val, 7);
  765. } else {
  766. $res_a['href'] = $val;
  767. }
  768. }
  769. $this->mlist[$field] = $res_a;
  770. }
  771. /**
  772. * function to get the address strings out of the header.
  773. * example1: header->getAddr_s('to').
  774. * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
  775. * @param mixed $arr string or array of strings
  776. * @param string $separator
  777. * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
  778. * @return string
  779. */
  780. function getAddr_s($arr, $separator = ',',$encoded=false) {
  781. $s = '';
  782. if (is_array($arr)) {
  783. foreach($arr as $arg) {
  784. if ($this->getAddr_s($arg, $separator, $encoded)) {
  785. $s .= $separator;
  786. }
  787. }
  788. $s = ($s ? substr($s, 2) : $s);
  789. } else {
  790. $addr = $this->{$arr};
  791. if (is_array($addr)) {
  792. foreach ($addr as $addr_o) {
  793. if (is_object($addr_o)) {
  794. if ($encoded) {
  795. $s .= $addr_o->getEncodedAddress() . $separator;
  796. } else {
  797. $s .= $addr_o->getAddress() . $separator;
  798. }
  799. }
  800. }
  801. $s = substr($s, 0, -strlen($separator));
  802. } else {
  803. if (is_object($addr)) {
  804. if ($encoded) {
  805. $s .= $addr->getEncodedAddress();
  806. } else {
  807. $s .= $addr->getAddress();
  808. }
  809. }
  810. }
  811. }
  812. return $s;
  813. }
  814. /**
  815. * function to get the array of addresses out of the header.
  816. * @param mixed $arg string or array of strings
  817. * @param array $excl_arr array of excluded email addresses
  818. * @param array $arr array of added email addresses
  819. * @return array
  820. */
  821. function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
  822. if (is_array($arg)) {
  823. foreach($arg as $argument) {
  824. $arr = $this->getAddr_a($argument, $excl_arr, $arr);
  825. }
  826. } else {
  827. $addr = $this->{$arg};
  828. if (is_array($addr)) {
  829. foreach ($addr as $next_addr) {
  830. if (is_object($next_addr)) {
  831. if (isset($next_addr->host) && ($next_addr->host != '')) {
  832. $email = $next_addr->mailbox . '@' . $next_addr->host;
  833. } else {
  834. $email = $next_addr->mailbox;
  835. }
  836. $email = strtolower($email);
  837. if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
  838. $arr[$email] = $next_addr->personal;
  839. }
  840. }
  841. }
  842. } else {
  843. if (is_object($addr)) {
  844. $email = $addr->mailbox;
  845. $email .= (isset($addr->host) ? '@' . $addr->host : '');
  846. $email = strtolower($email);
  847. if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
  848. $arr[$email] = $addr->personal;
  849. }
  850. }
  851. }
  852. }
  853. return $arr;
  854. }
  855. /**
  856. * @param mixed $address array or string
  857. * @param boolean $recurs
  858. * @return mixed array, boolean
  859. * @since 1.3.2
  860. */
  861. function findAddress($address, $recurs = false) {
  862. $result = false;
  863. if (is_array($address)) {
  864. $i=0;
  865. foreach($address as $argument) {
  866. $match = $this->findAddress($argument, true);
  867. if ($match[1]) {
  868. return $i;
  869. } else {
  870. if (count($match[0]) && !$result) {
  871. $result = $i;
  872. }
  873. }
  874. ++$i;
  875. }
  876. } else {
  877. if (!is_array($this->cc)) $this->cc = array();
  878. $srch_addr = $this->parseAddress($address);
  879. $results = array();
  880. foreach ($this->to as $to) {
  881. if ($to->host == $srch_addr->host) {
  882. if ($to->mailbox == $srch_addr->mailbox) {
  883. $results[] = $srch_addr;
  884. if ($to->personal == $srch_addr->personal) {
  885. if ($recurs) {
  886. return array($results, true);
  887. } else {
  888. return true;
  889. }
  890. }
  891. }
  892. }
  893. }
  894. foreach ($this->cc as $cc) {
  895. if ($cc->host == $srch_addr->host) {
  896. if ($cc->mailbox == $srch_addr->mailbox) {
  897. $results[] = $srch_addr;
  898. if ($cc->personal == $srch_addr->personal) {
  899. if ($recurs) {
  900. return array($results, true);
  901. } else {
  902. return true;
  903. }
  904. }
  905. }
  906. }
  907. }
  908. if ($recurs) {
  909. return array($results, false);
  910. } elseif (count($result)) {
  911. return true;
  912. } else {
  913. return false;
  914. }
  915. }
  916. //exit;
  917. return $result;
  918. }
  919. /**
  920. * @param string $type0 media type
  921. * @param string $type1 media subtype
  922. * @return array media properties
  923. * @todo check use of media type arguments
  924. */
  925. function getContentType($type0, $type1) {
  926. $type0 = $this->content_type->type0;
  927. $type1 = $this->content_type->type1;
  928. return $this->content_type->properties;
  929. }
  930. }