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