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) { |
9 | if ($type0 == "multipart") { |
10 | if ($body[0] == "") |
11 | $i = 1; |
12 | else |
13 | $i = 0; |
14 | |
15 | $bound = trim($bound); |
16 | $bound = "--$bound"; |
17 | while ($i < count($body)) { |
18 | if (trim($body[$i]) == $bound) { |
19 | $j = $i + 1; |
20 | $p = 0; |
21 | |
bcb432a3 |
22 | while ((substr(trim($body[$j]), 0, strlen($bound)) != $bound) && (trim($body[$j]) != "")) { |
23 | $entity_header[$p] = $body[$j]; |
aceb0d5c |
24 | $j++; |
25 | $p++; |
26 | } |
bcb432a3 |
27 | |
7c9499e1 |
28 | fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename); |
bcb432a3 |
29 | |
30 | if ($ent_type0 == "text") { |
31 | while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { |
32 | $entity_body[$p] = $body[$j]; |
33 | $j++; |
34 | $p++; |
35 | } |
36 | } else { |
7c9499e1 |
37 | $j++; |
38 | $entity_body = ""; |
bcb432a3 |
39 | while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) { |
40 | $entity_body .= $body[$j]; |
41 | $j++; |
42 | } |
43 | } |
7c9499e1 |
44 | $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename); |
aceb0d5c |
45 | |
46 | $q = count($full_message); |
d4467150 |
47 | $full_message[$q] = $entity[0]; |
aceb0d5c |
48 | } |
49 | $i++; |
50 | } |
d4467150 |
51 | } else { |
52 | $full_message = getEntity($body, $bound, $type0, $type1); |
53 | } |
54 | |
55 | return $full_message; |
56 | } |
57 | |
58 | /** This gets one entity's properties **/ |
7c9499e1 |
59 | function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) { |
d4467150 |
60 | $msg[0]["TYPE0"] = $type0; |
61 | $msg[0]["TYPE1"] = $type1; |
62 | $msg[0]["ENCODING"] = $encoding; |
63 | $msg[0]["CHARSET"] = $charset; |
7c9499e1 |
64 | $msg[0]["FILENAME"] = $filename; |
d4467150 |
65 | |
7c9499e1 |
66 | echo "$type0 / $type1<BR>"; |
d4467150 |
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 | } |
7c9499e1 |
88 | } else { |
bcb432a3 |
89 | $msg[0]["PRIORITY"] == 5; |
90 | $msg[0]["BODY"][0] = $body; |
aceb0d5c |
91 | } |
92 | |
d4467150 |
93 | return $msg; |
94 | } |
95 | |
96 | function formatBody($message) { |
97 | for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
98 | if ($message["ENTITIES"][$i]["TYPE0"] == "text") { |
99 | if ($message["ENTITIES"][$i]["PRIORITY"] > $priority) |
100 | $priority = $message["ENTITIES"][$i]["PRIORITY"]; |
101 | } |
102 | } |
103 | |
104 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
105 | switch ($priority) { |
106 | /** HTML **/ |
107 | case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
108 | if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) { |
109 | $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); |
110 | } |
111 | } |
112 | break; |
113 | /** PLAIN **/ |
114 | case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
115 | if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) { |
116 | $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]); |
117 | } |
118 | } |
119 | break; |
120 | /** UNKNOWN...SEND WHAT WE GOT **/ |
121 | case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++) { |
122 | if (($message["ENTITIES"][$i]["TYPE0"] == "text")) { |
123 | $pos = count($body); |
124 | for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++) { |
125 | $pos = $pos + $b; |
126 | $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b]; |
127 | } |
128 | } |
129 | } |
130 | break; |
131 | } |
132 | } |
8405ee35 |
133 | |
134 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
135 | $pos = count($body); |
136 | if ($message["ENTITIES"][$i]["TYPE0"] != "text") { |
137 | $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>"; |
7c9499e1 |
138 | $i = count($message["ENTITIES"]); |
8405ee35 |
139 | } |
140 | } |
141 | |
142 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
143 | $pos = count($body); |
7c9499e1 |
144 | if (($message["ENTITIES"][$i]["TYPE0"] == "image") || ($message["ENTITIES"][$i]["TYPE0"] == "application")){ |
145 | $filename = $message["ENTITIES"][$i]["FILENAME"]; |
146 | $body[$pos] = "<TT> <A HREF=\"../data/$filename\">" . $filename . "</A></TT><BR>"; |
147 | |
148 | $file = fopen("../data/$filename", "w"); |
149 | $image = base64_decode($message["ENTITIES"][$i]["BODY"][0]); |
150 | fwrite($file, $image); |
151 | fclose($file); |
8405ee35 |
152 | } |
153 | } |
154 | |
d4467150 |
155 | return $body; |
156 | } |
157 | |
158 | function decodeBody($body, $encoding) { |
159 | $encoding = strtolower($encoding); |
160 | if ($encoding == "us-ascii") { |
161 | $newbody = $body; // if only they all were this easy |
162 | } else if ($encoding == "quoted-printable") { |
163 | for ($q=0; $q < count($body); $q++) { |
164 | if (substr(trim($body[$q]), -1) == "=") { |
165 | $body[$q] = trim($body[$q]); |
166 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-1); |
167 | } else if (substr(trim($body[$q]), -3) == "=20") { |
168 | $body[$q] = trim($body[$q]); |
169 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-3); |
170 | $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno |
171 | } |
172 | } |
14d10786 |
173 | for ($q=0;$q < count($body);$q++) { |
174 | $body[$q] = ereg_replace("=3D", "=", $body[$q]); |
175 | } |
d4467150 |
176 | $newbody = $body; |
177 | } else { |
178 | $newbody = $body; |
179 | } |
180 | return $newbody; |
aceb0d5c |
181 | } |
182 | ?> |