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 $read = fgets($imapConnection, 1024);
14 while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) {
15 if (substr(Chop($read), -6) == "EXISTS") {
16 $array = explode(" ", $read);
17 $numberOfMessages = $array[1];
19 $read = fgets($imapConnection, 1024);
23 /** This function sends a request to the IMAP server for headers, 50 at a time
24 ** until $end is reached. I originally had it do them all at one time, but found
25 ** it slightly faster to do it this way.
27 ** Originally we had getMessageHeaders get the headers for one message at a time.
28 ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800
29 ** messages) to about 3.5 seconds.
31 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
33 if (($start > $end) ||
($start < 1)) {
34 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
39 while ($rel_start <= $end) {
40 if ($end - $rel_start > 50) {
41 $rel_end = $rel_start +
49;
45 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
46 $read = fgets($imapConnection, 1024);
48 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
50 if (substr($read, 0, 5) == "From:") {
51 $read = ereg_replace("<", "EMAILSTART--", $read);
52 $read = ereg_replace(">", "--EMAILEND", $read);
53 $from[$pos] = substr($read, 5, strlen($read) - 6);
55 else if (substr($read, 0, 5) == "Date:") {
56 $read = ereg_replace("<", "<", $read);
57 $read = ereg_replace(">", ">", $read);
58 $date[$pos] = substr($read, 5, strlen($read) - 6);
60 else if (substr($read, 0, 8) == "Subject:") {
61 $read = ereg_replace("<", "<", $read);
62 $read = ereg_replace(">", ">", $read);
63 $subject[$pos] = substr($read, 8, strlen($read) - 9);
64 if (strlen(Chop($subject[$pos])) == 0)
65 $subject[$pos] = "(no subject)";
67 else if (substr($read, 0, 1) == ")") {
68 if ($subject[$pos] == "")
69 $subject[$pos] = "(no subject)";
70 else if ($from[$pos] == "")
71 $from[$pos] = "(unknown sender)";
72 else if ($date[$pos] == "")
73 $from[$pos] = gettimeofday();
78 $read = fgets($imapConnection, 1024);
80 $rel_start = $rel_start +
50;
84 function setMessageFlag($imapConnection, $i, $q, $flag) {
85 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
88 /** This function gets the flags for message $j. It does only one message at a
89 ** time, rather than doing groups of messages (like getMessageHeaders does).
90 ** I found it one or two seconds quicker (on a box of 800 messages) to do it
91 ** individually. I'm not sure why it happens like that, but that's what my
92 ** testing found. Perhaps later I will be proven wrong and this will change.
94 function getMessageFlags($imapConnection, $j, &$flags) {
95 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
96 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
97 $read = fgets($imapConnection, 1024);
99 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
100 if (strpos($read, "FLAGS")) {
101 $read = ereg_replace("\(", "", $read);
102 $read = ereg_replace("\)", "", $read);
103 $read = substr($read, strpos($read, "FLAGS")+
6, strlen($read));
105 $flags = explode(" ", $read);;
107 while ($s < count($flags)) {
108 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
115 $read = fgets($imapConnection, 1024);
119 function decodeEmailAddr($sender) {
120 $emailAddr = getEmailAddr($sender);
121 $emailStart = strpos($emailAddr, "EMAILSTART--");
122 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
124 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
125 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
129 function getEmailAddr($sender) {
130 if (strpos($sender, "EMAILSTART--") == false)
133 $start = strpos($sender, "EMAILSTART--");
134 $emailAddr = substr($sender, $start, strlen($sender));
139 function getSender($sender) {
140 if (strpos($sender, "EMAILSTART--") == false)
143 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
144 $second = substr($sender, strpos($sender, "--EMAILEND") +
10, strlen($sender));
145 return "$first$second";
148 function getSenderName($sender) {
149 $name = getSender($sender);
150 $emailAddr = getEmailAddr($sender);
151 $emailStart = strpos($emailAddr, "EMAILSTART--");
152 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
154 if (($emailAddr == "") && ($name == "")) {
157 else if ((strstr($name, "?") != false) ||
(strstr($name, "$") != false) ||
(strstr($name, "%") != false)){
158 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
159 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
162 else if (strlen($name) > 0) {
165 else if (strlen($emailAddr > 0)) {
166 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
167 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
173 // strip out any quotes if they exist
174 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
175 $from = substr($from, 1, strlen($from) - 2);
180 /** returns "true" if the copy was completed successfully.
181 ** returns "false" with an error message if unsuccessful.
183 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
184 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
185 $read = fgets($imapConnection, 1024);
186 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
187 $read = fgets($imapConnection, 1024);
190 if (substr($read, 0, 15) == "mailboxStore NO") {
191 echo "ERROR... $read<BR>";
193 } else if (substr($read, 0, 15) == "mailboxStore OK") {
197 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
201 /** expunges a mailbox **/
202 function expungeBox($imapConnection, $mailbox) {
203 selectMailbox($imapConnection, $mailbox, $num);
204 fputs($imapConnection, "1 EXPUNGE\n");
207 function getFolderNameMinusINBOX($mailbox) {
208 if (substr($mailbox, 0, 6) == "INBOX.")
209 $box = substr($mailbox, 6, strlen($mailbox));
216 /** This function will fetch the body of a given message and format
217 it into our standard format. **/
218 function fetchBody($imapConnection, $id) {
219 fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
221 $read[$count] = fgets($imapConnection, 1024);
222 while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) {
224 $read[$count] = fgets($imapConnection, 1024);
227 /** this loop removes the first line, and the last two which
228 are IMAP information that we don't need. **/
231 while ($i < count($read)) {
232 if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){
233 $readtmp[$j] = $read[$i];
240 /** This loop formats the text, creating links out of linkable stuff too **/
243 while ($count < count($read)) {
244 $read[$count] = "^^$read[$count]";
246 if (strpos(strtolower($read[$count]), "<html>") == true) {
248 } else if (strpos(strtolower($read[$count]), "</html>") == true) {
252 $read[$count] = substr($read[$count], 2, strlen($read[$count]));
254 if ($useHTML == false) {
255 $read[$count] = parsePlainBodyText($read[$count]);
257 $read[$count] = parseHTMLBodyText($read[$count]);
265 function parseHTMLBodyText($line) {
269 function parsePlainBodyText($line) {
272 if ((strpos(strtolower($line), "<!") == false) &&
273 (strpos(strtolower($line), "<html>") == false) &&
274 (strpos(strtolower($line), "</html>") == false)) {
275 $line = str_replace("<", "<", $line);
276 $line = str_replace(">", ">", $line);
279 $wrap_at = 80; // Make this configurable int the config file some time
280 if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning
281 $line = wordWrap($line, $wrap_at);
283 $line = str_replace(" ", " ", $line);
284 $line = str_replace("\t", " ", $line);
286 /** if >> or > are found at the beginning of a line, I'll assume that was
287 replied text, so make it different colors **/
288 if (strpos(trim(str_replace(" ", "", $line)), ">>") == 2) {
289 $line = substr($line, 2, strlen($line));
290 $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n";
291 } else if (strpos(trim(str_replace(" ", "", $line)), ">") == 2) {
292 $line = substr($line, 2, strlen($line));
293 $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n";
295 $line = substr($line, 2, strlen($line));
296 $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n";
299 /** This translates "http://" into a link. It could be made better to accept
300 "www" and "mailto" also. That should probably be added later. **/
301 if (strpos(strtolower($line), "http://") != false) {
302 $start = strpos(strtolower($line), "http://");
303 $text = substr($line, $start, strlen($line));
304 $link = ereg_replace("<BR>", "", $text);
306 if (strpos($link, "&"))
307 $end = strpos($link, "&");
308 else if (strpos($link, "<"))
309 $end = strpos($link, "<");
311 $end = strlen($link);
313 $link = substr($link, 0, $end);
314 $line = str_replace($text, "<A HREF=\"$link\" TARGET=_top>$text</A>", $line);