| 1 | <? |
| 2 | /** mime.php |
| 3 | ** |
| 4 | ** This contains the functions necessary to detect and decode MIME messages. |
| 5 | **/ |
| 6 | |
| 7 | |
| 8 | function decodeMime($body, $bound, $type0, $type1) { |
| 9 | echo "$type0/$type1<BR>"; |
| 10 | if ($type0 == "multipart") { |
| 11 | if ($body[0] == "") |
| 12 | $i = 1; |
| 13 | else |
| 14 | $i = 0; |
| 15 | |
| 16 | $bound = trim($bound); |
| 17 | $bound = "--$bound"; |
| 18 | while ($i < count($body)) { |
| 19 | if (trim($body[$i]) == $bound) { |
| 20 | $j = $i + 1; |
| 21 | $p = 0; |
| 22 | |
| 23 | while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { |
| 24 | $entity_body[$p] = $body[$j]; |
| 25 | $j++; |
| 26 | $p++; |
| 27 | } |
| 28 | fetchEntityHeader($imapConnection, $entity_body, $ent_type0, $ent_type1, $ent_bound, &$encoding, &$charset); |
| 29 | $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset); |
| 30 | |
| 31 | $q = count($full_message); |
| 32 | $full_message[$q] = $entity[0]; |
| 33 | } |
| 34 | $i++; |
| 35 | } |
| 36 | } else { |
| 37 | $full_message = getEntity($body, $bound, $type0, $type1); |
| 38 | } |
| 39 | |
| 40 | return $full_message; |
| 41 | } |
| 42 | |
| 43 | /** This gets one entity's properties **/ |
| 44 | function getEntity($body, $bound, $type0, $type1, $encoding, $charset) { |
| 45 | echo "$type0/$type1<BR>"; |
| 46 | $msg[0]["TYPE0"] = $type0; |
| 47 | $msg[0]["TYPE1"] = $type1; |
| 48 | $msg[0]["ENCODING"] = $encoding; |
| 49 | $msg[0]["CHARSET"] = $charset; |
| 50 | |
| 51 | if ($type0 == "text") { |
| 52 | if ($type1 == "plain") { |
| 53 | $msg[0]["PRIORITY"] = 10; |
| 54 | for ($p = 0;$p < count($body);$p++) { |
| 55 | $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]); |
| 56 | } |
| 57 | } else if ($type1 == "html") { |
| 58 | $msg[0]["PRIORITY"] = 20; |
| 59 | $msg[0]["BODY"] = $body; |
| 60 | } else { |
| 61 | $msg[0]["PRIORITY"] = 1; |
| 62 | $msg[0]["BODY"][0] = "This entity is of an unknown format. Doing my best to display anyway...<BR><BR>"; |
| 63 | for ($p = 1;$p < count($body);$p++) { |
| 64 | $q = $p - 1; |
| 65 | $msg[0]["BODY"][$p] = $body[$q]; |
| 66 | } |
| 67 | } |
| 68 | } else if ($type0 == "image") { |
| 69 | $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This is an image. Squirrelmail currently cannot decode images.</FONT></B>"; |
| 70 | } else { |
| 71 | $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>"; |
| 72 | } |
| 73 | |
| 74 | return $msg; |
| 75 | } |
| 76 | |
| 77 | function formatBody($message) { |
| 78 | for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 79 | if ($message["ENTITIES"][$i]["TYPE0"] == "text") { |
| 80 | if ($message["ENTITIES"][$i]["PRIORITY"] > $priority) |
| 81 | $priority = $message["ENTITIES"][$i]["PRIORITY"]; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
| 86 | switch ($priority) { |
| 87 | /** HTML **/ |
| 88 | case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 89 | if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) { |
| 90 | $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); |
| 91 | } |
| 92 | } |
| 93 | break; |
| 94 | /** PLAIN **/ |
| 95 | case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 96 | if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) { |
| 97 | $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); |
| 98 | } |
| 99 | } |
| 100 | break; |
| 101 | /** UNKNOWN...SEND WHAT WE GOT **/ |
| 102 | case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 103 | if (($message["ENTITIES"][$i]["TYPE0"] == "text")) { |
| 104 | $pos = count($body); |
| 105 | for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++) { |
| 106 | $pos = $pos + $b; |
| 107 | $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b]; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | return $body; |
| 115 | } |
| 116 | |
| 117 | function decodeBody($body, $encoding) { |
| 118 | $encoding = strtolower($encoding); |
| 119 | if ($encoding == "us-ascii") { |
| 120 | $newbody = $body; // if only they all were this easy |
| 121 | } else if ($encoding == "quoted-printable") { |
| 122 | for ($q=0; $q < count($body); $q++) { |
| 123 | if (substr(trim($body[$q]), -1) == "=") { |
| 124 | $body[$q] = trim($body[$q]); |
| 125 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-1); |
| 126 | } else if (substr(trim($body[$q]), -3) == "=20") { |
| 127 | $body[$q] = trim($body[$q]); |
| 128 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-3); |
| 129 | $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno |
| 130 | } |
| 131 | } |
| 132 | for ($q=0;$q < count($body);$q++) { |
| 133 | $body[$q] = ereg_replace("=3D", "=", $body[$q]); |
| 134 | } |
| 135 | $newbody = $body; |
| 136 | } else { |
| 137 | $newbody = $body; |
| 138 | } |
| 139 | return $newbody; |
| 140 | } |
| 141 | ?> |