mime.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. <?php
  2. /**
  3. * mime.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains the functions necessary to detect and decode MIME
  9. * messages.
  10. *
  11. * $Id$
  12. */
  13. require_once('../functions/imap.php');
  14. require_once('../functions/attachment_common.php');
  15. /** Setting up the objects that have the structure for the message **/
  16. class msg_header {
  17. /** msg_header contains generic variables for values that **/
  18. /** could be in a header. **/
  19. var $type0 = '', $type1 = '', $boundary = '', $charset = '',
  20. $encoding = '', $size = 0, $to = array(), $from = '', $date = '',
  21. $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
  22. $id = 0, $mailbox = '', $description = '', $filename = '',
  23. $entity_id = 0, $message_id = 0, $name = '', $priority = 3;
  24. }
  25. class message {
  26. /** message is the object that contains messages. It is a recursive
  27. object in that through the $entities variable, it can contain
  28. more objects of type message. See documentation in mime.txt for
  29. a better description of how this works.
  30. **/
  31. var $header = '', $entities = array();
  32. function addEntity ($msg) {
  33. $this->entities[] = $msg;
  34. }
  35. }
  36. /* --------------------------------------------------------------------------------- */
  37. /* MIME DECODING */
  38. /* --------------------------------------------------------------------------------- */
  39. /* This function gets the structure of a message and stores it in the "message" class.
  40. * It will return this object for use with all relevant header information and
  41. * fully parsed into the standard "message" object format.
  42. */
  43. function mime_structure ($imap_stream, $header) {
  44. $ssid = sqimap_session_id();
  45. $lsid = strlen( $ssid );
  46. $id = $header->id;
  47. fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
  48. //
  49. // This should use sqimap_read_data instead of reading it itself
  50. //
  51. $read = fgets ($imap_stream, 9216);
  52. $bodystructure = '';
  53. while ( substr($read, 0, $lsid) <> $ssid &&
  54. !feof( $imap_stream ) ) {
  55. $bodystructure .= $read;
  56. $read = fgets ($imap_stream, 9216);
  57. }
  58. $read = $bodystructure;
  59. // isolate the body structure and remove beginning and end parenthesis
  60. $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') + 13));
  61. $read = trim(substr ($read, 0, -1));
  62. $end = mime_match_parenthesis(0, $read);
  63. while ($end == strlen($read)-1) {
  64. $read = trim(substr ($read, 0, -1));
  65. $read = trim(substr ($read, 1));
  66. $end = mime_match_parenthesis(0, $read);
  67. }
  68. $msg = mime_parse_structure ($read, 0);
  69. $msg->header = $header;
  70. return( $msg );
  71. }
  72. /* this starts the parsing of a particular structure. It is called recursively,
  73. * so it can be passed different structures. It returns an object of type
  74. * $message.
  75. * First, it checks to see if it is a multipart message. If it is, then it
  76. * handles that as it sees is necessary. If it is just a regular entity,
  77. * then it parses it and adds the necessary header information (by calling out
  78. * to mime_get_elements()
  79. */
  80. function mime_parse_structure ($structure, $ent_id) {
  81. $msg = new message();
  82. if ($structure{0} == '(') {
  83. $ent_id = mime_new_element_level($ent_id);
  84. $start = $end = -1;
  85. do {
  86. $start = $end+1;
  87. $end = mime_match_parenthesis ($start, $structure);
  88. $element = substr($structure, $start+1, ($end - $start)-1);
  89. $ent_id = mime_increment_id ($ent_id);
  90. $newmsg = mime_parse_structure ($element, $ent_id);
  91. $msg->addEntity ($newmsg);
  92. } while ($structure{$end+1} == '(');
  93. } else {
  94. // parse the elements
  95. $msg = mime_get_element ($structure, $msg, $ent_id);
  96. }
  97. return $msg;
  98. }
  99. /* Increments the element ID. An element id can look like any of
  100. * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
  101. * the last number of the element id, changing 1.2 to 1.3.
  102. */
  103. function mime_increment_id ($id) {
  104. if (strpos($id, '.')) {
  105. $first = substr($id, 0, strrpos($id, '.'));
  106. $last = substr($id, strrpos($id, '.')+1);
  107. $last++;
  108. $new = $first . '.' .$last;
  109. } else {
  110. $new = $id + 1;
  111. }
  112. return $new;
  113. }
  114. /*
  115. * See comment for mime_increment_id().
  116. * This adds another level on to the entity_id changing 1.3 to 1.3.0
  117. * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
  118. * before it can be used. I left it this way so as not to have
  119. * to make a special case if it is the first entity_id. It
  120. * always increments it, and that works fine.
  121. */
  122. function mime_new_element_level ($id) {
  123. if (!$id) {
  124. $id = 0;
  125. } else {
  126. $id = $id . '.0';
  127. }
  128. return( $id );
  129. }
  130. function mime_get_element (&$structure, $msg, $ent_id) {
  131. $elem_num = 1;
  132. $msg->header = new msg_header();
  133. $msg->header->entity_id = $ent_id;
  134. $properties = array();
  135. while (strlen($structure) > 0) {
  136. $structure = trim($structure);
  137. $char = $structure{0};
  138. if (strtolower(substr($structure, 0, 3)) == 'nil') {
  139. $text = '';
  140. $structure = substr($structure, 3);
  141. } else if ($char == '"') {
  142. // loop through until we find the matching quote, and return that as a string
  143. $pos = 1;
  144. $text = '';
  145. while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
  146. $text .= $char;
  147. $pos++;
  148. }
  149. $structure = substr($structure, strlen($text) + 2);
  150. } else if ($char == '(') {
  151. // comment me
  152. $end = mime_match_parenthesis (0, $structure);
  153. $sub = substr($structure, 1, $end-1);
  154. $properties = mime_get_props($properties, $sub);
  155. $structure = substr($structure, strlen($sub) + 2);
  156. } else {
  157. // loop through until we find a space or an end parenthesis
  158. $pos = 0;
  159. $char = $structure{$pos};
  160. $text = '';
  161. while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
  162. $text .= $char;
  163. $pos++;
  164. $char = $structure{$pos};
  165. }
  166. $structure = substr($structure, strlen($text));
  167. }
  168. // This is where all the text parts get put into the header
  169. switch ($elem_num) {
  170. case 1:
  171. $msg->header->type0 = strtolower($text);
  172. break;
  173. case 2:
  174. $msg->header->type1 = strtolower($text);
  175. break;
  176. case 4: // Id
  177. // Invisimail enclose images with <>
  178. $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) );
  179. break;
  180. case 5:
  181. $msg->header->description = $text;
  182. break;
  183. case 6:
  184. $msg->header->encoding = strtolower($text);
  185. break;
  186. case 7:
  187. $msg->header->size = $text;
  188. break;
  189. default:
  190. if ($msg->header->type0 == 'text' && $elem_num == 8) {
  191. // This is a plain text message, so lets get the number of lines
  192. // that it contains.
  193. $msg->header->num_lines = $text;
  194. } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) {
  195. // This is an encapsulated message, so lets start all over again and
  196. // parse this message adding it on to the existing one.
  197. $structure = trim($structure);
  198. if ( $structure{0} == '(' ) {
  199. $e = mime_match_parenthesis (0, $structure);
  200. $structure = substr($structure, 0, $e);
  201. $structure = substr($structure, 1);
  202. $m = mime_parse_structure($structure, $msg->header->entity_id);
  203. // the following conditional is there to correct a bug that wasn't
  204. // incrementing the entity IDs correctly because of the special case
  205. // that message/rfc822 is. This fixes it fine.
  206. if (substr($structure, 1, 1) != '(')
  207. $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
  208. // Now we'll go through and reformat the results.
  209. if ($m->entities) {
  210. for ($i=0; $i < count($m->entities); $i++) {
  211. $msg->addEntity($m->entities[$i]);
  212. }
  213. } else {
  214. $msg->addEntity($m);
  215. }
  216. $structure = "";
  217. }
  218. }
  219. break;
  220. }
  221. $elem_num++;
  222. $text = "";
  223. }
  224. // loop through the additional properties and put those in the various headers
  225. // if ($msg->header->type0 != 'message') {
  226. for ($i=0; $i < count($properties); $i++) {
  227. $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
  228. }
  229. // }
  230. return $msg;
  231. }
  232. /*
  233. * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
  234. * figure out how to do this part, so I decided to go to bed. I woke up
  235. * in the morning and had a flash of insight. I went to the white-board
  236. * and scribbled it out, then spent a bit programming it, and this is the
  237. * result. Nothing complicated, but I think my brain was fried yesterday.
  238. * Funny how that happens some times.
  239. *
  240. * This gets properties in a nested parenthesisized list. For example,
  241. * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
  242. * This returns an array called $props with all paired up properties.
  243. * It ignores the "attachment" for now, maybe that should change later
  244. * down the road. In this case, what is returned is:
  245. * $props[0]["name"] = "filename";
  246. * $props[0]["value"] = "luke.tar.gz";
  247. */
  248. function mime_get_props ($props, $structure) {
  249. while (strlen($structure) > 0) {
  250. $structure = trim($structure);
  251. $char = $structure{0};
  252. if ($char == '"') {
  253. $pos = 1;
  254. $tmp = '';
  255. while ( ( $char = $structure{$pos} ) != '"' &&
  256. $pos < strlen($structure)) {
  257. $tmp .= $char;
  258. $pos++;
  259. }
  260. $structure = trim(substr($structure, strlen($tmp) + 2));
  261. $char = $structure{0};
  262. if ($char == '"') {
  263. $pos = 1;
  264. $value = '';
  265. while ( ( $char = $structure{$pos} ) != '"' &&
  266. $pos < strlen($structure) ) {
  267. $value .= $char;
  268. $pos++;
  269. }
  270. $structure = trim(substr($structure, strlen($tmp) + 2));
  271. $k = count($props);
  272. $props[$k]['name'] = strtolower($tmp);
  273. $props[$k]['value'] = $value;
  274. } else if ($char == '(') {
  275. $end = mime_match_parenthesis (0, $structure);
  276. $sub = substr($structure, 1, $end-1);
  277. if (! isset($props))
  278. $props = array();
  279. $props = mime_get_props($props, $sub);
  280. $structure = substr($structure, strlen($sub) + 2);
  281. }
  282. return $props;
  283. } else if ($char == '(') {
  284. $end = mime_match_parenthesis (0, $structure);
  285. $sub = substr($structure, 1, $end-1);
  286. $props = mime_get_props($props, $sub);
  287. $structure = substr($structure, strlen($sub) + 2);
  288. return $props;
  289. } else {
  290. return $props;
  291. }
  292. }
  293. }
  294. /*
  295. * Matches parenthesis. It will return the position of the matching
  296. * parenthesis in $structure. For instance, if $structure was:
  297. * ("text" "plain" ("val1name", "1") nil ... )
  298. * x x
  299. * then this would return 42 to match up those two.
  300. */
  301. function mime_match_parenthesis ($pos, $structure) {
  302. $j = strlen( $structure );
  303. // ignore all extra characters
  304. // If inside of a string, skip string -- Boundary IDs and other
  305. // things can have ) in them.
  306. if ( $structure{$pos} != '(' ) {
  307. return( $j );
  308. }
  309. while ( $pos < $j ) {
  310. $pos++;
  311. if ($structure{$pos} == ')') {
  312. return $pos;
  313. } elseif ($structure{$pos} == '"') {
  314. $pos++;
  315. while ( $structure{$pos} != '"' &&
  316. $pos < $j ) {
  317. if (substr($structure, $pos, 2) == '\\"') {
  318. $pos++;
  319. } elseif (substr($structure, $pos, 2) == '\\\\') {
  320. $pos++;
  321. }
  322. $pos++;
  323. }
  324. } elseif ( $structure{$pos} == '(' ) {
  325. $pos = mime_match_parenthesis ($pos, $structure);
  326. }
  327. }
  328. echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
  329. return( $pos );
  330. }
  331. function mime_fetch_body($imap_stream, $id, $ent_id ) {
  332. /*
  333. * do a bit of error correction. If we couldn't find the entity id, just guess
  334. * that it is the first one. That is usually the case anyway.
  335. */
  336. if (!$ent_id) {
  337. $ent_id = 1;
  338. }
  339. $cmd = "FETCH $id BODY[$ent_id]";
  340. $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message);
  341. do {
  342. $topline = array_shift( $data );
  343. } while( $topline && $topline == '*' && !preg_match( '/\\* [0-9] FETCH.*/i', $topline )) ;
  344. $wholemessage = implode('', $data);
  345. if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
  346. $ret = substr( $wholemessage, 0, $regs[1] );
  347. /*
  348. There is some information in the content info header that could be important
  349. in order to parse html messages. Let's get them here.
  350. */
  351. if ( $ret{0} == '<' ) {
  352. $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
  353. /* BASE within HTML documents is illegal (see w3 spec)
  354. * $base = '';
  355. * $k = 10;
  356. * foreach( $data as $d ) {
  357. * if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
  358. * $j = strlen( $d );
  359. * $i = 13;
  360. * $base = '';
  361. * while ( $i < $j &&
  362. * ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
  363. * $i++;
  364. * while ( $i < $j ) {
  365. * if ( isNoSep( $d{$i} ) )
  366. * $base .= $d{$i};
  367. * $i++;
  368. * }
  369. * $k = 0;
  370. * } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
  371. * $base .= substr( $d, 1 );
  372. * }
  373. * $k++;
  374. * }
  375. * if ( $base <> '' ) {
  376. * $ret = "<base href=\"$base\">" . $ret;
  377. * }
  378. * */
  379. }
  380. } else if (ereg('"([^"]*)"', $topline, $regs)) {
  381. $ret = $regs[1];
  382. } else {
  383. global $where, $what, $mailbox, $passed_id, $startMessage;
  384. $par = 'mailbox=' . urlencode($mailbox) . "&amp;passed_id=$passed_id";
  385. if (isset($where) && isset($what)) {
  386. $par .= '&amp;where='. urlencode($where) . "&amp;what=" . urlencode($what);
  387. } else {
  388. $par .= "&amp;startMessage=$startMessage&amp;show_more=0";
  389. }
  390. $par .= '&amp;response=' . urlencode($response) .
  391. '&amp;message=' . urlencode($message).
  392. '&amp;topline=' . urlencode($topline);
  393. echo '<tt><br>' .
  394. '<table width="80%"><tr>' .
  395. '<tr><td colspan=2>' .
  396. _("Body retrieval error. The reason for this is most probably that the message is malformed. Please help us making future versions better by submitting this message to the developers knowledgebase!") .
  397. " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
  398. _("Submit message") . '</A><BR>&nbsp;' .
  399. '</td></tr>' .
  400. '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
  401. '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
  402. '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
  403. '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
  404. "</table><BR></tt></font><hr>";
  405. $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
  406. array_shift($data);
  407. $wholemessage = implode('', $data);
  408. $ret = $wholemessage;
  409. }
  410. return( $ret );
  411. }
  412. function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
  413. // do a bit of error correction. If we couldn't find the entity id, just guess
  414. // that it is the first one. That is usually the case anyway.
  415. if (!$ent_id) {
  416. $ent_id = 1;
  417. }
  418. $sid = sqimap_session_id();
  419. // Don't kill the connection if the browser is over a dialup
  420. // and it would take over 30 seconds to download it.
  421. // don´t call set_time_limit in safe mode.
  422. if (!ini_get("safe_mode")) {
  423. set_time_limit(0);
  424. }
  425. fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
  426. $cnt = 0;
  427. $continue = true;
  428. $read = fgets ($imap_stream,4096);
  429. // This could be bad -- if the section has sqimap_session_id() . ' OK'
  430. // or similar, it will kill the download.
  431. while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
  432. if (trim($read) == ')==') {
  433. $read1 = $read;
  434. $read = fgets ($imap_stream,4096);
  435. if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
  436. return;
  437. } else {
  438. echo decodeBody($read1, $encoding) .
  439. decodeBody($read, $encoding);
  440. }
  441. } else if ($cnt) {
  442. echo decodeBody($read, $encoding);
  443. }
  444. $read = fgets ($imap_stream,4096);
  445. $cnt++;
  446. }
  447. }
  448. /* -[ END MIME DECODING ]----------------------------------------------------------- */
  449. /* This is the first function called. It decides if this is a multipart
  450. message or if it should be handled as a single entity
  451. */
  452. function decodeMime ($imap_stream, &$header) {
  453. global $username, $key, $imapServerAddress, $imapPort;
  454. return mime_structure ($imap_stream, $header);
  455. }
  456. // This is here for debugging purposese. It will print out a list
  457. // of all the entity IDs that are in the $message object.
  458. function listEntities ($message) {
  459. if ($message) {
  460. if ($message->header->entity_id)
  461. echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
  462. for ($i = 0; $message->entities[$i]; $i++) {
  463. $msg = listEntities($message->entities[$i], $ent_id);
  464. if ($msg)
  465. return $msg;
  466. }
  467. }
  468. }
  469. /* returns a $message object for a particular entity id */
  470. function getEntity ($message, $ent_id) {
  471. if ($message) {
  472. if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
  473. return $message;
  474. } else {
  475. for ($i = 0; isset($message->entities[$i]); $i++) {
  476. $msg = getEntity ($message->entities[$i], $ent_id);
  477. if ($msg) {
  478. return $msg;
  479. }
  480. }
  481. }
  482. }
  483. }
  484. /*
  485. * figures out what entity to display and returns the $message object
  486. * for that entity.
  487. */
  488. function findDisplayEntity ($message, $textOnly = 1) {
  489. global $show_html_default;
  490. $entity = 0;
  491. if ($message) {
  492. if ( $message->header->type0 == 'multipart' &&
  493. ( $message->header->type1 == 'alternative' ||
  494. $message->header->type1 == 'mixed' ||
  495. $message->header->type1 == 'related' ) &&
  496. $show_html_default && ! $textOnly ) {
  497. $entity = findDisplayEntityHTML($message);
  498. }
  499. // Show text/plain or text/html -- the first one we find.
  500. if ( $entity == 0 &&
  501. $message->header->type0 == 'text' &&
  502. ( $message->header->type1 == 'plain' ||
  503. $message->header->type1 == 'html' ) &&
  504. isset($message->header->entity_id) ) {
  505. $entity = $message->header->entity_id;
  506. }
  507. $i = 0;
  508. while ($entity == 0 && isset($message->entities[$i]) ) {
  509. $entity = findDisplayEntity($message->entities[$i], $textOnly);
  510. $i++;
  511. }
  512. }
  513. return( $entity );
  514. }
  515. /* Shows the HTML version */
  516. function findDisplayEntityHTML ($message) {
  517. if ( $message->header->type0 == 'text' &&
  518. $message->header->type1 == 'html' &&
  519. isset($message->header->entity_id)) {
  520. return $message->header->entity_id;
  521. }
  522. for ($i = 0; isset($message->entities[$i]); $i ++) {
  523. if ( $message->header->type0 == 'message' &&
  524. $message->header->type1 == 'rfc822' &&
  525. isset($message->header->entity_id)) {
  526. return 0;
  527. }
  528. $entity = findDisplayEntityHTML($message->entities[$i]);
  529. if ($entity != 0) {
  530. return $entity;
  531. }
  532. }
  533. return 0;
  534. }
  535. /* This returns a parsed string called $body. That string can then
  536. be displayed as the actual message in the HTML. It contains
  537. everything needed, including HTML Tags, Attachments at the
  538. bottom, etc.
  539. */
  540. function formatBody($imap_stream, $message, $color, $wrap_at) {
  541. // this if statement checks for the entity to show as the
  542. // primary message. To add more of them, just put them in the
  543. // order that is their priority.
  544. global $startMessage, $username, $key, $imapServerAddress, $imapPort, $body,
  545. $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
  546. $has_unsafe_images = 0;
  547. $id = $message->header->id;
  548. $urlmailbox = urlencode($message->header->mailbox);
  549. // Get the right entity and redefine message to be this entity
  550. // Pass the 0 to mean that we want the 'best' viewable one
  551. $ent_num = findDisplayEntity ($message, 0);
  552. $body_message = getEntity($message, $ent_num);
  553. if (($body_message->header->type0 == 'text') ||
  554. ($body_message->header->type0 == 'rfc822')) {
  555. $body = mime_fetch_body ($imap_stream, $id, $ent_num);
  556. $body = decodeBody($body, $body_message->header->encoding);
  557. $hookResults = do_hook("message_body", $body);
  558. $body = $hookResults[1];
  559. // If there are other types that shouldn't be formatted, add
  560. // them here
  561. if ($body_message->header->type1 == 'html') {
  562. if ( $show_html_default <> 1 ) {
  563. $body = strip_tags( $body );
  564. translateText($body, $wrap_at, $body_message->header->charset);
  565. } else {
  566. $body = MagicHTML( $body, $id );
  567. }
  568. } else {
  569. translateText($body, $wrap_at, $body_message->header->charset);
  570. }
  571. $body .= "<CENTER><SMALL><A HREF=\"../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;passed_ent_id=$ent_num&amp;mailbox=$urlmailbox&amp;showHeaders=1\">". _("Download this as a file") ."</A></SMALL></CENTER><BR>";
  572. if ($has_unsafe_images) {
  573. if ($view_unsafe_images) {
  574. $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&amp;mailbox=$urlmailbox&amp;sort=$sort&amp;startMessage=$startMessage&amp;show_more=0\">". _("Hide Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
  575. } else {
  576. $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&amp;mailbox=$urlmailbox&amp;sort=$sort&amp;startMessage=$startMessage&amp;show_more=0&amp;view_unsafe_images=1\">". _("View Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
  577. }
  578. }
  579. /** Display the ATTACHMENTS: message if there's more than one part **/
  580. if (isset($message->entities[0])) {
  581. $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
  582. }
  583. } else {
  584. $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
  585. }
  586. return ($body);
  587. }
  588. /*
  589. * A recursive function that returns a list of attachments with links
  590. * to where to download these attachments
  591. */
  592. function formatAttachments($message, $ent_id, $mailbox, $id) {
  593. global $where, $what;
  594. global $startMessage, $color;
  595. static $ShownHTML = 0;
  596. $body = '';
  597. if ($ShownHTML == 0) {
  598. $ShownHTML = 1;
  599. $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
  600. "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
  601. _("Attachments") . ':' .
  602. "</B></TH></TR><TR><TD>\n" .
  603. "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
  604. formatAttachments($message, $ent_id, $mailbox, $id) .
  605. "</TABLE></TD></TR></TABLE>";
  606. } else if ($message) {
  607. $header = $message->header;
  608. $type0 = strtolower($header->type0);
  609. $type1 = strtolower($header->type1);
  610. $name = decodeHeader($header->name);
  611. if ($type0 =='message' && $type1 = 'rfc822') {
  612. $filename = decodeHeader($message->header->filename);
  613. if (trim($filename) == '') {
  614. if (trim($name) == '') {
  615. $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
  616. } else {
  617. $display_filename = $name;
  618. $filename = $name;
  619. }
  620. } else {
  621. $display_filename = $filename;
  622. }
  623. $urlMailbox = urlencode($mailbox);
  624. $ent = urlencode($message->header->entity_id);
  625. $DefaultLink =
  626. "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  627. if ($where && $what) {
  628. $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
  629. }
  630. $Links['download link']['text'] = _("download");
  631. $Links['download link']['href'] =
  632. "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  633. $ImageURL = '';
  634. /* this executes the attachment hook with a specific MIME-type.
  635. * if that doens't have results, it tries if there's a rule
  636. * for a more generic type. */
  637. $HookResults = do_hook("attachment $type0/$type1", $Links,
  638. $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
  639. if(count($HookResults[1]) <= 1) {
  640. $HookResults = do_hook("attachment $type0/*", $Links,
  641. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  642. $display_filename, $where, $what);
  643. }
  644. $Links = $HookResults[1];
  645. $DefaultLink = $HookResults[6];
  646. $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
  647. "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
  648. '<TD><SMALL><b>' . show_readable_size($message->header->size) .
  649. '</b>&nbsp;&nbsp;</small></TD>' .
  650. "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
  651. '<TD><SMALL>';
  652. if ($message->header->description) {
  653. $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
  654. }
  655. $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
  656. $SkipSpaces = 1;
  657. foreach ($Links as $Val) {
  658. if ($SkipSpaces) {
  659. $SkipSpaces = 0;
  660. } else {
  661. $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
  662. }
  663. $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
  664. }
  665. unset($Links);
  666. $body .= "</SMALL></TD></TR>\n";
  667. return( $body );
  668. } elseif (!$message->entities) {
  669. $type0 = strtolower($message->header->type0);
  670. $type1 = strtolower($message->header->type1);
  671. $name = decodeHeader($message->header->name);
  672. if ($message->header->entity_id != $ent_id) {
  673. $filename = decodeHeader($message->header->filename);
  674. if (trim($filename) == '') {
  675. if (trim($name) == '') {
  676. if ( trim( $message->header->id ) == '' )
  677. $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
  678. else
  679. $display_filename = 'cid: ' . $message->header->id;
  680. // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
  681. } else {
  682. $display_filename = $name;
  683. $filename = $name;
  684. }
  685. } else {
  686. $display_filename = $filename;
  687. }
  688. $urlMailbox = urlencode($mailbox);
  689. $ent = urlencode($message->header->entity_id);
  690. $DefaultLink =
  691. "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  692. if ($where && $what) {
  693. $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
  694. }
  695. $Links['download link']['text'] = _("download");
  696. $Links['download link']['href'] =
  697. "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  698. $ImageURL = '';
  699. /* this executes the attachment hook with a specific MIME-type.
  700. * if that doens't have results, it tries if there's a rule
  701. * for a more generic type. */
  702. $HookResults = do_hook("attachment $type0/$type1", $Links,
  703. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  704. $display_filename, $where, $what);
  705. if(count($HookResults[1]) <= 1) {
  706. $HookResults = do_hook("attachment $type0/*", $Links,
  707. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  708. $display_filename, $where, $what);
  709. }
  710. $Links = $HookResults[1];
  711. $DefaultLink = $HookResults[6];
  712. $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
  713. "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
  714. '<TD><SMALL><b>' . show_readable_size($message->header->size) .
  715. '</b>&nbsp;&nbsp;</small></TD>' .
  716. "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
  717. '<TD><SMALL>';
  718. if ($message->header->description) {
  719. $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
  720. }
  721. $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
  722. $SkipSpaces = 1;
  723. foreach ($Links as $Val) {
  724. if ($SkipSpaces) {
  725. $SkipSpaces = 0;
  726. } else {
  727. $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
  728. }
  729. $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
  730. }
  731. unset($Links);
  732. $body .= "</SMALL></TD></TR>\n";
  733. }
  734. } else {
  735. for ($i = 0; $i < count($message->entities); $i++) {
  736. $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id);
  737. }
  738. }
  739. }
  740. return( $body );
  741. }
  742. /** this function decodes the body depending on the encoding type. **/
  743. function decodeBody($body, $encoding) {
  744. $body = str_replace("\r\n", "\n", $body);
  745. $encoding = strtolower($encoding);
  746. global $show_html_default;
  747. if ($encoding == 'quoted-printable') {
  748. $body = quoted_printable_decode($body);
  749. while (ereg("=\n", $body))
  750. $body = ereg_replace ("=\n", "", $body);
  751. } else if ($encoding == 'base64') {
  752. $body = base64_decode($body);
  753. }
  754. // All other encodings are returned raw.
  755. return $body;
  756. }
  757. /*
  758. * This functions decode strings that is encoded according to
  759. * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
  760. */
  761. function decodeHeader ($string, $utfencode=true) {
  762. if ( is_array( $string ) ) {
  763. $string = implode("\n", $string );
  764. }
  765. if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
  766. $string, $res)) {
  767. if (ucfirst($res[2]) == 'B') {
  768. $replace = base64_decode($res[3]);
  769. } else {
  770. $replace = str_replace('_', ' ', $res[3]);
  771. // Convert lowercase Quoted Printable to uppercase for
  772. // quoted_printable_decode to understand it.
  773. while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))",
  774. $replace, $res)) {
  775. $replace = str_replace($res[1], strtoupper($res[1]), $replace);
  776. }
  777. $replace = quoted_printable_decode($replace);
  778. }
  779. /* Only encode into entities by default. Some places
  780. don't need the encoding, like the compose form. */
  781. if ($utfencode){
  782. $replace = charset_decode ($res[1], $replace);
  783. }
  784. // Remove the name of the character set.
  785. $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
  786. $replace, $string);
  787. // In case there should be more encoding in the string: recurse
  788. $string = decodeHeader($string);
  789. }
  790. return ($string);
  791. }
  792. /*
  793. * Encode a string according to RFC 1522 for use in headers if it
  794. * contains 8-bit characters or anything that looks like it should
  795. * be encoded.
  796. */
  797. function encodeHeader ($string) {
  798. global $default_charset;
  799. // Encode only if the string contains 8-bit characters or =?
  800. $j = strlen( $string );
  801. $l = strstr($string, '=?'); // Must be encoded ?
  802. $ret = '';
  803. for( $i=0; $i < $j; ++$i) {
  804. switch( $string{$i} ) {
  805. case '=':
  806. $ret .= '=3D';
  807. break;
  808. case '?':
  809. $ret .= '=3F';
  810. break;
  811. case '_':
  812. $ret .= '=5F';
  813. break;
  814. case ' ':
  815. $ret .= '_';
  816. break;
  817. default:
  818. $k = ord( $string{$i} );
  819. if ( $k > 126 ) {
  820. $ret .= sprintf("=%02X", $k);
  821. $l = TRUE;
  822. } else
  823. $ret .= $string{$i};
  824. }
  825. }
  826. if ( $l ) {
  827. $string = "=?$default_charset?Q?$ret?=";
  828. }
  829. return( $string );
  830. }
  831. /*
  832. Strips dangerous tags from html messages.
  833. */
  834. function MagicHTML( $body, $id ) {
  835. global $message, $HTTP_SERVER_VARS,
  836. $attachment_common_show_images;
  837. $attachment_common_show_images =
  838. FALSE; // Don't display attached images in HTML mode
  839. $j = strlen( $body ); // Legnth of the HTML
  840. $ret = ''; // Returned string
  841. $bgcolor = '#ffffff'; // Background style color (defaults to white)
  842. $textcolor = '#000000'; // Foreground style color (defaults to black)
  843. $leftmargin = ''; // Left margin style
  844. $title = ''; // HTML title if any
  845. $i = 0;
  846. while ( $i < $j ) {
  847. if ( $body{$i} == '<' ) {
  848. $pos = $i + 1;
  849. $tag = '';
  850. while ($body{$pos} == ' ' || $body{$pos} == "\t" ||
  851. $body{$pos} == "\n" ) {
  852. $pos ++;
  853. }
  854. while (strlen($tag) < 4 && $body{$pos} != ' ' &&
  855. $body{$pos} != "\t" && $body{$pos} != "\n" &&
  856. $pos < $j ) {
  857. $tag .= $body{$pos};
  858. $pos ++;
  859. }
  860. /*
  861. A comment in HTML is only three characters and isn't
  862. guaranteed to have a space after it. This fudges so
  863. it will be caught by the switch statement.
  864. */
  865. if (ereg("!--", $tag)) {
  866. $tag = "!-- ";
  867. }
  868. switch( strtoupper( $tag ) ) {
  869. // Strips the entire tag and contents
  870. case 'APPL':
  871. case 'EMBE':
  872. case 'FRAM':
  873. case 'SCRI':
  874. case 'OBJE':
  875. $etg = '/' . $tag;
  876. while ( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg &&
  877. $i < $j ) $i++;
  878. while ( $i < $j && $body{++$i} <> '>' );
  879. // $ret .= "<!-- $tag removed -->";
  880. break;
  881. // Substitute Title
  882. case 'TITL':
  883. $i += 5;
  884. while ( $body{$i} <> '>' && // </title>
  885. $i < $j )
  886. $i++;
  887. $i++;
  888. $title = '';
  889. while ( $body{$i} <> '<' && // </title>
  890. $i < $j ) {
  891. $title .= $body{$i};
  892. $i++;
  893. }
  894. $i += 7;
  895. break;
  896. // Destroy these tags
  897. case 'HTML':
  898. case 'HEAD':
  899. case '/HTM':
  900. case '/HEA':
  901. case '!DOC':
  902. case 'META':
  903. //case 'DIV ':
  904. //case '/DIV':
  905. case '!-- ':
  906. $i += 4;
  907. while ( $body{$i} <> '>' &&
  908. $i < $j )
  909. $i++;
  910. // $i++;
  911. break;
  912. case 'STYL':
  913. $i += 5;
  914. while ( $body{$i} <> '>' && // </title>
  915. $i < $j )
  916. $i++;
  917. $i++;
  918. // We parse the style to look for interesting stuff
  919. $styleblk = '';
  920. while ( $body{$i} <> '>' &&
  921. $i < $j ) {
  922. // First we get the name of the style
  923. $style = '';
  924. while ( $body{$i} <> '>' &&
  925. $body{$i} <> '<' &&
  926. $body{$i} <> '{' &&
  927. $i < $j ) {
  928. if ( isnoSep( $body{$i} ) )
  929. $style .= $body{$i};
  930. $i++;
  931. }
  932. stripComments( $i, $j, $body );
  933. $style = strtoupper( trim( $style ) );
  934. if ( $style == 'BODY' ) {
  935. // Next we look into the definitions of the body style
  936. while ( $body{$i} <> '>' &&
  937. $body{$i} <> '}' &&
  938. $i < $j ) {
  939. // We look for the background color if any.
  940. if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
  941. $i += 17;
  942. $bgcolor = getStyleData( $i, $j, $body );
  943. } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
  944. $i += 12;
  945. $leftmargin = getStyleData( $i, $j, $body );
  946. }
  947. $i++;
  948. }
  949. } else {
  950. // Other style are mantained
  951. $styleblk .= "$style ";
  952. while ( $body{$i} <> '>' &&
  953. $body{$i} <> '<' &&
  954. $body{$i} <> '}' &&
  955. $i < $j ) {
  956. $styleblk .= $body{$i};
  957. $i++;
  958. }
  959. $styleblk .= $body{$i};
  960. }
  961. stripComments( $i, $j, $body );
  962. if ( $body{$i} <> '>' )
  963. $i++;
  964. }
  965. if ( $styleblk <> '' )
  966. $ret .= "<style>$styleblk";
  967. break;
  968. case 'BODY':
  969. if ( $title <> '' )
  970. $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
  971. $ret .= "<TABLE";
  972. $i += 5;
  973. if (! isset($base)) {
  974. $base = '';
  975. }
  976. $ret .= stripEvent( $i, $j, $body, $id, $base );
  977. $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
  978. if ( $leftmargin <> '' )
  979. $ret .= "<td width=$leftmargin>&nbsp;</td>";
  980. $ret .= '<td>';
  981. if (strtolower($bgcolor) == 'ffffff' ||
  982. strtolower($bgcolor) == '#ffffff')
  983. $ret .= '<font color=#000000>';
  984. break;
  985. case 'BASE':
  986. $i += 4;
  987. $base = '';
  988. if ( strncasecmp($body{$i}, 'font', 4) ) {
  989. $i += 5;
  990. while ( !isNoSep( $body{$i} ) && $i < $j ) {
  991. $i++;
  992. }
  993. while ( $body{$i} <> '>' && $i < $j ) {
  994. $base .= $body{$i};
  995. $i++;
  996. }
  997. $ret .= "<BASEFONT $base>\n";
  998. break;
  999. }
  1000. $i++;
  1001. while ( !isNoSep( $body{$i} ) &&
  1002. $i < $j ) {
  1003. $i++;
  1004. }
  1005. if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
  1006. $i += 5;
  1007. while ( !isNoSep( $body{$i} ) &&
  1008. $i < $j ) {
  1009. $i++;
  1010. }
  1011. while ( $body{$i} <> '>' &&
  1012. $i < $j ) {
  1013. if ( $body{$i} <> '"' ) {
  1014. $base .= $body{$i};
  1015. }
  1016. $i++;
  1017. }
  1018. // Debuging $ret .= "<!-- base == $base -->";
  1019. if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
  1020. $ret .= "\n<BASE HREF=\"$base\">\n";
  1021. }
  1022. }
  1023. break;
  1024. case '/BOD':
  1025. $ret .= '</font></td></tr></TABLE>';
  1026. $i += 6;
  1027. break;
  1028. default:
  1029. // Following tags can contain some event handler, lets search it
  1030. stripComments( $i, $j, $body );
  1031. if (! isset($base)) {
  1032. $base = '';
  1033. }
  1034. $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
  1035. // $ret .= "<!-- $tag detected -->";
  1036. }
  1037. } else {
  1038. $ret .= $body{$i};
  1039. }
  1040. $i++;
  1041. }
  1042. return( "\n\n<!-- HTML Output ahead -->\n" .
  1043. $ret .
  1044. /* Base is illegal within HTML
  1045. "\n<!-- END of HTML Output --><base href=\"".
  1046. get_location() . '/'.
  1047. "\">\n\n" );
  1048. */
  1049. "\n<!-- END of HTML Output -->\n\n" );
  1050. }
  1051. function isNoSep( $char ) {
  1052. switch( $char ) {
  1053. case ' ':
  1054. case "\n":
  1055. case "\t":
  1056. case "\r":
  1057. case '>':
  1058. case '"':
  1059. return( FALSE );
  1060. break;
  1061. default:
  1062. return( TRUE );
  1063. }
  1064. }
  1065. /*
  1066. The following function is usefull to remove extra data that can cause
  1067. html not to display properly. Especialy with MS stuff.
  1068. */
  1069. function stripComments( &$i, $j, &$body ) {
  1070. while ( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' &&
  1071. $i < $j ) {
  1072. $i += 5;
  1073. while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
  1074. $i < $j )
  1075. $i++;
  1076. $i++;
  1077. }
  1078. return;
  1079. }
  1080. /* Gets the style data of a specific style */
  1081. function getStyleData( &$i, $j, &$body ) {
  1082. // We skip spaces
  1083. while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
  1084. $i < $j ) {
  1085. $i++;
  1086. }
  1087. // And get the color
  1088. $ret = '';
  1089. while ( isNoSep( $body{$i} ) &&
  1090. $i < $j ) {
  1091. $ret .= $body{$i};
  1092. $i++;
  1093. }
  1094. return( $ret );
  1095. }
  1096. /*
  1097. Private function for strip_dangerous_tag. Look for event based coded and "remove" it
  1098. change on with no (onload -> noload)
  1099. */
  1100. function stripEvent( &$i, $j, &$body, $id, $base ) {
  1101. global $message, $base_uri, $has_unsafe_images, $view_unsafe_images;
  1102. $ret = '';
  1103. while ( $body{$i} <> '>' &&
  1104. $i < $j ) {
  1105. $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2});
  1106. switch( $etg ) {
  1107. case 'src':
  1108. // This is probably a src specification
  1109. $k = $i + 3;
  1110. while( !isNoSep( $body{$k} )) {
  1111. $k++;
  1112. }
  1113. if ( $body{$k} == '=' ) {
  1114. /* It is indeed */
  1115. $k++;
  1116. while( !isNoSep( $body{$k} ) &&
  1117. $k < $j ) {
  1118. $k++;
  1119. }
  1120. $src = '';
  1121. while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
  1122. $k < $j ) {
  1123. $src .= $body{$k};
  1124. $k++;
  1125. }
  1126. $k++;
  1127. while( !isNoSep( $body{$k} ) &&
  1128. $k < $j ) {
  1129. $k++;
  1130. }
  1131. $k++;
  1132. if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
  1133. $src = substr( $src, 4 );
  1134. $src = "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
  1135. urlencode( $message->header->mailbox ) .
  1136. "&amp;passed_ent_id=" . find_ent_id( $src, $message );
  1137. } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
  1138. stristr( $src, $base_uri ) ) {
  1139. /* Javascript and local urls goes out */
  1140. if (!$view_unsafe_images) {
  1141. $src = '../images/' . _("sec_remove_eng.png");
  1142. }
  1143. $has_unsafe_images = 1;
  1144. }
  1145. $ret .= 'src="' . $src . '" ';
  1146. $i = $k - 2;
  1147. } else {
  1148. $ret .= 'src';
  1149. $i = $i + 3;
  1150. }
  1151. break;
  1152. case '../':
  1153. // Retrolinks are not allowed without a base because they mess with SM security
  1154. if ( $base == '' ) {
  1155. $i += 2;
  1156. } else {
  1157. $ret .= '.';
  1158. }
  1159. break;
  1160. case 'cid':
  1161. // Internal link
  1162. $k = $i-1;
  1163. if ( $body{$i+3} == ':') {
  1164. $i +=4;
  1165. $name = '';
  1166. while ( isNoSep( $body{$i} ) &&
  1167. $i < $j ) {
  1168. $name .= $body{$i++};
  1169. }
  1170. if ( $name <> '' ) {
  1171. $ret .= "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
  1172. urlencode( $message->header->mailbox ) .
  1173. "&amp;passed_ent_id=" . find_ent_id( $name, $message );
  1174. if ( $body{$k} == '"' )
  1175. $ret .= '" ';
  1176. else
  1177. $ret .= ' ';
  1178. }
  1179. if ( $body{$i} == '>' )
  1180. $i -= 1;
  1181. }
  1182. break;
  1183. case ' on':
  1184. case "\non":
  1185. case "\ron":
  1186. case "\ton":
  1187. $ret .= ' no';
  1188. $i += 2;
  1189. break;
  1190. case 'pt:':
  1191. if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) {
  1192. $ret .= '_no/';
  1193. } else {
  1194. $ret .= $etg;
  1195. }
  1196. $i += 2;
  1197. break;
  1198. default:
  1199. $ret .= $body{$i};
  1200. }
  1201. $i++;
  1202. }
  1203. return( $ret );
  1204. }
  1205. /* This function trys to locate the entity_id of a specific mime element */
  1206. function find_ent_id( $id, $message ) {
  1207. $ret = '';
  1208. for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
  1209. if ( $message->entities[$i]->header->entity_id == '' ) {
  1210. $ret = find_ent_id( $id, $message->entities[$i] );
  1211. } else {
  1212. if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
  1213. $ret = $message->entities[$i]->header->entity_id;
  1214. }
  1215. }
  1216. return( $ret );
  1217. }
  1218. ?>