<?
- /**
- ** imap.php
- **
- ** Functions for the IMAP connection
- **
+ /** This just includes the different sections of the imap functions.
+ ** They have been organized into these sections for simplicity sake.
**/
-
- /** 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 "<BR><B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Could not complete request.</B> </FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000> <B>Reason given:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
- exit;
- } else if ($response == "BAD") {
- echo "<BR><B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Bad or malformed request.</B></FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000> <B>Server responded:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
- 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. This should now be more compliant across
- different server types that vary in their RFC2060 compliance.
- **/
- function findMailboxDelimeter($imapConnection) {
- fputs($imapConnection, ". list \"\" *\n");
- $read = imapReadData($imapConnection, ".", true, $a, $b);
- $quotePosition = strpos($read[0], "\"");
- $delim = substr($read[0], $quotePosition+1, 1);
-
- return $delim;
- }
-
- 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, $hide) {
- require("../config/config.php");
-
- $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
- if (!$imapConnection) {
- echo "Error connecting to IMAP Server.<br>";
- echo "$errorNumber : $errorString<br>";
- 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<BR>";
- }
-
- /** 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<BR>";
- exit;
- }
- else if (substr($read, 0, 7) == "a001 NO") {
- echo "<HTML><BODY BGCOLOR=FFFFFF><BR>";
- echo "<TABLE COLS=1 WIDTH=70% NOBORDER BGCOLOR=FFFFFF ALIGN=CENTER>";
- echo " <TR>";
- echo " <TD BGCOLOR=\"DCDCDC\">";
- echo " <FONT FACE=\"Arial,Helvetica\" COLOR=CC0000><B><CENTER>ERROR</CENTER></B></FONT>";
- echo " </TD></TR><TR><TD>";
- echo " <CENTER><FONT FACE=\"Arial,Helvetica\"><BR>Unknown user or password incorrect.<BR><A HREF=\"login.php\" TARGET=_top>Click here to try again</A>.</FONT></CENTER>";
- echo " </TD></TR>";
- echo "</TABLE>";
- echo "</BODY></HTML>";
- exit;
- }
- else {
- echo "Unknown error: $read<BR>";
- exit;
- }
- } else {
- exit;
- }
- }
-
- return $imapConnection;
- }
-
- /** must be sent in the form: user.<USER>.<FOLDER> **/
- function createFolder($imapConnection, $folder, $type) {
- require ("../config/config.php");
-
- if (strtolower($type) == "noselect") {
- $dm = findMailboxDelimeter($imapConnection);
- $folder = "$folder$dm";
- } else {
- $folder = "$folder";
- }
- fputs($imapConnection, "1 create \"$folder\"\n");
- $data = imapReadData($imapConnection, "1", false, $response, $message);
-
- if ($response == "NO") {
- echo "<BR><B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Could not complete request.</B> </FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000> <B>Reason given:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
- echo "<FONT FACE=\"Arial,Helvetica\">Possible solutions:<BR><LI>You may need to specify that the folder is a subfolder of INBOX</LI>";
- echo "<LI>Try renaming the folder to something different.</LI>";
- exit;
- } else if ($response == "BAD") {
- echo "<B><FONT FACE=\"Arial,Helvetica\" COLOR=FF0000>ERROR</FONT FACE=\"Arial,Helvetica\"><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000>: Bad or malformed request.</B></FONT FACE=\"Arial,Helvetica\"><BR><FONT FACE=\"Arial,Helvetica\" COLOR=CC0000> <B>Server responded:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
- exit;
- }
- fputs($imapConnection, "1 SUBSCRIBE \"$folder\"\n");
- $data = imapReadData($imapConnection, "1", true, $response, $message);
- }
-
- /**
- This is a recursive function that checks to see if the folder has any subfolders,
- and if so, it calls removeFolder on the subfolders first, then removes the parent
- folder.
- **/
- function removeFolder($imapConnection, $folder, $delimiter) {
- global $boxes;
-
- // bug if there are 2 subfolders of a folder, it won't get to the second one
- for ($i = 0; $i < count($boxes); $i++) {
- if (strstr($boxes[$i]["UNFORMATTED"], $folder . $delimiter)) {
- $newDelete = $boxes[$i]["UNFORMATTED"];
- $boxes = removeElement($boxes, $i);
- removeFolder($imapConnection, $newDelete, $boxes, $delimiter);
- }
- }
-
- fputs ($imapConnection, "1 unsubscribe \"$folder\"\n");
- $data = imapReadData($imapConnection, "1", true, $response, $message);
- fputs($imapConnection, "1 delete \"$folder\"\n");
- $data = imapReadData($imapConnection, "1", false, $response, $message);
- if ($response == "NO") {
- echo "<FONT FACE=\"Arial,Helvetica\" COLOR=FF0000><B>ERROR</B>: Could not delete the folder $folder.</FONT><BR>";
- echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\">Probable causes:</FONT><BR>";
- echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\"><LI>This folder may contain subfolders. Delete all subfolders first</LI></FONT>";
- echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\"><BR><BR>The actual message returned from the server was:<BR>$message</FONT>";
- echo "</BODY></HTML>";
- exit;
- } else if ($response == "BAD") {
- echo "<B><FONT COLOR=FF0000>ERROR</FONT><FONT COLOR=CC0000>: Bad or malformed request.</B></FONT><BR><FONT COLOR=CC0000> <B>Server responded:</B> $message</FONT><BR><BR>";
- echo "</BODY></HTML>";
- exit;
- }
- }
-
- /** Sends back two arrays, boxesFormatted and boxesUnformatted **/
- function getFolderList($imapConnection, &$boxes) {
- require ("../config/config.php");
- if (!function_exists("ary_sort"))
- include("../functions/array.php");
-
- /** First we get the inbox **/
- fputs($imapConnection, "1 LIST \"\" INBOX\n");
- $str = imapReadData($imapConnection, "1", true, $response, $message);
- $dm = findMailboxDelimeter($imapConnection);
- $g = 0;
- for ($i = 0;$i < count($str); $i++) {
- $mailbox = chop($str[$i]);
- if (substr(findMailboxName($mailbox), 0, 1) != ".") {
- $boxes[$g]["RAW"] = $mailbox;
-
- $mailbox = findMailboxName($mailbox);
- $periodCount = countCharInString($mailbox, $dm);
- if (substr($mailbox, -1) == $dm)
- $periodCount--;
-
- // indent the correct number of spaces.
- for ($j = 0;$j < $periodCount;$j++)
- $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . " ";
-
- $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm);
- $boxes[$g]["UNFORMATTED"] = $mailbox;
- $boxes[$g]["ID"] = $g;
- $g++;
- }
- }
-
- /** Next, we get all subscribed folders **/
- fputs($imapConnection, "1 LSUB \"\" *\n");
- $str = imapReadData($imapConnection, "1", true, $response, $message);
- $dm = findMailboxDelimeter($imapConnection);
- for ($i = 0;$i < count($str); $i++) {
- $mailbox = chop($str[$i]);
- if (substr(findMailboxName($mailbox), 0, 1) != ".") {
- $boxes[$g]["RAW"] = $mailbox;
-
- // Get the mailbox name and format it. If there is a $dm at the end of it, remove it.
- $mailbox = findMailboxName($mailbox);
- $periodCount = countCharInString($mailbox, $dm);
- if (substr($mailbox, -1) == $dm)
- $periodCount = $periodCount - 1;
-
- // indent the correct number of spaces.
- for ($j = 0;$j < $periodCount;$j++)
- $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . " ";
-
- $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm);
- $boxes[$g]["UNFORMATTED"] = $mailbox;
- $boxes[$g]["ID"] = $g;
- $g++;
- }
- }
-
- $original = $boxes;
-
- for ($i = 0; $i < count($original); $i++) {
- $boxes[$i]["UNFORMATTED"] = strtolower($boxes[$i]["UNFORMATTED"]);
- }
-
- $boxes = ary_sort($boxes, "UNFORMATTED", 1);
-
- for ($i = 0; $i < count($original); $i++) {
- for ($j = 0; $j < count($original); $j++) {
- if ($boxes[$i]["ID"] == $original[$j]["ID"]) {
- $boxes[$i]["UNFORMATTED"] = $original[$j]["UNFORMATTED"];
- $boxes[$i]["FORMATTED"] = $original[$j]["FORMATTED"];
- $boxes[$i]["RAW"] = $original[$j]["RAW"];
- }
- }
- }
-
- for ($i = 0; $i < count($boxes); $i++) {
- if ($boxes[$i]["UNFORMATTED"] == $special_folders[0]) {
- $boxesnew[0]["FORMATTED"] = $boxes[$i]["FORMATTED"];
- $boxesnew[0]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
- $boxesnew[0]["RAW"] = trim($boxes[$i]["RAW"]);
- $boxes[$i]["USED"] = true;
- }
- }
- if ($list_special_folders_first == true) {
- for ($i = 0; $i < count($boxes); $i++) {
- for ($j = 1; $j < count($special_folders); $j++) {
- if (substr($boxes[$i]["UNFORMATTED"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
- $pos = count($boxesnew);
- $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
- $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
- $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
- $boxes[$i]["USED"] = true;
- }
- }
- }
- }
- for ($i = 0; $i < count($boxes); $i++) {
- if (($boxes[$i]["UNFORMATTED"] != $special_folders[0]) &&
- ($boxes[$i]["UNFORMATTED"] != ".mailboxlist") &&
- ($boxes[$i]["USED"] == false)) {
- $pos = count($boxesnew);
- $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
- $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
- $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
- $boxes[$i]["USED"] = true;
- }
- }
-
- $boxes = $boxesnew;
- }
-
- function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) {
- /** check if they would like to move it to the trash folder or not */
- if (($move_to_trash == true) && (folderExists($imapConnection, $trash_folder))) {
- $success = copyMessages($imapConnection, $a, $b, $trash_folder);
- if ($success == true)
- setMessageFlag($imapConnection, $a, $b, "Deleted");
- else
- echo "There was an error moving the messages.<BR>Messages NOT deleted.";
- } else {
- setMessageFlag($imapConnection, $a, $b, "Deleted");
- }
- if ($auto_expunge == true)
- expungeBox($imapConnection, $mailbox);
- }
-
- function stripComments($line) {
- if (strpos($line, ";")) {
- $line = substr($line, 0, strpos($line, ";"));
- }
-
- if (strpos($line, "(") && strpos($line, ")")) {
- $full_line = $full_line . substr($line, 0, strpos($line, "("));
- $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")"));
- } else {
- $full_line = $line;
- }
- return $full_line;
- }
-
- function folderExists($imapConnection, $folderName) {
- getFolderList($imapConnection, $folders);
- $found = false;
- for ($i = 0; ($i < count($folders)) && (!$found); $i++) {
- if ($folders[$i]["UNFORMATTED"] == $folderName)
- $found = true;
- }
- return $found;
- }
+
+ include ("../functions/imap_mailbox.php");
+ include ("../functions/imap_messages.php");
+ include ("../functions/imap_general.php");
?>
+
--- /dev/null
+<?
+ /**
+ ** imap.php
+ **
+ ** This implements all functions that do general imap functions.
+ **/
+
+ /******************************************************************************
+ ** Reads the output from the IMAP stream. If handle_errors is set to true,
+ ** this will also handle all errors that are received. If it is not set,
+ ** the errors will be sent back through $response and $message
+ ******************************************************************************/
+ function sqimap_read_data ($imap_stream, $pre, $handle_errors, $response, $message) {
+ global $color;
+
+ $read = fgets ($imap_stream, 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 ($imap_stream, 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 "<br><b><font face=\"arial,helvetica\" color=$color[2]>";
+ echo _("ERROR : Could not complete request.");
+ echo "</b><br>";
+ echo _("Reason Given: ");
+ echo "$message</font><br>";
+ exit;
+ } else if ($response == "BAD") {
+ echo "<br><b><font face=\"arial,helvetica\" color=$color[2]>";
+ echo _("ERROR : Bad or malformed request.");
+ echo "</b><br>";
+ echo _("Server responded: ");
+ echo "$message</font><br>";
+ exit;
+ }
+ }
+
+ return $data;
+ }
+
+
+
+
+ /******************************************************************************
+ ** Logs the user into the imap server. If $hide is set, no error messages
+ ** will be displayed. This function returns the imap connection handle.
+ ******************************************************************************/
+ function sqimap_login ($username, $password, $imap_server_address, $hide) {
+ global $color;
+ $imap_stream = fsockopen ($imap_server_address, 143, &$error_number, &$error_string);
+ $server_info = fgets ($imap_stream, 1024);
+
+ /** Do some error correction **/
+ if (!$imap_stream) {
+ if (!$hide) {
+ echo "Error connecting to IMAP server: $imap_server_address.<br>\n";
+ echo "$error_number : $error_string<br>\n";
+ }
+ exit;
+ }
+
+ fputs ($imap_stream, "a001 LOGIN \"$username\" \"$password\"\n");
+ $read = fgets ($imap_stream, 1024);
+
+ /** If the connection was not successful, lets see why **/
+ if (substr($read, 0, 7) != "a001 OK") {
+ if (!$hide) {
+ if (substr($read, 0, 8) == "a001 BAD") {
+ echo "Bad request: $read<br>\n";
+ exit;
+ } else if (substr($read, 0, 7) == "a001 NO") {
+ ?>
+ <html>
+ <body bgcolor=<? echo $color[4] ?>>
+ <br>
+ <table width=70% noborder bgcolor=<? echo $color[4] ?> align=center>
+ <tr>
+ <td bgcolor=<? echo $color[0] ?>>
+ <font face="arial,helvetica" color=<? echo $color[2] ?>>
+ <center>
+ <? echo _("ERROR") ?>
+ </center>
+ </font>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <font face="arial,helvetica" color=<? echo $color[2] ?>>
+ <center>
+ <? echo _("Unknown user or password incorrect.") ?><br>
+ <a href="login.php"><? echo _("Click here to try again") ?></a>
+ </center>
+ </font>
+ </td>
+ </tr>
+ </table>
+ </body>
+ </html>
+ <?
+ exit;
+ } else {
+ echo "Unknown error: $read<br>";
+ exit;
+ }
+ } else {
+ exit;
+ }
+ }
+
+ return $imap_stream;
+ }
+
+
+
+
+ /******************************************************************************
+ ** Simply logs out the imap session
+ ******************************************************************************/
+ function sqimap_logout ($imap_stream) {
+ fputs ($imap_stream, "a001 LOGOUT\n");
+ }
+
+
+
+ /******************************************************************************
+ ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
+ ******************************************************************************/
+ function sqimap_get_delimiter ($imap_stream) {
+ fputs ($imap_stream, ". LIST \"\" *\n");
+ $read = sqimap_read_data($imap_stream, ".", true, $a, $b);
+ $quote_position = strpos ($read[0], "\"");
+ $delim = substr ($read[0], $quote_position+1, 1);
+
+ return $delim;
+ }
+
+
+
+
+ /******************************************************************************
+ ** Gets the number of messages in the current mailbox.
+ ******************************************************************************/
+ function sqimap_get_num_messages ($imap_stream, $mailbox) {
+ fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\n");
+ $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
+ for ($i = 0; $i < count($read_ary); $i++) {
+ if (substr(trim($read_ary[$i]), -6) == EXISTS) {
+ $array = explode (" ", $read_ary[$i]);
+ $num = $array[1];
+ }
+ }
+ return $num;
+ }
+
+
+ /******************************************************************************
+ ** Returns a displayable email address
+ ******************************************************************************/
+ function sqimap_find_email ($string) {
+ /** Luke Ehresman <lehresma@css.tayloru.edu>
+ ** <lehresma@css.tayloru.edu>
+ ** lehresma@css.tayloru.edu
+ **/
+
+ if (strpos($string, "<") && strpos($string, ">")) {
+ $string = substr($string, strpos($string, "<")+1);
+ $string = substr($string, 0, strpos($string, ">"));
+ }
+ return $string;
+ }
+
+
+ /******************************************************************************
+ ** Returns a displayable email address
+ ******************************************************************************/
+ function sqimap_find_displayable_name ($string) {
+ if (strpos($string, "<") && strpos($string, ">")) {
+ if (strpos($string, "<") == 0) {
+ $string = sqimap_find_email($string);
+ } else {
+ $string = substr($string, 0, strpos($string, "<"));
+ }
+ }
+ return $string;
+ }
+
+
+
+ /******************************************************************************
+ ** Returns the number of unseen messages in this folder
+ ******************************************************************************/
+ function sqimap_unseen_messages ($imap_stream, &$num_unseen) {
+ fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\n");
+ $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
+ $unseen = false;
+
+ if (strlen($read_ary[0]) > 10) {
+ $unseen = true;
+ $ary = explode (" ", $read_ary[0]);
+ $num_unseen = count($ary) - 2;
+ } else {
+ $unseen = false;
+ $num_unseen = 0;
+ }
+
+ return $unseen;
+ }
+?>
--- /dev/null
+<?
+ /**
+ ** imap_mailbox.php
+ **
+ ** This impliments all functions that manipulate mailboxes
+ **/
+
+
+ /******************************************************************************
+ ** Expunges a mailbox
+ ******************************************************************************/
+ function sqimap_mailbox_expunge ($imap_stream, $mailbox) {
+ sqimap_mailbox_select ($imap_stream, $mailbox);
+ fputs ($imap_stream, "a001 EXPUNGE\n");
+ $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+ }
+
+
+ /******************************************************************************
+ ** Checks whether or not the specified mailbox exists
+ ******************************************************************************/
+ function sqimap_mailbox_exists ($imap_stream, $mailbox) {
+ $boxes = sqimap_mailbox_list ($imap_stream);
+ $found = false;
+ for ($i = 0; $i < count ($boxes); $i++) {
+ if ($boxes[$i]["unformatted"] == $mailbox)
+ $found = true;
+ }
+ return $found;
+ }
+
+
+
+ /******************************************************************************
+ ** Selects a mailbox
+ ******************************************************************************/
+ function sqimap_mailbox_select ($imap_stream, $mailbox) {
+ fputs ($imap_stream, "a001 SELECT \"$mailbox\"\n");
+ $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+ }
+
+
+
+ /******************************************************************************
+ ** Creates a folder
+ ******************************************************************************/
+ function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
+ if (strtolower($type) == "noselect") {
+ $dm = sqimap_get_delimiter($imap_stream);
+ $mailbox = $mailbox.$dm;
+ }
+ fputs ($imap_stream, "a001 CREATE \"$mailbox\"\n");
+ $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+
+ sqimap_subscribe ($imap_stream, $mailbox);
+ }
+
+
+
+ /******************************************************************************
+ ** Subscribes to an existing folder
+ ******************************************************************************/
+ function sqimap_subscribe ($imap_stream, $mailbox) {
+ fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\n");
+ $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+ }
+
+
+
+
+ /******************************************************************************
+ ** Unsubscribes to an existing folder
+ ******************************************************************************/
+ function sqimap_unsubscribe ($imap_stream, $mailbox) {
+ fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\n");
+ $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+ }
+
+
+
+
+ /******************************************************************************
+ ** This is a recursive function that checks to see if the folder has any
+ ** subfolders, and if so it calls itself on the subfolders first, then
+ ** removes the parent folder.
+ ******************************************************************************/
+ function sqimap_mailbox_delete ($imap_stream, $mailbox) {
+ global $boxes;
+
+ $dm = sqimap_get_delimiter($imap_stream);
+ for ($i = 0; $i < count($boxes); $i++) {
+ if (strstr($boxes[$i]["unformatted"], $mailbox . $dm)) {
+ $new_delete = $boxes[$i]["unformatted"];
+ $boxes = removeElement($boxes, $i);
+// sqimap_mailbox_delete ($imap_stream, $new_delete);
+ }
+ }
+ sqimap_unsubscribe ($imap_stream, $mailbox);
+ fputs ($imap_stream, "a001 DELETE \"$mailbox\"\n");
+ $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+ }
+
+
+
+ /******************************************************************************
+ ** Returns sorted mailbox lists in several different ways.
+ ** The array returned looks like this:
+ ******************************************************************************/
+ function sqimap_mailbox_list ($imap_stream) {
+ if (!function_exists ("ary_sort"))
+ include ("../functions/array.php");
+
+ $dm = sqimap_get_delimiter ($imap_stream);
+
+ fputs ($imap_stream, "a001 LIST \"\" INBOX\n");
+ $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+ $g = 0;
+ $phase = "inbox";
+ for ($i = 0; $i < count($read_ary); $i++) {
+ if (substr ($read_ary[$i], 0, 4) != "a001") {
+ $boxes[$g]["raw"] = $read_ary[$i];
+
+ $mailbox = find_mailbox_name($read_ary[$i]);
+ $dm_count = countCharInString($mailbox, $dm);
+ if (substr($mailbox, -1) == $dm)
+ $dm_count--;
+
+ for ($j = 0; $j < $dm_count; $j++)
+ $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
+ $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
+
+ if (substr($mailbox, -1) == $dm)
+ $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
+ $boxes[$g]["unformatted"] = $mailbox;
+ $boxes[$g]["id"] = $g;
+
+ /** Now lets get the flags for this mailbox **/
+ fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\n");
+ $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
+
+ $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
+ $flags = substr($flags, 0, strpos($flags, ")"));
+ $flags = str_replace("\\", "", $flags);
+ $flags = trim(strtolower($flags));
+ if ($flags) {
+ $boxes[$g]["flags"] = explode(" ", $flags);
+ }
+ }
+ $g++;
+
+ if (!$read_ary[$i+1]) {
+ if ($phase == "inbox") {
+ fputs ($imap_stream, "a001 LSUB \"\" *\n");
+ $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+ $phase = "lsub";
+ $i--;
+ }
+ }
+ }
+
+ $original = $boxes;
+ $boxes = ary_sort($boxes, "unformatted", 1);
+
+ for ($i = 0; $i < count($boxes); $i++) {
+ for ($j = 0; $j < count($original); $j++) {
+ if ($boxes[$i]["id"] == $original[$j]["id"]) {
+ $boxes[$i] = $original[$j];
+ }
+ }
+ }
+
+ for ($i = 0; $i < count($boxes); $i++) {
+ if ($boxes[$i]["unformatted"] == $special_folders[0]) {
+ $boxesnew[0] = $boxes[$i];
+ }
+ }
+ if ($list_special_folders_first == true) {
+ for ($i = 0; $i < count($boxes); $i++) {
+ for ($j = 1; $j < count($special_folders); $j++) {
+ if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
+ $pos = count($boxesnew);
+ $boxesnew[$pos] = $boxes[$i];
+ $boxes[$i]["used"] = true;
+ }
+ }
+ }
+ }
+ for ($i = 0; $i < count($boxes); $i++) {
+ if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
+ ($boxes[$i]["used"] == false)) {
+ $pos = count($boxesnew);
+ $boxesnew[$pos] = $boxes[$i];
+ $boxes[$i]["used"] = true;
+ }
+ }
+
+ return $boxes;
+ }
+
+?>
--- /dev/null
+<?
+ /**
+ ** imap_messages.php
+ **
+ ** This implements functions that manipulate messages
+ **/
+
+ /******************************************************************************
+ ** Copies specified messages to specified folder
+ ******************************************************************************/
+ function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
+ fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\n");
+ $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+ }
+
+ /******************************************************************************
+ ** Deletes specified messages and moves them to trash if possible
+ ******************************************************************************/
+ function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
+ global $move_to_trash, $trash_folder, $auto_expunge;
+
+ if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) {
+ sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
+ sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
+ } else {
+ sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
+ }
+ if ($auto_expunge == true)
+ sqimap_mailbox_expunge ($imap_stream, $mailbox);
+ }
+
+ /******************************************************************************
+ ** Sets the specified messages with specified flag
+ ******************************************************************************/
+ function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
+ fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\n");
+ $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+ }
+
+ /******************************************************************************
+ ** Returns some general header information -- FROM, DATE, and SUBJECT
+ ******************************************************************************/
+ function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) {
+ fputs ($imap_stream, "a001 FETCH $id:$id RFC822.HEADER.LINES (From Subject Date)\n");
+ $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+
+ for ($i = 0; $i < count($read); $i++) {
+ if (substr($read[$i], 0, 5) == "From:") {
+ $from = sqimap_find_displayable_name(substr($read[$i], 5));
+ } else if (substr($read[$i], 0, 5) == "Date:") {
+ $date = substr($read[$i], 5);
+ } else if (substr($read[$i], 0, 8) == "Subject:") {
+ $subject = htmlspecialchars(substr($read[$i], 8));
+ if (strlen(trim($subject)) == 0)
+ $subject = "(no subject)";
+ }
+ }
+ }
+
+ /******************************************************************************
+ ** Returns the flags for the specified messages
+ ******************************************************************************/
+ function sqimap_get_flags () {
+ }
+
+ /******************************************************************************
+ ** Returns a message array with all the information about a message. See
+ ** the documentation folder for more information about this array.
+ ******************************************************************************/
+ function sqimap_get_message ($imap_stream, $id, $mailbox) {
+ $message["INFO"]["ID"] = $id;
+ $message["INFO"]["MAILBOX"] = $mailbox;
+ $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
+ $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]);
+ return $message;
+ }
+
+ /******************************************************************************
+ ** Wrapper function that reformats the header information.
+ ******************************************************************************/
+ function sqimap_get_message_header ($imap_stream, $id) {
+ fputs ($imap_stream, "a001 FETCH $id:$id RFC822.HEADER\n");
+ $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+
+ return sqimap_get_header($imap_stream, $read);
+ }
+
+ /******************************************************************************
+ ** Wrapper function that returns entity headers for use by decodeMime
+ ******************************************************************************/
+ function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
+ $header = sqimap_get_header($imap_stream, $read);
+ $type0 = $header["TYPE0"];
+ $type1 = $header["TYPE1"];
+ $bound = $header["BOUNDARY"];
+ $encoding = $header["ENCODING"];
+ $charset = $header["CHARSET"];
+ $filename = $header["FILENAME"];
+ }
+
+ /******************************************************************************
+ ** Queries the IMAP server and gets all header information.
+ ******************************************************************************/
+ function sqimap_get_header ($imap_stream, $read) {
+ $i = 0;
+ while ($i < count($read)) {
+ if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
+ $header["MIME"] = true;
+ $i++;
+ }
+
+ /** ENCODING TYPE **/
+ else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
+ $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
+ $i++;
+ }
+
+ /** CONTENT-TYPE **/
+ else if (substr($read[$i], 0, 13) == "Content-Type:") {
+ $cont = strtolower(trim(substr($read[$i], 13)));
+ if (strpos($cont, ";"))
+ $cont = substr($cont, 0, strpos($cont, ";"));
+
+ if (strpos($cont, "/")) {
+ $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
+ $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
+ } else {
+ $header["TYPE0"] = $cont;
+ }
+
+ $line = $read[$i];
+ $i++;
+ while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
+ str_replace("\n", "", $line);
+ str_replace("\n", "", $read[$i]);
+ $line = "$line $read[$i]";
+ $i++;
+ }
+
+ /** Detect the boundary of a multipart message **/
+ if (strpos(strtolower(trim($line)), "boundary=")) {
+ $pos = strpos($line, "boundary=") + 9;
+ $bound = trim($line);
+ if (strpos($line, " ", $pos) > 0) {
+ $bound = substr($bound, $pos, strpos($line, " ", $pos));
+ } else {
+ $bound = substr($bound, $pos);
+ }
+ $bound = str_replace("\"", "", $bound);
+ $header["BOUNDARY"] = $bound;
+ }
+
+ /** Detect the charset **/
+ if (strpos(strtolower(trim($line)), "charset=")) {
+ $pos = strpos($line, "charset=") + 8;
+ $charset = trim($line);
+ if (strpos($line, " ", $pos) > 0) {
+ $charset = substr($charset, $pos, strpos($line, " ", $pos));
+ } else {
+ $charset = substr($charset, $pos);
+ }
+ $charset = str_replace("\"", "", $charset);
+ $header["CHARSET"] = $charset;
+ } else {
+ $header["CHARSET"] = "us-ascii";
+ }
+
+ /** Detects filename if any **/
+ if (strpos(strtolower(trim($line)), "name=")) {
+ $pos = strpos($line, "name=") + 5;
+ $name = trim($line);
+ if (strpos($line, " ", $pos) > 0) {
+ $name = substr($name, $pos, strpos($line, " ", $pos));
+ } else {
+ $name = substr($name, $pos);
+ }
+ $name = str_replace("\"", "", $name);
+ $header["FILENAME"] = $name;
+ }
+ }
+
+ /** REPLY-TO **/
+ else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
+ $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i])));
+ $i++;
+ }
+
+ /** FROM **/
+ else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
+ $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
+ if ($header["REPLYTO"] == "")
+ $header["REPLYTO"] = $header["FROM"];
+ $i++;
+ }
+ /** DATE **/
+ else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
+ $d = substr($read[$i], 5);
+ $d = trim($d);
+ $d = ereg_replace(" ", " ", $d);
+ $d = explode(" ", $d);
+ $header["DATE"] = getTimeStamp($d);
+ $i++;
+ }
+ /** SUBJECT **/
+ else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
+ $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
+ if (strlen(Chop($header["SUBJECT"])) == 0)
+ $header["SUBJECT"] = "(no subject)";
+ $i++;
+ }
+ /** CC **/
+ else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
+ $pos = 0;
+ $header["CC"][$pos] = trim(substr($read[$i], 4));
+ $i++;
+ while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) {
+ $pos++;
+ $header["CC"][$pos] = trim($read[$i]);
+ $i++;
+ }
+ }
+ /** TO **/
+ else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
+ $pos = 0;
+ $header["TO"][$pos] = trim(substr($read[$i], 4));
+ $i++;
+ while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){
+ $pos++;
+ $header["TO"][$pos] = trim($read[$i]);
+ $i++;
+ }
+ }
+
+ /** ERROR CORRECTION **/
+ else if (substr($read[$i], 0, 1) == ")") {
+ if ($header["SUBJECT"] == "")
+ $header["SUBJECT"] = "(no subject)";
+
+ if ($header["FROM"] == "")
+ $header["FROM"] = "(unknown sender)";
+
+ if ($header["DATE"] == "")
+ $header["DATE"] = time();
+ $i++;
+ }
+ else {
+ $i++;
+ }
+ }
+ return $header;
+ }
+
+ /******************************************************************************
+ ** Returns the body of a message.
+ ******************************************************************************/
+ function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1) {
+ fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\n");
+ $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+
+ $i = 0;
+ $j = 0;
+ while ($i < count($read)) {
+ if ( ($i != 0) && ($i != count($read)-1) && ($i != count($read)) ) {
+ $bodytmp[$j] = $read[$i];
+ $j++;
+ }
+ $i++;
+ }
+ $body = $bodytmp;
+ return decodeMime($body, $bound, $type0, $type1);
+ }
+?>
function printMessageInfo($imapConnection, $t, $i, $from, $subject, $dateString, $answered, $seen, $mailbox, $sort, $startMessage) {
require ("../config/config.php");
- $senderName = getSenderName($from);
+ $senderName = $from;
$urlMailbox = urlencode($mailbox);
$subject = trim(stripslashes($subject));
echo "<TR>\n";
function showMessagesForMailbox($imapConnection, $mailbox, $numMessages, $startMessage, $sort, $color) {
include ("../config/config.php");
- if (1 <= $numMessages) {
- getMessageHeaders($imapConnection, 1, $numMessages, $from, $subject, $date);
- getMessageFlags($imapConnection, 1, $numMessages, $flags);
+ if ($numMessages >= 1) {
+ for ($q = 0; $q < $numMessages; $q++) {
+ sqimap_get_small_header ($imapConnection, $q+1, $f, $s, $d);
+ $from[$q] = $f;
+ $date[$q] = $d;
+ $subject[$q] = $s;
+ }
+ $flags = sqimap_get_flags ($imapConnection, 1, $numMessages);
}
$j = 0;
$messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
$messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
$messages[$j]["ID"] = $j+1;
- $messages[$j]["FROM"] = getSenderName($from[$j]);
+ $messages[$j]["FROM"] = $from[$j];
$messages[$j]["SUBJECT"] = $subject[$j];
$messages[$j]["FLAG_DELETED"] = false;
$messages[$j]["FLAG_ANSWERED"] = false;
echo " <NOBR><FONT FACE=\"Arial,Helvetica\"><SMALL>". _("Move selected to:") ."</SMALL></FONT>";
echo " <TT><SMALL><SELECT NAME=\"targetMailbox\">";
- getFolderList($imapConnection, $boxes);
+ $boxes = sqimap_mailbox_list($imapConnection, $boxes);
for ($i = 0; $i < count($boxes); $i++) {
$use_folder = true;
for ($p = 0; $p < count($special_folders); $p++) {
- if ($boxes[$i]["UNFORMATTED"] == $special_folders[0]) {
+ if ($boxes[$i]["unformatted"] == $special_folders[0]) {
$use_folder = true;
- } else if ($boxes[$i]["UNFORMATTED"] == $special_folders[$p]) {
+ } else if ($boxes[$i]["unformatted"] == $special_folders[$p]) {
$use_folder = false;
- } else if (substr($boxes[$i]["UNFORMATTED"], 0, strlen($trash_folder)) == $trash_folder) {
+ } else if (substr($boxes[$i]["unformatted"], 0, strlen($trash_folder)) == $trash_folder) {
$use_folder = false;
}
}
if ($use_folder == true) {
- $box = $boxes[$i]["UNFORMATTED"];
- $box2 = $boxes[$i]["FORMATTED"];
+ $box = $boxes[$i]["unformatted"];
+ $box2 = replace_spaces($boxes[$i]["formatted"]);
echo " <OPTION VALUE=\"$box\">$box2\n";
}
}
echo " <A HREF=\"right_main.php?sort=5&startMessage=1&mailbox=$urlMailbox\" TARGET=\"right\"><IMG SRC=\"../images/sort_none.gif\" BORDER=0></A></TD>\n";
echo "</TR>";
+
// loop through and display the info for each message.
$t = 0; // $t is used for the checkbox number
if ($numMessages == 0) { // if there's no messages in this folder
- echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN=4><CENTER><BR><B>THIS FOLDER IS EMPTY</B><BR> </CENTER></TD></TR>";
+ echo "<TR><TD BGCOLOR=\"$color[4]\" COLSPAN=4><CENTER><BR><B>". _("THIS FOLDER IS EMPTY") ."</B><BR> </CENTER></TD></TR>";
} else if ($startMessage == $endMessage) { // if there's only one message in the box, handle it different.
$i = $startMessage - 1;
printMessageInfo($imapConnection, $t, $msgs[$i]["ID"], $msgs[$i]["FROM"], $msgs[$i]["SUBJECT"], $msgs[$i]["DATE_STRING"], $msgs[$i]["FLAG_ANSWERED"], $msgs[$i]["FLAG_SEEN"], $mailbox, $sort, $startMessage);
$p++;
}
/** All of these values are getting passed back to us **/
- fetchEntityHeader($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
+ sqimap_get_entity_header($imapConnection, $entity_header, $ent_type0, $ent_type1, $ent_bound, $encoding, $charset, $filename);
}
if (substr($haystack, -1) == $needle)
$haystack = substr($haystack, 0, strlen($haystack) - 1);
- if (strpos($haystack, $needle)) {
+ if (strrpos($haystack, $needle)) {
$pos = strrpos($haystack, $needle) + 1;
$data = substr($haystack, $pos, strlen($haystack));
} else {
/* SquirrelMail version number -- DO NOT CHANGE */
$version = "0.3pre1";
+
+
+ function find_mailbox_name ($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);
+ } else {
+ $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
+ }
+ return $box;
+ }
+
+ function replace_spaces ($string) {
+ return str_replace(" ", " ", $string);
+ }
+
+ function replace_escaped_spaces ($string) {
+ return str_replace(" ", " ", $string);
+ }
?>