Uses the brand new configuration directive email_address to set the
[squirrelmail.git] / functions / mime.php
CommitLineData
aceb0d5c 1<?
2 /** mime.php
3 **
4 ** This contains the functions necessary to detect and decode MIME messages.
5 **/
6
7
1fd97780 8 if (!isset($i18n_php))
9 include "../functions/i18n.php";
10
4809f489 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 **/
4ccd80f8 14 function decodeMime($body, $bound, $type0, $type1, $encoding, &$entities) {
aceb0d5c 15 if ($type0 == "multipart") {
aceb0d5c 16 $bound = trim($bound);
f7835374 17 $i = 0;
4809f489 18 while (($i < count($body)) && (substr($body[$i], 0, strlen("--$bound--")) != "--$bound--")) {
19 if (trim($body[$i]) == "--$bound") {
20 $j = $i+1;
aceb0d5c 21 $p = 0;
22
4809f489 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";
aceb0d5c 29 $j++;
4809f489 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 **/
d29aac0e 38 sqimap_get_entity_header($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
aceb0d5c 39 }
bcb432a3 40
bcb432a3 41
4809f489 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;
bcb432a3 52 } else {
7c9499e1 53 $j++;
54 $entity_body = "";
4809f489 55 while (substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") {
bcb432a3 56 $entity_body .= $body[$j];
57 $j++;
58 }
4809f489 59 $count = count($entities);
60 $entities[$count] = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
bcb432a3 61 }
aceb0d5c 62 }
63 $i++;
64 }
d4467150 65 } else {
7831268e 66 /** If this isn't a multipart message **/
67 $j = 0;
68 $entity_body = "";
78509c54 69 while ($j < count($body)) {
7831268e 70 $entity_body .= $body[$j];
71 $j++;
72 }
73
4809f489 74 $count = count($entities);
7831268e 75 $entities[$count] = getEntity($entity_body, $bound, $type0, $type1, $encoding, $charset, $filename);
d4467150 76 }
77
4809f489 78 return $entities;
d4467150 79 }
80
81 /** This gets one entity's properties **/
7c9499e1 82 function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
4809f489 83 $msg["TYPE0"] = $type0;
84 $msg["TYPE1"] = $type1;
85 $msg["ENCODING"] = $encoding;
86 $msg["CHARSET"] = $charset;
87 $msg["FILENAME"] = $filename;
d4467150 88
7831268e 89 $msg["BODY"] = $body;
aceb0d5c 90
d4467150 91 return $msg;
92 }
93
4809f489 94 /** This will check whether or not the message contains a certain type. It
95 searches through all the entities for a match.
96 **/
b1dadc61 97 function containsType($message, $type0, $type1, &$ent_num) {
98 $type0 = strtolower($type0);
99 $type1 = strtolower($type1);
d4467150 100 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
b1dadc61 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 }
d4467150 114 }
115 }
b1dadc61 116 return false;
117 }
8405ee35 118
4809f489 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 **/
a8648d75 123 function formatBody($message, $color, $wrap_at) {
7831268e 124
4809f489 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 **/
7085fa78 128 $id = $message["INFO"]["ID"];
129 $urlmailbox = urlencode($message["INFO"]["MAILBOX"]);
7085fa78 130
b1dadc61 131 if (containsType($message, "text", "html", $ent_num)) {
a8648d75 132 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
b1dadc61 133 } else if (containsType($message, "text", "plain", $ent_num)) {
a8648d75 134 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
4809f489 135 }
136 // add other primary displaying message types here
b1dadc61 137 else {
138 // find any type that's displayable
139 if (containsType($message, "text", "any_type", $ent_num)) {
a8648d75 140 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
b1dadc61 141 } else if (containsType($message, "message", "any_type", $ent_num)) {
a8648d75 142 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
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")
a8648d75 148 $body = translateText($body, $wrap_at);
149
78509c54 150
9f9d7d28 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>";
7831268e 152
b1dadc61 153 /** Display the ATTACHMENTS: message if there's more than one part **/
154 if (count($message["ENTITIES"]) > 1) {
7831268e 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]\">";
b1dadc61 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++;
7c9499e1 169 $filename = $message["ENTITIES"][$i]["FILENAME"];
b1dadc61 170 if (trim($filename) == "") {
7085fa78 171 $display_filename = "untitled$i";
b1dadc61 172 } else {
173 $display_filename = $filename;
174 }
7c9499e1 175
97be2168 176 $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
177 $id = $message["INFO"]["ID"];
7831268e 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>";
8405ee35 179 }
7831268e 180 $body .= "</TD></TR></TABLE>";
8405ee35 181 }
d4467150 182 return $body;
183 }
184
4809f489 185
186
187 /** this function decodes the body depending on the encoding type. **/
d4467150 188 function decodeBody($body, $encoding) {
189 $encoding = strtolower($encoding);
7831268e 190
ef3f274f 191 if ($encoding == "quoted-printable") {
192 $body = quoted_printable_decode($body);
db87f79c 193
ef3f274f 194 while (ereg("=\n", $body))
195 $body = ereg_replace ("=\n", "", $body);
97be2168 196 } else if ($encoding == "base64") {
ef3f274f 197 $body = base64_decode($body);
d4467150 198 }
ef3f274f 199
200 // All other encodings are returned raw.
201 return $body;
aceb0d5c 202 }
a4c2cd49 203
204
205 // This functions decode strings that is encoded according to
206 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
2e434774 207 function decodeHeader ($string) {
1fd97780 208 if (eregi('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
a4c2cd49 209 $string, $res)) {
1fd97780 210 if (ucfirst($res[2]) == "B") {
211 $replace = base64_decode($res[3]);
a4c2cd49 212 } else {
1fd97780 213 $replace = ereg_replace("_", " ", $res[3]);
a4c2cd49 214 $replace = quoted_printable_decode($replace);
215 }
216
1fd97780 217 $replace = charset_decode ($res[1], $replace);
a4c2cd49 218
219 $string = eregi_replace
1fd97780 220 ('=\?([^?]+)\?(q|b)\?([^?]+)\?=',
a4c2cd49 221 $replace, $string);
2e434774 222 // In case there should be more encoding in the string: recurse
223 return (decodeHeader($string));
a4c2cd49 224 } else
225 return ($string);
226 }
227
9f9d7d28 228?>