5 ** This contains functions that request information about a mailbox. Including
6 ** reading and parsing headers, getting folder information, etc.
10 function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
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];
22 function unseenMessages($imapConnection, &$numUnseen) {
23 fputs($imapConnection, "1 SEARCH UNSEEN NOT DELETED\n");
24 $read = fgets($imapConnection, 1024);
27 if (strlen($read) > 10) {
29 $ary = explode(" ", $read);
30 $numUnseen = count($ary) - 2;
37 $read = fgets($imapConnection, 1024);
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.
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.
49 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
51 if (($start > $end) ||
($start < 1)) {
52 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
57 while ($rel_start <= $end) {
58 if ($end - $rel_start > 50) {
59 $rel_end = $rel_start +
49;
63 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
64 $read = fgets($imapConnection, 1024);
66 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
68 if (substr($read, 0, 5) == "From:") {
69 $read = encodeEmailAddr("$read");
70 $from[$pos] = substr($read, 5, strlen($read) - 6);
72 else if (substr($read, 0, 5) == "Date:") {
73 $read = ereg_replace("<", "<", $read);
74 $read = ereg_replace(">", ">", $read);
75 $date[$pos] = substr($read, 5, strlen($read) - 6);
77 else if (substr($read, 0, 8) == "Subject:") {
78 $read = ereg_replace("<", "<", $read);
79 $read = ereg_replace(">", ">", $read);
80 $subject[$pos] = substr($read, 8, strlen($read) - 9);
81 if (strlen(Chop($subject[$pos])) == 0)
82 $subject[$pos] = "(no subject)";
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();
95 $read = fgets($imapConnection, 1024);
97 $rel_start = $rel_start +
50;
101 function encodeEmailAddr($string) {
102 $string = ereg_replace("<", "EMAILSTART--", $string);
103 $string = ereg_replace(">", "--EMAILEND", $string);
107 function setMessageFlag($imapConnection, $i, $q, $flag) {
108 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
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.
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);
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));
128 $flags = explode(" ", $read);;
130 while ($s < count($flags)) {
131 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
138 $read = fgets($imapConnection, 1024);
142 function decodeEmailAddr($sender) {
143 $emailAddr = getEmailAddr($sender);
144 if (strpos($emailAddr, "EMAILSTART--")) {
146 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
147 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
149 $emailAddr = $emailAddr;
154 function getEmailAddr($sender) {
155 if (strpos($sender, "EMAILSTART--") == false)
158 $emailStart = strpos($sender, "EMAILSTART--") +
12;
159 $emailAddr = substr($sender, $emailStart, strlen($sender));
160 $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND"));
165 function getSender($sender) {
166 if (strpos($sender, "EMAILSTART--") == false)
169 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
170 $second = substr($sender, strpos($sender, "--EMAILEND") +
10, strlen($sender));
171 return "$first $second";
174 function getSenderName($sender) {
175 $name = getSender($sender);
176 $emailAddr = getEmailAddr($sender);
177 $emailStart = strpos($emailAddr, "EMAILSTART--");
178 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
180 if (($emailAddr == "") && ($name == "")) {
183 else if ((strstr($name, "?") != false) ||
(strstr($name, "$") != false) ||
(strstr($name, "%") != false)){
184 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
185 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
188 else if (strlen($name) > 0) {
191 else if (strlen($emailAddr > 0)) {
192 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
193 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
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);
206 /** returns "true" if the copy was completed successfully.
207 ** returns "false" with an error message if unsuccessful.
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);
216 if (substr($read, 0, 15) == "mailboxStore NO") {
218 } else if (substr($read, 0, 15) == "mailboxStore OK") {
222 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
226 /** expunges a mailbox **/
227 function expungeBox($imapConnection, $mailbox) {
228 selectMailbox($imapConnection, $mailbox, $num);
229 fputs($imapConnection, "1 EXPUNGE\n");
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));
242 /** This function gets all the information about a message. Including Header and body **/
243 function fetchMessage($imapConnection, $id, $mailbox) {
244 $message["INFO"]["ID"] = $id;
245 $message["INFO"]["MAILBOX"] = $mailbox;
246 $message["HEADER"] = fetchHeader($imapConnection, $id);
247 $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]);
251 function fetchHeader($imapConnection, $id) {
252 fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER\n");
253 $read = fgets($imapConnection, 1024);
255 /** defaults... if the don't get overwritten, it will display text **/
256 $header["TYPE0"] = "text";
257 $header["TYPE1"] = "plain";
258 $header["ENCODING"] = "us-ascii";
259 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
261 if (substr($read, 0, 17) == "MIME-Version: 1.0") {
262 $header["MIME"] = true;
263 $read = fgets($imapConnection, 1024);
266 /** ENCODING TYPE **/
267 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
268 $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
272 else if (substr($read, 0, 13) == "Content-Type:") {
273 $cont = strtolower(trim(substr($read, 13)));
274 if (strpos($cont, ";"))
275 $cont = substr($cont, 0, strpos($cont, ";"));
277 if (strpos($cont, "/")) {
278 $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
279 $header["TYPE1"] = substr($cont, strpos($cont, "/")+
1);
281 $header["TYPE0"] = $cont;
285 $read = fgets($imapConnection, 1024);
286 while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) {
287 str_replace("\n", "", $line);
288 str_replace("\n", "", $read);
289 $line = "$line $read";
290 $read = fgets($imapConnection, 1024);
293 /** Detect the boundary of a multipart message **/
294 if (strpos(strtolower(trim($line)), "boundary=")) {
295 $pos = strpos($line, "boundary=") +
9;
296 $bound = trim($line);
297 if (strpos($line, " ", $pos) > 0) {
298 $bound = substr($bound, $pos, strpos($line, " ", $pos));
300 $bound = substr($bound, $pos);
302 $bound = str_replace("\"", "", $bound);
303 $header["BOUNDARY"] = $bound;
306 /** Detect the charset **/
307 if (strpos(strtolower(trim($line)), "charset=")) {
308 $pos = strpos($line, "charset=") +
8;
309 $charset = trim($line);
310 if (strpos($line, " ", $pos) > 0) {
311 $charset = substr($charset, $pos, strpos($line, " ", $pos));
313 $charset = substr($charset, $pos);
315 $charset = str_replace("\"", "", $charset);
316 $header["CHARSET"] = $charset;
318 $header["CHARSET"] = "us-ascii";
321 /** Detects filename if any **/
322 if (strpos(strtolower(trim($line)), "name=")) {
323 $pos = strpos($line, "name=") +
5;
325 if (strpos($line, " ", $pos) > 0) {
326 $name = substr($name, $pos, strpos($line, " ", $pos));
328 $name = substr($name, $pos);
330 $name = str_replace("\"", "", $name);
331 $header["FILENAME"] = $name;
336 else if (strtolower(substr($read, 0, 9)) == "reply-to:") {
337 $header["REPLYTO"] = trim(substr($read, 9, strlen($read)));
338 $read = fgets($imapConnection, 1024);
342 else if (strtolower(substr($read, 0, 5)) == "from:") {
343 $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
344 if ($header["REPLYTO"] == "")
345 $header["REPLYTO"] = $header["FROM"];
346 $read = fgets($imapConnection, 1024);
349 else if (strtolower(substr($read, 0, 5)) == "date:") {
350 $d = substr($read, 5, strlen($read) - 6);
352 $d = ereg_replace(" ", " ", $d);
353 $d = explode(" ", $d);
354 $header["DATE"] = getTimeStamp($d);
355 $read = fgets($imapConnection, 1024);
358 else if (strtolower(substr($read, 0, 8)) == "subject:") {
359 $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
360 if (strlen(Chop($header["SUBJECT"])) == 0)
361 $header["SUBJECT"] = "(no subject)";
362 $read = fgets($imapConnection, 1024);
365 else if (strtolower(substr($read, 0, 3)) == "cc:") {
367 $header["CC"][$pos] = trim(substr($read, 4));
368 $read = fgets($imapConnection, 1024);
369 while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
371 $header["CC"][$pos] = trim($read);
372 $read = fgets($imapConnection, 1024);
376 else if (strtolower(substr($read, 0, 3)) == "to:") {
378 $header["TO"][$pos] = trim(substr($read, 4));
379 $read = fgets($imapConnection, 1024);
380 while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
382 $header["TO"][$pos] = trim($read);
383 $read = fgets($imapConnection, 1024);
387 /** ERROR CORRECTION **/
388 else if (substr($read, 0, 1) == ")") {
389 if ($header["SUBJECT"] == "")
390 $header["SUBJECT"] = "(no subject)";
392 if ($header["FROM"] == "")
393 $header["FROM"] = "(unknown sender)";
395 if ($header["DATE"] == "")
396 $header["DATE"] = time();
397 $read = fgets($imapConnection, 1024);
400 $read = fgets($imapConnection, 1024);
406 function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
407 /** This first part reads in the full body of the message **/
408 fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
409 $read = fgets($imapConnection, 1024);
412 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
413 $body[$count] = $read;
416 $read = fgets($imapConnection, 1024);
419 /** this deletes the first line, and the last two (imap stuff we ignore) **/
422 while ($i < count($body)) {
423 if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
424 $bodytmp[$j] = $body[$i];
431 /** Now, lets work out the MIME stuff **/
432 /** (needs mime.php included) **/
433 return decodeMime($body, $bound, $type0, $type1);
436 function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
437 /** defaults... if the don't get overwritten, it will display text **/
440 $encoding = "us-ascii";
442 while (trim($read[$i]) != "") {
443 if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
444 $encoding = strtolower(trim(substr($read[$i], 26)));
446 } else if (substr($read[$i], 0, 13) == "Content-Type:") {
447 $cont = strtolower(trim(substr($read[$i], 13)));
448 if (strpos($cont, ";"))
449 $cont = substr($cont, 0, strpos($cont, ";"));
451 if (strpos($cont, "/")) {
452 $type0 = substr($cont, 0, strpos($cont, "/"));
453 $type1 = substr($cont, strpos($cont, "/")+
1);
458 $read[$i] = trim($read[$i]);
461 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
462 str_replace("\n", "", $line);
463 str_replace("\n", "", $read[$i]);
464 $line = "$line $read[$i]";
466 $read[$i] = trim($read[$i]);
470 /** Detect the boundary of a multipart message **/
471 if (strpos(strtolower(trim($line)), "boundary=")) {
472 $pos = strpos($line, "boundary=") +
9;
473 $bound = trim($line);
474 if (strpos($line, " ", $pos) > 0) {
475 $bound = substr($bound, $pos, strpos($line, " ", $pos));
477 $bound = substr($bound, $pos);
479 $bound = str_replace("\"", "", $bound);
482 /** Detect the charset **/
483 if (strpos(strtolower(trim($line)), "charset=")) {
484 $pos = strpos($line, "charset=") +
8;
485 $charset = trim($line);
486 if (strpos($line, " ", $pos) > 0) {
487 $charset = substr($charset, $pos, strpos($line, " ", $pos));
489 $charset = substr($charset, $pos);
491 $charset = str_replace("\"", "", $charset);
494 /** Detects filename if any **/
495 if (strpos(strtolower(trim($line)), "name=")) {
496 $pos = strpos($line, "name=") +
5;
498 if (strpos($line, " ", $pos) > 0) {
499 $name = substr($name, $pos, strpos($line, " ", $pos));
501 $name = substr($name, $pos);
503 $name = str_replace("\"", "", $name);
510 /** remove the header from the entity **/
512 while (trim($read[$i]) != "") {
517 for ($p = 0; $i < count($read); $p++
) {
518 $entity[$p] = $read[$i];
525 function parseHTMLMessage($line) {
526 /** Add any parsing you want to in here **/
530 function parsePlainTextMessage($line) {
531 /** Add any parsing you want to in here */
535 if ((strpos(strtolower($line), "<!") == false) &&
536 (strpos(strtolower($line), "<html>") == false) &&
537 (strpos(strtolower($line), "</html>") == false)) {
538 $line = str_replace("<", "<", $line);
539 $line = str_replace(">", ">", $line);
542 $wrap_at = 86; // Make this configurable int the config file some time
543 if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
544 $line = wordWrap($line, $wrap_at);
546 $line = str_replace(" ", " ", $line);
547 $line = str_replace("\t", " ", $line);
548 $line = str_replace("\n", "", $line);
550 if (strpos(trim(str_replace(" ", "", $line)), ">>") == 2) {
551 $line = substr($line, 2, strlen($line));
552 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
553 } else if (strpos(trim(str_replace(" ", "", $line)), ">") == 2) {
554 $line = substr($line, 2, strlen($line));
555 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
557 $line = substr($line, 2, strlen($line));
558 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
561 if (strpos(strtolower($line), "http://") != false) {
562 $line = ereg_replace("<BR>", "", $line);
563 $start = strpos(strtolower($line), "http://");
564 $link = substr($line, $start, strlen($line));
566 if (strpos($link, " ")) {
567 $end = strpos($link, " ")-1;
569 else if (strpos($link, " ")) {
570 $end = strpos($link, " ")-1;
572 else if (strpos($link, "<")) {
573 $end = strpos($link, "<");
575 else if (strpos($link, ">")) {
576 $end = strpos($link, ">");
578 else if (strpos($link, "(")) {
579 $end = strpos($link, "(")-1;
581 else if (strpos($link, ")")) {
582 $end = strpos($link, ")")-1;
584 else if (strpos($link, "{")) {
585 $end = strpos($link, "{")-1;
587 else if (strpos($link, "}")) {
588 $end = strpos($link, "}")-1;
591 $end = strlen($link);
593 $link = substr($line, $start, $end);
594 $end = $end +
$start;
595 $before = substr($line, 0, $start);
596 $after = substr($line, $end, strlen($line));
598 $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";