Changed so that attachments don't have to be saved on the server.
[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 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
22 while ((substr(trim($body[$j]), 0, strlen($bound)) != $bound) && (trim($body[$j]) != "")) {
23 $entity_header[$p] = $body[$j];
24 $j++;
25 $p++;
26 }
27
28 fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
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 {
37 $j++;
38 $entity_body = "";
39 while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
40 $entity_body .= $body[$j];
41 $j++;
42 }
43 }
44 $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset, $filename);
45
46 $q = count($full_message);
47 $full_message[$q] = $entity[0];
48 }
49 $i++;
50 }
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 **/
59 function getEntity($body, $bound, $type0, $type1, $encoding, $charset, $filename) {
60 $msg[0]["TYPE0"] = $type0;
61 $msg[0]["TYPE1"] = $type1;
62 $msg[0]["ENCODING"] = $encoding;
63 $msg[0]["CHARSET"] = $charset;
64 $msg[0]["FILENAME"] = $filename;
65
66 if ($type0 == "text") {
67 // error correcting if they didn't follow RFC standards
68 if (trim($type1) == "")
69 $type1 = "plain";
70
71 if ($type1 == "plain") {
72 for ($p = 0;$p < count($body);$p++) {
73 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
74 }
75 } else {
76 $msg[0]["BODY"] = $body;
77 }
78 } else {
79 $msg[0]["BODY"][0] = $body;
80 }
81
82 return $msg;
83 }
84
85 function containsType($message, $type0, $type1, &$ent_num) {
86 $type0 = strtolower($type0);
87 $type1 = strtolower($type1);
88 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
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 }
102 }
103 }
104 return false;
105 }
106
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"]);
120 }
121 }
122
123
124 /** Display the ATTACHMENTS: message if there's more than one part **/
125 if (count($message["ENTITIES"]) > 1) {
126 $pos = count($body);
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++;
139 $filename = $message["ENTITIES"][$i]["FILENAME"];
140 if (trim($filename) == "") {
141 $filename = "UNKNOWN_FORMAT_" . time() . $i;
142 $display_filename = "Attachment $i";
143 } else {
144 $display_filename = $filename;
145 }
146
147
148 $urlMailbox = urlencode($message["INFO"]["MAILBOX"]);
149 $id = $message["INFO"]["ID"];
150 $body[$pos] .= "<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>";
151
152 }
153 }
154
155 return $body;
156 }
157
158 function decodeBody($body, $encoding) {
159 $encoding = strtolower($encoding);
160 if ($encoding == "us-ascii") {
161 $newbody = $body; // if only they all were this easy
162 } else if ($encoding == "quoted-printable") {
163 for ($q=0; $q < count($body); $q++) {
164 if (substr(trim($body[$q]), -1) == "=") {
165 $body[$q] = trim($body[$q]);
166 $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
167 } else if (substr(trim($body[$q]), -3) == "=20") {
168 $body[$q] = trim($body[$q]);
169 $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
170 $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
171 }
172 }
173 for ($q=0;$q < count($body);$q++) {
174 $body[$q] = ereg_replace("=3D", "=", $body[$q]);
175 }
176 $newbody = $body;
177 } else if ($encoding == "base64") {
178 $newbody = base64_decode($body);
179 } else {
180 $newbody = $body;
181 }
182 return $newbody;
183 }
184 ?>