Message.class.php 39 KB

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