I believe everything is compatable with all IMAP servers now
[squirrelmail.git] / functions / imap.php
1 <?
2 /**
3 ** imap.php
4 **
5 ** Functions for the IMAP connection
6 **
7 **/
8
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");
12
13 $read = fgets($connection, 1024);
14 $counter = 0;
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);
20 $counter++;
21 }
22 if (substr($read, 0, strlen("$pre OK")) == "$pre OK") {
23 $response = "OK";
24 $message = trim(substr($read, strlen("$pre OK"), strlen($read)));
25 } else if (substr($read, 0, strlen("$pre BAD")) == "$pre BAD") {
26 $response = "BAD";
27 $message = trim(substr($read, strlen("$pre BAD"), strlen($read)));
28 } else {
29 $response = "NO";
30 $message = trim(substr($read, strlen("$pre NO"), strlen($read)));
31 }
32
33 if ($handle_errors == true) {
34 if ($response == "NO") {
35 echo "<B><FONT COLOR=FF0000>ERROR</FONT><FONT COLOR=CC0000>: Could not complete request.</B> </FONT><BR><FONT COLOR=CC0000>&nbsp;&nbsp;<B>Reason given:</B> $message</FONT><BR><BR>";
36 exit;
37 } else if ($response == "BAD") {
38 echo "<B><FONT COLOR=FF0000>ERROR</FONT><FONT COLOR=CC0000>: Bad or malformed request.</B></FONT><BR><FONT COLOR=CC0000>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT><BR><BR>";
39 exit;
40 }
41 }
42
43 return $data;
44 }
45
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));
53 } else {
54 $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
55 }
56 return $box;
57 }
58
59 /** Finds the delimeter between mailboxes **/
60 function findMailboxDelimeter($imapConnection) {
61 fputs($imapConnection, ". list \"\" \"\"\n");
62 $read = fgets($imapConnection, 1024);
63
64 $pos = strrpos($read, "\"");
65 $read = substr($read, 0, $pos);
66
67 $pos = strrpos($read, "\"");
68 $read = substr($read, 0, $pos);
69
70 $pos = strrpos($read, "\"");
71 $read = substr($read, 0, $pos);
72
73 $pos = strrpos($read, "\"");
74 $read = substr($read, $pos+1, strlen($read));
75
76 $tmp = fgets($imapConnection, 1024);
77 return $read;
78 }
79
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);
87 return $mailbox;
88 }
89
90 // handles logging onto an imap server.
91 function loginToImapServer($username, $key, $imapServerAddress, $hide) {
92 require("../config/config.php");
93
94 $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
95 if (!$imapConnection) {
96 echo "Error connecting to IMAP Server.<br>";
97 echo "$errorNumber : $errorString<br>";
98 exit;
99 }
100 $serverInfo = fgets($imapConnection, 256);
101
102 // login
103 fputs($imapConnection, "a001 LOGIN $username $key\n");
104 $read = fgets($imapConnection, 1024);
105 if ($debug_login == true) {
106 echo "SERVER SAYS: $read<BR>";
107 }
108
109 if (substr($read, 0, 7) != "a001 OK") {
110 if (!$hide) {
111 if (substr($read, 0, 8) == "a001 BAD") {
112 echo "Bad request: $read<BR>";
113 exit;
114 }
115 else if (substr($read, 0, 7) == "a001 NO") {
116 echo "<BR>";
117 echo "<TABLE COLS=1 WIDTH=70% NOBORDER BGCOLOR=\"$color[4]\" ALIGN=CENTER>";
118 echo " <TR>";
119 echo " <TD BGCOLOR=\"$color[0]\">";
120 echo " <FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[2]\"><B><CENTER>ERROR</CENTER></B></FONT>";
121 echo " </TD></TR><TR><TD>";
122 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>";
123 echo " </TD></TR>";
124 echo "</TABLE>";
125 echo "</BODY></HTML>";
126 exit;
127 }
128 else {
129 echo "Unknown error: $read<BR>";
130 exit;
131 }
132 } else {
133 exit;
134 }
135 }
136
137 return $imapConnection;
138 }
139
140 /** must be sent in the form: user.<USER>.<FOLDER> **/
141 function createFolder($imapConnection, $folder, $type) {
142 require ("../config/config.php");
143
144 if (strtolower($type) == "noselect") {
145 $dm = findMailboxDelimeter($imapConnection);
146 $folder = "$folder$dm";
147 } else {
148 $folder = "$folder";
149 }
150 fputs($imapConnection, "1 create \"$folder\"\n");
151 $data = imapReadData($imapConnection, "1", false, $response, $message);
152
153 if ($response == "NO") {
154 echo "<B><FONT COLOR=FF0000>ERROR</FONT><FONT COLOR=CC0000>: Could not complete request.</B> </FONT><BR><FONT COLOR=CC0000>&nbsp;&nbsp;<B>Reason given:</B> $message</FONT><BR><BR>";
155 echo "Possible solutions:<BR><LI>You may need to specify that the folder is a subfolder of INBOX</LI>";
156 exit;
157 } else if ($response == "BAD") {
158 echo "<B><FONT COLOR=FF0000>ERROR</FONT><FONT COLOR=CC0000>: Bad or malformed request.</B></FONT><BR><FONT COLOR=CC0000>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT><BR><BR>";
159 exit;
160 }
161 }
162
163 function removeFolder($imapConnection, $folder) {
164 fputs($imapConnection, "1 delete \"$folder\"\n");
165 $data = imapReadData($imapConnection, "1", false, $response, $message);
166 if ($response == "NO") {
167 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=FF0000><B>ERROR</B>: Could not delete the folder $folder.</FONT>";
168 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\">Probable causes:</FONT><BR>";
169 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\"><LI>This folder may contain subfolders. Delete all subfolders first</LI></FONT>";
170 exit;
171 } else if ($response == "BAD") {
172 echo "<B><FONT COLOR=FF0000>ERROR</FONT><FONT COLOR=CC0000>: Bad or malformed request.</B></FONT><BR><FONT COLOR=CC0000>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT><BR><BR>";
173 exit;
174 }
175 }
176
177 /** Sends back two arrays, boxesFormatted and boxesUnformatted **/
178 function getFolderList($imapConnection, &$boxesFormatted, &$boxesUnformatted, &$boxesRaw) {
179 fputs($imapConnection, "1 list \"\" *\n");
180 $str = imapReadData($imapConnection, "1", true, $response, $message);
181
182 $dm = findMailboxDelimeter($imapConnection);
183 for ($i = 0;$i < count($str); $i++) {
184 $mailbox = chop($str[$i]);
185 $boxesRaw[$i] = $mailbox;
186
187 $mailbox = findMailboxName($mailbox);
188 $periodCount = countCharInString($mailbox, $dm);
189
190 // indent the correct number of spaces.
191 for ($j = 0;$j < $periodCount;$j++)
192 $boxesFormatted[$i] = "$boxesFormatted[$i]&nbsp;&nbsp;";
193
194 $boxesFormatted[$i] = $boxesFormatted[$i] . readShortMailboxName($mailbox, $dm);
195 $boxesUnformatted[$i] = $mailbox;
196 }
197 }
198
199 function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) {
200 /** check if they would like to move it to the trash folder or not */
201 if ($move_to_trash == true) {
202 $success = copyMessages($imapConnection, $a, $b, $trash_folder);
203 if ($success == true)
204 setMessageFlag($imapConnection, $a, $b, "Deleted");
205 } else {
206 setMessageFlag($imapConnection, $a, $b, "Deleted");
207 }
208 }
209 function stripComments($line) {
210 if (strpos($line, ";")) {
211 $line = substr($line, 0, strpos($line, ";"));
212 }
213
214 if (strpos($line, "(") && strpos($line, ")")) {
215 $full_line = $full_line . substr($line, 0, strpos($line, "("));
216 $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")"));
217 } else {
218 $full_line = $line;
219 }
220 return $full_line;
221 }
222 ?>