aabd9cd88ec5ea5f2faad34127325f6fe77cf212
[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 echo "$type0/$type1<BR>";
10 if ($type0 == "multipart") {
11 if ($body[0] == "")
12 $i = 1;
13 else
14 $i = 0;
15
16 $bound = trim($bound);
17 $bound = "--$bound";
18 while ($i < count($body)) {
19 if (trim($body[$i]) == $bound) {
20 $j = $i + 1;
21 $p = 0;
22
23 while (substr(trim($body[$j]), 0, strlen($bound)) != $bound) {
24 $entity_body[$p] = $body[$j];
25 $j++;
26 $p++;
27 }
28 fetchEntityHeader($imapConnection, $entity_body, $ent_type0, $ent_type1, $ent_bound, &$encoding, &$charset);
29 $entity = getEntity($entity_body, $ent_bound, $ent_type0, $ent_type1, $encoding, $charset);
30
31 $q = count($full_message);
32 $full_message[$q] = $entity[0];
33 }
34 $i++;
35 }
36 } else {
37 $full_message = getEntity($body, $bound, $type0, $type1);
38 }
39
40 return $full_message;
41 }
42
43 /** This gets one entity's properties **/
44 function getEntity($body, $bound, $type0, $type1, $encoding, $charset) {
45 echo "$type0/$type1<BR>";
46 $msg[0]["TYPE0"] = $type0;
47 $msg[0]["TYPE1"] = $type1;
48 $msg[0]["ENCODING"] = $encoding;
49 $msg[0]["CHARSET"] = $charset;
50
51 if ($type0 == "text") {
52 if ($type1 == "plain") {
53 $msg[0]["PRIORITY"] = 10;
54 for ($p = 0;$p < count($body);$p++) {
55 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
56 }
57 } else if ($type1 == "html") {
58 $msg[0]["PRIORITY"] = 20;
59 $msg[0]["BODY"] = $body;
60 } else {
61 $msg[0]["PRIORITY"] = 1;
62 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is an unknown text format: $type0/$type1</FONT></B>";
63 }
64 } else if ($type0 == "image") {
65 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This is an image. Squirrelmail currently cannot decode images.</FONT></B>";
66 } else {
67 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>";
68 }
69
70 return $msg;
71 }
72
73 function formatBody($message) {
74 for ($i=0; $i < count($message["ENTITIES"]); $i++) {
75 if ($message["ENTITIES"][$i]["TYPE0"] == "text") {
76 if ($message["ENTITIES"][$i]["PRIORITY"] > $priority)
77 $priority = $message["ENTITIES"][$i]["PRIORITY"];
78 }
79 }
80
81 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
82 switch ($priority) {
83 /** HTML **/
84 case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
85 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) {
86 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
87 }
88 }
89 break;
90 /** PLAIN **/
91 case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
92 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) {
93 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
94 }
95 }
96 break;
97 /** UNKNOWN...SEND WHAT WE GOT **/
98 case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
99 if (($message["ENTITIES"][$i]["TYPE0"] == "text")) {
100 $pos = count($body);
101 for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++) {
102 $pos = $pos + $b;
103 $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b];
104 }
105 }
106 }
107 break;
108 }
109 }
110 return $body;
111 }
112
113 function decodeBody($body, $encoding) {
114 $encoding = strtolower($encoding);
115 if ($encoding == "us-ascii") {
116 $newbody = $body; // if only they all were this easy
117 } else if ($encoding == "quoted-printable") {
118 for ($q=0; $q < count($body); $q++) {
119 if (substr(trim($body[$q]), -1) == "=") {
120 $body[$q] = trim($body[$q]);
121 $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
122 } else if (substr(trim($body[$q]), -3) == "=20") {
123 $body[$q] = trim($body[$q]);
124 $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
125 $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
126 }
127 }
128 for ($q=0;$q < count($body);$q++) {
129 $body[$q] = ereg_replace("=3D", "=", $body[$q]);
130 }
131 $newbody = $body;
132 } else {
133 $newbody = $body;
134 }
135 return $newbody;
136 }
137 ?>