Made MIME much more efficient
[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
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 = "";
65 while ((substr(trim($body[$j]), 0, strlen("--$bound")) != "--$bound") && ($j < count($body))) {
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 **/
b1dadc61 119 function formatBody($message) {
7831268e 120 include ("../config/config.php");
121
4809f489 122 /** this if statement checks for the entity to show as the primary message. To
123 add more of them, just put them in the order that is their priority.
124 **/
b1dadc61 125 if (containsType($message, "text", "html", $ent_num)) {
126 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
127 } else if (containsType($message, "text", "plain", $ent_num)) {
128 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
7831268e 129 $body = "<TT>" . nl2br($body) . "</TT>";
4809f489 130 }
131 // add other primary displaying message types here
b1dadc61 132 else {
133 // find any type that's displayable
134 if (containsType($message, "text", "any_type", $ent_num)) {
135 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
7831268e 136 $body = "<TT>" . nl2br($body) . "</TT>";
b1dadc61 137 } else if (containsType($message, "message", "any_type", $ent_num)) {
138 $body = decodeBody($message["ENTITIES"][$ent_num]["BODY"], $message["ENTITIES"][$ent_num]["ENCODING"]);
7831268e 139 $body = "<TT>" . nl2br($body) . "</TT>";
8405ee35 140 }
141 }
142
7831268e 143 $body .= "<BR>";
144
b1dadc61 145 /** Display the ATTACHMENTS: message if there's more than one part **/
146 if (count($message["ENTITIES"]) > 1) {
7831268e 147 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=4 BORDER=0><TR><TD BGCOLOR=\"$color[0]\">";
148 $body .= "<TT><B>ATTACHMENTS:</B></TT>";
149 $body .= "</TD></TR><TR><TD BGCOLOR=\"$color[0]\">";
b1dadc61 150 $num = 0;
151
152 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
153 /** If we've displayed this entity, go to the next one **/
154 if ($ent_num == $i)
155 continue;
156
157 $type0 = strtolower($message["ENTITIES"][$i]["TYPE0"]);
158 $type1 = strtolower($message["ENTITIES"][$i]["TYPE1"]);
159
160 $num++;
7c9499e1 161 $filename = $message["ENTITIES"][$i]["FILENAME"];
b1dadc61 162 if (trim($filename) == "") {
163 $filename = "UNKNOWN_FORMAT_" . time() . $i;
164 $display_filename = "Attachment $i";
165 } else {
166 $display_filename = $filename;
167 }
7c9499e1 168
97be2168 169 $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
170 $id = $message["INFO"]["ID"];
7831268e 171 $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 172 }
7831268e 173 $body .= "</TD></TR></TABLE>";
8405ee35 174 }
d4467150 175 return $body;
176 }
177
4809f489 178
179
180 /** this function decodes the body depending on the encoding type. **/
d4467150 181 function decodeBody($body, $encoding) {
182 $encoding = strtolower($encoding);
7831268e 183
d4467150 184 if ($encoding == "us-ascii") {
185 $newbody = $body; // if only they all were this easy
7831268e 186
d4467150 187 } else if ($encoding == "quoted-printable") {
7831268e 188 echo "$body";
189 $body = ereg_replace("=3D", "=", $body);
190 $body = ereg_replace("=\n", "", $body);
191 $body = ereg_replace("=20", "\n", $body);
192 $newbody= $body;
193
97be2168 194 } else if ($encoding == "base64") {
195 $newbody = base64_decode($body);
7831268e 196
d4467150 197 } else {
198 $newbody = $body;
199 }
200 return $newbody;
aceb0d5c 201 }
202?>