Message.class.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. <?php
  2. /**
  3. * Message.class.php
  4. *
  5. * This file contains functions needed to handle mime messages.
  6. *
  7. * @copyright &copy; 2003-2007 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. * function parseQuote
  634. *
  635. * This extract the string value from a quoted string. After the end-quote
  636. * character is found it returns the string. The offset $i when calling
  637. * this function points to the first double quote. At the end it points to
  638. * The ending quote. This function takes care of escaped double quotes.
  639. * "some \"string\""
  640. * ^ ^
  641. * initial $i end position $i
  642. *
  643. * @param string $read
  644. * @param integer $i offset in $read
  645. * @return string string inbetween the double quotes
  646. * @author Marc Groot Koerkamp
  647. */
  648. function parseQuote($read, &$i) {
  649. $s = '';
  650. $iPos = ++$i;
  651. $iPosStart = $iPos;
  652. while (true) {
  653. $iPos = strpos($read,'"',$iPos);
  654. if (!$iPos) break;
  655. if ($iPos && $read{$iPos -1} != '\\') {
  656. $s = substr($read,$i,($iPos-$i));
  657. $i = $iPos;
  658. break;
  659. } else if ($iPos > 1 && $read{$iPos -1} == '\\' && $read{$iPos-2} == '\\') {
  660. // This is an unique situation where the fast detection of the string
  661. // fails. If the quote string ends with \\ then we need to iterate
  662. // through the entire string to make sure we detect the unexcaped
  663. // double quotes correctly.
  664. $s = '';
  665. $bEscaped = false;
  666. $k = 0;
  667. for ($j=$iPosStart,$iCnt=strlen($read);$j<$iCnt;++$j) {
  668. $cChar = $read{$j};
  669. switch ($cChar) {
  670. case '\\':
  671. $bEscaped = !$bEscaped;
  672. $s .= $cChar;
  673. break;
  674. case '"':
  675. if ($bEscaped) {
  676. $s .= $cChar;
  677. $bEscaped = false;
  678. } else {
  679. $i = $j;
  680. break 3;
  681. }
  682. break;
  683. default:
  684. if ($bEscaped) {
  685. $bEscaped = false;
  686. }
  687. $s .= $cChar;
  688. break;
  689. }
  690. }
  691. }
  692. ++$iPos;
  693. if ($iPos > strlen($read)) {
  694. break;
  695. }
  696. }
  697. return $s;
  698. }
  699. /**
  700. * @param string $read
  701. * @param integer $i
  702. * @return object AddressStructure object
  703. */
  704. function parseAddress($read, &$i) {
  705. $arg_a = array();
  706. for (; $read{$i} != ')'; ++$i) {
  707. $char = strtoupper($read{$i});
  708. switch ($char) {
  709. case '"': $arg_a[] = $this->parseQuote($read, $i); break;
  710. case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
  711. case 'n':
  712. case 'N':
  713. if (strtoupper(substr($read, $i, 3)) == 'NIL') {
  714. $arg_a[] = '';
  715. $i += 2;
  716. }
  717. break;
  718. default: break;
  719. }
  720. }
  721. if (count($arg_a) == 4) {
  722. $adr = new AddressStructure();
  723. $adr->personal = $arg_a[0];
  724. $adr->adl = $arg_a[1];
  725. $adr->mailbox = $arg_a[2];
  726. $adr->host = $arg_a[3];
  727. } else {
  728. $adr = '';
  729. }
  730. return $adr;
  731. }
  732. /**
  733. * @param string $read
  734. * @param integer $i
  735. * @param object Disposition object or empty string
  736. */
  737. function parseDisposition($read, &$i) {
  738. $arg_a = array();
  739. for (; $read{$i} != ')'; ++$i) {
  740. switch ($read{$i}) {
  741. case '"': $arg_a[] = $this->parseQuote($read, $i); break;
  742. case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
  743. case '(': $arg_a[] = $this->parseProperties($read, $i); break;
  744. default: break;
  745. }
  746. }
  747. if (isset($arg_a[0])) {
  748. $disp = new Disposition($arg_a[0]);
  749. if (isset($arg_a[1])) {
  750. $disp->properties = $arg_a[1];
  751. }
  752. }
  753. return (is_object($disp) ? $disp : '');
  754. }
  755. /**
  756. * @param string $read
  757. * @param integer $i
  758. * @return object Language object or empty string
  759. */
  760. function parseLanguage($read, &$i) {
  761. /* no idea how to process this one without examples */
  762. $arg_a = array();
  763. for (; $read{$i} != ')'; ++$i) {
  764. switch ($read{$i}) {
  765. case '"': $arg_a[] = $this->parseQuote($read, $i); break;
  766. case '{': $arg_a[] = $this->parseLiteral($read, $i); break;
  767. case '(': $arg_a[] = $this->parseProperties($read, $i); break;
  768. default: break;
  769. }
  770. }
  771. if (isset($arg_a[0])) {
  772. $lang = new Language($arg_a[0]);
  773. if (isset($arg_a[1])) {
  774. $lang->properties = $arg_a[1];
  775. }
  776. }
  777. return (is_object($lang) ? $lang : '');
  778. }
  779. /**
  780. * Parse message text enclosed in parenthesis
  781. * @param string $read
  782. * @param integer $i
  783. * @return integer
  784. */
  785. function parseParenthesis($read, $i) {
  786. for (; $read{$i} != ')'; ++$i) {
  787. switch ($read{$i}) {
  788. case '"': $this->parseQuote($read, $i); break;
  789. case '{': $this->parseLiteral($read, $i); break;
  790. case '(': $this->parseProperties($read, $i); break;
  791. default: break;
  792. }
  793. }
  794. return $i;
  795. }
  796. /**
  797. * Function to fill the message structure in case the
  798. * bodystructure is not available
  799. * NOT FINISHED YET
  800. * @param string $read
  801. * @param string $type0 message part type
  802. * @param string $type1 message part subtype
  803. * @return string (only when type0 is not message or multipart)
  804. */
  805. function parseMessage($read, $type0, $type1) {
  806. switch ($type0) {
  807. case 'message':
  808. $rfc822_header = true;
  809. $mime_header = false;
  810. break;
  811. case 'multipart':
  812. $rfc822_header = false;
  813. $mime_header = true;
  814. break;
  815. default: return $read;
  816. }
  817. for ($i = 1; $i < $count; ++$i) {
  818. $line = trim($body[$i]);
  819. if (($mime_header || $rfc822_header) &&
  820. (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) {
  821. $bnd = $reg[1];
  822. $bndreg = $bnd;
  823. $bndreg = str_replace("\\", "\\\\", $bndreg);
  824. $bndreg = str_replace("?", "\\?", $bndreg);
  825. $bndreg = str_replace("+", "\\+", $bndreg);
  826. $bndreg = str_replace(".", "\\.", $bndreg);
  827. $bndreg = str_replace("/", "\\/", $bndreg);
  828. $bndreg = str_replace("-", "\\-", $bndreg);
  829. $bndreg = str_replace("(", "\\(", $bndreg);
  830. $bndreg = str_replace(")", "\\)", $bndreg);
  831. } else if ($rfc822_header && $line == '') {
  832. $rfc822_header = false;
  833. if ($msg->type0 == 'multipart') {
  834. $mime_header = true;
  835. }
  836. }
  837. if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) {
  838. $cnt = count($boundaries)-1;
  839. $bnd = $boundaries[$cnt]['bnd'];
  840. $bndreg = $boundaries[$cnt]['bndreg'];
  841. $regstr = '/^--'."($bndreg)".".*".'/';
  842. if (preg_match($regstr, $line, $reg)) {
  843. $bndlen = strlen($reg[1]);
  844. $bndend = false;
  845. if (strlen($line) > ($bndlen + 3)) {
  846. if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) {
  847. $bndend = true;
  848. }
  849. }
  850. if ($bndend) {
  851. /* calc offset and return $msg */
  852. //$entStr = CalcEntity("$entStr", -1);
  853. array_pop($boundaries);
  854. $mime_header = true;
  855. $bnd_end = true;
  856. } else {
  857. $mime_header = true;
  858. $bnd_end = false;
  859. //$entStr = CalcEntity("$entStr", 0);
  860. ++$content_indx;
  861. }
  862. } else {
  863. if ($header) { }
  864. }
  865. }
  866. }
  867. }
  868. /**
  869. * @param array $entity
  870. * @param array $alt_order
  871. * @param boolean $strict
  872. * @return array
  873. */
  874. function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) {
  875. $found = false;
  876. if ($this->type0 == 'multipart') {
  877. if($this->type1 == 'alternative') {
  878. $msg = $this->findAlternativeEntity($alt_order);
  879. if (count($msg->entities) == 0) {
  880. $entity[] = $msg->entity_id;
  881. } else {
  882. $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
  883. }
  884. $found = true;
  885. } else if ($this->type1 == 'related') { /* RFC 2387 */
  886. $msgs = $this->findRelatedEntity();
  887. foreach ($msgs as $msg) {
  888. if (count($msg->entities) == 0) {
  889. $entity[] = $msg->entity_id;
  890. } else {
  891. $entity = $msg->findDisplayEntity($entity, $alt_order, $strict);
  892. }
  893. }
  894. if (count($msgs) > 0) {
  895. $found = true;
  896. }
  897. } else { /* Treat as multipart/mixed */
  898. foreach ($this->entities as $ent) {
  899. if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
  900. (!isset($ent->header->parameters['filename'])) &&
  901. (!isset($ent->header->parameters['name'])) &&
  902. (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
  903. $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
  904. $found = true;
  905. }
  906. }
  907. }
  908. } else { /* If not multipart, then just compare with each entry from $alt_order */
  909. $type = $this->type0.'/'.$this->type1;
  910. // $alt_order[] = "message/rfc822";
  911. foreach ($alt_order as $alt) {
  912. if( ($alt == $type) && isset($this->entity_id) ) {
  913. if ((count($this->entities) == 0) &&
  914. (!isset($this->header->parameters['filename'])) &&
  915. (!isset($this->header->parameters['name'])) &&
  916. isset($this->header->disposition) && is_object($this->header->disposition) &&
  917. !(is_object($this->header->disposition) && strtolower($this->header->disposition->name) == 'attachment')) {
  918. $entity[] = $this->entity_id;
  919. $found = true;
  920. }
  921. }
  922. }
  923. }
  924. if(!$found) {
  925. foreach ($this->entities as $ent) {
  926. if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') &&
  927. (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) {
  928. $entity = $ent->findDisplayEntity($entity, $alt_order, $strict);
  929. $found = true;
  930. }
  931. }
  932. }
  933. if(!$strict && !$found) {
  934. if (($this->type0 == 'text') &&
  935. in_array($this->type1, array('plain', 'html', 'message')) &&
  936. isset($this->entity_id)) {
  937. if (count($this->entities) == 0) {
  938. if (!is_object($this->header->disposition) || strtolower($this->header->disposition->name) != 'attachment') {
  939. $entity[] = $this->entity_id;
  940. }
  941. }
  942. }
  943. }
  944. return $entity;
  945. }
  946. /**
  947. * @param array $alt_order
  948. * @return array
  949. */
  950. function findAlternativeEntity($alt_order) {
  951. /* If we are dealing with alternative parts then we */
  952. /* choose the best viewable message supported by SM. */
  953. $best_view = 0;
  954. $entity = array();
  955. foreach($this->entities as $ent) {
  956. $type = $ent->header->type0 . '/' . $ent->header->type1;
  957. if ($type == 'multipart/related') {
  958. $type = $ent->header->getParameter('type');
  959. // Mozilla bug. Mozilla does not provide the parameter type.
  960. if (!$type) $type = 'text/html';
  961. }
  962. $altCount = count($alt_order);
  963. for ($j = $best_view; $j < $altCount; ++$j) {
  964. if (($alt_order[$j] == $type) && ($j >= $best_view)) {
  965. $best_view = $j;
  966. $entity = $ent;
  967. }
  968. }
  969. }
  970. return $entity;
  971. }
  972. /**
  973. * @return array
  974. */
  975. function findRelatedEntity() {
  976. $msgs = array();
  977. $related_type = $this->header->getParameter('type');
  978. // Mozilla bug. Mozilla does not provide the parameter type.
  979. if (!$related_type) $related_type = 'text/html';
  980. $entCount = count($this->entities);
  981. for ($i = 0; $i < $entCount; ++$i) {
  982. $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1;
  983. if ($related_type == $type) {
  984. $msgs[] = $this->entities[$i];
  985. }
  986. }
  987. return $msgs;
  988. }
  989. /**
  990. * @param array $exclude_id
  991. * @param array $result
  992. * @return array
  993. */
  994. function getAttachments($exclude_id=array(), $result = array()) {
  995. /*
  996. if (($this->type0 == 'message') &&
  997. ($this->type1 == 'rfc822') &&
  998. ($this->entity_id) ) {
  999. $this = $this->entities[0];
  1000. }
  1001. */
  1002. if (count($this->entities)) {
  1003. foreach ($this->entities as $entity) {
  1004. $exclude = false;
  1005. foreach ($exclude_id as $excl) {
  1006. if ($entity->entity_id === $excl) {
  1007. $exclude = true;
  1008. }
  1009. }
  1010. if (!$exclude) {
  1011. if (($entity->type0 == 'multipart') &&
  1012. ($entity->type1 != 'related')) {
  1013. $result = $entity->getAttachments($exclude_id, $result);
  1014. } else if ($entity->type0 != 'multipart') {
  1015. $result[] = $entity;
  1016. }
  1017. }
  1018. }
  1019. } else {
  1020. $exclude = false;
  1021. foreach ($exclude_id as $excl) {
  1022. $exclude = $exclude || ($this->entity_id == $excl);
  1023. }
  1024. if (!$exclude) {
  1025. $result[] = $this;
  1026. }
  1027. }
  1028. return $result;
  1029. }
  1030. /**
  1031. * Add attachment to message object
  1032. * @param string $type attachment type
  1033. * @param string $name attachment name
  1034. * @param string $location path to attachment
  1035. */
  1036. function initAttachment($type, $name, $location) {
  1037. $attachment = new Message();
  1038. $mime_header = new MessageHeader();
  1039. $mime_header->setParameter('name', $name);
  1040. // FIXME: duplicate code. see ContentType class
  1041. $pos = strpos($type, '/');
  1042. if ($pos > 0) {
  1043. $mime_header->type0 = substr($type, 0, $pos);
  1044. $mime_header->type1 = substr($type, $pos+1);
  1045. } else {
  1046. $mime_header->type0 = $type;
  1047. }
  1048. $attachment->att_local_name = $location;
  1049. $disposition = new Disposition('attachment');
  1050. $disposition->properties['filename'] = $name;
  1051. $mime_header->disposition = $disposition;
  1052. $attachment->mime_header = $mime_header;
  1053. $this->entities[]=$attachment;
  1054. }
  1055. /**
  1056. * Delete all attachments from this object from disk.
  1057. * @since 1.5.1
  1058. */
  1059. function purgeAttachments() {
  1060. if ($this->att_local_name && file_exists($this->att_local_name)) {
  1061. unlink($this->att_local_name);
  1062. }
  1063. // recursively delete attachments from entities contained in this object
  1064. for ($i=0, $entCount=count($this->entities);$i< $entCount; ++$i) {
  1065. $this->entities[$i]->purgeAttachments();
  1066. }
  1067. }
  1068. }