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