mime.php 23 KB

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