Added BASIC mime support.
[squirrelmail.git] / functions / mailbox.php
CommitLineData
3302d0d4 1<?
2 /**
a09387f4 3 ** mailbox.php
3302d0d4 4 **
5 ** This contains functions that request information about a mailbox. Including
6 ** reading and parsing headers, getting folder information, etc.
7 **
8 **/
9
10 function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
11 // select mailbox
12 fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n");
13 $read = fgets($imapConnection, 1024);
ff89ac8a 14 $unseen = false;
3302d0d4 15 while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
16 if (substr(Chop($read), -6) == "EXISTS") {
17 $array = explode(" ", $read);
18 $numberOfMessages = $array[1];
19 }
20 $read = fgets($imapConnection, 1024);
21 }
22 }
23
ff89ac8a 24 function unseenMessages($imapConnection, &$numUnseen) {
25 fputs($imapConnection, "1 SEARCH UNSEEN NOT DELETED\n");
26 $read = fgets($imapConnection, 1024);
27 $unseen = false;
28
29 if (strlen($read) > 10) {
30 $unseen = true;
31 $ary = explode(" ", $read);
32 $numUnseen = count($ary) - 2;
33 }
34 else {
35 $unseen = false;
36 $numUnseen = 0;
37 }
38
39 $read = fgets($imapConnection, 1024);
40 return $unseen;
41 }
42
0e919368 43 /** This function sends a request to the IMAP server for headers, 50 at a time
44 ** until $end is reached. I originally had it do them all at one time, but found
45 ** it slightly faster to do it this way.
46 **
47 ** Originally we had getMessageHeaders get the headers for one message at a time.
48 ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800
49 ** messages) to about 3.5 seconds.
50 **/
3e054c43 51 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
3e054c43 52 $rel_start = $start;
3e054c43 53 if (($start > $end) || ($start < 1)) {
54 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
55 exit;
56 }
3302d0d4 57
e550d551 58 $pos = 0;
3e054c43 59 while ($rel_start <= $end) {
60 if ($end - $rel_start > 50) {
e550d551 61 $rel_end = $rel_start + 49;
3e054c43 62 } else {
63 $rel_end = $end;
64 }
aceb0d5c 65 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date To Cc)\n");
3302d0d4 66 $read = fgets($imapConnection, 1024);
3e054c43 67
3e054c43 68 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
e550d551 69
3e054c43 70 if (substr($read, 0, 5) == "From:") {
71 $read = ereg_replace("<", "EMAILSTART--", $read);
72 $read = ereg_replace(">", "--EMAILEND", $read);
e550d551 73 $from[$pos] = substr($read, 5, strlen($read) - 6);
3e054c43 74 }
75 else if (substr($read, 0, 5) == "Date:") {
df978e80 76 $read = ereg_replace("<", "&lt;", $read);
77 $read = ereg_replace(">", "&gt;", $read);
e550d551 78 $date[$pos] = substr($read, 5, strlen($read) - 6);
3e054c43 79 }
80 else if (substr($read, 0, 8) == "Subject:") {
df978e80 81 $read = ereg_replace("<", "&lt;", $read);
82 $read = ereg_replace(">", "&gt;", $read);
e550d551 83 $subject[$pos] = substr($read, 8, strlen($read) - 9);
84 if (strlen(Chop($subject[$pos])) == 0)
85 $subject[$pos] = "(no subject)";
86 }
87 else if (substr($read, 0, 1) == ")") {
88 if ($subject[$pos] == "")
89 $subject[$pos] = "(no subject)";
90 else if ($from[$pos] == "")
91 $from[$pos] = "(unknown sender)";
92 else if ($date[$pos] == "")
93 $from[$pos] = gettimeofday();
94
95 $pos++;
3e054c43 96 }
e550d551 97
3e054c43 98 $read = fgets($imapConnection, 1024);
99 }
100 $rel_start = $rel_start + 50;
3302d0d4 101 }
102 }
103
61a4ac35 104 function setMessageFlag($imapConnection, $i, $q, $flag) {
105 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
aa4c3749 106 }
107
0e919368 108 /** This function gets the flags for message $j. It does only one message at a
109 ** time, rather than doing groups of messages (like getMessageHeaders does).
110 ** I found it one or two seconds quicker (on a box of 800 messages) to do it
111 ** individually. I'm not sure why it happens like that, but that's what my
112 ** testing found. Perhaps later I will be proven wrong and this will change.
113 **/
3e054c43 114 function getMessageFlags($imapConnection, $j, &$flags) {
3302d0d4 115 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
3e054c43 116 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
aa4c3749 117 $read = fgets($imapConnection, 1024);
3e054c43 118 $count = 0;
3302d0d4 119 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
120 if (strpos($read, "FLAGS")) {
121 $read = ereg_replace("\(", "", $read);
122 $read = ereg_replace("\)", "", $read);
123 $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
124 $read = trim($read);
125 $flags = explode(" ", $read);;
54a8ae32 126 $s = 0;
127 while ($s < count($flags)) {
128 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
129 $s++;
3302d0d4 130 }
131 } else {
132 $flags[0] = "None";
133 }
3e054c43 134 $count++;
3302d0d4 135 $read = fgets($imapConnection, 1024);
136 }
137 }
138
31f3d7c0 139 function decodeEmailAddr($sender) {
140 $emailAddr = getEmailAddr($sender);
40884065 141 if (strpos($emailAddr, "EMAILSTART--")) {
31f3d7c0 142
40884065 143 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
144 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
145 } else {
146 $emailAddr = $emailAddr;
147 }
31f3d7c0 148 return $emailAddr;
149 }
150
3302d0d4 151 function getEmailAddr($sender) {
152 if (strpos($sender, "EMAILSTART--") == false)
40884065 153 return "$sender";
3302d0d4 154
40884065 155 $emailStart = strpos($sender, "EMAILSTART--") + 12;
156 $emailAddr = substr($sender, $emailStart, strlen($sender));
157 $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND"));
3302d0d4 158
159 return $emailAddr;
160 }
161
162 function getSender($sender) {
163 if (strpos($sender, "EMAILSTART--") == false)
40884065 164 return "$sender";
3302d0d4 165
166 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
167 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
40884065 168 return "$first $second";
3302d0d4 169 }
170
171 function getSenderName($sender) {
172 $name = getSender($sender);
173 $emailAddr = getEmailAddr($sender);
174 $emailStart = strpos($emailAddr, "EMAILSTART--");
175 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
176
177 if (($emailAddr == "") && ($name == "")) {
178 $from = $sender;
179 }
180 else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
181 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
182 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
183 $from = $emailAddr;
184 }
185 else if (strlen($name) > 0) {
186 $from = $name;
187 }
188 else if (strlen($emailAddr > 0)) {
189 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
190 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
191 $from = $emailAddr;
192 }
193
194 $from = trim($from);
195
196 // strip out any quotes if they exist
197 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
198 $from = substr($from, 1, strlen($from) - 2);
199
200 return $from;
201 }
aa4c3749 202
203 /** returns "true" if the copy was completed successfully.
204 ** returns "false" with an error message if unsuccessful.
205 **/
206 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
207 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
208 $read = fgets($imapConnection, 1024);
209 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
210 $read = fgets($imapConnection, 1024);
211 }
212
213 if (substr($read, 0, 15) == "mailboxStore NO") {
aa4c3749 214 return false;
215 } else if (substr($read, 0, 15) == "mailboxStore OK") {
216 return true;
217 }
218
219 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
220 return false;
221 }
61a4ac35 222
223 /** expunges a mailbox **/
224 function expungeBox($imapConnection, $mailbox) {
225 selectMailbox($imapConnection, $mailbox, $num);
226 fputs($imapConnection, "1 EXPUNGE\n");
227 }
8c7dfc99 228
229 function getFolderNameMinusINBOX($mailbox) {
230 if (substr($mailbox, 0, 6) == "INBOX.")
231 $box = substr($mailbox, 6, strlen($mailbox));
232 else
233 $box = $mailbox;
234
235 return $box;
236 }
05207a68 237
aceb0d5c 238 /** This function gets all the information about a message. Including Header and body **/
239 function fetchMessage($imapConnection, $id) {
240 $message["HEADER"] = fetchHeader($imapConnection, $id);
241 $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE"][0], $message["HEADER"]["TYPE"][1]);
242
243 return $message;
244 }
245
246 function fetchHeader($imapConnection, $id) {
247 fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER.LINES (From Subject Date To Cc Content-Type MIME-Version)\n");
248 $read = fgets($imapConnection, 1024);
249
250 /** defaults... if the don't get overwritten, it will display text **/
251 $header["TYPE"][0] = "text";
252 $header["TYPE"][1] = "plain";
253 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
254 /** MIME-VERSION **/
255 if (substr($read, 0, 17) == "MIME-Version: 1.0") {
256 $header["MIME"] = true;
257 $read = fgets($imapConnection, 1024);
258 }
259 /** CONTENT-TYPE **/
260 else if (substr($read, 0, 13) == "Content-Type:") {
261 $cont = trim(substr($read, 13));
262 $cont = substr($cont, 0, strpos($cont, ";"));
263 $header["TYPE"][0] = substr($cont, 0, strpos($cont, "/"));
264 $header["TYPE"][1] = substr($cont, strpos($cont, "/")+1);
265
266 $read = fgets($imapConnection, 1024);
267 if (substr(strtolower(trim($read)), 0, 9) == "boundary=") {
268 $bound = trim($read);
269 $bound = substr($bound, 9);
270 $bound = str_replace("\"", "", $bound);
271 $header["BOUNDARY"] = $bound;
272 $read = fgets($imapConnection, 1024);
273 }
274 }
275 /** FROM **/
276 else if (substr($read, 0, 5) == "From:") {
277 $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
278 $read = fgets($imapConnection, 1024);
279 }
280 /** DATE **/
281 else if (substr($read, 0, 5) == "Date:") {
282 $d = substr($read, 5, strlen($read) - 6);
283 $d = trim($d);
284 $d = ereg_replace(" ", " ", $d);
285 $d = explode(" ", $d);
286 $header["DATE"] = getTimeStamp($d);
287 $read = fgets($imapConnection, 1024);
288 }
289 /** SUBJECT **/
290 else if (substr($read, 0, 8) == "Subject:") {
291 $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
292 if (strlen(Chop($header["SUBJECT"])) == 0)
293 $header["SUBJECT"] = "(no subject)";
294 $read = fgets($imapConnection, 1024);
295 }
296 /** CC **/
297 else if (substr($read, 0, 3) == "CC:") {
298 $pos = 0;
299 $header["CC"][$pos] = trim(substr($read, 4));
300 $read = fgets($imapConnection, 1024);
301 while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
302 $pos++;
303 $header["CC"][$pos] = trim($read);
304 $read = fgets($imapConnection, 1024);
305 }
306 }
307 /** TO **/
308 else if (substr($read, 0, 3) == "To:") {
309 $pos = 0;
310 $header["TO"][$pos] = trim(substr($read, 4));
311 $read = fgets($imapConnection, 1024);
312 while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
313 $pos++;
314 $header["TO"][$pos] = trim($read);
315 $read = fgets($imapConnection, 1024);
316 }
317 }
318
319 /** ERROR CORRECTION **/
320 else if (substr($read, 0, 1) == ")") {
321 if ($header["SUBJECT"] == "")
322 $header["SUBJECT"] = "(no subject)";
323
324 if ($header["FROM"] == "")
325 $header["FROM"] = "(unknown sender)";
326
327 if ($header["DATE"] == "")
328 $header["DATE"] = time();
329 $read = fgets($imapConnection, 1024);
330 }
331 else {
332 $read = fgets($imapConnection, 1024);
333 }
334 }
335 return $header;
336 }
337
338 function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
339 /** This first part reads in the full body of the message **/
05207a68 340 fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
aceb0d5c 341 $read = fgets($imapConnection, 1024);
342
05207a68 343 $count = 0;
aceb0d5c 344 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
345 $body[$count] = $read;
05207a68 346 $count++;
aceb0d5c 347
348 $read = fgets($imapConnection, 1024);
05207a68 349 }
350
aceb0d5c 351 /** this deletes the first line, and the last two (imap stuff we ignore) **/
e550d551 352 $i = 0;
353 $j = 0;
aceb0d5c 354 while ($i < count($body)) {
355 if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
356 $bodytmp[$j] = $body[$i];
e550d551 357 $j++;
358 }
359 $i++;
360 }
aceb0d5c 361 $body = $bodytmp;
e550d551 362
aceb0d5c 363 /** Now, lets work out the MIME stuff **/
364 /** (needs mime.php included) **/
365 return decodeMime($body, $bound, $type0, $type1);
366 }
05207a68 367
aceb0d5c 368 function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound) {
369 /** defaults... if the don't get overwritten, it will display text **/
370 $type0 = "text";
371 $type1 = "plain";
372 $i = 0;
373 while (trim($read[$i]) != "") {
374 if (substr($read[$i], 0, 13) == "Content-Type:") {
375 $cont = trim(substr($read[$i], 13));
376 $cont = substr($cont, 0, strpos($cont, ";"));
377 $type0 = substr($cont, 0, strpos($cont, "/"));
378 $type1 = substr($cont, strpos($cont, "/")+1);
379
380 if (substr(strtolower(trim($read[$i])), 0, 9) == "boundary=") {
381 $bound = trim($read[$i]);
382 $bound = substr($bound, 9);
383 $bound = str_replace("\"", "", $bound);
384 }
e550d551 385 }
aceb0d5c 386 $i++;
387 }
05207a68 388
aceb0d5c 389 /** remove the header from the entity **/
390 $i = 0;
391 while (trim($read[$i]) != "") {
392 $i++;
e550d551 393 }
aceb0d5c 394 $i++;
05207a68 395
aceb0d5c 396 for ($p = 0; $i < count($read); $p++) {
397 $entity[$p] = $read[$i];
398 $i++;
399 }
400
401 $read = $entity;
e550d551 402 }
05207a68 403
aceb0d5c 404 function parsePlainTextMessage($line) {
e550d551 405 $line = "^^$line";
05207a68 406
e550d551 407 if ((strpos(strtolower($line), "<!") == false) &&
408 (strpos(strtolower($line), "<html>") == false) &&
409 (strpos(strtolower($line), "</html>") == false)) {
410 $line = str_replace("<", "&lt;", $line);
411 $line = str_replace(">", "&gt;", $line);
05207a68 412 }
413
ff89ac8a 414 $wrap_at = 86; // Make this configurable int the config file some time
8467bf00 415 if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
416 $line = wordWrap($line, $wrap_at);
b8ea4ed6 417
e550d551 418 $line = str_replace(" ", "&nbsp;", $line);
419 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
420
421 /** if >> or > are found at the beginning of a line, I'll assume that was
422 replied text, so make it different colors **/
423 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
424 $line = substr($line, 2, strlen($line));
425 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
426 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
427 $line = substr($line, 2, strlen($line));
428 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
429 } else {
430 $line = substr($line, 2, strlen($line));
431 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
432 }
433
434 /** This translates "http://" into a link. It could be made better to accept
435 "www" and "mailto" also. That should probably be added later. **/
436 if (strpos(strtolower($line), "http://") != false) {
9774bdef 437 $line = ereg_replace("<BR>", "", $line);
e550d551 438 $start = strpos(strtolower($line), "http://");
9774bdef 439 $link = substr($line, $start, strlen($line));
e550d551 440
9774bdef 441 if (strpos($link, " ")) {
442 $end = strpos($link, " ")-1;
443 }
444 else if (strpos($link, "&nbsp;")) {
445 $end = strpos($link, "&nbsp;")-1;
446 }
447 else if (strpos($link, "<")) {
e550d551 448 $end = strpos($link, "<");
9774bdef 449 }
450 else if (strpos($link, ">")) {
451 $end = strpos($link, ">");
452 }
453 else if (strpos($link, "(")) {
454 $end = strpos($link, "(")-1;
455 }
456 else if (strpos($link, ")")) {
457 $end = strpos($link, ")")-1;
458 }
459 else if (strpos($link, "{")) {
460 $end = strpos($link, "{")-1;
461 }
462 else if (strpos($link, "}")) {
463 $end = strpos($link, "}")-1;
464 }
e550d551 465 else
466 $end = strlen($link);
467
9774bdef 468 $link = substr($line, $start, $end);
469 $end = $end + $start;
470 $before = substr($line, 0, $start);
471 $after = substr($line, $end, strlen($line));
472
473 $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";
e550d551 474 }
475
476 return $line;
05207a68 477 }
a09387f4 478?>