4347fd50880dd942c796d8d3d684313703649e3f
[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 // error correcting if they didn't follow RFC standards
53 if (trim($type1) == "")
54 $type1 = "plain";
55
56 if ($type1 == "plain") {
57 $msg[0]["PRIORITY"] = 10;
58 for ($p = 0;$p < count($body);$p++) {
59 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
60 }
61 } else if ($type1 == "html") {
62 $msg[0]["PRIORITY"] = 20;
63 $msg[0]["BODY"] = $body;
64 } else {
65 $msg[0]["PRIORITY"] = 1;
66 $msg[0]["BODY"][0] = "This entity is of an unknown format. Doing my best to display anyway...<BR><BR>";
67 for ($p = 1;$p < count($body);$p++) {
68 $q = $p - 1;
69 $msg[0]["BODY"][$p] = $body[$q];
70 }
71 }
72 } else {
73 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>";
74 }
75
76 return $msg;
77 }
78
79 function formatBody($message) {
80 for ($i=0; $i < count($message["ENTITIES"]); $i++) {
81 if ($message["ENTITIES"][$i]["TYPE0"] == "text") {
82 if ($message["ENTITIES"][$i]["PRIORITY"] > $priority)
83 $priority = $message["ENTITIES"][$i]["PRIORITY"];
84 }
85 }
86
87 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
88 switch ($priority) {
89 /** HTML **/
90 case 20: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
91 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "html")) {
92 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
93 }
94 }
95 break;
96 /** PLAIN **/
97 case 10: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
98 if (($message["ENTITIES"][$i]["TYPE0"] == "text") && ($message["ENTITIES"][$i]["TYPE1"] == "plain")) {
99 $body = decodeBody($message["ENTITIES"][$i]["BODY"], $message["ENTITIES"][$i]["ENCODING"]);
100 }
101 }
102 break;
103 /** UNKNOWN...SEND WHAT WE GOT **/
104 case 1: for ($i=0; $i < count($message["ENTITIES"]); $i++) {
105 if (($message["ENTITIES"][$i]["TYPE0"] == "text")) {
106 $pos = count($body);
107 for ($b=0; $b < count($message["ENTITIES"][$i]["BODY"]); $b++) {
108 $pos = $pos + $b;
109 $body[$pos] = $message["ENTITIES"][$i]["BODY"][$b];
110 }
111 }
112 }
113 break;
114 }
115 }
116
117 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
118 $pos = count($body);
119 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
120 $body[$pos] = "<BR><TT><U><B>ATTACHMENTS:</B></U></TT><BR>";
121 }
122 }
123
124 for ($i = 0; $i < count($message["ENTITIES"]); $i++) {
125 $pos = count($body);
126 if ($message["ENTITIES"][$i]["TYPE0"] != "text") {
127 if ($message["ENTITIES"][$i]["TYPE0"] == "image") {
128 $body[$pos] = "<TT>&nbsp;&nbsp;&nbsp;Image: " . strtoupper($message["ENTITIES"][$i]["TYPE1"]) . "</TT><BR>";
129 } else {
130 $body[$pos] = "<TT>&nbsp;&nbsp;&nbsp;Unknown Type: " . $message["ENTITIES"][$i]["TYPE0"] . "/" . $message["ENTITIES"][$i]["TYPE1"] . "</TT><BR>";
131 }
132 }
133 }
134
135 return $body;
136 }
137
138 function decodeBody($body, $encoding) {
139 $encoding = strtolower($encoding);
140 if ($encoding == "us-ascii") {
141 $newbody = $body; // if only they all were this easy
142 } else if ($encoding == "quoted-printable") {
143 for ($q=0; $q < count($body); $q++) {
144 if (substr(trim($body[$q]), -1) == "=") {
145 $body[$q] = trim($body[$q]);
146 $body[$q] = substr($body[$q], 0, strlen($body[$q])-1);
147 } else if (substr(trim($body[$q]), -3) == "=20") {
148 $body[$q] = trim($body[$q]);
149 $body[$q] = substr($body[$q], 0, strlen($body[$q])-3);
150 $body[$q] = "$body[$q]\n"; // maybe should be \n.. dunno
151 }
152 }
153 for ($q=0;$q < count($body);$q++) {
154 $body[$q] = ereg_replace("=3D", "=", $body[$q]);
155 }
156 $newbody = $body;
157 } else {
158 $newbody = $body;
159 }
160 return $newbody;
161 }
162 ?>