mime.class.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. <?php
  2. /**
  3. * mime.class
  4. *
  5. * Copyright (c) 2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. *
  9. * This contains functions needed to handle mime messages.
  10. *
  11. * $Id$
  12. */
  13. /*
  14. * rdc822_header class
  15. * input: header_string or array
  16. */
  17. class rfc822_header
  18. {
  19. var $date = '',
  20. $subject = '',
  21. $from = array(),
  22. $sender = '',
  23. $reply_to = array(),
  24. $to = array(),
  25. $cc = array(),
  26. $bcc = array(),
  27. $in_reply_to = '',
  28. $message_id = '',
  29. $mime = false,
  30. $content_type = '',
  31. $disposition = '',
  32. $xmailer = '',
  33. $priority = 3,
  34. $dnt = '',
  35. $mlist = array(),
  36. $more_headers = array(); /* only needed for constructing headers
  37. in smtp.php */
  38. function parseHeader($hdr)
  39. {
  40. if (is_array($hdr))
  41. {
  42. $hdr = implode('',$hdr);
  43. }
  44. /* first we unfold the header */
  45. $hdr = trim(str_replace(array("\r\n\t","\r\n "),array('',''),$hdr));
  46. /*
  47. * now we can make a new header array with each element representing
  48. * a headerline
  49. */
  50. $hdr = explode("\r\n" , $hdr);
  51. foreach ($hdr as $line)
  52. {
  53. $pos = strpos($line,':');
  54. if ($pos > 0)
  55. {
  56. $field = substr($line,0,$pos);
  57. $value = trim(substr($line,$pos+1));
  58. if(!preg_match('/^X.*/i',$field)) {
  59. $value = $this->stripComments($value);
  60. }
  61. $this->parseField($field,$value);
  62. }
  63. }
  64. if ($this->content_type == '')
  65. {
  66. $this->parseContentType('text/plain; charset=us-ascii');
  67. }
  68. }
  69. function stripComments($value)
  70. {
  71. $cnt = strlen($value);
  72. $s = '';
  73. $i = 0;
  74. while ($i < $cnt)
  75. {
  76. switch ($value{$i})
  77. {
  78. case ('"'):
  79. $s .= '"';
  80. $i++;
  81. while ($value{$i} != '"')
  82. {
  83. if ($value{$i} == '\\')
  84. {
  85. $s .= '\\';
  86. $i++;
  87. }
  88. $s .= $value{$i};
  89. $i++;
  90. if ($i > $cnt) break;
  91. }
  92. $s .= $value{$i};
  93. break;
  94. case ('('):
  95. while ($value{$i} != ')')
  96. {
  97. if ($value{$i} == '\\')
  98. {
  99. $i++;
  100. }
  101. $i++;
  102. }
  103. break;
  104. default:
  105. $s .= $value{$i};
  106. break;
  107. }
  108. $i++;
  109. }
  110. return $s;
  111. }
  112. function parseField($field,$value)
  113. {
  114. $field = strtolower($field);
  115. switch($field)
  116. {
  117. case ('date'):
  118. $d = strtr($value, array(' ' => ' '));
  119. $d = explode(' ', $d);
  120. $this->date = getTimeStamp($d);
  121. break;
  122. case ('subject'):
  123. $this->subject = $value;
  124. break;
  125. case ('from'):
  126. $this->from = $this->parseAddress($value,true);
  127. break;
  128. case ('sender'):
  129. $this->sender = $this->parseAddress($value);
  130. break;
  131. case ('reply-to'):
  132. $this->reply_to = $this->parseAddress($value, true);
  133. break;
  134. case ('to'):
  135. $this->to = $this->parseAddress($value, true);
  136. break;
  137. case ('cc'):
  138. $this->cc = $this->parseAddress($value, true);
  139. break;
  140. case ('bcc'):
  141. $this->bcc = $this->parseAddress($value, true);
  142. break;
  143. case ('in-reply-to'):
  144. $this->in_reply_to = $value;
  145. break;
  146. case ('message_id'):
  147. $this->message_id = $value;
  148. break;
  149. case ('disposition-notification-to'):
  150. $this->dnt = $this->parseAddress($value);
  151. break;
  152. case ('mime-Version'):
  153. $value = str_replace(' ','',$value);
  154. if ($value == '1.0')
  155. {
  156. $this->mime = true;
  157. }
  158. break;
  159. case ('content-type'):
  160. $this->parseContentType($value);
  161. break;
  162. case ('content-disposition'):
  163. $this->parseDisposition($value);
  164. break;
  165. case ('x-mailer'):
  166. $this->xmailer = $value;
  167. break;
  168. case ('x-priority'):
  169. $this->priority = $value;
  170. break;
  171. case ('list-post'):
  172. $this->mlist('post',$value);
  173. break;
  174. case ('list-reply'):
  175. $this->mlist('reply',$value);
  176. break;
  177. case ('list-subscribe'):
  178. $this->mlist('subscribe',$value);
  179. break;
  180. case ('list-unsubscribe'):
  181. $this->mlist('unsubscribe',$value);
  182. break;
  183. case ('list-archive'):
  184. $this->mlist('archive',$value);
  185. break;
  186. case ('list-owner'):
  187. $this->mlist('owner',$value);
  188. break;
  189. case ('list-help'):
  190. $this->mlist('help',$value);
  191. break;
  192. case ('list-id'):
  193. $this->mlist('id',$value);
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. function parseAddress($address, $ar=false, $addr_ar = array(), $group = '')
  200. {
  201. $pos = 0;
  202. $j = strlen( $address );
  203. $name = '';
  204. $addr = '';
  205. while ( $pos < $j ) {
  206. switch ($address{$pos})
  207. {
  208. case ('"'): /* get the personal name */
  209. $pos++;
  210. if ($address{$pos} == '"')
  211. {
  212. $pos++;
  213. } else
  214. {
  215. while ( $pos < $j && $address{$pos} != '"')
  216. {
  217. if (substr($address, $pos, 2) == '\\"')
  218. {
  219. $name .= $address{$pos};
  220. $pos++;
  221. } elseif (substr($address, $pos, 2) == '\\\\')
  222. {
  223. $name .= $address{$pos};
  224. $pos++;
  225. }
  226. $name .= $address{$pos};
  227. $pos++;
  228. }
  229. }
  230. $pos++;
  231. break;
  232. case ('<'): /* get email address */
  233. $addr_start=$pos;
  234. $pos++;
  235. while ( $pos < $j && $address{$pos} != '>' )
  236. {
  237. $addr .= $address{$pos};
  238. $pos++;
  239. }
  240. $pos++;
  241. break;
  242. case ('('): /* rip off comments */
  243. $addr_start=$pos;
  244. $pos++;
  245. while ( $pos < $j && $address{$pos} != ')' )
  246. {
  247. $addr .= $address{$pos};
  248. $pos++;
  249. }
  250. $address_start = substr($address,0,$addr_start);
  251. $address_end = substr($address,$pos+1);
  252. $address = $address_start . $address_end;
  253. $j = strlen( $address );
  254. $pos = $addr_start;
  255. $pos++;
  256. break;
  257. case (','): /* we reached a delimiter */
  258. if ($addr == '')
  259. {
  260. $addr = substr($address,0,$pos);
  261. } elseif ($name == '') {
  262. $name = trim(substr($address,0,$addr_start));
  263. }
  264. $at = strpos($addr, '@');
  265. $addr_structure = new address_structure();
  266. $addr_structure->personal = $name;
  267. $addr_structure->group = $group;
  268. if ($at)
  269. {
  270. $addr_structure->mailbox = substr($addr,0,$at);
  271. $addr_structure->host = substr($addr,$at+1);
  272. } else
  273. {
  274. $addr_structure->mailbox = $addr;
  275. }
  276. $address = trim(substr($address,$pos+1));
  277. $j = strlen( $address );
  278. $pos = 0;
  279. $name = '';
  280. $addr = '';
  281. $addr_ar[] = $addr_structure;
  282. break;
  283. case (':'): /* process the group addresses */
  284. /* group marker */
  285. $group = substr($address,0,$pos);
  286. $address = substr($address,$pos+1);
  287. $result = $this->parseAddress($address, $ar, $addr_ar, $group);
  288. $addr_ar = $result[0];
  289. $pos = $result[1];
  290. $address = substr($address,$pos);
  291. $j = strlen( $address );
  292. $group = '';
  293. $pos++;
  294. break;
  295. case (';'):
  296. if ($group)
  297. {
  298. $address = substr($address, 0, $pos-1);
  299. }
  300. $pos++;
  301. break;
  302. default:
  303. $pos++;
  304. break;
  305. }
  306. }
  307. if ($addr == '')
  308. {
  309. $addr = substr($address,0,$pos);
  310. } elseif ($name == '')
  311. {
  312. $name = trim(substr($address,0,$addr_start));
  313. }
  314. $at = strpos($addr, '@');
  315. $addr_structure = new address_structure();
  316. $addr_structure->group = $group;
  317. if ($at)
  318. {
  319. $addr_structure->mailbox = trim(substr($addr,0,$at));
  320. $addr_structure->host = trim(substr($addr,$at+1));
  321. } else
  322. {
  323. $addr_structure->mailbox = trim($addr);
  324. }
  325. if ($group && $addr == '') /* no addresses found in group */
  326. {
  327. $name = "$group: Undisclosed recipients;";
  328. $addr_structure->personal = $name;
  329. $addr_ar[] = $addr_structure;
  330. return (array($addr_ar,$pos+1));
  331. } else
  332. {
  333. $addr_structure->personal = $name;
  334. if ($name || $addr)
  335. {
  336. $addr_ar[] = $addr_structure;
  337. }
  338. }
  339. if ($ar)
  340. {
  341. return ($addr_ar);
  342. } else
  343. {
  344. return ($addr_ar[0]);
  345. }
  346. }
  347. function parseContentType($value)
  348. {
  349. $pos = strpos($value,';');
  350. $props = '';
  351. if ($pos > 0)
  352. {
  353. $type = trim(substr($value,0,$pos));
  354. $props = trim(substr($type,$pos+1));
  355. } else
  356. {
  357. $type = $value;
  358. }
  359. $content_type = new content_type($type);
  360. if ($props)
  361. {
  362. $properties = $this->parseProperties($props);
  363. if (!isset($properties['charset']))
  364. {
  365. $properties['charset'] = 'us-ascii';
  366. }
  367. $content_type->properties = $this->parseProperties($props);
  368. }
  369. $this->content_type = $content_type;
  370. }
  371. function parseProperties($value)
  372. {
  373. $propArray = explode(';',$value);
  374. $propResultArray = array();
  375. foreach ($propArray as $prop)
  376. {
  377. $prop = trim($prop);
  378. $pos = strpos($prop,'=');
  379. if ($pos>0)
  380. {
  381. $key = trim(substr($prop,0,$pos));
  382. $val = trim(substr($prop,$pos+1));
  383. if ($val{0} == '"')
  384. {
  385. $val = substr($val,1,-1);
  386. }
  387. $propResultArray[$key] = $val;
  388. }
  389. }
  390. return $propResultArray;
  391. }
  392. function parseDisposition($value)
  393. {
  394. $pos = strpos($value,';');
  395. $props = '';
  396. if ($pos > 0)
  397. {
  398. $name = trim(substr($value,0,$pos));
  399. $props = trim(substr($type,$pos+1));
  400. } else
  401. {
  402. $name = $value;
  403. }
  404. $props_a = $this->parseProperties($props);
  405. $disp = new disposition($name);
  406. $disp->properties = $props_a;
  407. $this->disposition = $disp;
  408. }
  409. function mlist($field, $value)
  410. {
  411. $res_a = array();
  412. $value_a = explode(',',$value);
  413. foreach ($value_a as $val) {
  414. $val = trim($val);
  415. if ($val{0} == '<')
  416. {
  417. $val = substr($val,1,-1);
  418. }
  419. if (substr($val,0,7) == 'mailto:')
  420. {
  421. $res_a['mailto'] = substr($val,7);
  422. } else
  423. {
  424. $res_a['href'] = $val;
  425. }
  426. }
  427. $this->mlist[$field] = $res_a;
  428. }
  429. /*
  430. * function to get the addres strings out of the header.
  431. * Arguments: string or array of strings !
  432. * example1: header->getAddr_s('to').
  433. * example2: header->getAddr_s(array('to','cc','bcc'))
  434. */
  435. function getAddr_s($arr, $separator=', ')
  436. {
  437. if (is_array($arr))
  438. {
  439. $s = '';
  440. foreach($arr as $arg )
  441. {
  442. $result = $this->getAddr_s($arg);
  443. if ($result)
  444. {
  445. $s .= $separator . $result;
  446. }
  447. }
  448. if ($s) $s = substr($s,2);
  449. return $s;
  450. } else
  451. {
  452. $s = '';
  453. eval('$addr = $this->'.$arr.';') ;
  454. if (is_array($addr))
  455. {
  456. foreach ($addr as $addr_o)
  457. {
  458. if (is_object($addr_o))
  459. {
  460. $s .= $addr_o->getAddress() . $separator;
  461. }
  462. }
  463. $s = substr($s,0,-strlen($separator));
  464. } else
  465. {
  466. if (is_object($addr))
  467. {
  468. $s .= $addr->getAddress();
  469. }
  470. }
  471. return $s;
  472. }
  473. }
  474. function getAddr_a($arg, $excl_arr=array(), $arr = array())
  475. {
  476. if (is_array($arg))
  477. {
  478. foreach($arg as $argument )
  479. {
  480. $arr = $this->getAddr_a($argument, $excl_arr, $arr);
  481. }
  482. return $arr;
  483. } else
  484. {
  485. eval('$addr = $this->'.$arg.';') ;
  486. if (is_array($addr))
  487. {
  488. foreach ($addr as $addr_o)
  489. {
  490. if (is_object($addr_o))
  491. {
  492. if (isset($addr_o->host) && $addr_o->host !='')
  493. {
  494. $email = $addr_o->mailbox.'@'.$addr_o->host;
  495. } else
  496. {
  497. $email = $addr_o->mailbox;
  498. }
  499. $email = strtolower($email);
  500. if ($email && !isset($arr[$email]) && !isset($excl_arr[$email]))
  501. {
  502. $arr[$email] = $addr_o->personal;
  503. }
  504. }
  505. }
  506. } else
  507. {
  508. if (is_object($addr))
  509. {
  510. if (isset($addr->host))
  511. {
  512. $email = $addr->mailbox.'@'.$addr->host;
  513. } else
  514. {
  515. $email = $addr->mailbox;
  516. }
  517. $email = strtolower($email);
  518. if ($email && !isset($arr[$email]) && !isset($excl_arr[$email]))
  519. {
  520. $arr[$email] = $addr->personal;
  521. }
  522. }
  523. }
  524. return $arr;
  525. }
  526. }
  527. function getContentType($type0, $type1)
  528. {
  529. $type0 = $this->content_type->type0;
  530. $type1 = $this->content_type->type1;
  531. return $this->content_type->properties;
  532. }
  533. }
  534. class msg_header
  535. {
  536. /** msg_header contains all variables available in a bodystructure **/
  537. /** entity like described in rfc2060 **/
  538. var $type0 = '',
  539. $type1 = '',
  540. $parameters = array(),
  541. $id = 0,
  542. $description = '',
  543. $encoding='',
  544. $size = 0,
  545. $md5='',
  546. $disposition = '',
  547. $language='';
  548. /*
  549. * returns addres_list of supplied argument
  550. * arguments: array('to', 'from', ...) or just a string like 'to'.
  551. * result: string: address1, addres2, ....
  552. */
  553. function setVar($var, $value)
  554. {
  555. $this->{$var} = $value;
  556. }
  557. function getParameter($par)
  558. {
  559. $value = strtolower($par);
  560. if (isset($this->parameters[$par]))
  561. {
  562. return $this->parameters[$par];
  563. }
  564. return '';
  565. }
  566. function setParameter($parameter, $value)
  567. {
  568. $this->parameters[strtolower($parameter)] = $value;
  569. }
  570. }
  571. class address_structure
  572. {
  573. var $personal = '', $adl = '', $mailbox = '', $host = '', $group = '';
  574. function getAddress($full=true)
  575. {
  576. if (is_object($this))
  577. {
  578. if (isset($this->host) && $this->host !='')
  579. {
  580. $email = $this->mailbox.'@'.$this->host;
  581. } else
  582. {
  583. $email = $this->mailbox;
  584. }
  585. if (trim($this->personal) !='')
  586. {
  587. if ($email)
  588. {
  589. $addr = '"' . $this->personal . '" <' .$email.'>';
  590. } else
  591. {
  592. $addr = $this->personal;
  593. }
  594. $best_dpl = $this->personal;
  595. } else
  596. {
  597. $addr = $email;
  598. $best_dpl = $email;
  599. }
  600. if ($full)
  601. {
  602. return $addr;
  603. } else
  604. {
  605. return $best_dpl;
  606. }
  607. } else return '';
  608. }
  609. }
  610. class message
  611. {
  612. /** message is the object that contains messages. It is a recursive
  613. object in that through the $entities variable, it can contain
  614. more objects of type message. See documentation in mime.txt for
  615. a better description of how this works.
  616. **/
  617. var $rfc822_header = '',
  618. $mime_header = '',
  619. $flags = '',
  620. $type0='',
  621. $type1='',
  622. $entities = array(),
  623. $parent_ent, $entity,
  624. $parent = '', $decoded_body='',
  625. $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0,
  626. $is_mdnsent = 0,
  627. $body_part = '',
  628. $offset = 0, /* for fetching body parts out of raw messages */
  629. $length = 0; /* for fetching body parts out of raw messages */
  630. function setEnt($ent)
  631. {
  632. $this->entity_id= $ent;
  633. }
  634. function addEntity ($msg)
  635. {
  636. $msg->parent = &$this;
  637. $this->entities[] = $msg;
  638. }
  639. function addRFC822Header($read)
  640. {
  641. $header = new rfc822_header();
  642. $this->rfc822_header = $header->parseHeader($read);
  643. }
  644. function getEntity($ent)
  645. {
  646. $cur_ent = $this->entity_id;
  647. $msg = $this;
  648. if ($cur_ent == '' || $cur_ent == '0')
  649. {
  650. $cur_ent_a = array();
  651. } else
  652. {
  653. $cur_ent_a = explode('.',$this->entity_id);
  654. }
  655. $ent_a = explode('.',$ent);
  656. $cnt = count($ent_a);
  657. for ($i=0;$i<$cnt -1;$i++)
  658. {
  659. if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i])
  660. {
  661. $msg = $msg->parent;
  662. $cur_ent_a = explode('.',$msg->entity_id);
  663. $i--;
  664. } else if (!isset($cur_ent_a[$i]))
  665. {
  666. if (isset($msg->entities[($ent_a[$i]-1)]))
  667. {
  668. $msg = $msg->entities[($ent_a[$i]-1)];
  669. } else {
  670. $msg = $msg->entities[0];
  671. }
  672. }
  673. if ($msg->type0 == 'message' && $msg->type1 == 'rfc822')
  674. {
  675. /*this is a header for a message/rfc822 entity */
  676. $msg = $msg->entities[0];
  677. }
  678. }
  679. if ($msg->type0 == 'message' && $msg->type1 == 'rfc822')
  680. {
  681. /*this is a header for a message/rfc822 entity */
  682. $msg = $msg->entities[0];
  683. }
  684. if (isset($msg->entities[($ent_a[$cnt-1])-1]))
  685. {
  686. $msg = $msg->entities[($ent_a[$cnt-1]-1)];
  687. }
  688. return $msg;
  689. }
  690. function setBody($s)
  691. {
  692. $this->body_part = $s;
  693. }
  694. function clean_up()
  695. {
  696. $msg = $this;
  697. $msg->body_part = '';
  698. $i=0;
  699. while ( isset($msg->entities[$i]))
  700. {
  701. $msg->entities[$i]->clean_up();
  702. $i++;
  703. }
  704. }
  705. function getMailbox()
  706. {
  707. $msg = $this;
  708. while (is_object($msg->parent))
  709. {
  710. $msg = $msg->parent;
  711. }
  712. return $msg->mailbox;
  713. }
  714. function calcEntity($msg)
  715. {
  716. if ($this->type0 == 'message' && $this->type1 == 'rfc822')
  717. {
  718. $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */
  719. } else if (isset($this->entity_id) && $this->entity_id !='')
  720. {
  721. $ent_no = count($this->entities)+1;
  722. $par_ent = substr($this->entity_id,-2);
  723. if ($par_ent{0} == '.')
  724. {
  725. $par_ent = $par_ent{1};
  726. }
  727. if ($par_ent == '0')
  728. {
  729. $ent_no = count($this->entities)+1;
  730. if ($ent_no > 0)
  731. {
  732. $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.'));
  733. if ($ent)
  734. {
  735. $ent = $ent . ".$ent_no";
  736. } else
  737. {
  738. $ent = $ent_no;
  739. }
  740. $msg->entity_id = $ent;
  741. } else
  742. {
  743. $msg->entity_id = $ent_no;
  744. }
  745. } else
  746. {
  747. $ent = $this->entity_id . ".$ent_no";
  748. $msg->entity_id = $ent;
  749. }
  750. } else
  751. {
  752. $msg->entity_id = '0';
  753. }
  754. return $msg->entity_id;
  755. }
  756. /*
  757. * Bodystructure parser, a recursive function for generating the
  758. * entity-tree with all the mime-parts.
  759. *
  760. * It follows RFC2060 and stores all the described fields in the
  761. * message object.
  762. *
  763. * Question/Bugs:
  764. *
  765. * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net.
  766. *
  767. */
  768. function parseStructure($read, $i=0)
  769. {
  770. $arg_no = 0;
  771. $arg_a = array();
  772. $cnt = strlen($read);
  773. while ($i < $cnt)
  774. {
  775. $char = strtoupper($read{$i});
  776. switch ($char)
  777. {
  778. case '(':
  779. if ($arg_no == 0 )
  780. {
  781. if (!isset($msg))
  782. {
  783. $msg = new message();
  784. $hdr = new msg_header();
  785. $hdr->type0 = 'text';
  786. $hdr->type1 = 'plain';
  787. $hdr->encoding = 'us-ascii';
  788. $msg->entity_id = $this->calcEntity($msg);
  789. } else
  790. {
  791. $msg->header->type0 = 'multipart';
  792. $msg->type0 = 'multipart';
  793. while ($read{$i} == '(')
  794. {
  795. $res = $msg->parseStructure($read,$i);
  796. $i = $res[1];
  797. $msg->addEntity($res[0]);
  798. }
  799. }
  800. } else
  801. {
  802. switch ($arg_no)
  803. {
  804. case 1:
  805. /* multipart properties */
  806. $i++;
  807. $res = $this->parseProperties($read,$i);
  808. $arg_a[] = $res[0];
  809. $i = $res[1];
  810. $arg_no++;
  811. break;
  812. case 2:
  813. if (isset($msg->type0) && $msg->type0 == 'multipart')
  814. {
  815. $i++;
  816. $res = $msg->parseDisposition($read,$i);
  817. $arg_a[] = $res[0];
  818. $i = $res[1];
  819. } else /* properties */
  820. {
  821. $res = $msg->parseProperties($read,$i);
  822. $arg_a[] = $res[0];
  823. $i = $res[1];
  824. }
  825. $arg_no++;
  826. break;
  827. case 3:
  828. if (isset($msg->type0) && $msg->type0 == 'multipart')
  829. {
  830. $i++;
  831. $res= $msg->parseLanguage($read,$i);
  832. $arg_a[] = $res[0];
  833. $i = $res[1];
  834. }
  835. case 7:
  836. if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')
  837. {
  838. $msg->header->type0 = $arg_a[0];
  839. $msg->type0 = $arg_a[0];
  840. $msg->header->type1 = $arg_a[1];
  841. $msg->type1 = $arg_a[1];
  842. $rfc822_hdr = new rfc822_header();
  843. $res = $msg->parseEnvelope($read,$i,$rfc822_hdr);
  844. $i = $res[1];
  845. $msg->rfc822_header = $res[0];
  846. $i++;
  847. while ($i < $cnt && $read{$i} != '(')
  848. {
  849. $i++;
  850. }
  851. $res = $msg->parseStructure($read,$i);
  852. $i = $res[1];
  853. $msg->addEntity($res[0]);
  854. }
  855. break;
  856. case 8:
  857. $i++;
  858. $res = $msg->parseDisposition($read,$i);
  859. $arg_a[] = $res[0];
  860. $i = $res[1];
  861. $arg_no++;
  862. break;
  863. case 9:
  864. if ($arg_a[0] == 'text' ||
  865. ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
  866. {
  867. $i++;
  868. $res = $msg->parseDisposition($read,$i);
  869. $arg_a[] = $res[0];
  870. $i = $res[1];
  871. } else
  872. {
  873. $i++;
  874. $res = $msg->parseLanguage($read,$i);
  875. $arg_a[] = $res[0];
  876. $i = $res[1];
  877. }
  878. $arg_no++;
  879. break;
  880. case 10:
  881. if ($arg_a[0] == 'text' ||
  882. ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
  883. {
  884. $i++;
  885. $res = $msg->parseLanguage($read,$i);
  886. $arg_a[] = $res[0];
  887. $i = $res[1];
  888. } else
  889. {
  890. $i = $msg->parseParenthesis($read,$i);
  891. $arg_a[] = ''; /* not yet desribed in rfc2060 */
  892. }
  893. $arg_no++;
  894. break;
  895. default:
  896. /* unknown argument, skip this part */
  897. $i = $msg->parseParenthesis($read,$i);
  898. $arg_a[] = '';
  899. $arg_no++;
  900. break;
  901. } /* switch */
  902. }
  903. break;
  904. case '"':
  905. /* inside an entity -> start processing */
  906. $debug = substr($read,$i,20);
  907. $res = $msg->parseQuote($read,$i);
  908. $arg_s = $res[0];
  909. $i = $res[1];
  910. $arg_no++;
  911. if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */
  912. $arg_a[] = $arg_s;
  913. break;
  914. case 'n':
  915. case 'N':
  916. /* probably NIL argument */
  917. if (strtoupper(substr($read,$i,4)) == 'NIL ' ||
  918. strtoupper(substr($read,$i,4)) == 'NIL)')
  919. {
  920. $arg_a[] = '';
  921. $arg_no++;
  922. $i = $i+2;
  923. }
  924. break;
  925. case '{':
  926. /* process the literal value */
  927. $res = $msg->parseLiteral($read,$i);
  928. $arg_s = $res[0];
  929. $i = $res[1];
  930. $arg_no++;
  931. break;
  932. case (is_numeric($read{$i}) ):
  933. /* process integers */
  934. if ($read{$i} == ' ') break;
  935. $arg_s = $read{$i};;
  936. $i++;
  937. while (preg_match('/^[0-9]{1}$/',$read{$i}))
  938. {
  939. $arg_s .= $read{$i};
  940. $i++;
  941. }
  942. $arg_no++;
  943. $arg_a[] = $arg_s;
  944. break;
  945. case ')':
  946. if (isset($msg->type0) && $msg->type0 == 'multipart')
  947. {
  948. $multipart = true;
  949. } else
  950. {
  951. $multipart = false;
  952. }
  953. if (!$multipart)
  954. {
  955. if ($arg_a[0] == 'text' ||
  956. ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822'))
  957. {
  958. $shifted_args = true;
  959. } else
  960. {
  961. $shifted_args = false;
  962. }
  963. $hdr->type0 = $arg_a[0];
  964. $hdr->type1 = $arg_a[1];
  965. $msg->type0 = $arg_a[0];
  966. $msg->type1 = $arg_a[1];
  967. $arr = $arg_a[2];
  968. if (is_array($arr))
  969. {
  970. $hdr->parameters = $arg_a[2];
  971. }
  972. $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) );
  973. $hdr->description = $arg_a[4];
  974. $hdr->encoding = strtolower($arg_a[5]);
  975. $hdr->entity_id = $msg->entity_id;
  976. $hdr->size = $arg_a[6];
  977. if ($shifted_args)
  978. {
  979. $hdr->lines = $arg_a[7];
  980. if (isset($arg_a[8]))
  981. {
  982. $hdr->md5 = $arg_a[8];
  983. }
  984. if (isset($arg_a[9]))
  985. {
  986. $hdr->disposition = $arg_a[9];
  987. }
  988. if (isset($arg_a[10]))
  989. {
  990. $hdr->language = $arg_a[10];
  991. }
  992. } else
  993. {
  994. if (isset($arg_a[7]))
  995. {
  996. $hdr->md5 = $arg_a[7];
  997. }
  998. if (isset($arg_a[8]))
  999. {
  1000. $hdr->disposition = $arg_a[8];
  1001. }
  1002. if (isset($arg_a[9]))
  1003. {
  1004. $hdr->language = $arg_a[9];
  1005. }
  1006. }
  1007. $msg->header = $hdr;
  1008. $arg_no = 0;
  1009. $i++;
  1010. if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart')
  1011. {
  1012. $msg->entity_id++;
  1013. }
  1014. return (array($msg, $i));
  1015. } else
  1016. {
  1017. $hdr->type0 = 'multipart';
  1018. $hdr->type1 = $arg_a[0];
  1019. $msg->type0 = 'multipart';
  1020. $msg->type1 = $arg_a[0];
  1021. if (is_array($arg_a[1]))
  1022. {
  1023. $hdr->parameters = $arg_a[1];
  1024. }
  1025. if (isset($arg_a[2]))
  1026. {
  1027. $hdr->disposition = $arg_a[2];
  1028. }
  1029. if (isset($arg_a[3]))
  1030. {
  1031. $hdr->language = $arg_a[3];
  1032. }
  1033. $msg->header = $hdr;
  1034. return (array($msg, $i));
  1035. }
  1036. default:
  1037. break;
  1038. } /* switch */
  1039. $i++;
  1040. } /* while */
  1041. } /* parsestructure */
  1042. function parseProperties($read, $i)
  1043. {
  1044. $properties = array();
  1045. $arg_s = '';
  1046. $prop_name = '';
  1047. while ($read{$i} != ')')
  1048. {
  1049. if ($read{$i} == '"')
  1050. {
  1051. $res = $this->parseQuote($read,$i);
  1052. $arg_s = $res[0];
  1053. $i = $res[1];
  1054. } else if ($read{$i} == '{')
  1055. {
  1056. $res = $this->parseLiteral($read,$i);
  1057. $arg_s = $res[0];
  1058. $i = $res[1];
  1059. }
  1060. if ($prop_name == '' && $arg_s)
  1061. {
  1062. $prop_name = strtolower($arg_s);
  1063. $properties[$prop_name] = '';
  1064. $arg_s = '';
  1065. } elseif ($prop_name != '' && $arg_s != '')
  1066. {
  1067. $properties[$prop_name] = $arg_s;
  1068. $prop_name = '';
  1069. $arg_s = '';
  1070. }
  1071. $i++;
  1072. }
  1073. return (array($properties, $i));
  1074. }
  1075. function parseEnvelope($read, $i, $hdr)
  1076. {
  1077. $arg_no = 0;
  1078. $arg_a = array();
  1079. $cnt = strlen($read);
  1080. while ($i< $cnt && $read{$i} != ')')
  1081. {
  1082. $i++;
  1083. $char = strtoupper($read{$i});
  1084. switch ($char)
  1085. {
  1086. case '"':
  1087. $res = $this->parseQuote($read,$i);
  1088. $arg_a[] = $res[0];
  1089. $i = $res[1];
  1090. $arg_no++;
  1091. break;
  1092. case '{':
  1093. $res = $this->parseLiteral($read,$i);
  1094. $arg_a[] = $res[0];
  1095. $i = $res[1];
  1096. $arg_no++;
  1097. break;
  1098. case 'N':
  1099. /* probably NIL argument */
  1100. if (strtoupper(substr($read,$i,3)) == 'NIL') {
  1101. $arg_a[] = '';
  1102. $arg_no++;
  1103. $i = $i+2;
  1104. }
  1105. break;
  1106. case '(':
  1107. /* Address structure
  1108. * With group support.
  1109. * Note: Group support is useless on SMTP connections
  1110. * because the protocol doesn't support it
  1111. */
  1112. $addr_a = array();
  1113. $group = '';
  1114. $a=0;
  1115. while ($i < $cnt && $read{$i} != ')')
  1116. {
  1117. if ($read{$i} == '(')
  1118. {
  1119. $res = $this->parseAddress($read,$i);
  1120. $addr = $res[0];
  1121. $i = $res[1];
  1122. if ($addr->host == '' && $addr->mailbox != '')
  1123. {
  1124. /* start of group */
  1125. $group = $addr->mailbox;
  1126. $group_addr = $addr;
  1127. $j = $a;
  1128. } elseif ($group && $addr->host == '' && $addr->mailbox == '')
  1129. {
  1130. /* end group */
  1131. if ($a == $j+1) /* no group members */
  1132. {
  1133. $group_addr->group = $group;
  1134. $group_addr->mailbox = '';
  1135. $group_addr->personal = "$group: Undisclosed recipients;";
  1136. $addr_a[] = $group_addr;
  1137. $group ='';
  1138. }
  1139. } else
  1140. {
  1141. $addr->group = $group;
  1142. $addr_a[] = $addr;
  1143. }
  1144. $a++;
  1145. }
  1146. $i++;
  1147. }
  1148. $arg_a[] = $addr_a;
  1149. break;
  1150. default:
  1151. break;
  1152. }
  1153. $i++;
  1154. }
  1155. if (count($arg_a) > 9)
  1156. {
  1157. /* argument 1: date */
  1158. $d = strtr($arg_a[0], array(' ' => ' '));
  1159. $d = explode(' ', $d);
  1160. $hdr->date = getTimeStamp($d);
  1161. /* argument 2: subject */
  1162. if (!trim($arg_a[1]))
  1163. {
  1164. $arg_a[1]= _("(no subject)");
  1165. }
  1166. $hdr->subject = $arg_a[1];
  1167. /* argument 3: from */
  1168. $hdr->from = $arg_a[2][0];
  1169. /* argument 4: sender */
  1170. $hdr->sender = $arg_a[3][0];
  1171. /* argument 5: reply-to */
  1172. $hdr->replyto = $arg_a[4][0];
  1173. /* argument 6: to */
  1174. $hdr->to = $arg_a[5];
  1175. /* argument 7: cc */
  1176. $hdr->cc = $arg_a[6];
  1177. /* argument 8: bcc */
  1178. $hdr->bcc = $arg_a[7];
  1179. /* argument 9: in-reply-to */
  1180. $hdr->inreplyto = $arg_a[8];
  1181. /* argument 10: message-id */
  1182. $hdr->message_id = $arg_a[9];
  1183. }
  1184. return (array($hdr,$i));
  1185. }
  1186. function parseLiteral($read, $i)
  1187. {
  1188. $lit_cnt = '';
  1189. $i++;
  1190. while ($read{$i} != '}')
  1191. {
  1192. $lit_cnt .= $read{$i};
  1193. $i++;
  1194. }
  1195. $lit_cnt +=2; /* add the { and } characters */
  1196. $s = '';
  1197. for ($j = 0; $j < $lit_cnt; $j++)
  1198. {
  1199. $i++;
  1200. $s .= $read{$i};
  1201. }
  1202. return (array($s, $i));
  1203. }
  1204. function parseQuote($read, $i)
  1205. {
  1206. $i++;
  1207. $s = '';
  1208. while ($read{$i} != '"')
  1209. {
  1210. if ($read{$i} == '\\')
  1211. {
  1212. $i++;
  1213. }
  1214. $s .= $read{$i};
  1215. $i++;
  1216. }
  1217. return (array($s, $i));
  1218. }
  1219. function parseAddress($read, $i)
  1220. {
  1221. $arg_a = array();
  1222. while ($read{$i} != ')' )
  1223. {
  1224. $char = strtoupper($read{$i});
  1225. switch ($char)
  1226. {
  1227. case '"':
  1228. $res = $this->parseQuote($read,$i);
  1229. $arg_a[] = $res[0];
  1230. $i = $res[1];
  1231. break;
  1232. case '{':
  1233. $res = $this->parseLiteral($read,$i);
  1234. $arg_a[] = $res[0];
  1235. $i = $res[1];
  1236. break;
  1237. case 'n':
  1238. case 'N':
  1239. if (strtoupper(substr($read,$i,3)) == 'NIL') {
  1240. $arg_a[] = '';
  1241. $i = $i+2;
  1242. }
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. $i++;
  1248. }
  1249. if (count($arg_a) == 4)
  1250. {
  1251. $adr = new address_structure();
  1252. $adr->personal = $arg_a[0];
  1253. $adr->adl = $arg_a[1];
  1254. $adr->mailbox = $arg_a[2];
  1255. $adr->host = $arg_a[3];
  1256. } else
  1257. {
  1258. $adr = '';
  1259. }
  1260. return (array($adr,$i));
  1261. }
  1262. function parseDisposition($read,$i)
  1263. {
  1264. $arg_a = array();
  1265. while ($read{$i} != ')')
  1266. {
  1267. switch ($read{$i})
  1268. {
  1269. case '"':
  1270. $res = $this->parseQuote($read,$i);
  1271. $arg_a[] = $res[0];
  1272. $i = $res[1];
  1273. break;
  1274. case '{':
  1275. $res = $this->parseLiteral($read,$i);
  1276. $arg_a[] = $res[0];
  1277. $i = $res[1];
  1278. break;
  1279. case '(':
  1280. $res = $this->parseProperties($read,$i);
  1281. $arg_a[] = $res[0];
  1282. $i = $res[1];
  1283. break;
  1284. default:
  1285. break;
  1286. }
  1287. $i++;
  1288. }
  1289. if (isset($arg_a[0]))
  1290. {
  1291. $disp = new disposition($arg_a[0]);
  1292. if (isset($arg_a[1]))
  1293. {
  1294. $disp->properties = $arg_a[1];
  1295. }
  1296. }
  1297. if (is_object($disp))
  1298. {
  1299. return (array($disp, $i));
  1300. } else
  1301. {
  1302. return (array('',$i));
  1303. }
  1304. }
  1305. function parseLanguage($read,$i)
  1306. {
  1307. /* no idea how to process this one without examples */
  1308. $arg_a = array();
  1309. while ($read{$i} != ')')
  1310. {
  1311. switch ($read{$i})
  1312. {
  1313. case '"':
  1314. $res = $this->parseQuote($read,$i);
  1315. $arg_a[] = $res[0];
  1316. $i = $res[1];
  1317. break;
  1318. case '{':
  1319. $res = $this->parseLiteral($read,$i);
  1320. $arg_a[] = $res[0];
  1321. $i = $res[1];
  1322. break;
  1323. case '(':
  1324. $res = $this->parseProperties($read,$i);
  1325. $arg_a[] = $res[0];
  1326. $i = $res[1];
  1327. break;
  1328. default:
  1329. break;
  1330. }
  1331. $i++;
  1332. }
  1333. if (isset($arg_a[0]))
  1334. {
  1335. $lang = new language($arg_a[0]);
  1336. if (isset($arg_a[1]))
  1337. {
  1338. $lang->properties = $arg_a[1];
  1339. }
  1340. }
  1341. if (is_object($lang))
  1342. {
  1343. return (array($lang, $i));
  1344. } else
  1345. {
  1346. return (array('', $i));
  1347. }
  1348. }
  1349. function parseParenthesis($read,$i)
  1350. {
  1351. while ($read{$i} != ')')
  1352. {
  1353. switch ($read{$i})
  1354. {
  1355. case '"':
  1356. $res = $this->parseQuote($read,$i);
  1357. $i = $res[1];
  1358. break;
  1359. case '{':
  1360. $res = $this->parseLiteral($read,$i);
  1361. $i = $res[1];
  1362. break;
  1363. case '(':
  1364. $res = $this->parseParenthesis($read,$i);
  1365. $i = $res[1];
  1366. break;
  1367. default:
  1368. break;
  1369. }
  1370. $i++;
  1371. }
  1372. return $i;
  1373. }
  1374. /* function to fill the message structure in case the bodystructure
  1375. isn't available NOT FINISHED YET
  1376. */
  1377. function parseMessage($read, $type0, $type1)
  1378. {
  1379. switch ($type0)
  1380. {
  1381. case 'message':
  1382. $rfc822_header = true;
  1383. $mime_header = false;
  1384. break;
  1385. case 'multipart':
  1386. $mime_header = true;
  1387. $rfc822_header = false;
  1388. break;
  1389. default:
  1390. return $read;
  1391. }
  1392. for ($i=1; $i < $count; $i++)
  1393. {
  1394. $line = trim($body[$i]);
  1395. if ( ( $mime_header || $rfc822_header) &&
  1396. (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i",$line,$reg)) )
  1397. {
  1398. $bnd = $reg[1];
  1399. $bndreg = $bnd;
  1400. $bndreg = str_replace("\\","\\\\",$bndreg);
  1401. $bndreg = str_replace("?","\\?",$bndreg);
  1402. $bndreg = str_replace("+","\\+",$bndreg);
  1403. $bndreg = str_replace(".","\\.",$bndreg);
  1404. $bndreg = str_replace("/","\\/",$bndreg);
  1405. $bndreg = str_replace("-","\\-",$bndreg);
  1406. $bndreg = str_replace("(","\\(",$bndreg);
  1407. $bndreg = str_replace(")","\\)",$bndreg);
  1408. } elseif ( $rfc822_header && $line == '' )
  1409. {
  1410. $rfc822_header = false;
  1411. if ($msg->type0 == 'multipart')
  1412. {
  1413. $mime_header = true;
  1414. }
  1415. }
  1416. if (($line{0} == '-' || $rfc822_header) && isset($boundaries[0]))
  1417. {
  1418. $cnt=count($boundaries)-1;
  1419. $bnd = $boundaries[$cnt]['bnd'];
  1420. $bndreg = $boundaries[$cnt]['bndreg'];
  1421. $regstr = '/^--'."($bndreg)".".*".'/';
  1422. if (preg_match($regstr,$line,$reg) )
  1423. {
  1424. $bndlen = strlen($reg[1]);
  1425. $bndend = false;
  1426. if (strlen($line) > ($bndlen + 3))
  1427. {
  1428. if ($line{$bndlen+2} == '-' && $line{$bndlen+3} == '-')
  1429. $bndend = true;
  1430. }
  1431. if ($bndend)
  1432. {
  1433. /* calc offset and return $msg */
  1434. // $entStr = CalcEntity("$entStr",-1);
  1435. array_pop($boundaries);
  1436. $mime_header = true;
  1437. $bnd_end = true;
  1438. } else
  1439. {
  1440. $mime_header = true;
  1441. $bnd_end = false;
  1442. // $entStr = CalcEntity("$entStr",0);
  1443. $content_indx++;
  1444. }
  1445. } else
  1446. {
  1447. if ($header)
  1448. {
  1449. }
  1450. }
  1451. }
  1452. }
  1453. }
  1454. function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html'))
  1455. {
  1456. $found = false;
  1457. $type = $this->type0.'/'.$this->type1;
  1458. if ( $type == 'multipart/alternative')
  1459. {
  1460. $msg = $this->findAlternativeEntity($alt_order);
  1461. if (count($msg->entities) == 0)
  1462. {
  1463. $entity[] = $msg->entity_id;
  1464. } else
  1465. {
  1466. $entity = $msg->findDisplayEntity($entity, $alt_order);
  1467. }
  1468. $found = true;
  1469. } else if ( $type == 'multipart/related')
  1470. {
  1471. $msgs = $this->findRelatedEntity();
  1472. for ($i = 0; $i < count($msgs); $i++)
  1473. {
  1474. $msg = $msgs[$i];
  1475. if (count($msg->entities) == 0)
  1476. {
  1477. $entity[] = $msg->entity_id;
  1478. } else
  1479. {
  1480. $entity = $msg->findDisplayEntity($entity,$alt_order);
  1481. }
  1482. $found = true;
  1483. }
  1484. } else if ( $this->type0 == 'text' &&
  1485. ( $this->type1 == 'plain' ||
  1486. $this->type1 == 'html' ||
  1487. $this->type1 == 'message') &&
  1488. isset($this->entity_id) )
  1489. {
  1490. if (count($this->entities) == 0)
  1491. {
  1492. if (strtolower($this->header->disposition->name) != 'attachment')
  1493. {
  1494. $entity[] = $this->entity_id;
  1495. }
  1496. }
  1497. }
  1498. $i = 0;
  1499. while ( isset($this->entities[$i]) && !$found &&
  1500. (strtolower($this->entities[$i]->header->disposition->name)
  1501. != 'attachment') &&
  1502. ($this->entities[$i]->type0 != 'message' &&
  1503. $this->entities[$i]->type1 != 'rfc822' )
  1504. )
  1505. {
  1506. $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order);
  1507. $i++;
  1508. }
  1509. return( $entity );
  1510. }
  1511. function findAlternativeEntity ($alt_order)
  1512. {
  1513. /* if we are dealing with alternative parts then we choose the best
  1514. * viewable message supported by SM.
  1515. */
  1516. $best_view = 0;
  1517. $ent_id = 0;
  1518. $k = 0;
  1519. for ($i = 0; $i < count($this->entities); $i ++)
  1520. {
  1521. $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
  1522. if ($type == 'multipart/related')
  1523. {
  1524. $type = $this->entities[$i]->header->getParameter('type');
  1525. }
  1526. for ($j = $k; $j < count($alt_order); $j++)
  1527. {
  1528. if ($alt_order[$j] == $type && $j > $best_view)
  1529. {
  1530. $best_view = $j;
  1531. $ent_id = $i;
  1532. $k = $j;
  1533. }
  1534. }
  1535. }
  1536. return $this->entities[$ent_id];
  1537. }
  1538. function findRelatedEntity ()
  1539. {
  1540. $msgs = array();
  1541. for ($i = 0; $i < count($this->entities); $i ++)
  1542. {
  1543. $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
  1544. if ($this->header->getParameter('type') == $type)
  1545. {
  1546. $msgs[] = $this->entities[$i];
  1547. }
  1548. }
  1549. return $msgs;
  1550. }
  1551. function getAttachments($exclude_id=array(), $result = array())
  1552. {
  1553. if ($this->type0 == 'message' && $this->type1 == 'rfc822')
  1554. {
  1555. $this = $this->entities[0];
  1556. }
  1557. if (count($this->entities))
  1558. {
  1559. foreach ($this->entities as $entity)
  1560. {
  1561. $exclude = false;
  1562. foreach ($exclude_id as $excl)
  1563. {
  1564. if ($entity->entity_id == $excl)
  1565. {
  1566. $exclude = true;
  1567. }
  1568. }
  1569. if (!$exclude)
  1570. {
  1571. if ($entity->type0 == 'multipart' &&
  1572. $entity->type1 != 'related')
  1573. {
  1574. $result = $entity->getAttachments($exclude_id, $result);
  1575. } else if ($entity->type0 != 'multipart')
  1576. {
  1577. $result[] = $entity;
  1578. }
  1579. }
  1580. }
  1581. } else
  1582. {
  1583. $exclude = false;
  1584. foreach ($exclude_id as $excl)
  1585. {
  1586. if ($this->entity_id == $excl)
  1587. {
  1588. $exclude = true;
  1589. }
  1590. }
  1591. if (!$exclude)
  1592. {
  1593. $result[] = $this;
  1594. }
  1595. }
  1596. return $result;
  1597. }
  1598. }
  1599. class smime_message
  1600. {
  1601. }
  1602. class disposition
  1603. {
  1604. function disposition($name)
  1605. {
  1606. $this->name = $name;
  1607. $this->properties = array();
  1608. }
  1609. function getProperty($par)
  1610. {
  1611. $value = strtolower($par);
  1612. if (isset($this->properties[$par]))
  1613. {
  1614. return $this->properties[$par];
  1615. }
  1616. return '';
  1617. }
  1618. }
  1619. class language
  1620. {
  1621. function language($name)
  1622. {
  1623. $this->name = $name;
  1624. $this->properties = array();
  1625. }
  1626. }
  1627. class content_type
  1628. {
  1629. var $type0='text',
  1630. $type1='plain',
  1631. $properties='';
  1632. function content_type($type)
  1633. {
  1634. $pos = strpos($type,'/');
  1635. if ($pos > 0)
  1636. {
  1637. $this->type0 = substr($type,0,$pos);
  1638. $this->type1 = substr($type,$pos+1);
  1639. } else
  1640. {
  1641. $this->type0 = $type;
  1642. }
  1643. $this->properties = array();
  1644. }
  1645. }
  1646. ?>