X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=blobdiff_plain;f=functions%2Fimap.php;h=7d6eb9a4e36b2c37a8814580835e28d5762acf4e;hp=4f11ed9072d7f43691a5f7232e6204fa280d4137;hb=23fc64b344f7a063ace1dbe877cae9052ede79d6;hpb=aceb0d5c925965586360e1a9560b09e6173d49d6
diff --git a/functions/imap.php b/functions/imap.php
index 4f11ed90..7d6eb9a4 100644
--- a/functions/imap.php
+++ b/functions/imap.php
@@ -7,33 +7,99 @@
**/
/** Read from the connection until we get either an OK or BAD message. **/
- function imapReadData($connection) {
+ function imapReadData($connection, $pre, $handle_errors, &$response, &$message) {
+ require ("../config/config.php");
+
$read = fgets($connection, 1024);
$counter = 0;
- while ((substr($read, strpos($read, " ") + 1, 2) != "OK") && (substr($read, strpos($read, " ") + 1, 3) != "BAD")) {
+ 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) {
- // start at -2 so that we skip the initial quote at the end of the mailbox name
- $i = -2;
- $char = substr($mailbox, $i, 1);
- while ($char != "\"") {
- $i--;
- $temp .= $char;
- $char = substr($mailbox, $i, 1);
- }
- echo $tmp;
- return strrev($temp);
+ $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);
+
+ if (strrpos($read, "\"") == strlen($read)) {
+ $pos = strrpos($read, "\"");
+ $read = substr($read, 0, $pos);
+
+ $pos = strrpos($read, "\"");
+ $read = substr($read, 0, $pos);
+ } else {
+ $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($imapConnection, $mailbox) {
+ $name = findMailboxName($mailbox);
+ fputs ($imapConnection, "1 LIST \"$name\" *\n");
+ $data = imapReadData($imapConnection, "1", true, $response, $message);
+ $mailbox = $data[0];
+ $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) {
+ function loginToImapServer($username, $key, $imapServerAddress, $hide) {
+ require("../config/config.php");
+
$imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
if (!$imapConnection) {
echo "Error connecting to IMAP Server.
";
@@ -43,56 +109,217 @@
$serverInfo = fgets($imapConnection, 256);
// login
- fputs($imapConnection, "1 login $username $key\n");
+ fputs($imapConnection, "a001 LOGIN \"$username\" \"$key\"\n");
$read = fgets($imapConnection, 1024);
-
- if (strpos($read, "NO")) {
- error_username_password_incorrect();
- exit;
+ if ($debug_login == true) {
+ echo "SERVER SAYS: $read
";
}
-
+
+ /** If the login attempt was UNsuccessful, lets see why **/
+ 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 " Unknown user or password incorrect. Click here to try again. |