+
+ function getFolderNameMinusINBOX($mailbox) {
+ if (substr($mailbox, 0, 6) == "INBOX.")
+ $box = substr($mailbox, 6, 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) {
+ 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]), "<html>") == true) {
+ $useHTML = true;
+ } else if (strpos(strtolower($read[$count]), "</html>") == 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), "<html>") == false) &&
+ (strpos(strtolower($line), "</html>") == 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 = "<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) {
+ $start = strpos(strtolower($line), "http://");
+ $text = substr($line, $start, strlen($line));
+ $link = ereg_replace("<BR>", "", $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, "<A HREF=\"$link\" TARGET=_top>$text</A>", $line);
+ }
+
+ return $line;
+ }
+
+ function getMessageHeadersTo($imapConnection, $start, $end, &$to) {
+ $rel_start = $start;
+ if (($start > $end) || ($start < 1)) {
+ echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
+ exit;
+ }
+
+ $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 (To)\n");
+ $read = fgets($imapConnection, 1024);
+
+ while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
+ if (substr($read, 0, 3) == "To:") {
+ $read = ereg_replace("<", "<", $read);
+ $read = ereg_replace(">", ">", $read);
+ $to[$pos] = substr($read, 3, strlen($read));
+ if (strlen(Chop($to[$pos])) == 0)
+ $to[$pos] = "Unknown Recipients";
+ }
+ else if (substr($read, 0, 1) == ")") {
+ if ($subject[$pos] == "")
+ $subject[$pos] = "Unknown Recipients";
+ $pos++;
+ }
+
+ $read = fgets($imapConnection, 1024);
+ }
+ $rel_start = $rel_start + 50;
+ }
+ }
+