8f9c92ab0e5cc17595a909f639d78e245f6b9350
[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 $read = fgets($imapConnection, 1024);
14 $unseen = false;
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
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
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 **/
51 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
52 $rel_start = $start;
53 if (($start > $end) || ($start < 1)) {
54 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
55 exit;
56 }
57
58 $pos = 0;
59 while ($rel_start <= $end) {
60 if ($end - $rel_start > 50) {
61 $rel_end = $rel_start + 49;
62 } else {
63 $rel_end = $end;
64 }
65 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date To Cc)\n");
66 $read = fgets($imapConnection, 1024);
67
68 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
69
70 if (substr($read, 0, 5) == "From:") {
71 $read = ereg_replace("<", "EMAILSTART--", $read);
72 $read = ereg_replace(">", "--EMAILEND", $read);
73 $from[$pos] = substr($read, 5, strlen($read) - 6);
74 }
75 else if (substr($read, 0, 5) == "Date:") {
76 $read = ereg_replace("<", "&lt;", $read);
77 $read = ereg_replace(">", "&gt;", $read);
78 $date[$pos] = substr($read, 5, strlen($read) - 6);
79 }
80 else if (substr($read, 0, 8) == "Subject:") {
81 $read = ereg_replace("<", "&lt;", $read);
82 $read = ereg_replace(">", "&gt;", $read);
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++;
96 }
97
98 $read = fgets($imapConnection, 1024);
99 }
100 $rel_start = $rel_start + 50;
101 }
102 }
103
104 function setMessageFlag($imapConnection, $i, $q, $flag) {
105 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
106 }
107
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 **/
114 function getMessageFlags($imapConnection, $j, &$flags) {
115 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
116 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
117 $read = fgets($imapConnection, 1024);
118 $count = 0;
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);;
126 $s = 0;
127 while ($s < count($flags)) {
128 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
129 $s++;
130 }
131 } else {
132 $flags[0] = "None";
133 }
134 $count++;
135 $read = fgets($imapConnection, 1024);
136 }
137 }
138
139 function decodeEmailAddr($sender) {
140 $emailAddr = getEmailAddr($sender);
141 if (strpos($emailAddr, "EMAILSTART--")) {
142
143 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
144 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
145 } else {
146 $emailAddr = $emailAddr;
147 }
148 return $emailAddr;
149 }
150
151 function getEmailAddr($sender) {
152 if (strpos($sender, "EMAILSTART--") == false)
153 return "$sender";
154
155 $emailStart = strpos($sender, "EMAILSTART--") + 12;
156 $emailAddr = substr($sender, $emailStart, strlen($sender));
157 $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND"));
158
159 return $emailAddr;
160 }
161
162 function getSender($sender) {
163 if (strpos($sender, "EMAILSTART--") == false)
164 return "$sender";
165
166 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
167 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
168 return "$first $second";
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 }
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") {
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 }
222
223 /** expunges a mailbox **/
224 function expungeBox($imapConnection, $mailbox) {
225 selectMailbox($imapConnection, $mailbox, $num);
226 fputs($imapConnection, "1 EXPUNGE\n");
227 }
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 }
237
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"]["TYPE0"], $message["HEADER"]["TYPE1"]);
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["TYPE0"] = "text";
252 $header["TYPE1"] = "plain";
253 $header["ENCODING"] = "us-ascii";
254 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
255 /** MIME-VERSION **/
256 if (substr($read, 0, 17) == "MIME-Version: 1.0") {
257 $header["MIME"] = true;
258 $read = fgets($imapConnection, 1024);
259 }
260
261 /** ENCODING TYPE **/
262 else if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") {
263 $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
264 }
265
266 /** CONTENT-TYPE **/
267 else if (substr($read, 0, 13) == "Content-Type:") {
268 $cont = strtolower(trim(substr($read, 13)));
269 if (strpos($cont, ";"))
270 $cont = substr($cont, 0, strpos($cont, ";"));
271 $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
272 $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
273
274 $line = $read;
275 $read = fgets($imapConnection, 1024);
276 while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) {
277 str_replace("\n", "", $line);
278 str_replace("\n", "", $read);
279 $line = "$line $read";
280 $read = fgets($imapConnection, 1024);
281 }
282
283 /** Detect the boundary of a multipart message **/
284 if (strpos(strtolower(trim($line)), "boundary=")) {
285 $pos = strpos($line, "boundary=") + 9;
286 $bound = trim($line);
287 if (strpos($line, " ", $pos) > 0) {
288 $bound = substr($bound, $pos, strpos($line, " ", $pos));
289 } else {
290 $bound = substr($bound, $pos);
291 }
292 $bound = str_replace("\"", "", $bound);
293 $header["BOUNDARY"] = $bound;
294 }
295
296 /** Detect the charset **/
297 if (strpos(strtolower(trim($line)), "charset=")) {
298 $pos = strpos($line, "charset=") + 8;
299 $charset = trim($line);
300 if (strpos($line, " ", $pos) > 0) {
301 $charset = substr($charset, $pos, strpos($line, " ", $pos));
302 } else {
303 $charset = substr($charset, $pos);
304 }
305 $charset = str_replace("\"", "", $charset);
306 $header["CHARSET"] = $charset;
307 } else {
308 $header["CHARSET"] = "us-ascii";
309 }
310
311 }
312 /** FROM **/
313 else if (substr($read, 0, 5) == "From:") {
314 $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
315 $read = fgets($imapConnection, 1024);
316 }
317 /** DATE **/
318 else if (substr($read, 0, 5) == "Date:") {
319 $d = substr($read, 5, strlen($read) - 6);
320 $d = trim($d);
321 $d = ereg_replace(" ", " ", $d);
322 $d = explode(" ", $d);
323 $header["DATE"] = getTimeStamp($d);
324 $read = fgets($imapConnection, 1024);
325 }
326 /** SUBJECT **/
327 else if (substr($read, 0, 8) == "Subject:") {
328 $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
329 if (strlen(Chop($header["SUBJECT"])) == 0)
330 $header["SUBJECT"] = "(no subject)";
331 $read = fgets($imapConnection, 1024);
332 }
333 /** CC **/
334 else if (substr($read, 0, 3) == "CC:") {
335 $pos = 0;
336 $header["CC"][$pos] = trim(substr($read, 4));
337 $read = fgets($imapConnection, 1024);
338 while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
339 $pos++;
340 $header["CC"][$pos] = trim($read);
341 $read = fgets($imapConnection, 1024);
342 }
343 }
344 /** TO **/
345 else if (substr($read, 0, 3) == "To:") {
346 $pos = 0;
347 $header["TO"][$pos] = trim(substr($read, 4));
348 $read = fgets($imapConnection, 1024);
349 while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
350 $pos++;
351 $header["TO"][$pos] = trim($read);
352 $read = fgets($imapConnection, 1024);
353 }
354 }
355
356 /** ERROR CORRECTION **/
357 else if (substr($read, 0, 1) == ")") {
358 if ($header["SUBJECT"] == "")
359 $header["SUBJECT"] = "(no subject)";
360
361 if ($header["FROM"] == "")
362 $header["FROM"] = "(unknown sender)";
363
364 if ($header["DATE"] == "")
365 $header["DATE"] = time();
366 $read = fgets($imapConnection, 1024);
367 }
368 else {
369 $read = fgets($imapConnection, 1024);
370 }
371 }
372 return $header;
373 }
374
375 function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
376 /** This first part reads in the full body of the message **/
377 fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
378 $read = fgets($imapConnection, 1024);
379
380 $count = 0;
381 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
382 $body[$count] = $read;
383 $count++;
384
385 $read = fgets($imapConnection, 1024);
386 }
387
388 /** this deletes the first line, and the last two (imap stuff we ignore) **/
389 $i = 0;
390 $j = 0;
391 while ($i < count($body)) {
392 if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
393 $bodytmp[$j] = $body[$i];
394 $j++;
395 }
396 $i++;
397 }
398 $body = $bodytmp;
399
400 /** Now, lets work out the MIME stuff **/
401 /** (needs mime.php included) **/
402 return decodeMime($body, $bound, $type0, $type1);
403 }
404
405 function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset) {
406 /** defaults... if the don't get overwritten, it will display text **/
407 $type0 = "text";
408 $type1 = "plain";
409 $encoding = "us-ascii";
410 $i = 0;
411 while (trim($read[$i]) != "") {
412 if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") {
413 $encoding = strtolower(trim(substr($read[$i], 26)));
414
415 } else if (substr($read[$i], 0, 13) == "Content-Type:") {
416 $cont = strtolower(trim(substr($read[$i], 13)));
417 if (strpos($cont, ";"))
418 $cont = substr($cont, 0, strpos($cont, ";"));
419 $type0 = substr($cont, 0, strpos($cont, "/"));
420 $type1 = substr($cont, strpos($cont, "/")+1);
421
422 $line = $read[$i];
423 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
424 str_replace("\n", "", $line);
425 str_replace("\n", "", $read[$i]);
426 $line = "$line $read[$i]";
427 $i++;
428 }
429
430 /** Detect the boundary of a multipart message **/
431 if (strpos(strtolower(trim($line)), "boundary=")) {
432 $pos = strpos($line, "boundary=") + 9;
433 $bound = trim($line);
434 if (strpos($line, " ", $pos) > 0) {
435 $bound = substr($bound, $pos, strpos($line, " ", $pos));
436 } else {
437 $bound = substr($bound, $pos);
438 }
439 $bound = str_replace("\"", "", $bound);
440 }
441
442 /** Detect the charset **/
443 if (strpos(strtolower(trim($line)), "charset=")) {
444 $pos = strpos($line, "charset=") + 8;
445 $charset = trim($line);
446 if (strpos($line, " ", $pos) > 0) {
447 $charset = substr($charset, $pos, strpos($line, " ", $pos));
448 } else {
449 $charset = substr($charset, $pos);
450 }
451 $charset = str_replace("\"", "", $charset);
452 }
453 }
454 $i++;
455 }
456
457 /** remove the header from the entity **/
458 $i = 0;
459 while (trim($read[$i]) != "") {
460 $i++;
461 }
462 $i++;
463
464 for ($p = 0; $i < count($read); $p++) {
465 $entity[$p] = $read[$i];
466 $i++;
467 }
468
469 $read = $entity;
470 }
471
472 function parsePlainTextMessage($line) {
473 $line = "^^$line";
474
475 if ((strpos(strtolower($line), "<!") == false) &&
476 (strpos(strtolower($line), "<html>") == false) &&
477 (strpos(strtolower($line), "</html>") == false)) {
478 $line = str_replace("<", "&lt;", $line);
479 $line = str_replace(">", "&gt;", $line);
480 }
481
482 $wrap_at = 86; // Make this configurable int the config file some time
483 if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
484 $line = wordWrap($line, $wrap_at);
485
486 $line = str_replace(" ", "&nbsp;", $line);
487 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
488
489 /** if >> or > are found at the beginning of a line, I'll assume that was
490 replied text, so make it different colors **/
491 if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;&gt;") == 2) {
492 $line = substr($line, 2, strlen($line));
493 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
494 } else if (strpos(trim(str_replace("&nbsp;", "", $line)), "&gt;") == 2) {
495 $line = substr($line, 2, strlen($line));
496 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
497 } else {
498 $line = substr($line, 2, strlen($line));
499 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
500 }
501
502 /** This translates "http://" into a link. It could be made better to accept
503 "www" and "mailto" also. That should probably be added later. **/
504 if (strpos(strtolower($line), "http://") != false) {
505 $line = ereg_replace("<BR>", "", $line);
506 $start = strpos(strtolower($line), "http://");
507 $link = substr($line, $start, strlen($line));
508
509 if (strpos($link, " ")) {
510 $end = strpos($link, " ")-1;
511 }
512 else if (strpos($link, "&nbsp;")) {
513 $end = strpos($link, "&nbsp;")-1;
514 }
515 else if (strpos($link, "<")) {
516 $end = strpos($link, "<");
517 }
518 else if (strpos($link, ">")) {
519 $end = strpos($link, ">");
520 }
521 else if (strpos($link, "(")) {
522 $end = strpos($link, "(")-1;
523 }
524 else if (strpos($link, ")")) {
525 $end = strpos($link, ")")-1;
526 }
527 else if (strpos($link, "{")) {
528 $end = strpos($link, "{")-1;
529 }
530 else if (strpos($link, "}")) {
531 $end = strpos($link, "}")-1;
532 }
533 else
534 $end = strlen($link);
535
536 $link = substr($line, $start, $end);
537 $end = $end + $start;
538 $before = substr($line, 0, $start);
539 $after = substr($line, $end, strlen($line));
540
541 $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";
542 }
543
544 return $line;
545 }
546 ?>