mime.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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 = trim(array_shift( $data ));
  343. } while( $topline && $topline[0] == '*' && !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. /*
  536. * translateText
  537. * Extracted from strings.php 23/03/2002
  538. */
  539. function translateText(&$body, $wrap_at, $charset) {
  540. global $where, $what; /* from searching */
  541. global $color; /* color theme */
  542. require_once('../functions/url_parser.php');
  543. $body_ary = explode("\n", $body);
  544. $PriorQuotes = 0;
  545. for ($i=0; $i < count($body_ary); $i++) {
  546. $line = $body_ary[$i];
  547. if (strlen($line) - 2 >= $wrap_at) {
  548. sqWordWrap($line, $wrap_at);
  549. }
  550. $line = charset_decode($charset, $line);
  551. $line = str_replace("\t", ' ', $line);
  552. parseUrl ($line);
  553. $Quotes = 0;
  554. $pos = 0;
  555. $j = strlen( $line );
  556. while ( $pos < $j ) {
  557. if ($line[$pos] == ' ') {
  558. $pos ++;
  559. } else if (strpos($line, '&gt;', $pos) === $pos) {
  560. $pos += 4;
  561. $Quotes ++;
  562. } else {
  563. break;
  564. }
  565. }
  566. if ($Quotes > 1) {
  567. if (! isset($color[14])) {
  568. $color[14] = '#FF0000';
  569. }
  570. $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>';
  571. } elseif ($Quotes) {
  572. if (! isset($color[13])) {
  573. $color[13] = '#800000';
  574. }
  575. $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>';
  576. }
  577. $body_ary[$i] = $line;
  578. }
  579. $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
  580. }
  581. /* This returns a parsed string called $body. That string can then
  582. be displayed as the actual message in the HTML. It contains
  583. everything needed, including HTML Tags, Attachments at the
  584. bottom, etc.
  585. */
  586. function formatBody($imap_stream, $message, $color, $wrap_at) {
  587. // this if statement checks for the entity to show as the
  588. // primary message. To add more of them, just put them in the
  589. // order that is their priority.
  590. global $startMessage, $username, $key, $imapServerAddress, $imapPort, $body,
  591. $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
  592. $has_unsafe_images = 0;
  593. $id = $message->header->id;
  594. $urlmailbox = urlencode($message->header->mailbox);
  595. // Get the right entity and redefine message to be this entity
  596. // Pass the 0 to mean that we want the 'best' viewable one
  597. $ent_num = findDisplayEntity ($message, 0);
  598. $body_message = getEntity($message, $ent_num);
  599. if (($body_message->header->type0 == 'text') ||
  600. ($body_message->header->type0 == 'rfc822')) {
  601. $body = mime_fetch_body ($imap_stream, $id, $ent_num);
  602. $body = decodeBody($body, $body_message->header->encoding);
  603. $hookResults = do_hook("message_body", $body);
  604. $body = $hookResults[1];
  605. // If there are other types that shouldn't be formatted, add
  606. // them here
  607. if ($body_message->header->type1 == 'html') {
  608. if ( $show_html_default <> 1 ) {
  609. $body = strip_tags( $body );
  610. translateText($body, $wrap_at, $body_message->header->charset);
  611. } else {
  612. $body = MagicHTML( $body, $id );
  613. }
  614. } else {
  615. translateText($body, $wrap_at, $body_message->header->charset);
  616. }
  617. $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>";
  618. if ($has_unsafe_images) {
  619. if ($view_unsafe_images) {
  620. $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";
  621. } else {
  622. $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";
  623. }
  624. }
  625. /** Display the ATTACHMENTS: message if there's more than one part **/
  626. if (isset($message->entities[0])) {
  627. $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
  628. }
  629. } else {
  630. $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
  631. }
  632. return ($body);
  633. }
  634. /*
  635. * A recursive function that returns a list of attachments with links
  636. * to where to download these attachments
  637. */
  638. function formatAttachments($message, $ent_id, $mailbox, $id) {
  639. global $where, $what;
  640. global $startMessage, $color;
  641. static $ShownHTML = 0;
  642. $body = '';
  643. if ($ShownHTML == 0) {
  644. $ShownHTML = 1;
  645. $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
  646. "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
  647. _("Attachments") . ':' .
  648. "</B></TH></TR><TR><TD>\n" .
  649. "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
  650. formatAttachments($message, $ent_id, $mailbox, $id) .
  651. "</TABLE></TD></TR></TABLE>";
  652. } else if ($message) {
  653. $header = $message->header;
  654. $type0 = strtolower($header->type0);
  655. $type1 = strtolower($header->type1);
  656. $name = decodeHeader($header->name);
  657. if ($type0 =='message' && $type1 == 'rfc822') {
  658. $filename = decodeHeader($message->header->filename);
  659. if (trim($filename) == '') {
  660. if (trim($name) == '') {
  661. $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
  662. } else {
  663. $display_filename = $name;
  664. $filename = $name;
  665. }
  666. } else {
  667. $display_filename = $filename;
  668. }
  669. $urlMailbox = urlencode($mailbox);
  670. $ent = urlencode($message->header->entity_id);
  671. $DefaultLink =
  672. "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  673. if ($where && $what) {
  674. $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
  675. }
  676. $Links['download link']['text'] = _("download");
  677. $Links['download link']['href'] =
  678. "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  679. $ImageURL = '';
  680. /* this executes the attachment hook with a specific MIME-type.
  681. * if that doens't have results, it tries if there's a rule
  682. * for a more generic type. */
  683. $HookResults = do_hook("attachment $type0/$type1", $Links,
  684. $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
  685. if(count($HookResults[1]) <= 1) {
  686. $HookResults = do_hook("attachment $type0/*", $Links,
  687. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  688. $display_filename, $where, $what);
  689. }
  690. $Links = $HookResults[1];
  691. $DefaultLink = $HookResults[6];
  692. $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
  693. "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
  694. '<TD><SMALL><b>' . show_readable_size($message->header->size) .
  695. '</b>&nbsp;&nbsp;</small></TD>' .
  696. "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
  697. '<TD><SMALL>';
  698. if ($message->header->description) {
  699. $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
  700. }
  701. $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
  702. $SkipSpaces = 1;
  703. foreach ($Links as $Val) {
  704. if ($SkipSpaces) {
  705. $SkipSpaces = 0;
  706. } else {
  707. $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
  708. }
  709. $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
  710. }
  711. unset($Links);
  712. $body .= "</SMALL></TD></TR>\n";
  713. return( $body );
  714. } elseif (!$message->entities) {
  715. $type0 = strtolower($message->header->type0);
  716. $type1 = strtolower($message->header->type1);
  717. $name = decodeHeader($message->header->name);
  718. if ($message->header->entity_id != $ent_id) {
  719. $filename = decodeHeader($message->header->filename);
  720. if (trim($filename) == '') {
  721. if (trim($name) == '') {
  722. if ( trim( $message->header->id ) == '' )
  723. $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
  724. else
  725. $display_filename = 'cid: ' . $message->header->id;
  726. // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
  727. } else {
  728. $display_filename = $name;
  729. $filename = $name;
  730. }
  731. } else {
  732. $display_filename = $filename;
  733. }
  734. $urlMailbox = urlencode($mailbox);
  735. $ent = urlencode($message->header->entity_id);
  736. $DefaultLink =
  737. "../src/download.php?startMessage=$startMessage&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  738. if ($where && $what) {
  739. $DefaultLink .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
  740. }
  741. $Links['download link']['text'] = _("download");
  742. $Links['download link']['href'] =
  743. "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;passed_ent_id=$ent";
  744. $ImageURL = '';
  745. /* this executes the attachment hook with a specific MIME-type.
  746. * if that doens't have results, it tries if there's a rule
  747. * for a more generic type. */
  748. $HookResults = do_hook("attachment $type0/$type1", $Links,
  749. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  750. $display_filename, $where, $what);
  751. if(count($HookResults[1]) <= 1) {
  752. $HookResults = do_hook("attachment $type0/*", $Links,
  753. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  754. $display_filename, $where, $what);
  755. }
  756. $Links = $HookResults[1];
  757. $DefaultLink = $HookResults[6];
  758. $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
  759. "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
  760. '<TD><SMALL><b>' . show_readable_size($message->header->size) .
  761. '</b>&nbsp;&nbsp;</small></TD>' .
  762. "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
  763. '<TD><SMALL>';
  764. if ($message->header->description) {
  765. $body .= '<b>' . htmlspecialchars(_($message->header->description)) . '</b>';
  766. }
  767. $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
  768. $SkipSpaces = 1;
  769. foreach ($Links as $Val) {
  770. if ($SkipSpaces) {
  771. $SkipSpaces = 0;
  772. } else {
  773. $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
  774. }
  775. $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
  776. }
  777. unset($Links);
  778. $body .= "</SMALL></TD></TR>\n";
  779. }
  780. } else {
  781. for ($i = 0; $i < count($message->entities); $i++) {
  782. $body .= formatAttachments($message->entities[$i], $ent_id, $mailbox, $id);
  783. }
  784. }
  785. }
  786. return( $body );
  787. }
  788. /** this function decodes the body depending on the encoding type. **/
  789. function decodeBody($body, $encoding) {
  790. $body = str_replace("\r\n", "\n", $body);
  791. $encoding = strtolower($encoding);
  792. global $show_html_default;
  793. if ($encoding == 'quoted-printable') {
  794. $body = quoted_printable_decode($body);
  795. while (ereg("=\n", $body))
  796. $body = ereg_replace ("=\n", "", $body);
  797. } else if ($encoding == 'base64') {
  798. $body = base64_decode($body);
  799. }
  800. // All other encodings are returned raw.
  801. return $body;
  802. }
  803. /*
  804. * This functions decode strings that is encoded according to
  805. * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
  806. * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
  807. */
  808. function decodeHeader ($string, $utfencode=true) {
  809. if (is_array($string)) {
  810. $string = implode("\n", $string);
  811. }
  812. $i = 0;
  813. while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
  814. $string, $res)) {
  815. $prefix = $res[1];
  816. // Ignore white-space between consecutive encoded-words
  817. if (strspn($res[2], " \t") != strlen($res[2])) {
  818. $prefix .= $res[2];
  819. }
  820. if (ucfirst($res[4]) == 'B') {
  821. $replace = base64_decode($res[5]);
  822. } else {
  823. $replace = str_replace('_', ' ', $res[5]);
  824. $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
  825. $replace);
  826. /* Only encode into entities by default. Some places
  827. don't need the encoding, like the compose form. */
  828. if ($utfencode) {
  829. $replace = charset_decode($res[3], $replace);
  830. }
  831. }
  832. $string = $prefix . $replace . substr($string, strlen($res[0]));
  833. $i = strlen($prefix) + strlen($replace);
  834. }
  835. return( $string );
  836. }
  837. /*
  838. * Encode a string according to RFC 1522 for use in headers if it
  839. * contains 8-bit characters or anything that looks like it should
  840. * be encoded.
  841. */
  842. function encodeHeader ($string) {
  843. global $default_charset;
  844. // Encode only if the string contains 8-bit characters or =?
  845. $j = strlen( $string );
  846. $l = strstr($string, '=?'); // Must be encoded ?
  847. $ret = '';
  848. for( $i=0; $i < $j; ++$i) {
  849. switch( $string{$i} ) {
  850. case '=':
  851. $ret .= '=3D';
  852. break;
  853. case '?':
  854. $ret .= '=3F';
  855. break;
  856. case '_':
  857. $ret .= '=5F';
  858. break;
  859. case ' ':
  860. $ret .= '_';
  861. break;
  862. default:
  863. $k = ord( $string{$i} );
  864. if ( $k > 126 ) {
  865. $ret .= sprintf("=%02X", $k);
  866. $l = TRUE;
  867. } else
  868. $ret .= $string{$i};
  869. }
  870. }
  871. if ( $l ) {
  872. $string = "=?$default_charset?Q?$ret?=";
  873. }
  874. return( $string );
  875. }
  876. /*
  877. Strips dangerous tags from html messages.
  878. */
  879. function MagicHTML( $body, $id ) {
  880. global $message, $HTTP_SERVER_VARS,
  881. $attachment_common_show_images;
  882. $attachment_common_show_images =
  883. FALSE; // Don't display attached images in HTML mode
  884. $j = strlen( $body ); // Legnth of the HTML
  885. $ret = ''; // Returned string
  886. $bgcolor = '#ffffff'; // Background style color (defaults to white)
  887. $textcolor = '#000000'; // Foreground style color (defaults to black)
  888. $leftmargin = ''; // Left margin style
  889. $title = ''; // HTML title if any
  890. $i = 0;
  891. while ( $i < $j ) {
  892. if ( $body{$i} == '<' ) {
  893. $pos = $i + 1;
  894. $tag = '';
  895. while ($body{$pos} == ' ' || $body{$pos} == "\t" ||
  896. $body{$pos} == "\n" ) {
  897. $pos ++;
  898. }
  899. while (strlen($tag) < 4 && $body{$pos} != ' ' &&
  900. $body{$pos} != "\t" && $body{$pos} != "\n" &&
  901. $pos < $j ) {
  902. $tag .= $body{$pos};
  903. $pos ++;
  904. }
  905. /*
  906. A comment in HTML is only three characters and isn't
  907. guaranteed to have a space after it. This fudges so
  908. it will be caught by the switch statement.
  909. */
  910. if (ereg("!--", $tag)) {
  911. $tag = "!-- ";
  912. }
  913. switch( strtoupper( $tag ) ) {
  914. // Strips the entire tag and contents
  915. case 'APPL':
  916. case 'EMBE':
  917. case 'FRAM':
  918. case 'SCRI':
  919. case 'OBJE':
  920. $etg = '/' . $tag;
  921. while ( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg &&
  922. $i < $j ) $i++;
  923. while ( $i < $j && $body{++$i} <> '>' );
  924. // $ret .= "<!-- $tag removed -->";
  925. break;
  926. // Substitute Title
  927. case 'TITL':
  928. $i += 5;
  929. while ( $body{$i} <> '>' && // </title>
  930. $i < $j )
  931. $i++;
  932. $i++;
  933. $title = '';
  934. while ( $body{$i} <> '<' && // </title>
  935. $i < $j ) {
  936. $title .= $body{$i};
  937. $i++;
  938. }
  939. $i += 7;
  940. break;
  941. // Destroy these tags
  942. case 'HTML':
  943. case 'HEAD':
  944. case '/HTM':
  945. case '/HEA':
  946. case '!DOC':
  947. case 'META':
  948. //case 'DIV ':
  949. //case '/DIV':
  950. case '!-- ':
  951. $i += 4;
  952. while ( $body{$i} <> '>' &&
  953. $i < $j )
  954. $i++;
  955. // $i++;
  956. break;
  957. case 'STYL':
  958. $i += 5;
  959. while ( $body{$i} <> '>' && // </title>
  960. $i < $j )
  961. $i++;
  962. $i++;
  963. // We parse the style to look for interesting stuff
  964. $styleblk = '';
  965. while ( $body{$i} <> '>' &&
  966. $i < $j ) {
  967. // First we get the name of the style
  968. $style = '';
  969. while ( $body{$i} <> '>' &&
  970. $body{$i} <> '<' &&
  971. $body{$i} <> '{' &&
  972. $i < $j ) {
  973. if ( isnoSep( $body{$i} ) )
  974. $style .= $body{$i};
  975. $i++;
  976. }
  977. stripComments( $i, $j, $body );
  978. $style = strtoupper( trim( $style ) );
  979. if ( $style == 'BODY' ) {
  980. // Next we look into the definitions of the body style
  981. while ( $body{$i} <> '>' &&
  982. $body{$i} <> '}' &&
  983. $i < $j ) {
  984. // We look for the background color if any.
  985. if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
  986. $i += 17;
  987. $bgcolor = getStyleData( $i, $j, $body );
  988. } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
  989. $i += 12;
  990. $leftmargin = getStyleData( $i, $j, $body );
  991. }
  992. $i++;
  993. }
  994. } else {
  995. // Other style are mantained
  996. $styleblk .= "$style ";
  997. while ( $body{$i} <> '>' &&
  998. $body{$i} <> '<' &&
  999. $body{$i} <> '}' &&
  1000. $i < $j ) {
  1001. $styleblk .= $body{$i};
  1002. $i++;
  1003. }
  1004. $styleblk .= $body{$i};
  1005. }
  1006. stripComments( $i, $j, $body );
  1007. if ( $body{$i} <> '>' )
  1008. $i++;
  1009. }
  1010. if ( $styleblk <> '' )
  1011. $ret .= "<style>$styleblk";
  1012. break;
  1013. case 'BODY':
  1014. if ( $title <> '' )
  1015. $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
  1016. $ret .= "<TABLE";
  1017. $i += 5;
  1018. if (! isset($base)) {
  1019. $base = '';
  1020. }
  1021. $ret .= stripEvent( $i, $j, $body, $id, $base );
  1022. $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
  1023. if ( $leftmargin <> '' )
  1024. $ret .= "<td width=$leftmargin>&nbsp;</td>";
  1025. $ret .= '<td>';
  1026. if (strtolower($bgcolor) == 'ffffff' ||
  1027. strtolower($bgcolor) == '#ffffff')
  1028. $ret .= '<font color=#000000>';
  1029. break;
  1030. case 'BASE':
  1031. $i += 4;
  1032. $base = '';
  1033. if ( strncasecmp($body{$i}, 'font', 4) ) {
  1034. $i += 5;
  1035. while ( !isNoSep( $body{$i} ) && $i < $j ) {
  1036. $i++;
  1037. }
  1038. while ( $body{$i} <> '>' && $i < $j ) {
  1039. $base .= $body{$i};
  1040. $i++;
  1041. }
  1042. $ret .= "<BASEFONT $base>\n";
  1043. break;
  1044. }
  1045. $i++;
  1046. while ( !isNoSep( $body{$i} ) &&
  1047. $i < $j ) {
  1048. $i++;
  1049. }
  1050. if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
  1051. $i += 5;
  1052. while ( !isNoSep( $body{$i} ) &&
  1053. $i < $j ) {
  1054. $i++;
  1055. }
  1056. while ( $body{$i} <> '>' &&
  1057. $i < $j ) {
  1058. if ( $body{$i} <> '"' ) {
  1059. $base .= $body{$i};
  1060. }
  1061. $i++;
  1062. }
  1063. // Debuging $ret .= "<!-- base == $base -->";
  1064. if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
  1065. $ret .= "\n<BASE HREF=\"$base\">\n";
  1066. }
  1067. }
  1068. break;
  1069. case '/BOD':
  1070. $ret .= '</font></td></tr></TABLE>';
  1071. $i += 6;
  1072. break;
  1073. default:
  1074. // Following tags can contain some event handler, lets search it
  1075. stripComments( $i, $j, $body );
  1076. if (! isset($base)) {
  1077. $base = '';
  1078. }
  1079. $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
  1080. // $ret .= "<!-- $tag detected -->";
  1081. }
  1082. } else {
  1083. $ret .= $body{$i};
  1084. }
  1085. $i++;
  1086. }
  1087. return( "\n\n<!-- HTML Output ahead -->\n" .
  1088. $ret .
  1089. /* Base is illegal within HTML
  1090. "\n<!-- END of HTML Output --><base href=\"".
  1091. get_location() . '/'.
  1092. "\">\n\n" );
  1093. */
  1094. "\n<!-- END of HTML Output -->\n\n" );
  1095. }
  1096. function isNoSep( $char ) {
  1097. switch( $char ) {
  1098. case ' ':
  1099. case "\n":
  1100. case "\t":
  1101. case "\r":
  1102. case '>':
  1103. case '"':
  1104. return( FALSE );
  1105. break;
  1106. default:
  1107. return( TRUE );
  1108. }
  1109. }
  1110. /*
  1111. The following function is usefull to remove extra data that can cause
  1112. html not to display properly. Especialy with MS stuff.
  1113. */
  1114. function stripComments( &$i, $j, &$body ) {
  1115. while ( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' &&
  1116. $i < $j ) {
  1117. $i += 5;
  1118. while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
  1119. $i < $j )
  1120. $i++;
  1121. $i++;
  1122. }
  1123. return;
  1124. }
  1125. /* Gets the style data of a specific style */
  1126. function getStyleData( &$i, $j, &$body ) {
  1127. // We skip spaces
  1128. while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
  1129. $i < $j ) {
  1130. $i++;
  1131. }
  1132. // And get the color
  1133. $ret = '';
  1134. while ( isNoSep( $body{$i} ) &&
  1135. $i < $j ) {
  1136. $ret .= $body{$i};
  1137. $i++;
  1138. }
  1139. return( $ret );
  1140. }
  1141. /*
  1142. Private function for strip_dangerous_tag. Look for event based coded and "remove" it
  1143. change on with no (onload -> noload)
  1144. */
  1145. function stripEvent( &$i, $j, &$body, $id, $base ) {
  1146. global $message, $base_uri, $has_unsafe_images, $view_unsafe_images;
  1147. $ret = '';
  1148. while ( $body{$i} <> '>' &&
  1149. $i < $j ) {
  1150. $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2});
  1151. switch( $etg ) {
  1152. case 'src':
  1153. // This is probably a src specification
  1154. $k = $i + 3;
  1155. while( !isNoSep( $body{$k} )) {
  1156. $k++;
  1157. }
  1158. if ( $body{$k} == '=' ) {
  1159. /* It is indeed */
  1160. $k++;
  1161. while( !isNoSep( $body{$k} ) &&
  1162. $k < $j ) {
  1163. $k++;
  1164. }
  1165. $src = '';
  1166. while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
  1167. $k < $j ) {
  1168. $src .= $body{$k};
  1169. $k++;
  1170. }
  1171. $k++;
  1172. while( !isNoSep( $body{$k} ) &&
  1173. $k < $j ) {
  1174. $k++;
  1175. }
  1176. $k++;
  1177. if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
  1178. $src = substr( $src, 4 );
  1179. $src = "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
  1180. urlencode( $message->header->mailbox ) .
  1181. "&amp;passed_ent_id=" . find_ent_id( $src, $message );
  1182. } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
  1183. stristr( $src, $base_uri ) ) {
  1184. /* Javascript and local urls goes out */
  1185. if (!$view_unsafe_images) {
  1186. $src = '../images/' . _("sec_remove_eng.png");
  1187. }
  1188. $has_unsafe_images = 1;
  1189. }
  1190. $ret .= 'src="' . $src . '" ';
  1191. $i = $k - 2;
  1192. } else {
  1193. $ret .= 'src';
  1194. $i = $i + 3;
  1195. }
  1196. break;
  1197. case '../':
  1198. // Retrolinks are not allowed without a base because they mess with SM security
  1199. if ( $base == '' ) {
  1200. $i += 2;
  1201. } else {
  1202. $ret .= '.';
  1203. }
  1204. break;
  1205. case 'cid':
  1206. // Internal link
  1207. $k = $i-1;
  1208. if ( $body{$i+3} == ':') {
  1209. $i +=4;
  1210. $name = '';
  1211. while ( isNoSep( $body{$i} ) &&
  1212. $i < $j ) {
  1213. $name .= $body{$i++};
  1214. }
  1215. if ( $name <> '' ) {
  1216. $ret .= "../src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=" .
  1217. urlencode( $message->header->mailbox ) .
  1218. "&amp;passed_ent_id=" . find_ent_id( $name, $message );
  1219. if ( $body{$k} == '"' )
  1220. $ret .= '" ';
  1221. else
  1222. $ret .= ' ';
  1223. }
  1224. if ( $body{$i} == '>' )
  1225. $i -= 1;
  1226. }
  1227. break;
  1228. case ' on':
  1229. case "\non":
  1230. case "\ron":
  1231. case "\ton":
  1232. $ret .= ' no';
  1233. $i += 2;
  1234. break;
  1235. case 'pt:':
  1236. if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) {
  1237. $ret .= '_no/';
  1238. } else {
  1239. $ret .= $etg;
  1240. }
  1241. $i += 2;
  1242. break;
  1243. default:
  1244. $ret .= $body{$i};
  1245. }
  1246. $i++;
  1247. }
  1248. return( $ret );
  1249. }
  1250. /* This function trys to locate the entity_id of a specific mime element */
  1251. function find_ent_id( $id, $message ) {
  1252. $ret = '';
  1253. for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
  1254. if ( $message->entities[$i]->header->entity_id == '' ) {
  1255. $ret = find_ent_id( $id, $message->entities[$i] );
  1256. } else {
  1257. if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
  1258. $ret = $message->entities[$i]->header->entity_id;
  1259. }
  1260. }
  1261. return( $ret );
  1262. }
  1263. ?>