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 | |
66 | if ($type0 == "text") { |
8405ee35 |
67 | // error correcting if they didn't follow RFC standards |
68 | if (trim($type1) == "") |
69 | $type1 = "plain"; |
70 | |
aceb0d5c |
71 | if ($type1 == "plain") { |
aceb0d5c |
72 | for ($p = 0;$p < count($body);$p++) { |
d4467150 |
73 | $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]); |
aceb0d5c |
74 | } |
d4467150 |
75 | } else { |
b1dadc61 |
76 | $msg[0]["BODY"] = $body; |
aceb0d5c |
77 | } |
7c9499e1 |
78 | } else { |
bcb432a3 |
79 | $msg[0]["BODY"][0] = $body; |
aceb0d5c |
80 | } |
81 | |
d4467150 |
82 | return $msg; |
83 | } |
84 | |
b1dadc61 |
85 | function containsType($message, $type0, $type1, &$ent_num) { |
86 | $type0 = strtolower($type0); |
87 | $type1 = strtolower($type1); |
d4467150 |
88 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
b1dadc61 |
89 | /** Check only on type0 **/ |
90 | if ( $type1 == "any_type" ) { |
91 | if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) { |
92 | $ent_num = $i; |
93 | return true; |
94 | } |
95 | |
96 | /** Check on type0 and type1 **/ |
97 | } else { |
98 | if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) { |
99 | $ent_num = $i; |
100 | return true; |
101 | } |
d4467150 |
102 | } |
103 | } |
b1dadc61 |
104 | return false; |
105 | } |
8405ee35 |
106 | |
b1dadc61 |
107 | function formatBody($message) { |
108 | if (containsType($message, "text", "html", $ent_num)) { |
109 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
110 | } else if (containsType($message, "text", "plain", $ent_num)) { |
111 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
112 | } // add other primary displaying message types here |
113 | |
114 | else { |
115 | // find any type that's displayable |
116 | if (containsType($message, "text", "any_type", $ent_num)) { |
117 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
118 | } else if (containsType($message, "message", "any_type", $ent_num)) { |
119 | $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
8405ee35 |
120 | } |
121 | } |
122 | |
b1dadc61 |
123 | |
124 | /** Display the ATTACHMENTS: message if there's more than one part **/ |
125 | if (count($message["ENTITIES"]) > 1) { |
8405ee35 |
126 | $pos = count($body); |
b1dadc61 |
127 | $body[$pos] .= "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>"; |
128 | $num = 0; |
129 | |
130 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
131 | /** If we've displayed this entity, go to the next one **/ |
132 | if ($ent_num == $i) |
133 | continue; |
134 | |
135 | $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]); |
136 | $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]); |
137 | |
138 | $num++; |
7c9499e1 |
139 | $filename = $message["ENTITIES"][$i]["FILENAME"]; |
b1dadc61 |
140 | if (trim($filename) == "") { |
141 | $filename = "UNKNOWN_FORMAT_" . time() . $i; |
142 | $display_filename = "Attachment $i"; |
143 | } else { |
144 | $display_filename = $filename; |
145 | } |
7c9499e1 |
146 | |
b1dadc61 |
147 | $body[$pos] .= "<TT> <A HREF=\"../data/$filename\">" . $display_filename . "</A> <SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>"; |
7c9499e1 |
148 | $file = fopen("../data/$filename", "w"); |
b1dadc61 |
149 | |
150 | /** Determine what encoding type is used **/ |
151 | if ($message["ENTITIES"][$i]["ENCODING"] == "base64") { |
152 | $thefile = base64_decode($message["ENTITIES"][$i]["BODY"][0]); |
153 | } else { |
154 | $thefile = $message["ENTITIES"][$i]["BODY"][0]; |
155 | } |
156 | |
157 | fwrite($file, $thefile); |
7c9499e1 |
158 | fclose($file); |
8405ee35 |
159 | } |
160 | } |
161 | |
d4467150 |
162 | return $body; |
163 | } |
164 | |
165 | function decodeBody($body, $encoding) { |
166 | $encoding = strtolower($encoding); |
167 | if ($encoding == "us-ascii") { |
168 | $newbody = $body; // if only they all were this easy |
169 | } else if ($encoding == "quoted-printable") { |
170 | for ($q=0; $q < count($body); $q++) { |
171 | if (substr(trim($body[$q]), -1) == "=") { |
172 | $body[$q] = trim($body[$q]); |
173 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-1); |
174 | } else if (substr(trim($body[$q]), -3) == "=20") { |
175 | $body[$q] = trim($body[$q]); |
176 | $body[$q] = substr($body[$q], 0, strlen($body[$q])-3); |
177 | $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno |
178 | } |
179 | } |
14d10786 |
180 | for ($q=0;$q < count($body);$q++) { |
181 | $body[$q] = ereg_replace("=3D", "=", $body[$q]); |
182 | } |
d4467150 |
183 | $newbody = $body; |
184 | } else { |
185 | $newbody = $body; |
186 | } |
187 | return $newbody; |
aceb0d5c |
188 | } |
189 | ?> |