5 ** Functions for the IMAP connection
9 /** Read from the connection until we get either an OK or BAD message. **/
10 function imapReadData($connection, $pre, $handle_errors, &$response, &$message) {
11 require ("../config/config.php");
13 $read = fgets($connection, 1024);
15 while ((substr($read, 0, strlen("$pre OK")) != "$pre OK") &&
16 (substr($read, 0, strlen("$pre BAD")) != "$pre BAD") &&
17 (substr($read, 0, strlen("$pre NO")) != "$pre NO")) {
18 $data[$counter] = $read;
19 $read = fgets($connection, 1024);
22 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
24 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
25 } else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
27 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
30 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
33 if ($handle_errors == true) {
34 if ($response == "NO") {
35 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>";
37 } else if ($response == "BAD") {
38 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>";
46 /** Parse the incoming mailbox name and return a string that is the FOLDER.MAILBOX **/
47 function findMailboxName($mailbox) {
48 $mailbox = trim($mailbox);
49 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
50 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
51 $pos = strrpos($mailbox, "\"") +
1;
52 $box = substr($mailbox, $pos, strlen($mailbox));
54 $box = substr($mailbox, strrpos($mailbox, " ")+
1, strlen($mailbox));
59 /** Finds the delimeter between mailboxes **/
60 function findMailboxDelimeter($imapConnection) {
61 fputs($imapConnection, ". list \"\" \"\"\n");
62 $read = fgets($imapConnection, 1024);
64 $pos = strrpos($read, "\"");
65 $read = substr($read, 0, $pos);
67 $pos = strrpos($read, "\"");
68 $read = substr($read, 0, $pos);
70 $pos = strrpos($read, "\"");
71 $read = substr($read, 0, $pos);
73 $pos = strrpos($read, "\"");
74 $read = substr($read, $pos+
1, strlen($read));
76 $tmp = fgets($imapConnection, 1024);
80 function getMailboxFlags($mailbox) {
81 $mailbox = trim($mailbox);
82 $mailbox = substr($mailbox, strpos($mailbox, "(")+
1, strlen($mailbox));
83 $mailbox = substr($mailbox, 0, strpos($mailbox, ")"));
84 $mailbox = str_replace("\\", "", $mailbox);
85 $mailbox = strtolower($mailbox);
86 $mailbox = explode(" ", $mailbox);
90 // handles logging onto an imap server.
91 function loginToImapServer($username, $key, $imapServerAddress, $hide) {
92 require("../config/config.php");
94 $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
95 if (!$imapConnection) {
96 echo "Error connecting to IMAP Server.<br>";
97 echo "$errorNumber : $errorString<br>";
100 $serverInfo = fgets($imapConnection, 256);
103 fputs($imapConnection, "a001 LOGIN $username $key\n");
104 $read = fgets($imapConnection, 1024);
105 if ($debug_login == true) {
106 echo "SERVER SAYS: $read<BR>";
109 /** If the login attempt was UNsuccessful, lets see why **/
110 if (substr($read, 0, 7) != "a001 OK") {
112 if (substr($read, 0, 8) == "a001 BAD") {
113 echo "Bad request: $read<BR>";
116 else if (substr($read, 0, 7) == "a001 NO") {
117 echo "<HTML><BODY BGCOLOR=FFFFFF><BR>";
118 echo "<TABLE COLS=1 WIDTH=70% NOBORDER BGCOLOR=FFFFFF ALIGN=CENTER>";
120 echo " <TD BGCOLOR=\"DCDCDC\">";
121 echo " <FONT FACE=\"Arial,Helvetica\" COLOR=CC0000><B><CENTER>ERROR</CENTER></B></FONT>";
122 echo " </TD></TR><TR><TD>";
123 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>";
126 echo "</BODY></HTML>";
130 echo "Unknown error: $read<BR>";
138 return $imapConnection;
141 /** must be sent in the form: user.<USER>.<FOLDER> **/
142 function createFolder($imapConnection, $folder, $type) {
143 require ("../config/config.php");
145 if (strtolower($type) == "noselect") {
146 $dm = findMailboxDelimeter($imapConnection);
147 $folder = "$folder$dm";
151 fputs($imapConnection, "1 create \"$folder\"\n");
152 $data = imapReadData($imapConnection, "1", false, $response, $message);
154 if ($response == "NO") {
155 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>";
156 echo "<FONT FACE=\"Arial,Helvetica\">Possible solutions:<BR><LI>You may need to specify that the folder is a subfolder of INBOX</LI>";
157 echo "<LI>Try renaming the folder to something different.</LI>";
159 } else if ($response == "BAD") {
160 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>";
165 function removeFolder($imapConnection, $folder) {
166 fputs($imapConnection, "1 delete \"$folder\"\n");
167 $data = imapReadData($imapConnection, "1", false, $response, $message);
168 if ($response == "NO") {
169 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=FF0000><B>ERROR</B>: Could not delete the folder $folder.</FONT>";
170 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\">Probable causes:</FONT><BR>";
171 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\"><LI>This folder may contain subfolders. Delete all subfolders first</LI></FONT>";
173 } else if ($response == "BAD") {
174 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>";
179 /** Sends back two arrays, boxesFormatted and boxesUnformatted **/
180 function getFolderList($imapConnection, &$boxes) {
181 require ("../config/config.php");
183 fputs($imapConnection, "1 list \"\" *\n");
184 $str = imapReadData($imapConnection, "1", true, $response, $message);
186 $dm = findMailboxDelimeter($imapConnection);
188 for ($i = 0;$i < count($str); $i++
) {
189 $mailbox = chop($str[$i]);
190 if (substr(findMailboxName($mailbox), 0, 1) != ".") {
191 $boxes[$g]["RAW"] = $mailbox;
193 $mailbox = findMailboxName($mailbox);
194 $periodCount = countCharInString($mailbox, $dm);
196 // indent the correct number of spaces.
197 for ($j = 0;$j < $periodCount;$j++
)
198 $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . " ";
200 $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm);
201 $boxes[$g]["UNFORMATTED"] = $mailbox;
202 $boxes[$g]["ID"] = $g;
209 for ($i = 0; $i < count($original); $i++
) {
210 $boxes[$i]["UNFORMATTED"] = strtolower($boxes[$i]["UNFORMATTED"]);
213 $boxes = ary_sort($boxes, "UNFORMATTED", 1);
215 for ($i = 0; $i < count($original); $i++
) {
216 for ($j = 0; $j < count($original); $j++
) {
217 if ($boxes[$i]["ID"] == $original[$j]["ID"]) {
218 $boxes[$i]["UNFORMATTED"] = $original[$j]["UNFORMATTED"];
219 $boxes[$i]["FORMATTED"] = $original[$j]["FORMATTED"];
220 $boxes[$i]["RAW"] = $original[$j]["RAW"];
225 for ($i = 0; $i < count($boxes); $i++
) {
226 if ($boxes[$i]["UNFORMATTED"] == $special_folders[0]) {
227 $boxesnew[0]["FORMATTED"] = $boxes[$i]["FORMATTED"];
228 $boxesnew[0]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
229 $boxesnew[0]["RAW"] = trim($boxes[$i]["RAW"]);
230 $boxes[$i]["USED"] = true;
233 if ($list_special_folders_first == true) {
234 for ($i = 0; $i < count($boxes); $i++
) {
235 for ($j = 1; $j < count($special_folders); $j++
) {
236 if (substr($boxes[$i]["UNFORMATTED"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
237 $pos = count($boxesnew);
238 $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
239 $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
240 $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
241 $boxes[$i]["USED"] = true;
246 for ($i = 0; $i < count($boxes); $i++
) {
247 if (($boxes[$i]["UNFORMATTED"] != $special_folders[0]) &&
248 ($boxes[$i]["UNFORMATTED"] != ".mailboxlist") &&
249 ($boxes[$i]["USED"] == false)) {
250 $pos = count($boxesnew);
251 $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
252 $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
253 $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
254 $boxes[$i]["USED"] = true;
261 function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) {
262 /** check if they would like to move it to the trash folder or not */
263 if ($move_to_trash == true) {
264 $success = copyMessages($imapConnection, $a, $b, $trash_folder);
265 if ($success == true)
266 setMessageFlag($imapConnection, $a, $b, "Deleted");
268 setMessageFlag($imapConnection, $a, $b, "Deleted");
271 function stripComments($line) {
272 if (strpos($line, ";")) {
273 $line = substr($line, 0, strpos($line, ";"));
276 if (strpos($line, "(") && strpos($line, ")")) {
277 $full_line = $full_line . substr($line, 0, strpos($line, "("));
278 $full_line = $full_line . substr($line, strpos($line, ")")+
1, strlen($line) - strpos($line, ")"));