function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) {
// select mailbox
fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n");
- $read = fgets($imapConnection, 1024);
- $unseen = false;
- 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 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<BR>";
+ echo _("Error in message header fetching. Start message: "). $start, _("End message: "). "$end<BR>";
exit;
}
} else {
$rel_end = $end;
}
- fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date To Cc)\n");
+ fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end BODY[HEADER.FIELDS (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);
+ $read = encodeEmailAddr("$read");
$from[$pos] = substr($read, 5, strlen($read) - 6);
}
else if (substr($read, 0, 5) == "Date:") {
}
}
+ 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");
}
** 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) {
+ function getMessageFlags($imapConnection, $low, $high, &$flags) {
/** * 2 FETCH (FLAGS (\Answered \Seen)) */
- fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
+ fputs($imapConnection, "messageFetch FETCH $low:$high 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);
$read = ereg_replace("\)", "", $read);
+ $read = str_replace("\\", "", $read);
$read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
$read = trim($read);
- $flags = explode(" ", $read);;
- $s = 0;
- while ($s < count($flags)) {
- $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
- $s++;
- }
+ $flags[$count] = explode(" ", $read);;
} else {
- $flags[0] = "None";
+ $flags[$count][0] = "None";
}
$count++;
$read = fgets($imapConnection, 1024);
$emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
$from = $emailAddr;
}
- else if (strlen($name) > 0) {
+ else if (strlen(trim($name)) > 0) {
$from = $name;
}
else if (strlen($emailAddr > 0)) {
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;
}
/** This function gets all the information about a message. Including Header and body **/
- function fetchMessage($imapConnection, $id) {
+ 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"]["TYPE"][0], $message["HEADER"]["TYPE"][1]);
-
+ $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.LINES (From Subject Date To Cc Content-Type MIME-Version)\n");
+ 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["TYPE"][0] = "text";
- $header["TYPE"][1] = "plain";
+ $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 = trim(substr($read, 13));
- $cont = substr($cont, 0, strpos($cont, ";"));
- $header["TYPE"][0] = substr($cont, 0, strpos($cont, "/"));
- $header["TYPE"][1] = substr($cont, strpos($cont, "/")+1);
+ $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);
- if (substr(strtolower(trim($read)), 0, 9) == "boundary=") {
- $bound = trim($read);
- $bound = substr($bound, 9);
+ 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;
- $read = fgets($imapConnection, 1024);
+ }
+
+ /** 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 (substr($read, 0, 5) == "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 (substr($read, 0, 5) == "Date:") {
+ else if (strtolower(substr($read, 0, 5)) == "date:") {
$d = substr($read, 5, strlen($read) - 6);
$d = trim($d);
$d = ereg_replace(" ", " ", $d);
$read = fgets($imapConnection, 1024);
}
/** SUBJECT **/
- else if (substr($read, 0, 8) == "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 (substr($read, 0, 3) == "CC:") {
+ else if (strtolower(substr($read, 0, 3)) == "cc:") {
$pos = 0;
$header["CC"][$pos] = trim(substr($read, 4));
$read = fgets($imapConnection, 1024);
}
}
/** TO **/
- else if (substr($read, 0, 3) == "To:") {
+ else if (strtolower(substr($read, 0, 3)) == "to:") {
$pos = 0;
$header["TO"][$pos] = trim(substr($read, 4));
$read = fgets($imapConnection, 1024);
return decodeMime($body, $bound, $type0, $type1);
}
- function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound) {
+ 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($read[$i], 0, 13) == "Content-Type:") {
- $cont = trim(substr($read[$i], 13));
- $cont = substr($cont, 0, strpos($cont, ";"));
- $type0 = substr($cont, 0, strpos($cont, "/"));
- $type1 = substr($cont, strpos($cont, "/")+1);
-
- if (substr(strtolower(trim($read[$i])), 0, 9) == "boundary=") {
- $bound = trim($read[$i]);
- $bound = substr($bound, 9);
+ 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;
+ }
+
+ $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);
+ }
+
+ /** 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++;
}
$read = $entity;
}
- function parsePlainTextMessage($line) {
- $line = "^^$line";
-
- if ((strpos(strtolower($line), "<!") == false) &&
- (strpos(strtolower($line), "<html>") == false) &&
- (strpos(strtolower($line), "</html>") == false)) {
- $line = str_replace("<", "<", $line);
- $line = str_replace(">", ">", $line);
- }
-
- $wrap_at = 86; // 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 = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
- } else if (strpos(trim(str_replace(" ", "", $line)), ">") == 2) {
- $line = substr($line, 2, strlen($line));
- $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
- } else {
- $line = substr($line, 2, strlen($line));
- $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\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) {
- $line = ereg_replace("<BR>", "", $line);
- $start = strpos(strtolower($line), "http://");
- $link = substr($line, $start, strlen($line));
-
- if (strpos($link, " ")) {
- $end = strpos($link, " ")-1;
- }
- else if (strpos($link, " ")) {
- $end = strpos($link, " ")-1;
- }
- else if (strpos($link, "<")) {
- $end = strpos($link, "<");
- }
- else if (strpos($link, ">")) {
- $end = strpos($link, ">");
- }
- else if (strpos($link, "(")) {
- $end = strpos($link, "(")-1;
- }
- else if (strpos($link, ")")) {
- $end = strpos($link, ")")-1;
- }
- else if (strpos($link, "{")) {
- $end = strpos($link, "{")-1;
- }
- else if (strpos($link, "}")) {
- $end = strpos($link, "}")-1;
- }
- else
- $end = strlen($link);
-
- $link = substr($line, $start, $end);
- $end = $end + $start;
- $before = substr($line, 0, $start);
- $after = substr($line, $end, strlen($line));
-
- $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>";
- }
-
- return $line;
- }
?>