/**
** imap.php
**
** Functions for the IMAP connection
**
**/
/** Read from the connection until we get either an OK or BAD message. **/
function imapReadData($connection, $pre, $handle_errors, &$response, &$message) {
require ("../config/config.php");
$read = fgets($connection, 1024);
$counter = 0;
while ((substr($read, 0, strlen("$pre OK")) != "$pre OK") &&
(substr($read, 0, strlen("$pre BAD")) != "$pre BAD") &&
(substr($read, 0, strlen("$pre NO")) != "$pre NO")) {
$data[$counter] = $read;
$read = fgets($connection, 1024);
$counter++;
}
if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
$response = "OK";
$message = trim(substr($read, strlen("$pre OK"), strlen($read)));
} else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
$response = "BAD";
$message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
} else {
$response = "NO";
$message = trim(substr($read, strlen("$pre NO"), strlen($read)));
}
if ($handle_errors == true) {
if ($response == "NO") {
echo "ERROR: Could not complete request.
Reason given: $message
";
exit;
} else if ($response == "BAD") {
echo "ERROR: Bad or malformed request.
Server responded: $message
";
exit;
}
}
return $data;
}
/** Parse the incoming mailbox name and return a string that is the FOLDER.MAILBOX **/
function findMailboxName($mailbox) {
$mailbox = trim($mailbox);
if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
$mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
$pos = strrpos($mailbox, "\"") + 1;
$box = substr($mailbox, $pos, strlen($mailbox));
} else {
$box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
}
return $box;
}
/** Finds the delimeter between mailboxes **/
function findMailboxDelimeter($imapConnection) {
fputs($imapConnection, ". list \"\" \"\"\n");
$read = fgets($imapConnection, 1024);
$pos = strrpos($read, "\"");
$read = substr($read, 0, $pos);
$pos = strrpos($read, "\"");
$read = substr($read, 0, $pos);
$pos = strrpos($read, "\"");
$read = substr($read, 0, $pos);
$pos = strrpos($read, "\"");
$read = substr($read, $pos+1, strlen($read));
$tmp = fgets($imapConnection, 1024);
return $read;
}
function getMailboxFlags($mailbox) {
$mailbox = trim($mailbox);
$mailbox = substr($mailbox, strpos($mailbox, "(")+1, strlen($mailbox));
$mailbox = substr($mailbox, 0, strpos($mailbox, ")"));
$mailbox = str_replace("\\", "", $mailbox);
$mailbox = strtolower($mailbox);
$mailbox = explode(" ", $mailbox);
return $mailbox;
}
// handles logging onto an imap server.
function loginToImapServer($username, $key, $imapServerAddress, $hide) {
require("../config/config.php");
$imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
if (!$imapConnection) {
echo "Error connecting to IMAP Server.
";
echo "$errorNumber : $errorString
";
exit;
}
$serverInfo = fgets($imapConnection, 256);
// login
fputs($imapConnection, "a001 LOGIN $username $key\n");
$read = fgets($imapConnection, 1024);
if ($debug_login == true) {
echo "SERVER SAYS: $read
";
}
if (substr($read, 0, 7) != "a001 OK") {
if (!$hide) {
if (substr($read, 0, 8) == "a001 BAD") {
echo "Bad request: $read
";
exit;
}
else if (substr($read, 0, 7) == "a001 NO") {
echo "
";
echo "
";
echo " |
";
echo " Unknown user or password incorrect. Click here to try again. |