Message.class.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. <?php
  2. /**
  3. * Message.class.php
  4. *
  5. * This file contains functions needed to handle 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. * The object that contains a message.
  16. *
  17. * message is the object that contains messages. It is a recursive object in
  18. * that through the $entities variable, it can contain more objects of type
  19. * message. See documentation in mime.txt for a better description of how this
  20. * works.
  21. * @package squirrelmail
  22. * @subpackage mime
  23. * @since 1.3.0
  24. */
  25. class Message {
  26. /**
  27. * rfc822header object
  28. * @var object
  29. */
  30. var $rfc822_header = '';
  31. /**
  32. * MessageHeader object
  33. * @var object
  34. */
  35. var $mime_header = '';
  36. /**
  37. * @var mixed
  38. */
  39. var $flags = '';
  40. /**
  41. * Media type
  42. * @var string
  43. */
  44. var $type0='';
  45. /**
  46. * Media subtype
  47. * @var string
  48. */
  49. var $type1='';
  50. /**
  51. * Nested mime parts
  52. * @var array
  53. */
  54. var $entities = array();
  55. /**
  56. * Message part id
  57. * @var string
  58. */
  59. var $entity_id = '';
  60. /**
  61. * Parent message part id
  62. * @var string
  63. */
  64. var $parent_ent;
  65. /**
  66. * @var mixed
  67. */
  68. var $entity;
  69. /**
  70. * @var mixed
  71. */
  72. var $parent = '';
  73. /**
  74. * @var string
  75. */
  76. var $decoded_body='';
  77. /**
  78. * Message \seen status
  79. * @var boolean
  80. */
  81. var $is_seen = 0;
  82. /**
  83. * Message \answered status
  84. * @var boolean
  85. */
  86. var $is_answered = 0;
  87. /**
  88. * Message \deleted status
  89. * @var boolean
  90. */
  91. var $is_deleted = 0;
  92. /**
  93. * Message \flagged status
  94. * @var boolean
  95. */
  96. var $is_flagged = 0;
  97. /**
  98. * Message mdn status
  99. * @var boolean
  100. */
  101. var $is_mdnsent = 0;
  102. /**
  103. * Message text body
  104. * @var string
  105. */
  106. var $body_part = '';
  107. /**
  108. * Message part offset
  109. * for fetching body parts out of raw messages
  110. * @var integer
  111. */
  112. var $offset = 0;
  113. /**
  114. * Message part length
  115. * for fetching body parts out of raw messages
  116. * @var integer
  117. */
  118. var $length = 0;
  119. /**
  120. * Local attachment filename location where the tempory attachment is
  121. * stored. For use in delivery class.
  122. * @var string
  123. */
  124. var $att_local_name = '';
  125. /**
  126. * @param string $ent entity id
  127. */
  128. function setEnt($ent) {
  129. $this->entity_id= $ent;
  130. }
  131. /**
  132. * Add nested message part
  133. * @param object $msg
  134. */
  135. function addEntity ($msg) {
  136. $this->entities[] = $msg;
  137. }
  138. /**
  139. * Get file name used for mime part
  140. * @return string file name
  141. * @since 1.3.2
  142. */
  143. function getFilename() {
  144. $filename = '';
  145. $header = $this->header;
  146. if (is_object($header->disposition)) {
  147. $filename = $header->disposition->getProperty('filename');
  148. if (trim($filename) == '') {
  149. $name = decodeHeader($header->disposition->getProperty('name'));
  150. if (!trim($name)) {
  151. $name = $header->getParameter('name');
  152. if(!trim($name)) {
  153. if (!trim( $header->id )) {
  154. $filename = 'untitled-[' . $this->entity_id . ']' ;
  155. } else {
  156. $filename = 'cid: ' . $header->id;
  157. }
  158. } else {
  159. $filename = $name;
  160. }
  161. } else {
  162. $filename = $name;
  163. }
  164. }
  165. } else {
  166. $filename = $header->getParameter('filename');
  167. if (!trim($filename)) {
  168. $filename = $header->getParameter('name');
  169. if (!trim($filename)) {
  170. if (!trim( $header->id )) {
  171. $filename = 'untitled-[' . $this->entity_id . ']' ;
  172. } else {
  173. $filename = 'cid: ' . $header->id;
  174. }
  175. }
  176. }
  177. }
  178. return $filename;
  179. }
  180. /**
  181. * Add header object to message object.
  182. * WARNING: Unfinished code. Don't expect it to work in older sm versions.
  183. * @param mixed $read array or string with message headers
  184. * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object
  185. */
  186. function addRFC822Header($read) {
  187. $header = new Rfc822Header();
  188. $this->rfc822_header = $header->parseHeader($read);
  189. }
  190. /**
  191. * @param string $ent
  192. * @return mixed (object or string?)
  193. */
  194. function getEntity($ent) {
  195. $cur_ent = $this->entity_id;
  196. $msg = $this;
  197. if (($cur_ent == '') || ($cur_ent == '0')) {
  198. $cur_ent_a = array();
  199. } else {
  200. $cur_ent_a = explode('.', $this->entity_id);
  201. }
  202. $ent_a = explode('.', $ent);
  203. for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++$i) {
  204. if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) {
  205. $msg = $msg->parent;
  206. $cur_ent_a = explode('.', $msg->entity_id);
  207. --$i;
  208. } else if (!isset($cur_ent_a[$i])) {
  209. if (isset($msg->entities[($ent_a[$i]-1)])) {
  210. $msg = $msg->entities[($ent_a[$i]-1)];
  211. } else {
  212. $msg = $msg->entities[0];
  213. }
  214. }
  215. if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
  216. /*this is a header for a message/rfc822 entity */
  217. $msg = $msg->entities[0];
  218. }
  219. }
  220. if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) {
  221. /*this is a header for a message/rfc822 entity */
  222. $msg = $msg->entities[0];
  223. }
  224. if (isset($msg->entities[($ent_a[$entCount])-1])) {
  225. if (is_object($msg->entities[($ent_a[$entCount])-1])) {
  226. $msg = $msg->entities[($ent_a[$entCount]-1)];
  227. }
  228. }
  229. return $msg;
  230. }
  231. /**
  232. * Set message body
  233. * @param string $s message body
  234. */
  235. function setBody($s) {
  236. $this->body_part = $s;
  237. }
  238. /**
  239. * Clean message object
  240. */
  241. function clean_up() {
  242. $msg = $this;
  243. $msg->body_part = '';
  244. foreach ($msg->entities as $m) {
  245. $m->clean_up();
  246. }
  247. }
  248. /**
  249. * @return string
  250. */
  251. function getMailbox() {
  252. $msg = $this;
  253. while (is_object($msg->parent)) {
  254. $msg = $msg->parent;
  255. }
  256. return $msg->mailbox;
  257. }
  258. /*
  259. * Bodystructure parser, a recursive function for generating the
  260. * entity-tree with all the mime-parts.
  261. *
  262. * It follows RFC2060 and stores all the described fields in the
  263. * message object.
  264. *
  265. * Question/Bugs:
  266. *
  267. * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net)
  268. * @param string $read
  269. * @param integer $i
  270. * @param mixed $sub_msg
  271. * @return object Message object
  272. * @todo define argument and return types
  273. */
  274. function parseStructure($read, &$i, $sub_msg = '') {
  275. $msg = Message::parseBodyStructure($read, $i, $sub_msg);
  276. if($msg) $msg->setEntIds($msg,false,0);
  277. return $msg;
  278. }
  279. /**
  280. * @param object $msg
  281. * @param mixed $init
  282. * @param integer $i
  283. * @todo document me
  284. * @since 1.4.0
  285. */
  286. function setEntIds(&$msg,$init=false,$i=0) {
  287. $iCnt = count($msg->entities);
  288. if ($init !==false) {
  289. $iEntSub = $i+1;
  290. if ($msg->parent->type0 == 'message' &&
  291. $msg->parent->type1 == 'rfc822' &&
  292. $msg->type0 == 'multipart') {
  293. $iEntSub = '0';
  294. }
  295. if ($init) {
  296. $msg->entity_id = "$init.$iEntSub";
  297. } else {
  298. $msg->entity_id = $iEntSub;
  299. }
  300. } else if ($iCnt) {
  301. $msg->entity_id='0';
  302. } else {
  303. $msg->entity_id='1';
  304. }
  305. for ($i=0;$i<$iCnt;++$i) {
  306. $msg->entities[$i]->parent =& $msg;
  307. if (strrchr($msg->entity_id, '.') != '.0') {
  308. $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->entity_id,$i);
  309. } else {
  310. $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->parent->entity_id,$i);
  311. }
  312. }
  313. }
  314. /**
  315. * @param string $read
  316. * @param integer $i
  317. * @param mixed $sub_msg
  318. * @return object Message object
  319. * @todo document me
  320. * @since 1.4.0 (code was part of parseStructure() in 1.3.x)
  321. */
  322. function parseBodyStructure($read, &$i, $sub_msg = '') {
  323. $arg_no = 0;
  324. $arg_a = array();
  325. if ($sub_msg) {
  326. $message = $sub_msg;
  327. } else {
  328. $message = new Message();
  329. }
  330. for ($cnt = strlen($read); $i < $cnt; ++$i) {
  331. $char = strtoupper($read{$i});
  332. switch ($char) {
  333. case '(':
  334. switch($arg_no) {
  335. case 0:
  336. if (!isset($msg)) {
  337. $msg = new Message();
  338. $hdr = new MessageHeader();
  339. $hdr->type0 = 'text';
  340. $hdr->type1 = 'plain';
  341. $hdr->encoding = 'us-ascii';
  342. } else {
  343. $msg->header->type0 = 'multipart';
  344. $msg->type0 = 'multipart';
  345. while ($read{$i} == '(') {
  346. $msg->addEntity($msg->parseBodyStructure($read, $i, $msg));
  347. }
  348. }
  349. break;
  350. case 1:
  351. /* multipart properties */
  352. ++$i;
  353. $arg_a[] = $msg->parseProperties($read, $i);
  354. ++$arg_no;
  355. break;
  356. case 2:
  357. if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
  358. ++$i;
  359. $arg_a[] = $msg->parseDisposition($read, $i);
  360. } else { /* properties */
  361. $arg_a[] = $msg->parseProperties($read, $i);
  362. }
  363. ++$arg_no;
  364. break;
  365. case 3:
  366. if (isset($msg->type0) && ($msg->type0 == 'multipart')) {
  367. ++$i;
  368. $arg_a[]= $msg->parseLanguage($read, $i);
  369. }
  370. case 7:
  371. if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) {
  372. $msg->header->type0 = $arg_a[0];
  373. $msg->header->type1 = $arg_a[1];
  374. $msg->type0 = $arg_a[0];
  375. $msg->type1 = $arg_a[1];
  376. $rfc822_hdr = new Rfc822Header();
  377. $msg->rfc822_header = $msg->parseEnvelope($read, $i, $rfc822_hdr);
  378. while (($i < $cnt) && ($read{$i} != '(')) {
  379. ++$i;
  380. }
  381. $msg->addEntity($msg->parseBodyStructure($read, $i,$msg));
  382. }
  383. break;
  384. case 8:
  385. ++$i;
  386. $arg_a[] = $msg->parseDisposition($read, $i);
  387. ++$arg_no;
  388. break;
  389. case 9:
  390. ++$i;
  391. if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
  392. $arg_a[] = $msg->parseDisposition($read, $i);
  393. } else {
  394. $arg_a[] = $msg->parseLanguage($read, $i);
  395. }
  396. ++$arg_no;
  397. break;
  398. case 10:
  399. if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) {
  400. ++$i;
  401. $arg_a[] = $msg->parseLanguage($read, $i);
  402. } else {
  403. $i = $msg->parseParenthesis($read, $i);
  404. $arg_a[] = ''; /* not yet described in rfc2060 */
  405. }
  406. ++$arg_no;
  407. break;
  408. default:
  409. /* unknown argument, skip this part */
  410. $i = $msg->parseParenthesis($read, $i);
  411. $arg_a[] = '';
  412. ++$arg_no;
  413. break;
  414. } /* switch */
  415. break;
  416. case '"':
  417. /* inside an entity -> start processing */
  418. $arg_s = $msg->parseQuote($read, $i);
  419. ++$arg_no;
  420. if ($arg_no < 3) {
  421. $arg_s = strtolower($arg_s); /* type0 and type1 */
  422. }
  423. $arg_a[] = $arg_s;
  424. break;
  425. case 'n':
  426. case 'N':
  427. /* probably NIL argument */
  428. $tmpnil = strtoupper(substr($read, $i, 4));
  429. if ($tmpnil == 'NIL ' || $tmpnil == 'NIL)') {
  430. $arg_a[] = '';
  431. ++$arg_no;
  432. $i += 2;
  433. }
  434. break;
  435. case '{':
  436. /* process the literal value */
  437. $arg_a[] = $msg->parseLiteral($read, $i);
  438. ++$arg_no;
  439. break;
  440. case '0':
  441. case is_numeric($read{$i}):
  442. /* process integers */
  443. if ($read{$i} == ' ') { break; }
  444. ++$arg_no;
  445. if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) {
  446. $i += strlen($regs[1])-1;
  447. $arg_a[] = $regs[1];
  448. } else {
  449. $arg_a[] = 0;
  450. }
  451. break;
  452. case ')':
  453. $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart'));
  454. if (!$multipart) {
  455. $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')));
  456. $hdr->type0 = $arg_a[0];
  457. $hdr->type1 = $arg_a[1];
  458. $msg->type0 = $arg_a[0];
  459. $msg->type1 = $arg_a[1];
  460. $arr = $arg_a[2];
  461. if (is_array($arr)) {
  462. $hdr->parameters = $arg_a[2];
  463. }
  464. $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3]));
  465. $hdr->description = $arg_a[4];
  466. $hdr->encoding = strtolower($arg_a[5]);
  467. $hdr->entity_id = $msg->entity_id;
  468. $hdr->size = $arg_a[6];
  469. if ($shifted_args) {
  470. $hdr->lines = $arg_a[7];
  471. $s = 1;
  472. } else {
  473. $s = 0;
  474. }
  475. $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5);
  476. $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition);
  477. $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language);
  478. $msg->header = $hdr;
  479. } else {
  480. $hdr->type0 = 'multipart';
  481. $hdr->type1 = $arg_a[0];
  482. $msg->type0 = 'multipart';
  483. $msg->type1 = $arg_a[0];
  484. $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters);
  485. $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition);
  486. $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language);
  487. $msg->header = $hdr;
  488. }
  489. return $msg;
  490. default: break;
  491. } /* switch */
  492. } /* for */
  493. } /* parsestructure */
  494. /**
  495. * @param string $read
  496. * @param integer $i
  497. * @return array
  498. */
  499. function parseProperties($read, &$i) {
  500. $properties = array();
  501. $prop_name = '';
  502. for (; $read{$i} != ')'; ++$i) {
  503. $arg_s = '';
  504. if ($read{$i} == '"') {
  505. $arg_s = $this->parseQuote($read, $i);
  506. } else if ($read{$i} == '{') {
  507. $arg_s = $this->parseLiteral($read, $i);
  508. }
  509. if ($arg_s != '') {
  510. if ($prop_name == '') {
  511. $prop_name = strtolower($arg_s);
  512. $properties[$prop_name] = '';
  513. } else if ($prop_name != '') {
  514. $properties[$prop_name] = $arg_s;
  515. $prop_name = '';
  516. }
  517. }
  518. }
  519. return $properties;
  520. }
  521. /**
  522. * @param string $read
  523. * @param integer $i
  524. * @param object $hdr MessageHeader object
  525. * @return object MessageHeader object
  526. */
  527. function parseEnvelope($read, &$i, $hdr) {
  528. $arg_no = 0;
  529. $arg_a = array();
  530. ++$i;
  531. for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
  532. $char = strtoupper($read{$i});
  533. switch ($char) {
  534. case '"':
  535. $arg_a[] = $this->parseQuote($read, $i);
  536. ++$arg_no;
  537. break;
  538. case '{':
  539. $arg_a[] = $this->parseLiteral($read, $i);
  540. /* temp bugfix (SM 1.5 will have a working clean version)
  541. too much work to implement that version right now */
  542. // --$i;
  543. ++$arg_no;
  544. break;
  545. case 'N':
  546. /* probably NIL argument */
  547. if (strtoupper(substr($read, $i, 3)) == 'NIL') {
  548. $arg_a[] = '';
  549. ++$arg_no;
  550. $i += 2;
  551. }
  552. break;
  553. case '(':
  554. /* Address structure (with group support)
  555. * Note: Group support is useless on SMTP connections
  556. * because the protocol doesn't support it
  557. */
  558. $addr_a = array();
  559. $group = '';
  560. $a=0;
  561. for (; $i < $cnt && $read{$i} != ')'; ++$i) {
  562. if ($read{$i} == '(') {
  563. $addr = $this->parseAddress($read, $i);
  564. if (($addr->host == '') && ($addr->mailbox != '')) {
  565. /* start of group */
  566. $group = $addr->mailbox;
  567. $group_addr = $addr;
  568. $j = $a;
  569. } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) {
  570. /* end group */
  571. if ($a == ($j+1)) { /* no group members */
  572. $group_addr->group = $group;
  573. $group_addr->mailbox = '';
  574. $group_addr->personal = "$group: Undisclosed recipients;";
  575. $addr_a[] = $group_addr;
  576. $group ='';
  577. }
  578. } else {
  579. $addr->group = $group;
  580. $addr_a[] = $addr;
  581. }
  582. ++$a;
  583. }
  584. }
  585. $arg_a[] = $addr_a;
  586. break;
  587. default: break;
  588. }
  589. }
  590. if (count($arg_a) > 9) {
  591. $d = strtr($arg_a[0], array(' ' => ' '));
  592. $d = explode(' ', $d);
  593. if (!$arg_a[1]) $arg_a[1] = _("(no subject)");
  594. $hdr->date = getTimeStamp($d); /* argument 1: date */
  595. $hdr->subject = $arg_a[1]; /* argument 2: subject */
  596. $hdr->from = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */
  597. $hdr->sender = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */
  598. $hdr->replyto = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */
  599. $hdr->to = $arg_a[5]; /* argument 6: to */
  600. $hdr->cc = $arg_a[6]; /* argument 7: cc */
  601. $hdr->bcc = $arg_a[7]; /* argument 8: bcc */
  602. $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */
  603. $hdr->message_id = $arg_a[9]; /* argument 10: message-id */
  604. }
  605. return $hdr;
  606. }
  607. /**
  608. * @param string $read
  609. * @param integer $i
  610. * @return string
  611. * @todo document me
  612. */
  613. function parseLiteral($read, &$i) {
  614. $lit_cnt = '';
  615. ++$i;
  616. $iPos = strpos($read,'}',$i);
  617. if ($iPos) {
  618. $lit_cnt = substr($read, $i, $iPos - $i);
  619. $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */
  620. /* Now read the literal */
  621. $s = ($lit_cnt ? substr($read,$i,$lit_cnt): '');
  622. $i += $lit_cnt;
  623. /* temp bugfix (SM 1.5 will have a working clean version)
  624. too much work to implement that version right now */
  625. --$i;
  626. } else { /* should never happen */
  627. $i += 3; /* } + \r + \n */
  628. $s = '';
  629. }
  630. return $s;
  631. }
  632. /**
  633. * @param string $read
  634. * @param integer $i
  635. * @return string
  636. * @todo document me
  637. */
  638. function parseQuote($read, &$i) {
  639. $s = '';
  640. $iPos = ++$i;
  641. while (true) {
  642. $iPos = strpos($read,'"',$iPos);
  643. if (!$iPos) break;
  644. if ($iPos && $read{$iPos -1} != '\\') {
  645. $s = substr($read,$i,($iPos-$i));
  646. $i = $iPos;
  647. break;
  648. }
  649. ++$iPos;
  650. if ($iPos > strlen($read)) {
  651. break;
  652. }
  653. }
  654. return $s;
  655. }
  656. /**
  657. * @param string $read
  658. * @param integer $i
  659. * @return object AddressStructure object
  660. */
  661. function parseAddress($read, &$i) {
  662. $arg_a = array();
  663. for (; $read{$i} != ')'; ++$i) {
  664. $char = strtoupper($read{$i});
  665. switch ($char) {
  666. case '"': $arg_a[] = $this->parseQuote($read, $i); break;
  667. case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
  668. case 'n':
  669. case 'N':
  670. if (strtoupper(substr($read, $i, 3)) == 'NIL') {
  671. $arg_a[] = '';
  672. $i += 2;
  673. }
  674. break;
  675. default: break;
  676. }
  677. }
  678. if (count($arg_a) == 4) {
  679. $adr = new AddressStructure();
  680. $adr->personal = $arg_a[0];
  681. $adr->adl = $arg_a[1];
  682. $adr->mailbox = $arg_a[2];
  683. $adr->host = $arg_a[3];
  684. } else {
  685. $adr = '';
  686. }
  687. return $adr;
  688. }
  689. /**
  690. * @param string $read
  691. * @param integer $i
  692. * @param object Disposition object or empty string
  693. */
  694. function parseDisposition($read, &$i) {
  695. $arg_a = array();
  696. for (; $read{$i} != ')'; ++$i) {
  697. switch ($read{$i}) {
  698. case '"': $arg_a[] = $this->parseQuote($read, $i); break;
  699. case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
  700. case '(': $arg_a[] = $this->parseProperties($read, $i); break;
  701. default: break;
  702. }
  703. }
  704. if (isset($arg_a[0])) {
  705. $disp = new Disposition($arg_a[0]);
  706. if (isset($arg_a[1])) {
  707. $disp->properties = $arg_a[1];
  708. }
  709. }
  710. return (is_object($disp) ? $disp : '');
  711. }
  712. /**
  713. * @param string $read
  714. * @param integer $i
  715. * @return object Language object or empty string
  716. */
  717. function parseLanguage($read, &$i) {
  718. /* no idea how to process this one without examples */
  719. $arg_a = array();
  720. for (; $read{$i} != ')'; ++$i) {
  721. switch ($read{$i}) {
  722. case '"': $arg_a[] = $this->parseQuote($read, $i); break;
  723. case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
  724. case '(': $arg_a[] = $this->parseProperties($read, $i); break;
  725. default: break;
  726. }
  727. }
  728. if (isset($arg_a[0])) {
  729. $lang = new Language($arg_a[0]);
  730. if (isset($arg_a[1])) {
  731. $lang->properties = $arg_a[1];
  732. }
  733. }
  734. return (is_object($lang) ? $lang : '');
  735. }
  736. /**
  737. * Parse message text enclosed in parenthesis
  738. * @param string $read
  739. * @param integer $i
  740. * @return integer
  741. */
  742. function parseParenthesis($read, $i) {
  743. for (; $read{$i} != ')'; ++$i) {
  744. switch ($read{$i}) {
  745. case '"': $this->parseQuote($read, $i); break;
  746. case '{': $this->parseLiteral($read, $i); break;
  747. case '(': $this->parseProperties($read, $i); break;
  748. default: break;
  749. }
  750. }
  751. return $i;
  752. }
  753. /**
  754. * Function to fill the message structure in case the
  755. * bodystructure is not available
  756. * NOT FINISHED YET
  757. * @param string $read
  758. * @param string $type0 message part type
  759. * @param string $type1 message part subtype
  760. * @return string (only when type0 is not message or multipart)
  761. */
  762. function parseMessage($read, $type0, $type1) {
  763. switch ($type0) {
  764. case 'message':
  765. $rfc822_header = true;
  766. $mime_header = false;
  767. break;
  768. case 'multipart':
  769. $rfc822_header = false;
  770. $mime_header = true;
  771. break;
  772. default: return $read;
  773. }
  774. for ($i = 1; $i < $count; ++$i) {
  775. $line = trim($body[$i]);
  776. if (($mime_header || $rfc822_header) &&
  777. (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
  778. $bnd = $reg[1];
  779. $bndreg = $bnd;
  780. $bndreg = str_replace("\\", "\\\\", $bndreg);
  781. $bndreg = str_replace("?", "\\?", $bndreg);
  782. $bndreg = str_replace("+", "\\+", $bndreg);
  783. $bndreg = str_replace(".", "\\.", $bndreg);
  784. $bndreg = str_replace("/", "\\/", $bndreg);
  785. $bndreg = str_replace("-", "\\-", $bndreg);
  786. $bndreg = str_replace("(", "\\(", $bndreg);
  787. $bndreg = str_replace(")", "\\)", $bndreg);
  788. } else if ($rfc822_header && $line == '') {
  789. $rfc822_header = false;
  790. if ($msg->type0 == 'multipart') {
  791. $mime_header = true;
  792. }
  793. }
  794. if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
  795. $cnt = count($boundaries)-1;
  796. $bnd = $boundaries[$cnt]['bnd'];
  797. $bndreg = $boundaries[$cnt]['bndreg'];
  798. $regstr = '/^--'."($bndreg)".".*".'/';
  799. if (preg_match($regstr, $line, $reg)) {
  800. $bndlen = strlen($reg[1]);
  801. $bndend = false;
  802. if (strlen($line) > ($bndlen + 3)) {
  803. if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
  804. $bndend = true;
  805. }
  806. }
  807. if ($bndend) {
  808. /* calc offset and return $msg */
  809. //$entStr = CalcEntity("$entStr", -1);
  810. array_pop($boundaries);
  811. $mime_header = true;
  812. $bnd_end = true;
  813. } else {
  814. $mime_header = true;
  815. $bnd_end = false;
  816. //$entStr = CalcEntity("$entStr", 0);
  817. ++$content_indx;
  818. }
  819. } else {
  820. if ($header) { }
  821. }
  822. }
  823. }
  824. }
  825. /**
  826. * @param array $entity
  827. * @param array $alt_order
  828. * @param boolean $strict
  829. * @return array
  830. */
  831. function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
  832. $found = false;
  833. if ($this->type0 == 'multipart') {
  834. if($this->type1 == 'alternative') {
  835. $msg = $this->findAlternativeEntity($alt_order);
  836. if (count($msg->entities) == 0) {
  837. $entity[] = $msg->entity_id;
  838. } else {
  839. $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
  840. }
  841. $found = true;
  842. } else if ($this->type1 == 'related') { /* RFC 2387 */
  843. $msgs = $this->findRelatedEntity();
  844. foreach ($msgs as $msg) {
  845. if (count($msg->entities) == 0) {
  846. $entity[] = $msg->entity_id;
  847. } else {
  848. $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
  849. }
  850. }
  851. if (count($msgs) > 0) {
  852. $found = true;
  853. }
  854. } else { /* Treat as multipart/mixed */
  855. foreach ($this->entities as $ent) {
  856. if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
  857. (!isset($ent->header->parameters['filename'])) &&
  858. (!isset($ent->header->parameters['name'])) &&
  859. (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
  860. $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
  861. $found = true;
  862. }
  863. }
  864. }
  865. } else { /* If not multipart, then just compare with each entry from $alt_order */
  866. $type = $this->type0.'/'.$this->type1;
  867. // $alt_order[] = "message/rfc822";
  868. foreach ($alt_order as $alt) {
  869. if( ($alt == $type) && isset($this->entity_id) ) {
  870. if ((count($this->entities) == 0) &&
  871. (!isset($this->header->parameters['filename'])) &&
  872. (!isset($this->header->parameters['name'])) &&
  873. isset($this->header->disposition) && is_object($this->header->disposition) &&
  874. !(is_object($this->header->disposition) && strtolower($this->header->disposition->name) == 'attachment')) {
  875. $entity[] = $this->entity_id;
  876. $found = true;
  877. }
  878. }
  879. }
  880. }
  881. if(!$found) {
  882. foreach ($this->entities as $ent) {
  883. if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
  884. (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
  885. $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
  886. $found = true;
  887. }
  888. }
  889. }
  890. if(!$strict && !$found) {
  891. if (($this->type0 == 'text') &&
  892. in_array($this->type1, array('plain', 'html', 'message')) &&
  893. isset($this->entity_id)) {
  894. if (count($this->entities) == 0) {
  895. if (!is_object($this->header->disposition) || strtolower($this->header->disposition->name) != 'attachment') {
  896. $entity[] = $this->entity_id;
  897. }
  898. }
  899. }
  900. }
  901. return $entity;
  902. }
  903. /**
  904. * @param array $alt_order
  905. * @return array
  906. */
  907. function findAlternativeEntity($alt_order) {
  908. /* If we are dealing with alternative parts then we */
  909. /* choose the best viewable message supported by SM. */
  910. $best_view = 0;
  911. $entity = array();
  912. foreach($this->entities as $ent) {
  913. $type = $ent->header->type0 . '/' . $ent->header->type1;
  914. if ($type == 'multipart/related') {
  915. $type = $ent->header->getParameter('type');
  916. // Mozilla bug. Mozilla does not provide the parameter type.
  917. if (!$type) $type = 'text/html';
  918. }
  919. $altCount = count($alt_order);
  920. for ($j = $best_view; $j < $altCount; ++$j) {
  921. if (($alt_order[$j] == $type) && ($j >= $best_view)) {
  922. $best_view = $j;
  923. $entity = $ent;
  924. }
  925. }
  926. }
  927. return $entity;
  928. }
  929. /**
  930. * @return array
  931. */
  932. function findRelatedEntity() {
  933. $msgs = array();
  934. $related_type = $this->header->getParameter('type');
  935. // Mozilla bug. Mozilla does not provide the parameter type.
  936. if (!$related_type) $related_type = 'text/html';
  937. $entCount = count($this->entities);
  938. for ($i = 0; $i < $entCount; ++$i) {
  939. $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
  940. if ($related_type == $type) {
  941. $msgs[] = $this->entities[$i];
  942. }
  943. }
  944. return $msgs;
  945. }
  946. /**
  947. * @param array $exclude_id
  948. * @param array $result
  949. * @return array
  950. */
  951. function getAttachments($exclude_id=array(), $result = array()) {
  952. /*
  953. if (($this->type0 == 'message') &&
  954. ($this->type1 == 'rfc822') &&
  955. ($this->entity_id) ) {
  956. $this = $this->entities[0];
  957. }
  958. */
  959. if (count($this->entities)) {
  960. foreach ($this->entities as $entity) {
  961. $exclude = false;
  962. foreach ($exclude_id as $excl) {
  963. if ($entity->entity_id === $excl) {
  964. $exclude = true;
  965. }
  966. }
  967. if (!$exclude) {
  968. if (($entity->type0 == 'multipart') &&
  969. ($entity->type1 != 'related')) {
  970. $result = $entity->getAttachments($exclude_id, $result);
  971. } else if ($entity->type0 != 'multipart') {
  972. $result[] = $entity;
  973. }
  974. }
  975. }
  976. } else {
  977. $exclude = false;
  978. foreach ($exclude_id as $excl) {
  979. $exclude = $exclude || ($this->entity_id == $excl);
  980. }
  981. if (!$exclude) {
  982. $result[] = $this;
  983. }
  984. }
  985. return $result;
  986. }
  987. /**
  988. * Add attachment to message object
  989. * @param string $type attachment type
  990. * @param string $name attachment name
  991. * @param string $location path to attachment
  992. */
  993. function initAttachment($type, $name, $location) {
  994. $attachment = new Message();
  995. $mime_header = new MessageHeader();
  996. $mime_header->setParameter('name', $name);
  997. // FIXME: duplicate code. see ContentType class
  998. $pos = strpos($type, '/');
  999. if ($pos > 0) {
  1000. $mime_header->type0 = substr($type, 0, $pos);
  1001. $mime_header->type1 = substr($type, $pos+1);
  1002. } else {
  1003. $mime_header->type0 = $type;
  1004. }
  1005. $attachment->att_local_name = $location;
  1006. $disposition = new Disposition('attachment');
  1007. $disposition->properties['filename'] = $name;
  1008. $mime_header->disposition = $disposition;
  1009. $attachment->mime_header = $mime_header;
  1010. $this->entities[]=$attachment;
  1011. }
  1012. /**
  1013. * Delete all attachments from this object from disk.
  1014. * @since 1.5.1
  1015. */
  1016. function purgeAttachments() {
  1017. if ($this->att_local_name && file_exists($this->att_local_name)) {
  1018. unlink($this->att_local_name);
  1019. }
  1020. // recursively delete attachments from entities contained in this object
  1021. for ($i=0, $entCount=count($this->entities);$i< $entCount; ++$i) {
  1022. $this->entities[$i]->purgeAttachments();
  1023. }
  1024. }
  1025. }