X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fmailbox.php;h=45d9021ae6268fc56692bdbdd70227ffd8969926;hb=f8dc8d6180df3851ed15bcd39373fcf1760f3290;hp=9092744d48cad7db4c440c5f72d202217c533c7d;hpb=31f3d7c03a7c155d48a955b4beb15105ba154086;p=squirrelmail.git diff --git a/functions/mailbox.php b/functions/mailbox.php index 9092744d..45d9021a 100644 --- a/functions/mailbox.php +++ b/functions/mailbox.php @@ -10,16 +10,34 @@ function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) { // select mailbox fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n"); - $read = fgets($imapConnection, 1024); - while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) { - if (substr(Chop($read), -6) == "EXISTS") { - $array = explode(" ", $read); + $data = imapReadData($imapConnection, "mailboxSelect"); + for ($i = 0; $i < count($data); $i++) { + if (substr(Chop($data[$i]), -6) == "EXISTS") { + $array = explode(" ", $data[$i]); $numberOfMessages = $array[1]; } - $read = fgets($imapConnection, 1024); } } + function unseenMessages($imapConnection, &$numUnseen) { + fputs($imapConnection, "1 SEARCH UNSEEN NOT DELETED\n"); + $read = fgets($imapConnection, 1024); + $unseen = false; + + if (strlen($read) > 10) { + $unseen = true; + $ary = explode(" ", $read); + $numUnseen = count($ary) - 2; + } + else { + $unseen = false; + $numUnseen = 0; + } + + $read = fgets($imapConnection, 1024); + return $unseen; + } + /** 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. @@ -31,7 +49,7 @@ 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
"; + echo _("Error in message header fetching. Start message: "). $start, _("End message: "). "$end
"; exit; } @@ -48,8 +66,7 @@ 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); + $read = encodeEmailAddr("$read"); $from[$pos] = substr($read, 5, strlen($read) - 6); } else if (substr($read, 0, 5) == "Date:") { @@ -81,6 +98,12 @@ } } + function encodeEmailAddr($string) { + $string = ereg_replace("<", "EMAILSTART--", $string); + $string = ereg_replace(">", "--EMAILEND", $string); + return $string; + } + function setMessageFlag($imapConnection, $i, $q, $flag) { fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n"); } @@ -118,31 +141,34 @@ function decodeEmailAddr($sender) { $emailAddr = getEmailAddr($sender); - $emailStart = strpos($emailAddr, "EMAILSTART--"); - $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; + if (strpos($emailAddr, "EMAILSTART--")) { - $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); - $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); + $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); + $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); + } else { + $emailAddr = $emailAddr; + } return $emailAddr; } function getEmailAddr($sender) { if (strpos($sender, "EMAILSTART--") == false) - return ""; + return "$sender"; - $start = strpos($sender, "EMAILSTART--"); - $emailAddr = substr($sender, $start, strlen($sender)); + $emailStart = strpos($sender, "EMAILSTART--") + 12; + $emailAddr = substr($sender, $emailStart, strlen($sender)); + $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND")); return $emailAddr; } function getSender($sender) { if (strpos($sender, "EMAILSTART--") == false) - return ""; + return "$sender"; $first = substr($sender, 0, strpos($sender, "EMAILSTART--")); $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender)); - return "$first$second"; + return "$first $second"; } function getSenderName($sender) { @@ -188,7 +214,6 @@ } if (substr($read, 0, 15) == "mailboxStore NO") { - echo "ERROR... $read
"; return false; } else if (substr($read, 0, 15) == "mailboxStore OK") { return true; @@ -202,118 +227,300 @@ function expungeBox($imapConnection, $mailbox) { selectMailbox($imapConnection, $mailbox, $num); fputs($imapConnection, "1 EXPUNGE\n"); + imapReadData($imapConnection, "1", true, $response, $message); } - function getFolderNameMinusINBOX($mailbox) { - if (substr($mailbox, 0, 6) == "INBOX.") - $box = substr($mailbox, 6, strlen($mailbox)); + function getFolderNameMinusINBOX($mailbox, $del) { + $inbox = "INBOX" . $del; + if (substr($mailbox, 0, strlen($inbox)) == $inbox) + $box = substr($mailbox, strlen($inbox), strlen($mailbox)); else $box = $mailbox; return $box; } - /** This function will fetch the body of a given message and format - it into our standard format. **/ - function fetchBody($imapConnection, $id) { + /** This function gets all the information about a message. Including Header and body **/ + function fetchMessage($imapConnection, $id, $mailbox) { + $message["INFO"]["ID"] = $id; + $message["INFO"]["MAILBOX"] = $mailbox; + $message["HEADER"] = fetchHeader($imapConnection, $id); + $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]); + return $message; + } + + function fetchHeader($imapConnection, $id) { + fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER\n"); + $read = fgets($imapConnection, 1024); + + /** defaults... if the don't get overwritten, it will display text **/ + $header["TYPE0"] = "text"; + $header["TYPE1"] = "plain"; + $header["ENCODING"] = "us-ascii"; + while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { + /** MIME-VERSION **/ + if (substr($read, 0, 17) == "MIME-Version: 1.0") { + $header["MIME"] = true; + $read = fgets($imapConnection, 1024); + } + + /** ENCODING TYPE **/ + else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") { + $header["ENCODING"] = strtolower(trim(substr($read[$i], 26))); + } + + /** CONTENT-TYPE **/ + else if (substr($read, 0, 13) == "Content-Type:") { + $cont = strtolower(trim(substr($read, 13))); + if (strpos($cont, ";")) + $cont = substr($cont, 0, strpos($cont, ";")); + + if (strpos($cont, "/")) { + $header["TYPE0"] = substr($cont, 0, strpos($cont, "/")); + $header["TYPE1"] = substr($cont, strpos($cont, "/")+1); + } else { + $header["TYPE0"] = $cont; + } + + $line = $read; + $read = fgets($imapConnection, 1024); + while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) { + str_replace("\n", "", $line); + str_replace("\n", "", $read); + $line = "$line $read"; + $read = fgets($imapConnection, 1024); + } + + /** Detect the boundary of a multipart message **/ + if (strpos(strtolower(trim($line)), "boundary=")) { + $pos = strpos($line, "boundary=") + 9; + $bound = trim($line); + if (strpos($line, " ", $pos) > 0) { + $bound = substr($bound, $pos, strpos($line, " ", $pos)); + } else { + $bound = substr($bound, $pos); + } + $bound = str_replace("\"", "", $bound); + $header["BOUNDARY"] = $bound; + } + + /** Detect the charset **/ + if (strpos(strtolower(trim($line)), "charset=")) { + $pos = strpos($line, "charset=") + 8; + $charset = trim($line); + if (strpos($line, " ", $pos) > 0) { + $charset = substr($charset, $pos, strpos($line, " ", $pos)); + } else { + $charset = substr($charset, $pos); + } + $charset = str_replace("\"", "", $charset); + $header["CHARSET"] = $charset; + } else { + $header["CHARSET"] = "us-ascii"; + } + + /** Detects filename if any **/ + if (strpos(strtolower(trim($line)), "name=")) { + $pos = strpos($line, "name=") + 5; + $name = trim($line); + if (strpos($line, " ", $pos) > 0) { + $name = substr($name, $pos, strpos($line, " ", $pos)); + } else { + $name = substr($name, $pos); + } + $name = str_replace("\"", "", $name); + $header["FILENAME"] = $name; + } + } + + /** REPLY-TO **/ + else if (strtolower(substr($read, 0, 9)) == "reply-to:") { + $header["REPLYTO"] = trim(substr($read, 9, strlen($read))); + $read = fgets($imapConnection, 1024); + } + + /** FROM **/ + else if (strtolower(substr($read, 0, 5)) == "from:") { + $header["FROM"] = trim(substr($read, 5, strlen($read) - 6)); + if ($header["REPLYTO"] == "") + $header["REPLYTO"] = $header["FROM"]; + $read = fgets($imapConnection, 1024); + } + /** DATE **/ + else if (strtolower(substr($read, 0, 5)) == "date:") { + $d = substr($read, 5, strlen($read) - 6); + $d = trim($d); + $d = ereg_replace(" ", " ", $d); + $d = explode(" ", $d); + $header["DATE"] = getTimeStamp($d); + $read = fgets($imapConnection, 1024); + } + /** SUBJECT **/ + else if (strtolower(substr($read, 0, 8)) == "subject:") { + $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9)); + if (strlen(Chop($header["SUBJECT"])) == 0) + $header["SUBJECT"] = "(no subject)"; + $read = fgets($imapConnection, 1024); + } + /** CC **/ + else if (strtolower(substr($read, 0, 3)) == "cc:") { + $pos = 0; + $header["CC"][$pos] = trim(substr($read, 4)); + $read = fgets($imapConnection, 1024); + while ((substr($read, 0, 1) == " ") && (trim($read) != "")) { + $pos++; + $header["CC"][$pos] = trim($read); + $read = fgets($imapConnection, 1024); + } + } + /** TO **/ + else if (strtolower(substr($read, 0, 3)) == "to:") { + $pos = 0; + $header["TO"][$pos] = trim(substr($read, 4)); + $read = fgets($imapConnection, 1024); + while ((substr($read, 0, 1) == " ") && (trim($read) != "")){ + $pos++; + $header["TO"][$pos] = trim($read); + $read = fgets($imapConnection, 1024); + } + } + + /** ERROR CORRECTION **/ + else if (substr($read, 0, 1) == ")") { + if ($header["SUBJECT"] == "") + $header["SUBJECT"] = "(no subject)"; + + if ($header["FROM"] == "") + $header["FROM"] = "(unknown sender)"; + + if ($header["DATE"] == "") + $header["DATE"] = time(); + $read = fgets($imapConnection, 1024); + } + else { + $read = fgets($imapConnection, 1024); + } + } + return $header; + } + + function fetchBody($imapConnection, $bound, $id, $type0, $type1) { + /** This first part reads in the full body of the message **/ fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n"); + $read = fgets($imapConnection, 1024); + $count = 0; - $read[$count] = fgets($imapConnection, 1024); - while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) { + while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { + $body[$count] = $read; $count++; - $read[$count] = fgets($imapConnection, 1024); + + $read = fgets($imapConnection, 1024); } - /** this loop removes the first line, and the last two which - are IMAP information that we don't need. **/ + /** this deletes the first line, and the last two (imap stuff we ignore) **/ $i = 0; $j = 0; - while ($i < count($read)) { - if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){ - $readtmp[$j] = $read[$i]; + while ($i < count($body)) { + if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) { + $bodytmp[$j] = $body[$i]; $j++; } $i++; } - $read = $readtmp; + $body = $bodytmp; - /** 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; + /** Now, lets work out the MIME stuff **/ + /** (needs mime.php included) **/ + return decodeMime($body, $bound, $type0, $type1); } - function parseHTMLBodyText($line) { - return $line; - } + function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) { + /** defaults... if the don't get overwritten, it will display text **/ + $type0 = "text"; + $type1 = "plain"; + $encoding = "us-ascii"; + $i = 0; + while (trim($read[$i]) != "") { + if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") { + $encoding = strtolower(trim(substr($read[$i], 26))); + + } else if (substr($read[$i], 0, 13) == "Content-Type:") { + $cont = strtolower(trim(substr($read[$i], 13))); + if (strpos($cont, ";")) + $cont = substr($cont, 0, strpos($cont, ";")); + + if (strpos($cont, "/")) { + $type0 = substr($cont, 0, strpos($cont, "/")); + $type1 = substr($cont, strpos($cont, "/")+1); + } else { + $type0 = $cont; + } - function parsePlainBodyText($line) { - $line = "^^$line"; + $read[$i] = trim($read[$i]); + $line = $read[$i]; + $i++; + while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { + str_replace("\n", "", $line); + str_replace("\n", "", $read[$i]); + $line = "$line $read[$i]"; + $i++; + $read[$i] = trim($read[$i]); + } + $i--; + + /** Detect the boundary of a multipart message **/ + if (strpos(strtolower(trim($line)), "boundary=")) { + $pos = strpos($line, "boundary=") + 9; + $bound = trim($line); + if (strpos($line, " ", $pos) > 0) { + $bound = substr($bound, $pos, strpos($line, " ", $pos)); + } else { + $bound = substr($bound, $pos); + } + $bound = str_replace("\"", "", $bound); + } + + /** Detect the charset **/ + if (strpos(strtolower(trim($line)), "charset=")) { + $pos = strpos($line, "charset=") + 8; + $charset = trim($line); + if (strpos($line, " ", $pos) > 0) { + $charset = substr($charset, $pos, strpos($line, " ", $pos)); + } else { + $charset = substr($charset, $pos); + } + $charset = str_replace("\"", "", $charset); + } - if ((strpos(strtolower($line), "") == false) && - (strpos(strtolower($line), "") == false)) { - $line = str_replace("<", "<", $line); - $line = str_replace(">", ">", $line); + /** Detects filename if any **/ + if (strpos(strtolower(trim($line)), "name=")) { + $pos = strpos($line, "name=") + 5; + $name = trim($line); + if (strpos($line, " ", $pos) > 0) { + $name = substr($name, $pos, strpos($line, " ", $pos)); + } else { + $name = substr($name, $pos); + } + $name = str_replace("\"", "", $name); + $filename = $name; + } + } + $i++; } - $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"; + /** remove the header from the entity **/ + $i = 0; + while (trim($read[$i]) != "") { + $i++; } + $i++; - /** 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); + for ($p = 0; $i < count($read); $p++) { + $entity[$p] = $read[$i]; + $i++; } - return $line; + $read = $entity; } + ?>