aceb0d5c |
1 | <? |
2 | /** mime.php |
3 | ** |
4 | ** This contains the functions necessary to detect and decode MIME messages. |
5 | **/ |
6 | |
7 | |
4809f489 |
8 | /** This is the first function called. It decides if this is a multipart |
9 | message or if it should be handled as a single entity |
10 | **/ |
11 | function decodeMime($body, $bound, $type0, $type1, &$entities) { |
aceb0d5c |
12 | if ($type0 == "multipart") { |
aceb0d5c |
13 | $bound = trim($bound); |
4809f489 |
14 | while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) { |
15 | if (trim($body[$i]) == "--$bound") { |
16 | $j = $i+1; |
aceb0d5c |
17 | $p = 0; |
18 | |
4809f489 |
19 | /** Lets find the header for this entity **/ |
20 | /** If the first line after the boundary is blank, we use default values **/ |
21 | if (trim($body[$j]) == "") { |
22 | $ent_type0 = "text"; |
23 | $ent_type1 = "plain"; |
24 | $charset = "us-ascii"; |
aceb0d5c |
25 | $j++; |
4809f489 |
26 | /** If the first line ISNT blank, read in the header for this entity **/ |
27 | } else { |
28 | while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && (trim($body[$j]) != "")) { |
29 | $entity_header[$p] = $body[$j]; |
30 | $j++; |
31 | $p++; |
32 | } |
33 | /** All of these values are getting passed back to us **/ |
34 | fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename); |
aceb0d5c |
35 | } |
bcb432a3 |
36 | |
bcb432a3 |
37 | |
4809f489 |
38 | /** OK, we have the header information, now lets decide what to do with it **/ |
39 | if ($ent_type0 == "multipart") { |
40 | $y = 0; |
41 | while (substr($body[$j], 0, strlen("--$bound--")) != "--$bound--") { |
42 | $ent_body[$y] = $body[$j]; |
43 | $y++; |
44 | $j++; |
45 | } |
46 | $ent = decodeMime($ent_body, $ent_bound, $ent_type0, $ent_type1, $entities); |
47 | $entities = $ent; |
bcb432a3 |
48 | } else { |
7c9499e1 |
49 | $j++; |
50 | $entity_body = ""; |
4809f489 |
51 | while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") { |
bcb432a3 |
52 | $entity_body .= $body[$j]; |
53 | $j++; |
54 | } |
4809f489 |
55 | $count = count($entities); |
56 | $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename); |
bcb432a3 |
57 | } |
aceb0d5c |
58 | } |
59 | $i++; |
60 | } |
d4467150 |
61 | } else { |
7831268e |
62 | /** If this isn't a multipart message **/ |
63 | $j = 0; |
64 | $entity_body = ""; |
78509c54 |
65 | while ($j < count($body)) { |
7831268e |
66 | $entity_body .= $body[$j]; |
67 | $j++; |
68 | } |
69 | |
4809f489 |
70 | $count = count($entities); |
7831268e |
71 | $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename); |
d4467150 |
72 | } |
73 | |
4809f489 |
74 | return $entities; |
d4467150 |
75 | } |
76 | |
77 | /** This gets one entity's properties **/ |
7c9499e1 |
78 | function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) { |
4809f489 |
79 | $msg["TYPE0"] = $type0; |
80 | $msg["TYPE1"] = $type1; |
81 | $msg["ENCODING"] = $encoding; |
82 | $msg["CHARSET"] = $charset; |
83 | $msg["FILENAME"] = $filename; |
d4467150 |
84 | |
7831268e |
85 | $msg["BODY"] = $body; |
aceb0d5c |
86 | |
d4467150 |
87 | return $msg; |
88 | } |
89 | |
4809f489 |
90 | /** This will check whether or not the message contains a certain type. It |
91 | searches through all the entities for a match. |
92 | **/ |
b1dadc61 |
93 | function containsType($message, $type0, $type1, &$ent_num) { |
94 | $type0 = strtolower($type0); |
95 | $type1 = strtolower($type1); |
d4467150 |
96 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
b1dadc61 |
97 | /** Check only on type0 **/ |
98 | if ( $type1 == "any_type" ) { |
99 | if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) ) { |
100 | $ent_num = $i; |
101 | return true; |
102 | } |
103 | |
104 | /** Check on type0 and type1 **/ |
105 | } else { |
106 | if ( ($message["ENTITIES"][$i]["TYPE0"] == $type0) && ($message["ENTITIES"][$i]["TYPE1"] == $type1) ) { |
107 | $ent_num = $i; |
108 | return true; |
109 | } |
d4467150 |
110 | } |
111 | } |
b1dadc61 |
112 | return false; |
113 | } |
8405ee35 |
114 | |
4809f489 |
115 | /** This returns a parsed string called $body. That string can then be displayed |
116 | as the actual message in the HTML. It contains everything needed, including |
117 | HTML Tags, Attachments at the bottom, etc. |
118 | **/ |
d3cdb279 |
119 | function formatBody($message, $color) { |
7831268e |
120 | |
4809f489 |
121 | /** this if statement checks for the entity to show as the primary message. To |
122 | add more of them, just put them in the order that is their priority. |
123 | **/ |
7085fa78 |
124 | $id = $message["INFO"]["ID"]; |
125 | $urlmailbox = urlencode($message["INFO"]["MAILBOX"]); |
126 | $body = ""; |
127 | |
b1dadc61 |
128 | if (containsType($message, "text", "html", $ent_num)) { |
7085fa78 |
129 | $body .= decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
b1dadc61 |
130 | } else if (containsType($message, "text", "plain", $ent_num)) { |
7085fa78 |
131 | $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
d3cdb279 |
132 | $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>"; |
4809f489 |
133 | } |
134 | // add other primary displaying message types here |
b1dadc61 |
135 | else { |
136 | // find any type that's displayable |
137 | if (containsType($message, "text", "any_type", $ent_num)) { |
7085fa78 |
138 | $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
d3cdb279 |
139 | $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>"; |
b1dadc61 |
140 | } else if (containsType($message, "message", "any_type", $ent_num)) { |
7085fa78 |
141 | $tmpbody = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]); |
d3cdb279 |
142 | $body .= "<TT>" . nl2br(trim($tmpbody)) . "</TT>"; |
8405ee35 |
143 | } |
144 | } |
145 | |
78509c54 |
146 | /** If there are other types that shouldn't be formatted, add them here **/ |
147 | if ($message["ENTITIES"][$ent_num]["TYPE1"] != "html") |
148 | $body = translateText($body); |
149 | |
7085fa78 |
150 | $body .= "<BR><SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox\">Download this as a file</A></CENTER><BR></SMALL>"; |
7831268e |
151 | |
b1dadc61 |
152 | /** Display the ATTACHMENTS: message if there's more than one part **/ |
153 | if (count($message["ENTITIES"]) > 1) { |
7831268e |
154 | $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">"; |
155 | $body .= "<TT><B>ATTACHMENTS:</B></TT>"; |
156 | $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">"; |
b1dadc61 |
157 | $num = 0; |
158 | |
159 | for ($i = 0; $i < count($message["ENTITIES"]); $i++) { |
160 | /** If we've displayed this entity, go to the next one **/ |
161 | if ($ent_num == $i) |
162 | continue; |
163 | |
164 | $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]); |
165 | $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]); |
166 | |
167 | $num++; |
7c9499e1 |
168 | $filename = $message["ENTITIES"][$i]["FILENAME"]; |
b1dadc61 |
169 | if (trim($filename) == "") { |
7085fa78 |
170 | $display_filename = "untitled$i"; |
b1dadc61 |
171 | } else { |
172 | $display_filename = $filename; |
173 | } |
7c9499e1 |
174 | |
97be2168 |
175 | $urlMailbox = urlencode($message["INFO"]["MAILBOX"]); |
176 | $id = $message["INFO"]["ID"]; |
7831268e |
177 | $body .= "<TT> <A HREF=\"../src/download.php?passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$i\">" . $display_filename . "</A> <SMALL>(TYPE: $type0/$type1)</SMALL></TT><BR>"; |
8405ee35 |
178 | } |
7831268e |
179 | $body .= "</TD></TR></TABLE>"; |
8405ee35 |
180 | } |
d4467150 |
181 | return $body; |
182 | } |
183 | |
4809f489 |
184 | |
185 | |
186 | /** this function decodes the body depending on the encoding type. **/ |
d4467150 |
187 | function decodeBody($body, $encoding) { |
188 | $encoding = strtolower($encoding); |
7831268e |
189 | |
d4467150 |
190 | if ($encoding == "us-ascii") { |
191 | $newbody = $body; // if only they all were this easy |
7831268e |
192 | |
d4467150 |
193 | } else if ($encoding == "quoted-printable") { |
7831268e |
194 | echo "$body"; |
195 | $body = ereg_replace("=3D", "=", $body); |
196 | $body = ereg_replace("=\n", "", $body); |
197 | $body = ereg_replace("=20", "\n", $body); |
198 | $newbody= $body; |
199 | |
97be2168 |
200 | } else if ($encoding == "base64") { |
201 | $newbody = base64_decode($body); |
7831268e |
202 | |
d4467150 |
203 | } else { |
204 | $newbody = $body; |
205 | } |
206 | return $newbody; |
aceb0d5c |
207 | } |
208 | ?> |