mime.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. <?php
  2. /** mime.php
  3. **
  4. ** This contains the functions necessary to detect and decode MIME
  5. ** messages.
  6. **
  7. ** $Id$
  8. **/
  9. $debug_mime = false;
  10. $mime_php = true;
  11. if (!isset($i18n_php))
  12. include "../functions/i18n.php";
  13. if (!isset($imap_php))
  14. include "../functions/imap.php";
  15. if (!isset($config_php))
  16. include "../config/config.php";
  17. /** Setting up the objects that have the structure for the message **/
  18. class msg_header {
  19. /** msg_header contains generic variables for values that **/
  20. /** could be in a header. **/
  21. var $type0 = '', $type1 = '', $boundary = '', $charset = '';
  22. var $encoding = '', $size = 0, $to = '', $from = '', $date = '';
  23. var $cc = '', $bcc = '', $reply_to = '', $subject = '';
  24. var $id = 0, $mailbox = '', $description = '', $filename = '';
  25. var $entity_id = 0, $message_id = 0;
  26. }
  27. class message {
  28. /** message is the object that contains messages. It is a recursive
  29. object in that through the $entities variable, it can contain
  30. more objects of type message. See documentation in mime.txt for
  31. a better description of how this works.
  32. **/
  33. var $header = '';
  34. var $entities = '';
  35. function addEntity ($msg) {
  36. $this->entities[] = $msg;
  37. }
  38. }
  39. /* --------------------------------------------------------------------------------- */
  40. /* MIME DECODING */
  41. /* --------------------------------------------------------------------------------- */
  42. // This function gets the structure of a message and stores it in the "message" class.
  43. // It will return this object for use with all relevant header information and
  44. // fully parsed into the standard "message" object format.
  45. function mime_structure ($imap_stream, $header) {
  46. global $debug_mime;
  47. sqimap_messages_flag ($imap_stream, $header->id, $header->id, "Seen");
  48. $id = $header->id;
  49. fputs ($imap_stream, "a001 FETCH $id BODYSTRUCTURE\r\n");
  50. //
  51. // This should use sqimap_read_data instead of reading it itself
  52. //
  53. $read = fgets ($imap_stream, 10000);
  54. $response = substr($read, 0, 4);
  55. $bodystructure = "";
  56. while ($response != "a001") {
  57. $bodystructure .= $read;
  58. $read = fgets ($imap_stream, 10000);
  59. $response = substr($read, 0, 4);
  60. }
  61. $read = $bodystructure;
  62. if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
  63. // isolate the body structure and remove beginning and end parenthesis
  64. $read = trim(substr ($read, strpos(strtolower($read), "bodystructure") + 13));
  65. $read = trim(substr ($read, 0, -1));
  66. $end = mime_match_parenthesis(0, $read);
  67. while ($end == strlen($read)-1) {
  68. $read = trim(substr ($read, 0, -1));
  69. $read = trim(substr ($read, 1));
  70. $end = mime_match_parenthesis(0, $read);
  71. }
  72. if ($debug_mime) echo "<tt>$read</tt><br><br>\n";
  73. $msg = mime_parse_structure ($read, 0);
  74. $msg->header = $header;
  75. return $msg;
  76. }
  77. // this starts the parsing of a particular structure. It is called recursively,
  78. // so it can be passed different structures. It returns an object of type
  79. // $message.
  80. // First, it checks to see if it is a multipart message. If it is, then it
  81. // handles that as it sees is necessary. If it is just a regular entity,
  82. // then it parses it and adds the necessary header information (by calling out
  83. // to mime_get_elements()
  84. function mime_parse_structure ($structure, $ent_id) {
  85. global $debug_mime;
  86. if ($debug_mime) echo "<font color=008800><tt>START: mime_parse_structure()</tt></font><br>\n";
  87. $msg = new message();
  88. if (substr($structure, 0, 1) == "(") {
  89. $ent_id = mime_new_element_level($ent_id);
  90. $start = $end = -1;
  91. if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
  92. do {
  93. if ($debug_mime) echo "<font color=008800><tt>Found entity...</tt></font><br>";
  94. $start = $end+1;
  95. $end = mime_match_parenthesis ($start, $structure);
  96. $element = substr($structure, $start+1, ($end - $start)-1);
  97. $ent_id = mime_increment_id ($ent_id);
  98. $newmsg = mime_parse_structure ($element, $ent_id);
  99. $msg->addEntity ($newmsg);
  100. } while (substr($structure, $end+1, 1) == "(");
  101. } else {
  102. // parse the elements
  103. if ($debug_mime) echo "<br><font color=0000aa><tt>$structure</tt></font><br>";
  104. $msg = mime_get_element ($structure, $msg, $ent_id);
  105. if ($debug_mime) echo "<br>";
  106. }
  107. return $msg;
  108. if ($debug_mime) echo "<font color=008800><tt>&nbsp;&nbsp;END: mime_parse_structure()</tt></font><br>";
  109. }
  110. // Increments the element ID. An element id can look like any of
  111. // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
  112. // the last number of the element id, changing 1.2 to 1.3.
  113. function mime_increment_id ($id) {
  114. global $debug_mime;
  115. if (strpos($id, ".")) {
  116. $first = substr($id, 0, strrpos($id, "."));
  117. $last = substr($id, strrpos($id, ".")+1);
  118. $last++;
  119. $new = $first . "." .$last;
  120. } else {
  121. $new = $id + 1;
  122. }
  123. if ($debug_mime) echo "<b>INCREMENT: $new</b><br>";
  124. return $new;
  125. }
  126. // See comment for mime_increment_id().
  127. // This adds another level on to the entity_id changing 1.3 to 1.3.0
  128. // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
  129. // before it can be used. I left it this way so as not to have
  130. // to make a special case if it is the first entity_id. It
  131. // always increments it, and that works fine.
  132. function mime_new_element_level ($id) {
  133. if (!$id) $id = 0;
  134. else $id = $id . ".0";
  135. return $id;
  136. }
  137. function mime_get_element (&$structure, $msg, $ent_id) {
  138. global $debug_mime;
  139. $elem_num = 1;
  140. $msg->header = new msg_header();
  141. $msg->header->entity_id = $ent_id;
  142. while (strlen($structure) > 0) {
  143. $structure = trim($structure);
  144. $char = substr($structure, 0, 1);
  145. if (strtolower(substr($structure, 0, 3)) == "nil") {
  146. $text = "";
  147. $structure = substr($structure, 3);
  148. } else if ($char == "\"") {
  149. // loop through until we find the matching quote, and return that as a string
  150. $pos = 1;
  151. $char = substr($structure, $pos, 1);
  152. $text = "";
  153. while ($char != "\"" && $pos < strlen($structure)) {
  154. $text .= $char;
  155. $pos++;
  156. $char = substr($structure, $pos, 1);
  157. }
  158. $structure = substr($structure, strlen($text) + 2);
  159. } else if ($char == "(") {
  160. // comment me
  161. $end = mime_match_parenthesis (0, $structure);
  162. $sub = substr($structure, 1, $end-1);
  163. if (! isset($properties))
  164. $properties = array();
  165. $properties = mime_get_props($properties, $sub);
  166. $structure = substr($structure, strlen($sub) + 2);
  167. } else {
  168. // loop through until we find a space or an end parenthesis
  169. $pos = 0;
  170. $char = substr($structure, $pos, 1);
  171. while ($char != " " && $char != ")" && $pos < strlen($structure)) {
  172. $text .= $char;
  173. $pos++;
  174. $char = substr($structure, $pos, 1);
  175. }
  176. $structure = substr($structure, strlen($text));
  177. }
  178. if ($debug_mime) echo "<tt>$elem_num : $text</tt><br>";
  179. // This is where all the text parts get put into the header
  180. switch ($elem_num) {
  181. case 1:
  182. $msg->header->type0 = strtolower($text);
  183. if ($debug_mime) echo "<tt>type0 = ".strtolower($text)."</tt><br>";
  184. break;
  185. case 2:
  186. $msg->header->type1 = strtolower($text);
  187. if ($debug_mime) echo "<tt>type1 = ".strtolower($text)."</tt><br>";
  188. break;
  189. case 5:
  190. $msg->header->description = $text;
  191. if ($debug_mime) echo "<tt>description = $text</tt><br>";
  192. break;
  193. case 6:
  194. $msg->header->encoding = strtolower($text);
  195. if ($debug_mime) echo "<tt>encoding = ".strtolower($text)."</tt><br>";
  196. break;
  197. case 7:
  198. $msg->header->size = $text;
  199. if ($debug_mime) echo "<tt>size = $text</tt><br>";
  200. break;
  201. default:
  202. if ($msg->header->type0 == "text" && $elem_num == 8) {
  203. // This is a plain text message, so lets get the number of lines
  204. // that it contains.
  205. $msg->header->num_lines = $text;
  206. if ($debug_mime) echo "<tt>num_lines = $text</tt><br>";
  207. } else if ($msg->header->type0 == "message" && $msg->header->type1 == "rfc822" && $elem_num == 8) {
  208. // This is an encapsulated message, so lets start all over again and
  209. // parse this message adding it on to the existing one.
  210. $structure = trim($structure);
  211. if (substr($structure, 0, 1) == "(") {
  212. $e = mime_match_parenthesis (0, $structure);
  213. $structure = substr($structure, 0, $e);
  214. $structure = substr($structure, 1);
  215. $m = mime_parse_structure($structure, $msg->header->entity_id);
  216. // the following conditional is there to correct a bug that wasn't
  217. // incrementing the entity IDs correctly because of the special case
  218. // that message/rfc822 is. This fixes it fine.
  219. if (substr($structure, 1, 1) != "(")
  220. $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
  221. // Now we'll go through and reformat the results.
  222. if ($m->entities) {
  223. for ($i=0; $i < count($m->entities); $i++) {
  224. $msg->addEntity($m->entities[$i]);
  225. }
  226. } else {
  227. $msg->addEntity($m);
  228. }
  229. $structure = "";
  230. }
  231. }
  232. break;
  233. }
  234. $elem_num++;
  235. $text = "";
  236. }
  237. // loop through the additional properties and put those in the various headers
  238. if ($msg->header->type0 != "message") {
  239. for ($i=0; $i < count($properties); $i++) {
  240. $msg->header->{$properties[$i]["name"]} = $properties[$i]["value"];
  241. if ($debug_mime) echo "<tt>".$properties[$i]["name"]." = " . $properties[$i]["value"] . "</tt><br>";
  242. }
  243. }
  244. return $msg;
  245. }
  246. // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
  247. // figure out how to do this part, so I decided to go to bed. I woke up
  248. // in the morning and had a flash of insight. I went to the white-board
  249. // and scribbled it out, then spent a bit programming it, and this is the
  250. // result. Nothing complicated, but I think my brain was fried yesterday.
  251. // Funny how that happens some times.
  252. //
  253. // This gets properties in a nested parenthesisized list. For example,
  254. // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
  255. // This returns an array called $props with all paired up properties.
  256. // It ignores the "attachment" for now, maybe that should change later
  257. // down the road. In this case, what is returned is:
  258. // $props[0]["name"] = "filename";
  259. // $props[0]["value"] = "luke.tar.gz";
  260. function mime_get_props ($props, $structure) {
  261. global $debug_mime;
  262. while (strlen($structure) > 0) {
  263. $structure = trim($structure);
  264. $char = substr($structure, 0, 1);
  265. if ($char == "\"") {
  266. $pos = 1;
  267. $char = substr($structure, $pos, 1);
  268. $tmp = "";
  269. while ($char != "\"" && $pos < strlen($structure)) {
  270. $tmp .= $char;
  271. $pos++;
  272. $char = substr($structure, $pos, 1);
  273. }
  274. $structure = trim(substr($structure, strlen($tmp) + 2));
  275. $char = substr($structure, 0, 1);
  276. if ($char == "\"") {
  277. $pos = 1;
  278. $char = substr($structure, $pos, 1);
  279. $value = "";
  280. while ($char != "\"" && $pos < strlen($structure)) {
  281. $value .= $char;
  282. $pos++;
  283. $char = substr($structure, $pos, 1);
  284. }
  285. $structure = trim(substr($structure, strlen($tmp) + 2));
  286. $k = count($props);
  287. $props[$k]["name"] = strtolower($tmp);
  288. $props[$k]["value"] = $value;
  289. } else if ($char == "(") {
  290. $end = mime_match_parenthesis (0, $structure);
  291. $sub = substr($structure, 1, $end-1);
  292. if (! isset($props))
  293. $props = array();
  294. $props = mime_get_props($props, $sub);
  295. $structure = substr($structure, strlen($sub) + 2);
  296. }
  297. return $props;
  298. } else if ($char == "(") {
  299. $end = mime_match_parenthesis (0, $structure);
  300. $sub = substr($structure, 1, $end-1);
  301. $props = mime_get_props($props, $sub);
  302. $structure = substr($structure, strlen($sub) + 2);
  303. return $props;
  304. } else {
  305. return $props;
  306. }
  307. }
  308. }
  309. // Matches parenthesis. It will return the position of the matching
  310. // parenthesis in $structure. For instance, if $structure was:
  311. // ("text" "plain" ("val1name", "1") nil ... )
  312. // x x
  313. // then this would return 42 to match up those two.
  314. function mime_match_parenthesis ($pos, $structure) {
  315. $char = substr($structure, $pos, 1);
  316. // ignore all extra characters
  317. // If inside of a string, skip string -- Boundary IDs and other
  318. // things can have ) in them.
  319. while ($pos < strlen($structure)) {
  320. $pos++;
  321. $char = substr($structure, $pos, 1);
  322. if ($char == ")") {
  323. return $pos;
  324. } else if ($char == '"') {
  325. $pos ++;
  326. while (substr($structure, $pos, 1) != '"' &&
  327. $pos < strlen($structure)) {
  328. $pos ++;
  329. }
  330. } else if ($char == "(") {
  331. $pos = mime_match_parenthesis ($pos, $structure);
  332. }
  333. }
  334. }
  335. function mime_fetch_body ($imap_stream, $id, $ent_id) {
  336. // do a bit of error correction. If we couldn't find the entity id, just guess
  337. // that it is the first one. That is usually the case anyway.
  338. if (!$ent_id) $ent_id = 1;
  339. fputs ($imap_stream, "a010 FETCH $id BODY[$ent_id]\r\n");
  340. $data = sqimap_read_data ($imap_stream, 'a010', true, $response, $message);
  341. $topline = array_shift($data);
  342. while (! ereg('\* [0-9]+ FETCH ', $topline) && data)
  343. $topline = array_shift($data);
  344. $wholemessage = implode('', $data);
  345. if (ereg('\{([^\}]*)\}', $topline, $regs)) {
  346. return substr($wholemessage, 0, $regs[1]);
  347. }
  348. else if (ereg('"([^"]*)"', $topline, $regs)) {
  349. return $regs[1];
  350. }
  351. $str = "Body retrival error. Please report this bug!\n";
  352. $str .= "Response: $response\n";
  353. $str .= "Message: $message\n";
  354. $str .= "FETCH line: $topline";
  355. $str .= "---------------\n$wholemessage";
  356. foreach ($data as $d)
  357. {
  358. $str .= htmlspecialchars($d) . "\n";
  359. }
  360. return $str;
  361. return "Body retrival error, please report this bug!\n\nTop line is \"$topline\"\n";
  362. }
  363. function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
  364. // do a bit of error correction. If we couldn't find the entity id, just guess
  365. // that it is the first one. That is usually the case anyway.
  366. if (!$ent_id) $ent_id = 1;
  367. fputs ($imap_stream, "a001 FETCH $id BODY[$ent_id]\r\n");
  368. $cnt = 0;
  369. $continue = true;
  370. $read = fgets ($imap_stream,4096);
  371. while (!ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
  372. if (trim($read) == ")==") {
  373. $read1 = $read;
  374. $read = fgets ($imap_stream,4096);
  375. if (ereg("^a001 (OK|BAD|NO)(.*)$", $read, $regs)) {
  376. return;
  377. } else {
  378. echo decodeBody($read1, $encoding);
  379. echo decodeBody($read, $encoding);
  380. }
  381. } else if ($cnt) {
  382. echo decodeBody($read, $encoding);
  383. }
  384. $read = fgets ($imap_stream,4096);
  385. $cnt++;
  386. }
  387. }
  388. /* -[ END MIME DECODING ]----------------------------------------------------------- */
  389. /** This is the first function called. It decides if this is a multipart
  390. message or if it should be handled as a single entity
  391. **/
  392. function decodeMime ($imap_stream, &$header) {
  393. global $username, $key, $imapServerAddress, $imapPort;
  394. return mime_structure ($imap_stream, $header);
  395. }
  396. // This is here for debugging purposese. It will print out a list
  397. // of all the entity IDs that are in the $message object.
  398. function listEntities ($message) {
  399. if ($message) {
  400. if ($message->header->entity_id)
  401. echo "<tt>" . $message->header->entity_id . " : " . $message->header->type0 . "/" . $message->header->type1 . "<br>";
  402. for ($i = 0; $message->entities[$i]; $i++) {
  403. $msg = listEntities($message->entities[$i], $ent_id);
  404. if ($msg)
  405. return $msg;
  406. }
  407. }
  408. }
  409. // returns a $message object for a particular entity id
  410. function getEntity ($message, $ent_id) {
  411. if ($message) {
  412. if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
  413. return $message;
  414. } else {
  415. for ($i = 0; $message->entities[$i]; $i++) {
  416. $msg = getEntity ($message->entities[$i], $ent_id);
  417. if ($msg)
  418. return $msg;
  419. }
  420. }
  421. }
  422. }
  423. // figures out what entity to display and returns the $message object
  424. // for that entity.
  425. function findDisplayEntity ($message) {
  426. if ($message) {
  427. if ($message->header->type0 == "text") {
  428. if ($message->header->type1 == "plain" ||
  429. $message->header->type1 == "html") {
  430. if (isset($message->header->entity_id))
  431. return $message->header->entity_id;
  432. return 0;
  433. }
  434. } else {
  435. for ($i=0; $message->entities[$i]; $i++) {
  436. return findDisplayEntity($message->entities[$i]);
  437. }
  438. }
  439. }
  440. }
  441. /** This returns a parsed string called $body. That string can then
  442. be displayed as the actual message in the HTML. It contains
  443. everything needed, including HTML Tags, Attachments at the
  444. bottom, etc.
  445. **/
  446. function formatBody($imap_stream, $message, $color, $wrap_at) {
  447. // this if statement checks for the entity to show as the
  448. // primary message. To add more of them, just put them in the
  449. // order that is their priority.
  450. global $startMessage, $username, $key, $imapServerAddress, $imapPort;
  451. $id = $message->header->id;
  452. $urlmailbox = urlencode($message->header->mailbox);
  453. // Get the right entity and redefine message to be this entity
  454. $ent_num = findDisplayEntity ($message);
  455. $body_message = getEntity($message, $ent_num);
  456. if (($body_message->header->type0 == "text") ||
  457. ($body_message->header->type0 == "rfc822")) {
  458. $body = mime_fetch_body ($imap_stream, $id, $ent_num);
  459. $body = decodeBody($body, $body_message->header->encoding);
  460. // If there are other types that shouldn't be formatted, add
  461. // them here
  462. if ($body_message->header->type1 != "html") {
  463. translateText($body, $wrap_at, $body_message->header->charset);
  464. }
  465. $body .= "<SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox\">". _("Download this as a file") ."</A></CENTER><BR></SMALL>";
  466. /** Display the ATTACHMENTS: message if there's more than one part **/
  467. $body .= "</TD></TR></TABLE>";
  468. if (isset($message->entities[0])) {
  469. $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
  470. }
  471. } else {
  472. $body .= formatAttachments ($message, -1, $message->header->mailbox, $id);
  473. }
  474. return $body;
  475. }
  476. // A recursive function that returns a list of attachments with links
  477. // to where to download these attachments
  478. function formatAttachments ($message, $ent_id, $mailbox, $id) {
  479. global $where, $what;
  480. global $startMessage, $color;
  481. static $ShownHTML = 0;
  482. $body = "";
  483. if ($ShownHTML == 0)
  484. {
  485. $ShownHTML = 1;
  486. $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n";
  487. $body .= "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n";
  488. $body .= _("Attachments") . ':';
  489. $body .= "</B></TH></TR><TR><TD>\n";
  490. $body .= "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n";
  491. $body .= formatAttachments ($message, $ent_id, $mailbox, $id);
  492. $body .= "</TABLE></TD></TR></TABLE>";
  493. return $body;
  494. }
  495. if ($message) {
  496. if (!$message->entities) {
  497. $type0 = strtolower($message->header->type0);
  498. $type1 = strtolower($message->header->type1);
  499. if ($message->header->entity_id != $ent_id) {
  500. $filename = decodeHeader($message->header->filename);
  501. if (trim($filename) == "") {
  502. $display_filename = "untitled-".$message->header->entity_id;
  503. } else {
  504. $display_filename = $filename;
  505. }
  506. $urlMailbox = urlencode($mailbox);
  507. $ent = urlencode($message->header->entity_id);
  508. $DefaultLink =
  509. "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
  510. if ($where && $what)
  511. $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
  512. $Links['download link']['text'] = _("download");
  513. $Links['download link']['href'] =
  514. "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
  515. $ImageURL = '';
  516. $HookResults = do_hook("attachment $type0/$type1", $Links,
  517. $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
  518. $display_filename, $where, $what);
  519. $Links = $HookResults[1];
  520. $DefaultLink = $HookResults[6];
  521. $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>';
  522. $body .= "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>";
  523. $body .= '<TD><SMALL><b>' . show_readable_size($message->header->size) .
  524. '</b>&nbsp;&nbsp;</small></TD>';
  525. $body .= "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>";
  526. $body .= '<TD><SMALL>';
  527. if ($message->header->description)
  528. $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
  529. $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
  530. $SkipSpaces = 1;
  531. foreach ($Links as $Val)
  532. {
  533. if ($SkipSpaces)
  534. {
  535. $SkipSpaces = 0;
  536. }
  537. else
  538. {
  539. $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
  540. }
  541. $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
  542. }
  543. unset($Links);
  544. $body .= "</SMALL></TD></TR>\n";
  545. }
  546. return $body;
  547. } else {
  548. for ($i = 0; $i < count($message->entities); $i++) {
  549. $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
  550. }
  551. return $body;
  552. }
  553. }
  554. }
  555. /** this function decodes the body depending on the encoding type. **/
  556. function decodeBody($body, $encoding) {
  557. $body = str_replace("\r\n", "\n", $body);
  558. $encoding = strtolower($encoding);
  559. if ($encoding == "quoted-printable") {
  560. $body = quoted_printable_decode($body);
  561. while (ereg("=\n", $body))
  562. $body = ereg_replace ("=\n", "", $body);
  563. } else if ($encoding == "base64") {
  564. $body = base64_decode($body);
  565. }
  566. // All other encodings are returned raw.
  567. return $body;
  568. }
  569. // This functions decode strings that is encoded according to
  570. // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
  571. function decodeHeader ($string) {
  572. if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
  573. $string, $res)) {
  574. if (ucfirst($res[2]) == "B") {
  575. $replace = base64_decode($res[3]);
  576. } else {
  577. $replace = ereg_replace("_", " ", $res[3]);
  578. // Convert lowercase Quoted Printable to uppercase for
  579. // quoted_printable_decode to understand it.
  580. while (ereg("(=([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef]))", $replace, $res)) {
  581. $replace = str_replace($res[1], strtoupper($res[1]), $replace);
  582. }
  583. $replace = quoted_printable_decode($replace);
  584. }
  585. $replace = charset_decode ($res[1], $replace);
  586. $string = eregi_replace
  587. ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
  588. $replace, $string);
  589. // In case there should be more encoding in the string: recurse
  590. return (decodeHeader($string));
  591. } else
  592. return ($string);
  593. }
  594. // Encode a string according to RFC 1522 for use in headers if it
  595. // contains 8-bit characters or anything that looks like it should
  596. // be encoded.
  597. function encodeHeader ($string) {
  598. global $default_charset;
  599. // Encode only if the string contains 8-bit characters or =?
  600. if (ereg("([\200-\377]|=\\?)", $string)) {
  601. $newstring = "=?$default_charset?Q?";
  602. // First the special characters
  603. $string = str_replace("=", "=3D", $string);
  604. $string = str_replace("?", "=3F", $string);
  605. $string = str_replace("_", "=5F", $string);
  606. $string = str_replace(" ", "_", $string);
  607. for ( $ch = 127 ; $ch <= 255 ; $ch++ ) {
  608. $replace = chr($ch);
  609. $insert = sprintf("=%02X", $ch);
  610. $string = str_replace($replace, $insert, $string);
  611. $ch++;
  612. }
  613. $newstring = "=?$default_charset?Q?".$string."?=";
  614. return $newstring;
  615. }
  616. return $string;
  617. }
  618. ?>