X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=blobdiff_plain;f=functions%2Fmailbox.php;h=9092744d48cad7db4c440c5f72d202217c533c7d;hp=95caabd139a484c2ff7fd13595ea26f9851584de;hb=31f3d7c03a7c155d48a955b4beb15105ba154086;hpb=597d8f1dd82b77e993a99ea9dca1c26b19218b8b diff --git a/functions/mailbox.php b/functions/mailbox.php index 95caabd1..9092744d 100644 --- a/functions/mailbox.php +++ b/functions/mailbox.php @@ -20,31 +20,64 @@ } } - function getMessageHeaders($imapConnection, $i, &$from, &$subject, &$date) { - fputs($imapConnection, "messageFetch FETCH $i:$i RFC822.HEADER.LINES (From Subject Date)\n"); - $read = fgets($imapConnection, 1024); - /* I have to replace <> with [] because HTML uses <> as tags, thus not printing what's in <> */ - $read = ereg_replace("<", "[", $read); - $read = ereg_replace(">", "]", $read); + /** This function sends a request to the IMAP server for headers, 50 at a time + ** until $end is reached. I originally had it do them all at one time, but found + ** it slightly faster to do it this way. + ** + ** Originally we had getMessageHeaders get the headers for one message at a time. + ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800 + ** messages) to about 3.5 seconds. + **/ + function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) { + $rel_start = $start; + if (($start > $end) || ($start < 1)) { + echo "Error in message header fetching. Start message: $start, End message: $end
"; + exit; + } - while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { - if (substr($read, 0, 5) == "From:") { - $read = ereg_replace("<", "EMAILSTART--", $read); - $read = ereg_replace(">", "--EMAILEND", $read); - $from = substr($read, 5, strlen($read) - 6); - } - else if (substr($read, 0, 5) == "Date:") { - $read = ereg_replace("<", "[", $read); - $read = ereg_replace(">", "]", $read); - $date = substr($read, 5, strlen($read) - 6); - } - else if (substr($read, 0, 8) == "Subject:") { - $read = ereg_replace("<", "[", $read); - $read = ereg_replace(">", "]", $read); - $subject = substr($read, 8, strlen($read) - 9); + $pos = 0; + while ($rel_start <= $end) { + if ($end - $rel_start > 50) { + $rel_end = $rel_start + 49; + } else { + $rel_end = $end; } - + fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n"); $read = fgets($imapConnection, 1024); + + while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { + + if (substr($read, 0, 5) == "From:") { + $read = ereg_replace("<", "EMAILSTART--", $read); + $read = ereg_replace(">", "--EMAILEND", $read); + $from[$pos] = substr($read, 5, strlen($read) - 6); + } + else if (substr($read, 0, 5) == "Date:") { + $read = ereg_replace("<", "<", $read); + $read = ereg_replace(">", ">", $read); + $date[$pos] = substr($read, 5, strlen($read) - 6); + } + else if (substr($read, 0, 8) == "Subject:") { + $read = ereg_replace("<", "<", $read); + $read = ereg_replace(">", ">", $read); + $subject[$pos] = substr($read, 8, strlen($read) - 9); + if (strlen(Chop($subject[$pos])) == 0) + $subject[$pos] = "(no subject)"; + } + else if (substr($read, 0, 1) == ")") { + if ($subject[$pos] == "") + $subject[$pos] = "(no subject)"; + else if ($from[$pos] == "") + $from[$pos] = "(unknown sender)"; + else if ($date[$pos] == "") + $from[$pos] = gettimeofday(); + + $pos++; + } + + $read = fgets($imapConnection, 1024); + } + $rel_start = $rel_start + 50; } } @@ -52,10 +85,17 @@ fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n"); } - function getMessageFlags($imapConnection, $i, &$flags) { + /** This function gets the flags for message $j. It does only one message at a + ** time, rather than doing groups of messages (like getMessageHeaders does). + ** I found it one or two seconds quicker (on a box of 800 messages) to do it + ** individually. I'm not sure why it happens like that, but that's what my + ** testing found. Perhaps later I will be proven wrong and this will change. + **/ + function getMessageFlags($imapConnection, $j, &$flags) { /** * 2 FETCH (FLAGS (\Answered \Seen)) */ - fputs($imapConnection, "messageFetch FETCH $i:$i FLAGS\n"); + fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n"); $read = fgets($imapConnection, 1024); + $count = 0; while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { if (strpos($read, "FLAGS")) { $read = ereg_replace("\(", "", $read); @@ -71,10 +111,21 @@ } else { $flags[0] = "None"; } + $count++; $read = fgets($imapConnection, 1024); } } + function decodeEmailAddr($sender) { + $emailAddr = getEmailAddr($sender); + $emailStart = strpos($emailAddr, "EMAILSTART--"); + $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; + + $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); + $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); + return $emailAddr; + } + function getEmailAddr($sender) { if (strpos($sender, "EMAILSTART--") == false) return ""; @@ -161,4 +212,108 @@ return $box; } + + /** This function will fetch the body of a given message and format + it into our standard format. **/ + function fetchBody($imapConnection, $id) { + fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n"); + $count = 0; + $read[$count] = fgets($imapConnection, 1024); + while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) { + $count++; + $read[$count] = fgets($imapConnection, 1024); + } + + /** this loop removes the first line, and the last two which + are IMAP information that we don't need. **/ + $i = 0; + $j = 0; + while ($i < count($read)) { + if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){ + $readtmp[$j] = $read[$i]; + $j++; + } + $i++; + } + $read = $readtmp; + + /** This loop formats the text, creating links out of linkable stuff too **/ + $count = 0; + $useHTML= false; + while ($count < count($read)) { + $read[$count] = "^^$read[$count]"; + + if (strpos(strtolower($read[$count]), "") == true) { + $useHTML = true; + } else if (strpos(strtolower($read[$count]), "") == true) { + $useHTML = false; + } + + $read[$count] = substr($read[$count], 2, strlen($read[$count])); + + if ($useHTML == false) { + $read[$count] = parsePlainBodyText($read[$count]); + } else { + $read[$count] = parseHTMLBodyText($read[$count]); + } + + $count++; + } + return $read; + } + + function parseHTMLBodyText($line) { + return $line; + } + + function parsePlainBodyText($line) { + $line = "^^$line"; + + if ((strpos(strtolower($line), "") == false) && + (strpos(strtolower($line), "") == false)) { + $line = str_replace("<", "<", $line); + $line = str_replace(">", ">", $line); + } + + $wrap_at = 80; // Make this configurable int the config file some time + if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning + $line = wordWrap($line, $wrap_at); + + $line = str_replace(" ", " ", $line); + $line = str_replace("\t", "        ", $line); + + /** if >> or > are found at the beginning of a line, I'll assume that was + replied text, so make it different colors **/ + if (strpos(trim(str_replace(" ", "", $line)), ">>") == 2) { + $line = substr($line, 2, strlen($line)); + $line = "$line
\n"; + } else if (strpos(trim(str_replace(" ", "", $line)), ">") == 2) { + $line = substr($line, 2, strlen($line)); + $line = "$line
\n"; + } else { + $line = substr($line, 2, strlen($line)); + $line = "$line
\n"; + } + + /** This translates "http://" into a link. It could be made better to accept + "www" and "mailto" also. That should probably be added later. **/ + if (strpos(strtolower($line), "http://") != false) { + $start = strpos(strtolower($line), "http://"); + $text = substr($line, $start, strlen($line)); + $link = ereg_replace("
", "", $text); + + if (strpos($link, "&")) + $end = strpos($link, "&"); + else if (strpos($link, "<")) + $end = strpos($link, "<"); + else + $end = strlen($link); + + $link = substr($link, 0, $end); + $line = str_replace($text, "$text", $line); + } + + return $line; + } ?>