mime.php 51 KB

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