mime.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?
  2. /** mime.php
  3. **
  4. ** This contains the functions necessary to detect and decode MIME messages.
  5. **/
  6. /** This is the first function called. It decides if this is a multipart
  7. message or if it should be handled as a single entity
  8. **/
  9. function decodeMime($body, $bound, $type0, $type1, &$entities) {
  10. if ($type0 == "multipart") {
  11. $bound = trim($bound);
  12. while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) {
  13. if (trim($body[$i]) == "--$bound") {
  14. $j = $i+1;
  15. $p = 0;
  16. /** Lets find the header for this entity **/
  17. /** If the first line after the boundary is blank, we use default values **/
  18. if (trim($body[$j]) == "") {
  19. $ent_type0 = "text";
  20. $ent_type1 = "plain";
  21. $charset = "us-ascii";
  22. $j++;
  23. /** If the first line ISNT blank, read in the header for this entity **/
  24. } else {
  25. while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) {
  26. $entity_header[$p] = $body[$j];
  27. $j++;
  28. $p++;
  29. }
  30. /** All of these values are getting passed back to us **/
  31. fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
  32. }
  33. /** OK, we have the header information, now lets decide what to do with it **/
  34. if ($ent_type0 == "multipart") {
  35. $y = 0;
  36. while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") {
  37. $ent_body[$y] = $body[$j];
  38. $y++;
  39. $j++;
  40. }
  41. $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities);
  42. $entities = $ent;
  43. } else {
  44. $j++;
  45. $entity_body = "";
  46. while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
  47. $entity_body .= $body[$j];
  48. $j++;
  49. }
  50. $count = count($entities);
  51. $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
  52. }
  53. }
  54. $i++;
  55. }
  56. } else {
  57. /** If this isn't a multipart message **/
  58. $j = 0;
  59. $entity_body = "";
  60. while ($j < count($body)) {
  61. $entity_body .= $body[$j];
  62. $j++;
  63. }
  64. $count = count($entities);
  65. $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
  66. }
  67. return $entities;
  68. }
  69. /** This gets one entity's properties **/
  70. function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
  71. $msg["TYPE0"] = $type0;
  72. $msg["TYPE1"] = $type1;
  73. $msg["ENCODING"] = $encoding;
  74. $msg["CHARSET"] = $charset;
  75. $msg["FILENAME"] = $filename;
  76. $msg["BODY"] = $body;
  77. return $msg;
  78. }
  79. /** This will check whether or not the message contains a certain type. It
  80. searches through all the entities for a match.
  81. **/
  82. function containsType($message, $type0, $type1, &$ent_num) {
  83. $type0 = strtolower($type0);
  84. $type1 = strtolower($type1);
  85. for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
  86. /** Check only on type0 **/
  87. if ( $type1 == "any_type" ) {
  88. if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) {
  89. $ent_num = $i;
  90. return true;
  91. }
  92. /** Check on type0 and type1 **/
  93. } else {
  94. if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) {
  95. $ent_num = $i;
  96. return true;
  97. }
  98. }
  99. }
  100. return false;
  101. }
  102. /** This returns a parsed string called $body. That string can then be displayed
  103. as the actual message in the HTML. It contains everything needed, including
  104. HTML Tags, Attachments at the bottom, etc.
  105. **/
  106. function formatBody($message) {
  107. include ("../config/config.php");
  108. /** this if statement checks for the entity to show as the primary message. To
  109. add more of them, just put them in the order that is their priority.
  110. **/
  111. $id = $message["INFO"]["ID"];
  112. $urlmailbox = urlencode($message["INFO"]["MAILBOX"]);
  113. $body = "";
  114. if (containsType($message, "text", "html", $ent_num)) {
  115. $body .= decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
  116. } else if (containsType($message, "text", "plain", $ent_num)) {
  117. $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
  118. $body .= "<TT>" . nl2br($tmpbody) . "</TT>";
  119. }
  120. // add other primary displaying message types here
  121. else {
  122. // find any type that's displayable
  123. if (containsType($message, "text", "any_type", $ent_num)) {
  124. $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
  125. $body .= "<TT>" . nl2br($tmpbody) . "</TT>";
  126. } else if (containsType($message, "message", "any_type", $ent_num)) {
  127. $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
  128. $body .= "<TT>" . nl2br($tmpbody) . "</TT>";
  129. }
  130. }
  131. /** If there are other types that shouldn't be formatted, add them here **/
  132. if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html")
  133. $body = translateText($body);
  134. $body .= "<BR><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>";
  135. /** Display the ATTACHMENTS: message if there's more than one part **/
  136. if (count($message["ENTITIES"]) > 1) {
  137. $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
  138. $body .= "<TT><B>ATTACHMENTS:</B></TT>";
  139. $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
  140. $num = 0;
  141. for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
  142. /** If we've displayed this entity, go to the next one **/
  143. if ($ent_num == $i)
  144. continue;
  145. $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]);
  146. $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]);
  147. $num++;
  148. $filename = $message["ENTITIES"][$i]["FILENAME"];
  149. if (trim($filename) == "") {
  150. $display_filename = "untitled$i";
  151. } else {
  152. $display_filename = $filename;
  153. }
  154. $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
  155. $id = $message["INFO"]["ID"];
  156. $body .= "<TT>&nbsp;&nbsp;&nbsp;<A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$i\">" . $display_filename . "</A>&nbsp;&nbsp;<SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>";
  157. }
  158. $body .= "</TD></TR></TABLE>";
  159. }
  160. return $body;
  161. }
  162. /** this function decodes the body depending on the encoding type. **/
  163. function decodeBody($body, $encoding) {
  164. $encoding = strtolower($encoding);
  165. if ($encoding == "us-ascii") {
  166. $newbody = $body; // if only they all were this easy
  167. } else if ($encoding == "quoted-printable") {
  168. echo "$body";
  169. $body = ereg_replace("=3D", "=", $body);
  170. $body = ereg_replace("=\n", "", $body);
  171. $body = ereg_replace("=20", "\n", $body);
  172. $newbody= $body;
  173. } else if ($encoding == "base64") {
  174. $newbody = base64_decode($body);
  175. } else {
  176. $newbody = $body;
  177. }
  178. return $newbody;
  179. }
  180. ?>