Added BUG and TODO to reposatory
[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
8 function decodeMime($body, $bound, $type0, $type1) {
8405ee35 9// echo "$type0/$type1<BR>";
aceb0d5c 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) {
d4467150 24 $entity_body[$p] = $body[$j];
aceb0d5c 25 $j++;
26 $p++;
27 }
d4467150 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);
aceb0d5c 30
31 $q = count($full_message);
d4467150 32 $full_message[$q] = $entity[0];
aceb0d5c 33 }
34 $i++;
35 }
d4467150 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) {
8405ee35 45// echo "--$type0/$type1--<BR>";
d4467150 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") {
8405ee35 52 // error correcting if they didn't follow RFC standards
53 if (trim($type1) == "")
54 $type1 = "plain";
55
aceb0d5c 56 if ($type1 == "plain") {
d4467150 57 $msg[0]["PRIORITY"] = 10;
aceb0d5c 58 for ($p = 0;$p < count($body);$p++) {
d4467150 59 $msg[0]["BODY"][$p] = parsePlainTextMessage($body[$p]);
aceb0d5c 60 }
aceb0d5c 61 } else if ($type1 == "html") {
d4467150 62 $msg[0]["PRIORITY"] = 20;
63 $msg[0]["BODY"] = $body;
64 } else {
65 $msg[0]["PRIORITY"] = 1;
0f1835f3 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 }
aceb0d5c 71 }
d4467150 72 } else {
73 $msg[0]["BODY"][0] = "<B><FONT COLOR=DD0000>This attachment is of an unknown format: $type0/$type1</FONT></B>";
aceb0d5c 74 }
75
d4467150 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 }
8405ee35 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
d4467150 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 }
14d10786 153 for ($q=0;$q < count($body);$q++) {
154 $body[$q] = ereg_replace("=3D", "=", $body[$q]);
155 }
d4467150 156 $newbody = $body;
157 } else {
158 $newbody = $body;
159 }
160 return $newbody;
aceb0d5c 161 }
162?>