Rfc822Header.class.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. <?php
  2. /**
  3. * Rfc822Header.class.php
  4. *
  5. * This file contains functions needed to handle headers in mime messages.
  6. *
  7. * @copyright 2003-2025 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. * All headers, unparsed
  27. * @var array
  28. */
  29. var $raw_headers = array();
  30. /**
  31. * Date header
  32. * @var mixed
  33. */
  34. var $date = -1;
  35. /**
  36. * Original date header as fallback for unparsable dates
  37. * @var mixed
  38. */
  39. var $date_unparsed = '';
  40. /**
  41. * Subject header
  42. * @var string
  43. */
  44. var $subject = '';
  45. /**
  46. * From header
  47. * @var array
  48. */
  49. var $from = array();
  50. /**
  51. * @var mixed
  52. */
  53. var $sender = '';
  54. /**
  55. * Reply-To header
  56. * @var array
  57. */
  58. var $reply_to = array();
  59. /**
  60. * Mail-Followup-To header
  61. * @var array
  62. */
  63. var $mail_followup_to = array();
  64. /**
  65. * To header
  66. * @var array
  67. */
  68. var $to = array();
  69. /**
  70. * Cc header
  71. * @var array
  72. */
  73. var $cc = array();
  74. /**
  75. * Bcc header
  76. * @var array
  77. */
  78. var $bcc = array();
  79. /**
  80. * In-reply-to header
  81. * @var string
  82. */
  83. var $in_reply_to = '';
  84. /**
  85. * Message-ID header
  86. * @var string
  87. */
  88. var $message_id = '';
  89. /**
  90. * References header
  91. * @var string
  92. */
  93. var $references = '';
  94. /**
  95. * @var mixed
  96. */
  97. var $mime = false;
  98. /**
  99. * Content Type object
  100. * @var object
  101. */
  102. var $content_type = '';
  103. /**
  104. * @var mixed
  105. */
  106. var $disposition = '';
  107. /**
  108. * X-Mailer header
  109. * @var string
  110. */
  111. var $xmailer = '';
  112. /**
  113. * Priority header
  114. * @var integer
  115. */
  116. var $priority = 3;
  117. /**
  118. * Disposition notification for requesting message delivery notification (MDN)
  119. * @var mixed
  120. */
  121. var $dnt = '';
  122. /**
  123. * Address for requesting message delivery status notification (DSN)
  124. * @var mixed
  125. */
  126. var $dsn = '';
  127. /**
  128. * @var mixed
  129. */
  130. var $encoding = '';
  131. /**
  132. * @var mixed
  133. */
  134. var $content_id = '';
  135. /**
  136. * @var mixed
  137. */
  138. var $content_desc = '';
  139. /**
  140. * @var mixed
  141. */
  142. var $mlist = array();
  143. /**
  144. * SpamAssassin 'x-spam-status' header
  145. * @var mixed
  146. */
  147. var $x_spam_status = array();
  148. /**
  149. * Extra header
  150. * only needed for constructing headers in delivery class
  151. * @var array
  152. */
  153. var $more_headers = array();
  154. /**
  155. * @param mixed $hdr string or array with message headers
  156. */
  157. function parseHeader($hdr) {
  158. if (is_array($hdr)) {
  159. $hdr = implode('', $hdr);
  160. }
  161. /* First we replace \r\n by \n and unfold the header */
  162. /* FIXME: unfolding header with multiple spaces "\n( +)" */
  163. $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr));
  164. /* Now we can make a new header array with */
  165. /* each element representing a headerline */
  166. $hdr = explode("\n" , $hdr);
  167. foreach ($hdr as $line) {
  168. $pos = strpos($line, ':');
  169. if ($pos > 0) {
  170. $this->raw_headers[] = $line;
  171. $field = substr($line, 0, $pos);
  172. if (!strstr($field,' ')) { /* valid field */
  173. $value = trim(substr($line, $pos+1));
  174. $this->parseField($field, $value);
  175. }
  176. }
  177. }
  178. if (!is_object($this->content_type)) {
  179. $this->parseContentType('text/plain; charset=us-ascii');
  180. }
  181. }
  182. /**
  183. * @param string $value
  184. * @return string
  185. */
  186. function stripComments($value) {
  187. $result = '';
  188. $cnt = strlen($value);
  189. for ($i = 0; $i < $cnt; ++$i) {
  190. switch ($value[$i]) {
  191. case '"':
  192. $result .= '"';
  193. while ((++$i < $cnt) && ($value[$i] != '"')) {
  194. if ($value[$i] == '\\') {
  195. $result .= '\\';
  196. ++$i;
  197. }
  198. $result .= $value[$i];
  199. }
  200. if($i < $cnt) {
  201. $result .= $value[$i];
  202. }
  203. break;
  204. case '(':
  205. $depth = 1;
  206. while (($depth > 0) && (++$i < $cnt)) {
  207. switch($value[$i]) {
  208. case '\\':
  209. ++$i;
  210. break;
  211. case '(':
  212. ++$depth;
  213. break;
  214. case ')':
  215. --$depth;
  216. break;
  217. default:
  218. break;
  219. }
  220. }
  221. break;
  222. default:
  223. $result .= $value[$i];
  224. break;
  225. }
  226. }
  227. return $result;
  228. }
  229. /**
  230. * Parse header field according to field type
  231. * @param string $field field name
  232. * @param string $value field value
  233. */
  234. function parseField($field, $value) {
  235. $field = strtolower($field);
  236. switch($field) {
  237. case 'date':
  238. $value = $this->stripComments($value);
  239. $d = strtr($value, array(' ' => ' '));
  240. $d = explode(' ', $d);
  241. $this->date = getTimeStamp($d);
  242. $this->date_unparsed = strtr($value,'<>',' ');
  243. break;
  244. case 'subject':
  245. $this->subject = $value;
  246. break;
  247. case 'from':
  248. $this->from = $this->parseAddress($value,true);
  249. break;
  250. case 'sender':
  251. $this->sender = $this->parseAddress($value);
  252. break;
  253. case 'reply-to':
  254. $this->reply_to = $this->parseAddress($value, true);
  255. break;
  256. case 'mail-followup-to':
  257. $this->mail_followup_to = $this->parseAddress($value, true);
  258. break;
  259. case 'to':
  260. $this->to = $this->parseAddress($value, true);
  261. break;
  262. case 'cc':
  263. $this->cc = $this->parseAddress($value, true);
  264. break;
  265. case 'bcc':
  266. $this->bcc = $this->parseAddress($value, true);
  267. break;
  268. case 'in-reply-to':
  269. $this->in_reply_to = $value;
  270. break;
  271. case 'message-id':
  272. $value = $this->stripComments($value);
  273. $this->message_id = $value;
  274. break;
  275. case 'references':
  276. $value = $this->stripComments($value);
  277. $this->references = $value;
  278. break;
  279. case 'x-confirm-reading-to':
  280. case 'disposition-notification-to':
  281. $value = $this->stripComments($value);
  282. $this->dnt = $this->parseAddress($value);
  283. break;
  284. case 'return-receipt-to':
  285. $value = $this->stripComments($value);
  286. $this->dsn = $this->parseAddress($value);
  287. break;
  288. case 'mime-version':
  289. $value = $this->stripComments($value);
  290. $value = str_replace(' ', '', $value);
  291. $this->mime = ($value == '1.0' ? true : $this->mime);
  292. break;
  293. case 'content-type':
  294. $value = $this->stripComments($value);
  295. $this->parseContentType($value);
  296. break;
  297. case 'content-disposition':
  298. $value = $this->stripComments($value);
  299. $this->parseDisposition($value);
  300. break;
  301. case 'content-transfer-encoding':
  302. $this->encoding = $value;
  303. break;
  304. case 'content-description':
  305. $this->content_desc = $value;
  306. break;
  307. case 'content-id':
  308. $value = $this->stripComments($value);
  309. $this->content_id = $value;
  310. break;
  311. case 'user-agent':
  312. case 'x-mailer':
  313. $this->xmailer = $value;
  314. break;
  315. case 'x-priority':
  316. case 'importance':
  317. case 'priority':
  318. $this->priority = $this->parsePriority($value);
  319. break;
  320. case 'list-post':
  321. $value = $this->stripComments($value);
  322. $this->mlist('post', $value);
  323. break;
  324. case 'list-reply':
  325. $value = $this->stripComments($value);
  326. $this->mlist('reply', $value);
  327. break;
  328. case 'list-subscribe':
  329. $value = $this->stripComments($value);
  330. $this->mlist('subscribe', $value);
  331. break;
  332. case 'list-unsubscribe':
  333. $value = $this->stripComments($value);
  334. $this->mlist('unsubscribe', $value);
  335. break;
  336. case 'list-archive':
  337. $value = $this->stripComments($value);
  338. $this->mlist('archive', $value);
  339. break;
  340. case 'list-owner':
  341. $value = $this->stripComments($value);
  342. $this->mlist('owner', $value);
  343. break;
  344. case 'list-help':
  345. $value = $this->stripComments($value);
  346. $this->mlist('help', $value);
  347. break;
  348. case 'list-id':
  349. $value = $this->stripComments($value);
  350. $this->mlist('id', $value);
  351. break;
  352. case 'x-spam-status':
  353. case 'x-spam-score':
  354. $this->x_spam_status = $this->parseSpamStatus($value);
  355. break;
  356. case 'x-sm-flag-reply':
  357. $this->x_sm_flag_reply = $value;
  358. break;
  359. default:
  360. break;
  361. }
  362. }
  363. /**
  364. * @param string $address
  365. * @return array
  366. */
  367. function getAddressTokens($address) {
  368. $aTokens = array();
  369. $aSpecials = array('(' ,'<' ,',' ,';' ,':');
  370. $aReplace = array(' (',' <',' ,',' ;',' :');
  371. if (!empty($address)) {
  372. $address = str_replace($aSpecials,$aReplace,$address);
  373. $iCnt = strlen($address);
  374. } else {
  375. $iCnt = 0;
  376. }
  377. $i = 0;
  378. while ($i < $iCnt) {
  379. $cChar = $address[$i];
  380. switch($cChar)
  381. {
  382. case '<':
  383. $iEnd = strpos($address,'>',$i+1);
  384. if (!$iEnd) {
  385. $sToken = substr($address,$i);
  386. $i = $iCnt;
  387. } else {
  388. $sToken = substr($address,$i,$iEnd - $i +1);
  389. $i = $iEnd;
  390. }
  391. $sToken = str_replace($aReplace, $aSpecials,$sToken);
  392. if ($sToken) $aTokens[] = $sToken;
  393. break;
  394. case '"':
  395. $iEnd = strpos($address,$cChar,$i+1);
  396. if ($iEnd) {
  397. // skip escaped quotes
  398. $prev_char = $address[$iEnd-1];
  399. while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') {
  400. $iEnd = strpos($address,$cChar,$iEnd+1);
  401. if ($iEnd) {
  402. $prev_char = $address[$iEnd-1];
  403. } else {
  404. $prev_char = false;
  405. }
  406. }
  407. }
  408. if (!$iEnd) {
  409. $sToken = substr($address,$i);
  410. $i = $iCnt;
  411. } else {
  412. // also remove the surrounding quotes
  413. $sToken = substr($address,$i+1,$iEnd - $i -1);
  414. $i = $iEnd;
  415. }
  416. $sToken = str_replace($aReplace, $aSpecials,$sToken);
  417. if ($sToken) $aTokens[] = $sToken;
  418. break;
  419. case '(':
  420. array_pop($aTokens); //remove inserted space
  421. $iEnd = strpos($address,')',$i);
  422. if (!$iEnd) {
  423. $sToken = substr($address,$i);
  424. $i = $iCnt;
  425. } else {
  426. $iDepth = 1;
  427. $iComment = $i;
  428. while (($iDepth > 0) && (++$iComment < $iCnt)) {
  429. $cCharComment = $address[$iComment];
  430. switch($cCharComment) {
  431. case '\\':
  432. ++$iComment;
  433. break;
  434. case '(':
  435. ++$iDepth;
  436. break;
  437. case ')':
  438. --$iDepth;
  439. break;
  440. default:
  441. break;
  442. }
  443. }
  444. if ($iDepth == 0) {
  445. $sToken = substr($address,$i,$iComment - $i +1);
  446. $i = $iComment;
  447. } else {
  448. $sToken = substr($address,$i,$iEnd - $i + 1);
  449. $i = $iEnd;
  450. }
  451. }
  452. // check the next token in case comments appear in the middle of email addresses
  453. $prevToken = end($aTokens);
  454. if (!in_array($prevToken,$aSpecials,true)) {
  455. if ($i+1<strlen($address) && !in_array($address[$i+1],$aSpecials,true)) {
  456. $iEnd = strpos($address,' ',$i+1);
  457. if ($iEnd) {
  458. $sNextToken = trim(substr($address,$i+1,$iEnd - $i -1));
  459. $i = $iEnd-1;
  460. } else {
  461. $sNextToken = trim(substr($address,$i+1));
  462. $i = $iCnt;
  463. }
  464. // remove the token
  465. array_pop($aTokens);
  466. // create token and add it again
  467. $sNewToken = $prevToken . $sNextToken;
  468. if($sNewToken) $aTokens[] = $sNewToken;
  469. }
  470. }
  471. $sToken = str_replace($aReplace, $aSpecials,$sToken);
  472. if ($sToken) $aTokens[] = $sToken;
  473. break;
  474. case ',':
  475. case ':':
  476. case ';':
  477. case ' ':
  478. $aTokens[] = $cChar;
  479. break;
  480. default:
  481. $iEnd = strpos($address,' ',$i+1);
  482. if ($iEnd) {
  483. $sToken = trim(substr($address,$i,$iEnd - $i));
  484. $i = $iEnd-1;
  485. } else {
  486. $sToken = trim(substr($address,$i));
  487. $i = $iCnt;
  488. }
  489. if ($sToken) $aTokens[] = $sToken;
  490. }
  491. ++$i;
  492. }
  493. return $aTokens;
  494. }
  495. /**
  496. * @param array $aStack
  497. * @param array $aComment
  498. * @param string $sEmail
  499. * @param string $sGroup
  500. * @return object AddressStructure object
  501. */
  502. function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
  503. //$aStack=explode(' ',implode('',$aStack));
  504. if (!$sEmail) {
  505. while (count($aStack) && !$sEmail) {
  506. $sEmail = trim(array_pop($aStack));
  507. }
  508. }
  509. if (count($aStack)) {
  510. $sPersonal = trim(implode('',$aStack));
  511. } else {
  512. $sPersonal = '';
  513. }
  514. if (!$sPersonal && count($aComment)) {
  515. $sComment = trim(implode(' ',$aComment));
  516. $sPersonal .= $sComment;
  517. }
  518. $oAddr = new AddressStructure();
  519. if ($sPersonal && substr($sPersonal,0,2) == '=?') {
  520. $oAddr->personal = encodeHeader($sPersonal);
  521. } else {
  522. $oAddr->personal = $sPersonal;
  523. }
  524. // $oAddr->group = $sGroup;
  525. $iPosAt = strpos($sEmail,'@');
  526. if ($iPosAt) {
  527. $oAddr->mailbox = substr($sEmail, 0, $iPosAt);
  528. $oAddr->host = substr($sEmail, $iPosAt+1);
  529. } else {
  530. $oAddr->mailbox = $sEmail;
  531. $oAddr->host = false;
  532. }
  533. $sEmail = '';
  534. $aStack = $aComment = array();
  535. return $oAddr;
  536. }
  537. /**
  538. * recursive function for parsing address strings and storing them in an address stucture object.
  539. * personal name: encoded: =?charset?Q|B?string?=
  540. * quoted: "string"
  541. * normal: string
  542. * email : <mailbox@host>
  543. * : mailbox@host
  544. * This function is also used for validating addresses returned from compose
  545. * That's also the reason that the function became a little bit huge
  546. * @param string $address
  547. * @param boolean $ar return array instead of only the first element
  548. * @param array $addr_ar (obsolete) array with parsed addresses
  549. * @param string $group (obsolete)
  550. * @param string $host default domainname in case of addresses without a domainname
  551. * @param string $lookup (since) callback function for lookup of address strings which are probably nicks (without @)
  552. * @return mixed array with AddressStructure objects or only one address_structure object.
  553. */
  554. function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
  555. $aTokens = $this->getAddressTokens($address);
  556. $sPersonal = $sEmail = $sGroup = '';
  557. $aStack = $aComment = array();
  558. foreach ($aTokens as $sToken) {
  559. $cChar = $sToken[0];
  560. switch ($cChar)
  561. {
  562. case '=':
  563. case '"':
  564. case ' ':
  565. $aStack[] = $sToken;
  566. break;
  567. case '(':
  568. $aComment[] = substr($sToken,1,-1);
  569. break;
  570. case ';':
  571. if ($sGroup) {
  572. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
  573. $oAddr = end($aAddress);
  574. if(!$oAddr || ((isset($oAddr)) && !strlen($oAddr->mailbox) && !$oAddr->personal)) {
  575. $sEmail = $sGroup . ':;';
  576. }
  577. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
  578. $sGroup = '';
  579. $aStack = $aComment = array();
  580. break;
  581. }
  582. case ',':
  583. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
  584. break;
  585. case ':':
  586. $sGroup = trim(implode(' ',$aStack));
  587. $sGroup = preg_replace('/\s+/',' ',$sGroup);
  588. $aStack = array();
  589. break;
  590. case '<':
  591. $sEmail = trim(substr($sToken,1,-1));
  592. break;
  593. case '>':
  594. /* skip */
  595. break;
  596. default: $aStack[] = $sToken; break;
  597. }
  598. }
  599. /* now do the action again for the last address */
  600. $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
  601. /* try to lookup the addresses in case of invalid email addresses */
  602. $aProcessedAddress = array();
  603. foreach ($aAddress as $oAddr) {
  604. $aAddrBookAddress = array();
  605. if (!$oAddr->host) {
  606. $grouplookup = false;
  607. if ($lookup) {
  608. $aAddr = call_user_func_array($lookup,array($oAddr->mailbox));
  609. if (isset($aAddr['email'])) {
  610. if (strpos($aAddr['email'],',')) {
  611. $grouplookup = true;
  612. $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
  613. } else {
  614. $iPosAt = strpos($aAddr['email'], '@');
  615. if ($iPosAt === FALSE) {
  616. $oAddr->mailbox = $aAddr['email'];
  617. $oAddr->host = FALSE;
  618. } else {
  619. $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
  620. $oAddr->host = substr($aAddr['email'], $iPosAt+1);
  621. }
  622. if (isset($aAddr['name'])) {
  623. $oAddr->personal = $aAddr['name'];
  624. } else {
  625. $oAddr->personal = encodeHeader($sPersonal);
  626. }
  627. }
  628. }
  629. }
  630. if (!$grouplookup && !strlen($oAddr->mailbox)) {
  631. $oAddr->mailbox = trim($sEmail);
  632. if ($sHost && strlen($oAddr->mailbox)) {
  633. $oAddr->host = $sHost;
  634. }
  635. } else if (!$grouplookup && !$oAddr->host) {
  636. if ($sHost && strlen($oAddr->mailbox)) {
  637. $oAddr->host = $sHost;
  638. }
  639. }
  640. }
  641. if (!$aAddrBookAddress && strlen($oAddr->mailbox)) {
  642. $aProcessedAddress[] = $oAddr;
  643. } else {
  644. $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
  645. }
  646. }
  647. if ($ar) {
  648. return $aProcessedAddress;
  649. } else {
  650. if (isset($aProcessedAddress[0]))
  651. return $aProcessedAddress[0];
  652. else
  653. return '';
  654. }
  655. }
  656. /**
  657. * Normalise the different Priority headers into a uniform value,
  658. * namely that of the X-Priority header (1, 3, 5). Supports:
  659. * Priority, X-Priority, Importance.
  660. * X-MS-Mail-Priority is not parsed because it always coincides
  661. * with one of the other headers.
  662. *
  663. * NOTE: this is actually a duplicate from the code in
  664. * functions/imap_messages:parseFetch().
  665. * I'm not sure if it's ok here to call
  666. * that function?
  667. * @param string $sValue literal priority name
  668. * @return integer
  669. */
  670. function parsePriority($sValue) {
  671. // don't use function call inside array_shift.
  672. $aValue = preg_split('/\s/',trim($sValue));
  673. $value = strtolower(array_shift($aValue));
  674. if ( is_numeric($value) ) {
  675. return $value;
  676. }
  677. if ( $value == 'urgent' || $value == 'high' ) {
  678. return 1;
  679. } elseif ( $value == 'non-urgent' || $value == 'low' ) {
  680. return 5;
  681. }
  682. // default is normal priority
  683. return 3;
  684. }
  685. /**
  686. * @param string $value content type header
  687. */
  688. function parseContentType($value) {
  689. $pos = strpos($value, ';');
  690. $props = '';
  691. if ($pos > 0) {
  692. $type = trim(substr($value, 0, $pos));
  693. $props = trim(substr($value, $pos+1));
  694. } else {
  695. $type = $value;
  696. }
  697. $content_type = new ContentType($type);
  698. if ($props) {
  699. $properties = $this->parseProperties($props);
  700. if (!isset($properties['charset'])) {
  701. $properties['charset'] = 'us-ascii';
  702. }
  703. $content_type->properties = $this->parseProperties($props);
  704. }
  705. $this->content_type = $content_type;
  706. }
  707. /**
  708. * RFC2184
  709. * @param array $aParameters
  710. * @return array
  711. */
  712. function processParameters($aParameters) {
  713. $aResults = array();
  714. $aCharset = array();
  715. // handle multiline parameters
  716. foreach($aParameters as $key => $value) {
  717. if ($iPos = strpos($key,'*')) {
  718. $sKey = substr($key,0,$iPos);
  719. if (!isset($aResults[$sKey])) {
  720. $aResults[$sKey] = $value;
  721. if (substr($key,-1) == '*') { // parameter contains language/charset info
  722. $aCharset[] = $sKey;
  723. }
  724. } else {
  725. $aResults[$sKey] .= $value;
  726. }
  727. } else {
  728. $aResults[$key] = $value;
  729. }
  730. }
  731. foreach ($aCharset as $key) {
  732. $value = $aResults[$key];
  733. // extract the charset & language
  734. $charset = substr($value,0,strpos($value,"'"));
  735. $value = substr($value,strlen($charset)+1);
  736. $language = substr($value,0,strpos($value,"'"));
  737. $value = substr($value,strlen($charset)+1);
  738. /* FIXME: What's the status of charset decode with language information ????
  739. * Maybe language information contains only ascii text and charset_decode()
  740. * only runs sm_encode_html_special_chars() on it. If it contains 8bit information, you
  741. * get html encoded text in charset used by selected translation.
  742. */
  743. $value = charset_decode($charset,$value);
  744. $aResults[$key] = $value;
  745. }
  746. return $aResults;
  747. }
  748. /**
  749. * @param string $value
  750. * @return array
  751. */
  752. function parseProperties($value) {
  753. $propArray = explode(';', $value);
  754. $propResultArray = array();
  755. foreach ($propArray as $prop) {
  756. $prop = trim($prop);
  757. $pos = strpos($prop, '=');
  758. if ($pos > 0) {
  759. $key = trim(substr($prop, 0, $pos));
  760. $val = trim(substr($prop, $pos+1));
  761. if (strlen($val) > 0 && $val[0] == '"') {
  762. $val = substr($val, 1, -1);
  763. }
  764. $propResultArray[$key] = $val;
  765. }
  766. }
  767. return $this->processParameters($propResultArray);
  768. }
  769. /**
  770. * Fills disposition object in rfc822Header object
  771. * @param string $value
  772. */
  773. function parseDisposition($value) {
  774. $pos = strpos($value, ';');
  775. $props = '';
  776. if ($pos > 0) {
  777. $name = trim(substr($value, 0, $pos));
  778. $props = trim(substr($value, $pos+1));
  779. } else {
  780. $name = $value;
  781. }
  782. $props_a = $this->parseProperties($props);
  783. $disp = new Disposition($name);
  784. $disp->properties = $props_a;
  785. $this->disposition = $disp;
  786. }
  787. /**
  788. * Fills mlist array keys in rfc822Header object
  789. * @param string $field
  790. * @param string $value
  791. */
  792. function mlist($field, $value) {
  793. $res_a = array();
  794. $value_a = explode(',', $value);
  795. foreach ($value_a as $val) {
  796. $val = trim($val);
  797. if (empty($val)) {
  798. $res_a['href'] = '';
  799. continue;
  800. }
  801. if ($val[0] == '<') {
  802. $val = substr($val, 1, -1);
  803. }
  804. if (substr($val, 0, 7) == 'mailto:') {
  805. $res_a['mailto'] = substr($val, 7);
  806. } else {
  807. $res_a['href'] = $val;
  808. }
  809. }
  810. $this->mlist[$field] = $res_a;
  811. }
  812. /**
  813. * Parses the X-Spam-Status or X-Spam-Score header
  814. * @param string $value
  815. */
  816. function parseSpamStatus($value) {
  817. // Header value looks like this:
  818. // No, score=1.5 required=5.0 tests=MSGID_FROM_MTA_ID,NO_REAL_NAME,UPPERCASE_25_50 autolearn=disabled version=3.1.0-gr0
  819. // Update circa 2018, this header can also be simply:
  820. // No, score=1.5
  821. // So we make the rest of the line optional (there are likely other permutations, so
  822. // each element is made optional except the first two... maybe even that's not flexible enough)
  823. //
  824. // Also now allow parsing of X-Spam-Score header, whose value is just a float
  825. $spam_status = array();
  826. if (preg_match ('/^(?:(No|Yes),\s+score=)?(-?\d+\.\d+)(?:\s+required=(-?\d+\.\d+))?(?:\s+tests=(.*?))?(?:\s+autolearn=(.*?))?(?:\s+version=(.+?))?$/i', $value, $matches)) {
  827. // full header
  828. $spam_status['bad_format'] = 0;
  829. $spam_status['value'] = $matches[0];
  830. // is_spam
  831. if (!empty($matches[1])) {
  832. if (strtolower($matches[1]) == 'yes')
  833. $spam_status['is_spam'] = true;
  834. else
  835. $spam_status['is_spam'] = false;
  836. }
  837. // score
  838. if (!empty($matches[2]))
  839. $spam_status['score'] = $matches[2];
  840. // required
  841. if (!empty($matches[3]))
  842. $spam_status['required'] = $matches[3];
  843. // tests
  844. if (isset($matches[4])) {
  845. $tests = array();
  846. $tests = explode(',', $matches[4]);
  847. foreach ($tests as $test) {
  848. $spam_status['tests'][] = trim($test);
  849. }
  850. }
  851. // autolearn
  852. if (isset($matches[5]))
  853. $spam_status['autolearn'] = $matches[5];
  854. // version
  855. if (isset($matches[6]))
  856. $spam_status['version'] = $matches[6];
  857. } else {
  858. $spam_status['bad_format'] = 1;
  859. $spam_status['value'] = $value;
  860. }
  861. return $spam_status;
  862. }
  863. /**
  864. * function to get the address strings out of the header.
  865. * example1: header->getAddr_s('to').
  866. * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
  867. * @param mixed $arr string or array of strings
  868. * @param string $separator
  869. * @param boolean $encoded (since 1.4.0) return encoded or plain text addresses
  870. * @param boolean $unconditionally_quote (since 1.4.21/1.5.2) When TRUE, always
  871. * quote the personal part,
  872. * whether or not it is
  873. * encoded, otherwise quoting
  874. * is only added if the
  875. * personal part is not encoded
  876. * @return string
  877. */
  878. function getAddr_s($arr, $separator = ', ', $encoded=false, $unconditionally_quote=FALSE) {
  879. $s = '';
  880. if (is_array($arr)) {
  881. foreach($arr as $arg) {
  882. if ($this->getAddr_s($arg, $separator, $encoded, $unconditionally_quote)) {
  883. $s .= $separator;
  884. }
  885. }
  886. $s = ($s ? substr($s, 2) : $s);
  887. } else {
  888. $addr = $this->{$arr};
  889. if (is_array($addr)) {
  890. foreach ($addr as $addr_o) {
  891. if (is_object($addr_o)) {
  892. if ($encoded) {
  893. $s .= $addr_o->getEncodedAddress($unconditionally_quote) . $separator;
  894. } else {
  895. $s .= $addr_o->getAddress(TRUE, FALSE, $unconditionally_quote) . $separator;
  896. }
  897. }
  898. }
  899. $s = substr($s, 0, -strlen($separator));
  900. } else {
  901. if (is_object($addr)) {
  902. if ($encoded) {
  903. $s .= $addr->getEncodedAddress($unconditionally_quote);
  904. } else {
  905. $s .= $addr->getAddress(TRUE, FALSE, $unconditionally_quote);
  906. }
  907. }
  908. }
  909. }
  910. return $s;
  911. }
  912. /**
  913. * function to get the array of addresses out of the header.
  914. * @param mixed $arg string or array of strings
  915. * @param array $excl_arr array of excluded email addresses
  916. * @param array $arr array of added email addresses
  917. * @return array
  918. */
  919. function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
  920. if (is_array($arg)) {
  921. foreach($arg as $argument) {
  922. $arr = $this->getAddr_a($argument, $excl_arr, $arr);
  923. }
  924. } else {
  925. $addr = $this->{$arg};
  926. if (is_array($addr)) {
  927. foreach ($addr as $next_addr) {
  928. if (is_object($next_addr)) {
  929. if (isset($next_addr->host) && ($next_addr->host != '')) {
  930. $email = $next_addr->mailbox . '@' . $next_addr->host;
  931. } else {
  932. $email = $next_addr->mailbox;
  933. }
  934. $email = strtolower($email);
  935. if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
  936. $arr[$email] = $next_addr->personal;
  937. }
  938. }
  939. }
  940. } else {
  941. if (is_object($addr)) {
  942. $email = $addr->mailbox;
  943. $email .= (isset($addr->host) ? '@' . $addr->host : '');
  944. $email = strtolower($email);
  945. if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
  946. $arr[$email] = $addr->personal;
  947. }
  948. }
  949. }
  950. }
  951. return $arr;
  952. }
  953. /**
  954. //FIXME: This needs some documentation (inside the function too)! Don't code w/out comments!
  955. * Looking at the code years after it was written,
  956. * this is my (Paul) best guess as to what this
  957. * function does (note that docs previously claimed
  958. * that this function returns boolean or an array,
  959. * but it no longer appears to return an array - an
  960. * integer instead):
  961. *
  962. * Inspects the TO and CC headers of the message
  963. * represented by this object, looking for the
  964. * address(es) given by $address
  965. *
  966. * If $address is a string:
  967. * Serves as a test (returns boolean) as to
  968. * whether or not the given address is found
  969. * anywhere in the TO or CC headers
  970. *
  971. * If $address is an array:
  972. * Looks through this list of addresses and
  973. * returns the array index (an integer even
  974. * if the array is given with keys of a
  975. * different type) of the first matching
  976. * $address found in this message's
  977. * TO or CC headers, unless there is an exact
  978. * match (meaning that the "personal
  979. * information" in addition to the email
  980. * address also matches), in which case that
  981. * index (the first one found) is returned
  982. *
  983. * @param mixed $address Address(es) to search for in this
  984. * message's TO and CC headers - please
  985. * see above how the format of this
  986. * argument affects the return value
  987. * of this function
  988. * @param boolean $recurs FOR INTERNAL USE ONLY
  989. *
  990. * @return mixed Boolean when $address is a scalar,
  991. * indicating whether or not the address
  992. * was found in the TO or CC headers.
  993. * An integer when $address is an array,
  994. * containing the index of the value in
  995. * that array that was found in the TO
  996. * or CC headers, or boolean FALSE if
  997. * there were no matches at all
  998. *
  999. * @since 1.3.2
  1000. */
  1001. function findAddress($address, $recurs = false) {
  1002. $result = false;
  1003. if (is_array($address)) {
  1004. $i=0;
  1005. foreach($address as $argument) {
  1006. $match = $this->findAddress($argument, true);
  1007. if ($match[1]) { // this indicates when the personal information matched
  1008. return $i;
  1009. } else {
  1010. if (count($match[0]) && $result === FALSE) {
  1011. $result = $i;
  1012. }
  1013. }
  1014. ++$i;
  1015. }
  1016. } else {
  1017. if (!is_array($this->cc)) $this->cc = array();
  1018. if (!is_array($this->to)) $this->to = array();
  1019. $srch_addr = $this->parseAddress($address);
  1020. $results = array();
  1021. foreach ($this->to as $to) {
  1022. if (strtolower($to->host) == strtolower($srch_addr->host)) {
  1023. if (strtolower($to->mailbox) == strtolower($srch_addr->mailbox)) {
  1024. $results[] = $srch_addr;
  1025. if (strtolower($to->personal) == strtolower($srch_addr->personal)) {
  1026. if ($recurs) {
  1027. return array($results, true);
  1028. } else {
  1029. return true;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. foreach ($this->cc as $cc) {
  1036. if (strtolower($cc->host) == strtolower($srch_addr->host)) {
  1037. if (strtolower($cc->mailbox) == strtolower($srch_addr->mailbox)) {
  1038. $results[] = $srch_addr;
  1039. if (strtolower($cc->personal) == strtolower($srch_addr->personal)) {
  1040. if ($recurs) {
  1041. return array($results, true);
  1042. } else {
  1043. return true;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. if ($recurs) {
  1050. return array($results, false);
  1051. } elseif (count($results)) {
  1052. return true;
  1053. } else {
  1054. return false;
  1055. }
  1056. }
  1057. //exit;
  1058. return $result;
  1059. }
  1060. /**
  1061. * @param string $type0 media type
  1062. * @param string $type1 media subtype
  1063. * @return array media properties
  1064. * @todo check use of media type arguments
  1065. */
  1066. function getContentType($type0, $type1) {
  1067. $type0 = $this->content_type->type0;
  1068. $type1 = $this->content_type->type1;
  1069. return $this->content_type->properties;
  1070. }
  1071. }