aceb0d5c |
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) { |
bcb432a3 |
9 | echo "<TT>decodeMime: $type0/$type1</TT><BR>"; |
aceb0d5c |
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 | |
bcb432a3 |
23 | while ((substr(trim($body[$j]), 0, strlen($bound)) != $bound) && (trim($body[$j]) != "")) { |
24 | $entity_header[$p] = $body[$j]; |
aceb0d5c |
25 | $j++; |
26 | $p++; |
27 | } |
bcb432a3 |
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 | } |
d4467150 |
45 | $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset); |
aceb0d5c |
46 | |
47 | $q = count($full_message); |
d4467150 |
48 | $full_message[$q] = $entity[0]; |
aceb0d5c |
49 | } |
50 | $i++; |
51 | } |
d4467150 |
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) { |
bcb432a3 |
61 | echo "<TT>getEntity: $type0/$type1</TT><BR>"; |
d4467150 |
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") { |
8405ee35 |
68 | // error correcting if they didn't follow RFC standards |
69 | if (trim($type1) == "") |
70 | $type1 = "plain"; |
71 | |
aceb0d5c |
72 | if ($type1 == "plain") { |
d4467150 |
73 | $msg[0]["PRIORITY"] = 10; |
aceb0d5c |
74 | for ($p = 0;$p < count($body);$p++) { |
d4467150 |
75 | $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]); |
aceb0d5c |
76 | } |
aceb0d5c |
77 | } else if ($type1 == "html") { |
d4467150 |
78 | $msg[0]["PRIORITY"] = 20; |
79 | $msg[0]["BODY"] = $body; |
80 | } else { |
81 | $msg[0]["PRIORITY"] = 1; |
0f1835f3 |
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 | } |
aceb0d5c |
87 | } |
bcb432a3 |
88 | } else if ($type0 == "image") { |
89 | $msg[0]["PRIORITY"] == 5; |
90 | $msg[0]["BODY"][0] = $body; |
d4467150 |
91 | } else { |
92 | $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>"; |
aceb0d5c |
93 | } |
94 | |
d4467150 |
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 | } |
8405ee35 |
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") { |
bcb432a3 |
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 | */ |
8405ee35 |
153 | } else { |
154 | $body[$pos] = "<TT> Unknown Type: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>"; |
155 | } |
156 | } |
157 | } |
158 | |
d4467150 |
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 | } |
14d10786 |
177 | for ($q=0;$q < count($body);$q++) { |
178 | $body[$q] = ereg_replace("=3D", "=", $body[$q]); |
179 | } |
d4467150 |
180 | $newbody = $body; |
181 | } else { |
182 | $newbody = $body; |
183 | } |
184 | return $newbody; |
aceb0d5c |
185 | } |
186 | ?> |