7054b41fdebd7b6a920d780abc98ba87a81f9811
[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 echo "$type0 / $type1<BR>";
67 if ($type0 == "text") {
68 // error correcting if they didn't follow RFC standards
69 if (trim($type1) == "")
70 $type1 = "plain";
71
72 if ($type1 == "plain") {
73 $msg[0]["PRIORITY"] = 10;
74 for ($p = 0;$p < count($body);$p++) {
75 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
76 }
77 } else if ($type1 == "html") {
78 $msg[0]["PRIORITY"] = 20;
79 $msg[0]["BODY"] = $body;
80 } else {
81 $msg[0]["PRIORITY"] = 1;
82 $msg[0]["BODY"][0] = "This entity is of an unknown format. Doing my best to display anyway...<BR><BR>";
83 for ($p = 1;$p < count($body);$p++) {
84 $q = $p - 1;
85 $msg[0]["BODY"][$p] = $body[$q];
86 }
87 }
88 } else {
89 $msg[0]["PRIORITY"] == 5;
90 $msg[0]["BODY"][0] = $body;
91 }
92
93 return $msg;
94 }
95
96 function formatBody($message) {
97 for ($i=0; $i < count($message["ENTITIES"]); $i++) {
98 if ($message["ENTITIES"][$i]["TYPE0"] == "text") {
99 if ($message["ENTITIES"][$i]["PRIORITY"] > $priority)
100 $priority = $message["ENTITIES"][$i]["PRIORITY"];
101 }
102 }
103
104 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
105 switch ($priority) {
106 /** HTML **/
107 case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
108 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) {
109 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
110 }
111 }
112 break;
113 /** PLAIN **/
114 case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
115 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) {
116 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
117 }
118 }
119 break;
120 /** UNKNOWN...SEND WHAT WE GOT **/
121 case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
122 if (($message["ENTITIES"][$i]["TYPE0"] == "text")) {
123 $pos = count($body);
124 for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++) {
125 $pos = $pos + $b;
126 $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b];
127 }
128 }
129 }
130 break;
131 }
132 }
133
134 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
135 $pos = count($body);
136 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
137 $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
138 $i = count($message["ENTITIES"]);
139 }
140 }
141
142 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
143 $pos = count($body);
144 if (($message["ENTITIES"][$i]["TYPE0"] == "image") || ($message["ENTITIES"][$i]["TYPE0"] == "application")){
145 $filename = $message["ENTITIES"][$i]["FILENAME"];
146 $body[$pos] = "<TT>&nbsp;&nbsp;&nbsp;<A HREF=\"../data/$filename\">" . $filename . "</A></TT><BR>";
147
148 $file = fopen("../data/$filename", "w");
149 $image = base64_decode($message["ENTITIES"][$i]["BODY"][0]);
150 fwrite($file, $image);
151 fclose($file);
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 {
178 $newbody = $body;
179 }
180 return $newbody;
181 }
182 ?>