| 1 | <? |
| 2 | /** |
| 3 | ** mailbox.php |
| 4 | ** |
| 5 | ** This contains functions that request information about a mailbox. Including |
| 6 | ** reading and parsing headers, getting folder information, etc. |
| 7 | ** |
| 8 | **/ |
| 9 | |
| 10 | function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) { |
| 11 | // select mailbox |
| 12 | fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n"); |
| 13 | $read = fgets($imapConnection, 1024); |
| 14 | $unseen = false; |
| 15 | while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) { |
| 16 | if (substr(Chop($read), -6) == "EXISTS") { |
| 17 | $array = explode(" ", $read); |
| 18 | $numberOfMessages = $array[1]; |
| 19 | } |
| 20 | $read = fgets($imapConnection, 1024); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | function unseenMessages($imapConnection, &$numUnseen) { |
| 25 | fputs($imapConnection, "1 SEARCH UNSEEN NOT DELETED\n"); |
| 26 | $read = fgets($imapConnection, 1024); |
| 27 | $unseen = false; |
| 28 | |
| 29 | if (strlen($read) > 10) { |
| 30 | $unseen = true; |
| 31 | $ary = explode(" ", $read); |
| 32 | $numUnseen = count($ary) - 2; |
| 33 | } |
| 34 | else { |
| 35 | $unseen = false; |
| 36 | $numUnseen = 0; |
| 37 | } |
| 38 | |
| 39 | $read = fgets($imapConnection, 1024); |
| 40 | return $unseen; |
| 41 | } |
| 42 | |
| 43 | /** This function sends a request to the IMAP server for headers, 50 at a time |
| 44 | ** until $end is reached. I originally had it do them all at one time, but found |
| 45 | ** it slightly faster to do it this way. |
| 46 | ** |
| 47 | ** Originally we had getMessageHeaders get the headers for one message at a time. |
| 48 | ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800 |
| 49 | ** messages) to about 3.5 seconds. |
| 50 | **/ |
| 51 | function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) { |
| 52 | $rel_start = $start; |
| 53 | if (($start > $end) || ($start < 1)) { |
| 54 | echo "Error in message header fetching. Start message: $start, End message: $end<BR>"; |
| 55 | exit; |
| 56 | } |
| 57 | |
| 58 | $pos = 0; |
| 59 | while ($rel_start <= $end) { |
| 60 | if ($end - $rel_start > 50) { |
| 61 | $rel_end = $rel_start + 49; |
| 62 | } else { |
| 63 | $rel_end = $end; |
| 64 | } |
| 65 | fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n"); |
| 66 | $read = fgets($imapConnection, 1024); |
| 67 | |
| 68 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
| 69 | |
| 70 | if (substr($read, 0, 5) == "From:") { |
| 71 | $read = ereg_replace("<", "EMAILSTART--", $read); |
| 72 | $read = ereg_replace(">", "--EMAILEND", $read); |
| 73 | $from[$pos] = substr($read, 5, strlen($read) - 6); |
| 74 | } |
| 75 | else if (substr($read, 0, 5) == "Date:") { |
| 76 | $read = ereg_replace("<", "<", $read); |
| 77 | $read = ereg_replace(">", ">", $read); |
| 78 | $date[$pos] = substr($read, 5, strlen($read) - 6); |
| 79 | } |
| 80 | else if (substr($read, 0, 8) == "Subject:") { |
| 81 | $read = ereg_replace("<", "<", $read); |
| 82 | $read = ereg_replace(">", ">", $read); |
| 83 | $subject[$pos] = substr($read, 8, strlen($read) - 9); |
| 84 | if (strlen(Chop($subject[$pos])) == 0) |
| 85 | $subject[$pos] = "(no subject)"; |
| 86 | } |
| 87 | else if (substr($read, 0, 1) == ")") { |
| 88 | if ($subject[$pos] == "") |
| 89 | $subject[$pos] = "(no subject)"; |
| 90 | else if ($from[$pos] == "") |
| 91 | $from[$pos] = "(unknown sender)"; |
| 92 | else if ($date[$pos] == "") |
| 93 | $from[$pos] = gettimeofday(); |
| 94 | |
| 95 | $pos++; |
| 96 | } |
| 97 | |
| 98 | $read = fgets($imapConnection, 1024); |
| 99 | } |
| 100 | $rel_start = $rel_start + 50; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function setMessageFlag($imapConnection, $i, $q, $flag) { |
| 105 | fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n"); |
| 106 | } |
| 107 | |
| 108 | /** This function gets the flags for message $j. It does only one message at a |
| 109 | ** time, rather than doing groups of messages (like getMessageHeaders does). |
| 110 | ** I found it one or two seconds quicker (on a box of 800 messages) to do it |
| 111 | ** individually. I'm not sure why it happens like that, but that's what my |
| 112 | ** testing found. Perhaps later I will be proven wrong and this will change. |
| 113 | **/ |
| 114 | function getMessageFlags($imapConnection, $j, &$flags) { |
| 115 | /** * 2 FETCH (FLAGS (\Answered \Seen)) */ |
| 116 | fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n"); |
| 117 | $read = fgets($imapConnection, 1024); |
| 118 | $count = 0; |
| 119 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
| 120 | if (strpos($read, "FLAGS")) { |
| 121 | $read = ereg_replace("\(", "", $read); |
| 122 | $read = ereg_replace("\)", "", $read); |
| 123 | $read = substr($read, strpos($read, "FLAGS")+6, strlen($read)); |
| 124 | $read = trim($read); |
| 125 | $flags = explode(" ", $read);; |
| 126 | $s = 0; |
| 127 | while ($s < count($flags)) { |
| 128 | $flags[$s] = substr($flags[$s], 1, strlen($flags[$s])); |
| 129 | $s++; |
| 130 | } |
| 131 | } else { |
| 132 | $flags[0] = "None"; |
| 133 | } |
| 134 | $count++; |
| 135 | $read = fgets($imapConnection, 1024); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | function decodeEmailAddr($sender) { |
| 140 | $emailAddr = getEmailAddr($sender); |
| 141 | $emailStart = strpos($emailAddr, "EMAILSTART--"); |
| 142 | $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; |
| 143 | |
| 144 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
| 145 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
| 146 | return $emailAddr; |
| 147 | } |
| 148 | |
| 149 | function getEmailAddr($sender) { |
| 150 | if (strpos($sender, "EMAILSTART--") == false) |
| 151 | return ""; |
| 152 | |
| 153 | $start = strpos($sender, "EMAILSTART--"); |
| 154 | $emailAddr = substr($sender, $start, strlen($sender)); |
| 155 | |
| 156 | return $emailAddr; |
| 157 | } |
| 158 | |
| 159 | function getSender($sender) { |
| 160 | if (strpos($sender, "EMAILSTART--") == false) |
| 161 | return ""; |
| 162 | |
| 163 | $first = substr($sender, 0, strpos($sender, "EMAILSTART--")); |
| 164 | $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender)); |
| 165 | return "$first$second"; |
| 166 | } |
| 167 | |
| 168 | function getSenderName($sender) { |
| 169 | $name = getSender($sender); |
| 170 | $emailAddr = getEmailAddr($sender); |
| 171 | $emailStart = strpos($emailAddr, "EMAILSTART--"); |
| 172 | $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; |
| 173 | |
| 174 | if (($emailAddr == "") && ($name == "")) { |
| 175 | $from = $sender; |
| 176 | } |
| 177 | else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){ |
| 178 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
| 179 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
| 180 | $from = $emailAddr; |
| 181 | } |
| 182 | else if (strlen($name) > 0) { |
| 183 | $from = $name; |
| 184 | } |
| 185 | else if (strlen($emailAddr > 0)) { |
| 186 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
| 187 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
| 188 | $from = $emailAddr; |
| 189 | } |
| 190 | |
| 191 | $from = trim($from); |
| 192 | |
| 193 | // strip out any quotes if they exist |
| 194 | if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\"")) |
| 195 | $from = substr($from, 1, strlen($from) - 2); |
| 196 | |
| 197 | return $from; |
| 198 | } |
| 199 | |
| 200 | /** returns "true" if the copy was completed successfully. |
| 201 | ** returns "false" with an error message if unsuccessful. |
| 202 | **/ |
| 203 | function copyMessages($imapConnection, $from_id, $to_id, $folder) { |
| 204 | fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n"); |
| 205 | $read = fgets($imapConnection, 1024); |
| 206 | while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) { |
| 207 | $read = fgets($imapConnection, 1024); |
| 208 | } |
| 209 | |
| 210 | if (substr($read, 0, 15) == "mailboxStore NO") { |
| 211 | return false; |
| 212 | } else if (substr($read, 0, 15) == "mailboxStore OK") { |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>"; |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | /** expunges a mailbox **/ |
| 221 | function expungeBox($imapConnection, $mailbox) { |
| 222 | selectMailbox($imapConnection, $mailbox, $num); |
| 223 | fputs($imapConnection, "1 EXPUNGE\n"); |
| 224 | } |
| 225 | |
| 226 | function getFolderNameMinusINBOX($mailbox) { |
| 227 | if (substr($mailbox, 0, 6) == "INBOX.") |
| 228 | $box = substr($mailbox, 6, strlen($mailbox)); |
| 229 | else |
| 230 | $box = $mailbox; |
| 231 | |
| 232 | return $box; |
| 233 | } |
| 234 | |
| 235 | /** This function will fetch the body of a given message and format |
| 236 | it into our standard format. **/ |
| 237 | function fetchBody($imapConnection, $id) { |
| 238 | fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n"); |
| 239 | $count = 0; |
| 240 | $read[$count] = fgets($imapConnection, 1024); |
| 241 | while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) { |
| 242 | $count++; |
| 243 | $read[$count] = fgets($imapConnection, 1024); |
| 244 | } |
| 245 | |
| 246 | /** this loop removes the first line, and the last two which |
| 247 | are IMAP information that we don't need. **/ |
| 248 | $i = 0; |
| 249 | $j = 0; |
| 250 | while ($i < count($read)) { |
| 251 | if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){ |
| 252 | $readtmp[$j] = $read[$i]; |
| 253 | $j++; |
| 254 | } |
| 255 | $i++; |
| 256 | } |
| 257 | $read = $readtmp; |
| 258 | |
| 259 | /** This loop formats the text, creating links out of linkable stuff too **/ |
| 260 | $count = 0; |
| 261 | $useHTML= false; |
| 262 | while ($count < count($read)) { |
| 263 | $read[$count] = "^^$read[$count]"; |
| 264 | |
| 265 | if (strpos(strtolower($read[$count]), "<html") == true) { |
| 266 | $useHTML = true; |
| 267 | } else if (strpos(strtolower($read[$count]), "</html>") == true) { |
| 268 | $useHTML = false; |
| 269 | } |
| 270 | |
| 271 | $read[$count] = substr($read[$count], 2, strlen($read[$count])); |
| 272 | |
| 273 | if ($useHTML == false) { |
| 274 | $read[$count] = parsePlainBodyText($read[$count]); |
| 275 | } else { |
| 276 | $read[$count] = parseHTMLBodyText($read[$count]); |
| 277 | } |
| 278 | |
| 279 | $count++; |
| 280 | } |
| 281 | return $read; |
| 282 | } |
| 283 | |
| 284 | function parseHTMLBodyText($line) { |
| 285 | return $line; |
| 286 | } |
| 287 | |
| 288 | function parsePlainBodyText($line) { |
| 289 | $line = "^^$line"; |
| 290 | |
| 291 | if ((strpos(strtolower($line), "<!") == false) && |
| 292 | (strpos(strtolower($line), "<html>") == false) && |
| 293 | (strpos(strtolower($line), "</html>") == false)) { |
| 294 | $line = str_replace("<", "<", $line); |
| 295 | $line = str_replace(">", ">", $line); |
| 296 | } |
| 297 | |
| 298 | $wrap_at = 86; // Make this configurable int the config file some time |
| 299 | if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning |
| 300 | $line = wordWrap($line, $wrap_at); |
| 301 | |
| 302 | $line = str_replace(" ", " ", $line); |
| 303 | $line = str_replace("\t", " ", $line); |
| 304 | |
| 305 | /** if >> or > are found at the beginning of a line, I'll assume that was |
| 306 | replied text, so make it different colors **/ |
| 307 | if (strpos(trim(str_replace(" ", "", $line)), ">>") == 2) { |
| 308 | $line = substr($line, 2, strlen($line)); |
| 309 | $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n"; |
| 310 | } else if (strpos(trim(str_replace(" ", "", $line)), ">") == 2) { |
| 311 | $line = substr($line, 2, strlen($line)); |
| 312 | $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n"; |
| 313 | } else { |
| 314 | $line = substr($line, 2, strlen($line)); |
| 315 | $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n"; |
| 316 | } |
| 317 | |
| 318 | /** This translates "http://" into a link. It could be made better to accept |
| 319 | "www" and "mailto" also. That should probably be added later. **/ |
| 320 | if (strpos(strtolower($line), "http://") != false) { |
| 321 | $line = ereg_replace("<BR>", "", $line); |
| 322 | $start = strpos(strtolower($line), "http://"); |
| 323 | $link = substr($line, $start, strlen($line)); |
| 324 | |
| 325 | if (strpos($link, " ")) { |
| 326 | $end = strpos($link, " ")-1; |
| 327 | } |
| 328 | else if (strpos($link, " ")) { |
| 329 | $end = strpos($link, " ")-1; |
| 330 | } |
| 331 | else if (strpos($link, "<")) { |
| 332 | $end = strpos($link, "<"); |
| 333 | } |
| 334 | else if (strpos($link, ">")) { |
| 335 | $end = strpos($link, ">"); |
| 336 | } |
| 337 | else if (strpos($link, "(")) { |
| 338 | $end = strpos($link, "(")-1; |
| 339 | } |
| 340 | else if (strpos($link, ")")) { |
| 341 | $end = strpos($link, ")")-1; |
| 342 | } |
| 343 | else if (strpos($link, "{")) { |
| 344 | $end = strpos($link, "{")-1; |
| 345 | } |
| 346 | else if (strpos($link, "}")) { |
| 347 | $end = strpos($link, "}")-1; |
| 348 | } |
| 349 | else |
| 350 | $end = strlen($link); |
| 351 | |
| 352 | $link = substr($line, $start, $end); |
| 353 | $end = $end + $start; |
| 354 | $before = substr($line, 0, $start); |
| 355 | $after = substr($line, $end, strlen($line)); |
| 356 | |
| 357 | $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>"; |
| 358 | } |
| 359 | |
| 360 | return $line; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | $start = strpos(strtolower($line), "http://"); |
| 365 | $text = substr($line, $start, strlen($line)); |
| 366 | $linktext = substr($link, 0, $end); |
| 367 | $link = trim(ereg_replace("<BR>", "", $linktext)); |
| 368 | |
| 369 | |
| 370 | // $line = str_replace($text, "<A HREF=\"$link\" TARGET=_top>$link</A>", $line); |
| 371 | */ |
| 372 | |
| 373 | function getMessageHeadersTo($imapConnection, $start, $end, &$to) { |
| 374 | $rel_start = $start; |
| 375 | if (($start > $end) || ($start < 1)) { |
| 376 | echo "Error in message header fetching. Start message: $start, End message: $end<BR>"; |
| 377 | exit; |
| 378 | } |
| 379 | |
| 380 | $pos = 0; |
| 381 | while ($rel_start <= $end) { |
| 382 | if ($end - $rel_start > 50) { |
| 383 | $rel_end = $rel_start + 49; |
| 384 | } else { |
| 385 | $rel_end = $end; |
| 386 | } |
| 387 | fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (To)\n"); |
| 388 | $read = fgets($imapConnection, 1024); |
| 389 | |
| 390 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
| 391 | if (substr($read, 0, 3) == "To:") { |
| 392 | $read = ereg_replace("<", "<", $read); |
| 393 | $read = ereg_replace(">", ">", $read); |
| 394 | $to[$pos] = substr($read, 3, strlen($read)); |
| 395 | if (strlen(Chop($to[$pos])) == 0) |
| 396 | $to[$pos] = "Unknown Recipients"; |
| 397 | } |
| 398 | else if (substr($read, 0, 1) == ")") { |
| 399 | if ($subject[$pos] == "") |
| 400 | $subject[$pos] = "Unknown Recipients"; |
| 401 | $pos++; |
| 402 | } |
| 403 | |
| 404 | $read = fgets($imapConnection, 1024); |
| 405 | } |
| 406 | $rel_start = $rel_start + 50; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | ?> |