| 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 "<TT>decodeMime: $type0/$type1</TT><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) && (trim($body[$j]) != "")) { |
| 24 | $entity_header[$p] = $body[$j]; |
| 25 | $j++; |
| 26 | $p++; |
| 27 | } |
| 28 | |
| 29 | fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset); |
| 30 | |
| 31 | if ($ent_type0 == "text") { |
| 32 | while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { |
| 33 | $entity_body[$p] = $body[$j]; |
| 34 | $j++; |
| 35 | $p++; |
| 36 | } |
| 37 | } else { |
| 38 | if (trim($body[$j]) == "") |
| 39 | $j++; |
| 40 | while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { |
| 41 | $entity_body .= $body[$j]; |
| 42 | $j++; |
| 43 | } |
| 44 | } |
| 45 | $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset); |
| 46 | |
| 47 | $q = count($full_message); |
| 48 | $full_message[$q] = $entity[0]; |
| 49 | } |
| 50 | $i++; |
| 51 | } |
| 52 | } else { |
| 53 | $full_message = getEntity($body, $bound, $type0, $type1); |
| 54 | } |
| 55 | |
| 56 | return $full_message; |
| 57 | } |
| 58 | |
| 59 | /** This gets one entity's properties **/ |
| 60 | function getEntity($body, $bound, $type0, $type1, $encoding, $charset) { |
| 61 | // echo "<TT>getEntity: $type0/$type1</TT><BR>"; |
| 62 | $msg[0]["TYPE0"] = $type0; |
| 63 | $msg[0]["TYPE1"] = $type1; |
| 64 | $msg[0]["ENCODING"] = $encoding; |
| 65 | $msg[0]["CHARSET"] = $charset; |
| 66 | |
| 67 | if ($type0 == "text") { |
| 68 | // error correcting if they didn't follow RFC standards |
| 69 | if (trim($type1) == "") |
| 70 | $type1 = "plain"; |
| 71 | |
| 72 | if ($type1 == "plain") { |
| 73 | $msg[0]["PRIORITY"] = 10; |
| 74 | for ($p = 0;$p < count($body);$p++) { |
| 75 | $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]); |
| 76 | } |
| 77 | } else if ($type1 == "html") { |
| 78 | $msg[0]["PRIORITY"] = 20; |
| 79 | $msg[0]["BODY"] = $body; |
| 80 | } else { |
| 81 | $msg[0]["PRIORITY"] = 1; |
| 82 | $msg[0]["BODY"][0] = "This entity is of an unknown format. Doing my best to display anyway...<BR><BR>"; |
| 83 | for ($p = 1;$p < count($body);$p++) { |
| 84 | $q = $p - 1; |
| 85 | $msg[0]["BODY"][$p] = $body[$q]; |
| 86 | } |
| 87 | } |
| 88 | } else if ($type0 == "image") { |
| 89 | $msg[0]["PRIORITY"] == 5; |
| 90 | $msg[0]["BODY"][0] = $body; |
| 91 | } else { |
| 92 | $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>"; |
| 93 | } |
| 94 | |
| 95 | return $msg; |
| 96 | } |
| 97 | |
| 98 | function formatBody($message) { |
| 99 | for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 100 | if ($message["ENTITIES"][$i]["TYPE0"] == "text") { |
| 101 | if ($message["ENTITIES"][$i]["PRIORITY"] > $priority) |
| 102 | $priority = $message["ENTITIES"][$i]["PRIORITY"]; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
| 107 | switch ($priority) { |
| 108 | /** HTML **/ |
| 109 | case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 110 | if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) { |
| 111 | $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); |
| 112 | } |
| 113 | } |
| 114 | break; |
| 115 | /** PLAIN **/ |
| 116 | case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 117 | if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) { |
| 118 | $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); |
| 119 | } |
| 120 | } |
| 121 | break; |
| 122 | /** UNKNOWN...SEND WHAT WE GOT **/ |
| 123 | case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
| 124 | if (($message["ENTITIES"][$i]["TYPE0"] == "text")) { |
| 125 | $pos = count($body); |
| 126 | for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++) { |
| 127 | $pos = $pos + $b; |
| 128 | $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b]; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
| 137 | $pos = count($body); |
| 138 | if ($message["ENTITIES"][$i]["TYPE0"] != "text") { |
| 139 | $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>"; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
| 144 | $pos = count($body); |
| 145 | if ($message["ENTITIES"][$i]["TYPE0"] != "text") { |
| 146 | if ($message["ENTITIES"][$i]["TYPE0"] == "image") { |
| 147 | $body[$pos] = "<TT> Image: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>"; |
| 148 | |
| 149 | /* $file = fopen("../data/tmp.png", "w"); |
| 150 | fwrite($file, base64_decode($message["ENTITIES"][$i]["BODY"][0])); |
| 151 | fclose($file); |
| 152 | */ |
| 153 | } else { |
| 154 | $body[$pos] = "<TT> Unknown Type: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>"; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return $body; |
| 160 | } |
| 161 | |
| 162 | function decodeBody($body, $encoding) { |
| 163 | $encoding = strtolower($encoding); |
| 164 | if ($encoding == "us-ascii") { |
| 165 | $newbody = $body; // if only they all were this easy |
| 166 | } else if ($encoding == "quoted-printable") { |
| 167 | for ($q=0; $q < count($body); $q++) { |
| 168 | if (substr(trim($body[$q]), -1) == "=") { |
| 169 | $body[$q] = trim($body[$q]); |
| 170 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-1); |
| 171 | } else if (substr(trim($body[$q]), -3) == "=20") { |
| 172 | $body[$q] = trim($body[$q]); |
| 173 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-3); |
| 174 | $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno |
| 175 | } |
| 176 | } |
| 177 | for ($q=0;$q < count($body);$q++) { |
| 178 | $body[$q] = ereg_replace("=3D", "=", $body[$q]); |
| 179 | } |
| 180 | $newbody = $body; |
| 181 | } else { |
| 182 | $newbody = $body; |
| 183 | } |
| 184 | return $newbody; |
| 185 | } |
| 186 | ?> |