added step for copying config_default.php to config.php
[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 "<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>&nbsp;&nbsp;<B>Reason given:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
36 exit;
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>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT FACE=\"Arial,Helvetica\"><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 if (strrpos($read, "\"") == strlen($read)) {
65 $pos = strrpos($read, "\"");
66 $read = substr($read, 0, $pos);
67
68 $pos = strrpos($read, "\"");
69 $read = substr($read, 0, $pos);
70 } else {
71 $pos = strrpos($read, " ");
72 $read = substr($read, 0, $pos);
73 }
74
75 $pos = strrpos($read, "\"");
76 $read = substr($read, 0, $pos);
77
78 $pos = strrpos($read, "\"");
79 $read = substr($read, $pos+1, strlen($read));
80
81 $tmp = fgets($imapConnection, 1024);
82 return $read;
83 }
84
85 function getMailboxFlags($mailbox) {
86 $mailbox = trim($mailbox);
87 $mailbox = substr($mailbox, strpos($mailbox, "(")+1, strlen($mailbox));
88 $mailbox = substr($mailbox, 0, strpos($mailbox, ")"));
89 $mailbox = str_replace("\\", "", $mailbox);
90 $mailbox = strtolower($mailbox);
91 $mailbox = explode(" ", $mailbox);
92 return $mailbox;
93 }
94
95 // handles logging onto an imap server.
96 function loginToImapServer($username, $key, $imapServerAddress, $hide) {
97 require("../config/config.php");
98
99 $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
100 if (!$imapConnection) {
101 echo "Error connecting to IMAP Server.<br>";
102 echo "$errorNumber : $errorString<br>";
103 exit;
104 }
105 $serverInfo = fgets($imapConnection, 256);
106
107 // login
108 fputs($imapConnection, "a001 LOGIN \"$username\" \"$key\"\n");
109 $read = fgets($imapConnection, 1024);
110 if ($debug_login == true) {
111 echo "SERVER SAYS: $read<BR>";
112 }
113
114 /** If the login attempt was UNsuccessful, lets see why **/
115 if (substr($read, 0, 7) != "a001 OK") {
116 if (!$hide) {
117 if (substr($read, 0, 8) == "a001 BAD") {
118 echo "Bad request: $read<BR>";
119 exit;
120 }
121 else if (substr($read, 0, 7) == "a001 NO") {
122 echo "<HTML><BODY BGCOLOR=FFFFFF><BR>";
123 echo "<TABLE COLS=1 WIDTH=70% NOBORDER BGCOLOR=FFFFFF ALIGN=CENTER>";
124 echo " <TR>";
125 echo " <TD BGCOLOR=\"DCDCDC\">";
126 echo " <FONT FACE=\"Arial,Helvetica\" COLOR=CC0000><B><CENTER>ERROR</CENTER></B></FONT>";
127 echo " </TD></TR><TR><TD>";
128 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>";
129 echo " </TD></TR>";
130 echo "</TABLE>";
131 echo "</BODY></HTML>";
132 exit;
133 }
134 else {
135 echo "Unknown error: $read<BR>";
136 exit;
137 }
138 } else {
139 exit;
140 }
141 }
142
143 return $imapConnection;
144 }
145
146 /** must be sent in the form: user.<USER>.<FOLDER> **/
147 function createFolder($imapConnection, $folder, $type) {
148 require ("../config/config.php");
149
150 if (strtolower($type) == "noselect") {
151 $dm = findMailboxDelimeter($imapConnection);
152 $folder = "$folder$dm";
153 } else {
154 $folder = "$folder";
155 }
156 fputs($imapConnection, "1 create \"$folder\"\n");
157 $data = imapReadData($imapConnection, "1", false, $response, $message);
158
159 if ($response == "NO") {
160 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>&nbsp;&nbsp;<B>Reason given:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
161 echo "<FONT FACE=\"Arial,Helvetica\">Possible solutions:<BR><LI>You may need to specify that the folder is a subfolder of INBOX</LI>";
162 echo "<LI>Try renaming the folder to something different.</LI>";
163 exit;
164 } else if ($response == "BAD") {
165 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>&nbsp;&nbsp;<B>Server responded:</B> $message</FONT FACE=\"Arial,Helvetica\"><BR><BR>";
166 exit;
167 }
168 fputs($imapConnection, "1 SUBSCRIBE \"$folder\"\n");
169 $data = imapReadData($imapConnection, "1", true, $response, $message);
170 }
171
172 function removeFolder($imapConnection, $folder) {
173 fputs($imapConnection, "1 delete \"$folder\"\n");
174 $data = imapReadData($imapConnection, "1", false, $response, $message);
175 if ($response == "NO") {
176 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=FF0000><B>ERROR</B>: Could not delete the folder $folder.</FONT>";
177 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\">Probable causes:</FONT><BR>";
178 echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\"><LI>This folder may contain subfolders. Delete all subfolders first</LI></FONT>";
179 exit;
180 } else if ($response == "BAD") {
181 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>";
182 exit;
183 }
184 }
185
186 /** Sends back two arrays, boxesFormatted and boxesUnformatted **/
187 function getFolderList($imapConnection, &$boxes) {
188 require ("../config/config.php");
189 if (!function_exists("ary_sort"))
190 include("../functions/array.php");
191
192 /** First we get the inbox **/
193 fputs($imapConnection, "1 LIST \"\" INBOX\n");
194 $str = imapReadData($imapConnection, "1", true, $response, $message);
195 $dm = findMailboxDelimeter($imapConnection);
196 $g = 0;
197 for ($i = 0;$i < count($str); $i++) {
198 $mailbox = chop($str[$i]);
199 if (substr(findMailboxName($mailbox), 0, 1) != ".") {
200 $boxes[$g]["RAW"] = $mailbox;
201
202 $mailbox = findMailboxName($mailbox);
203 $periodCount = countCharInString($mailbox, $dm);
204
205 // indent the correct number of spaces.
206 for ($j = 0;$j < $periodCount;$j++)
207 $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . "&nbsp;&nbsp;";
208
209 $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm);
210 $boxes[$g]["UNFORMATTED"] = $mailbox;
211 $boxes[$g]["ID"] = $g;
212 $g++;
213 }
214 }
215
216 /** Next, we get all subscribed folders **/
217 fputs($imapConnection, "1 LSUB \"\" *\n");
218 $str = imapReadData($imapConnection, "1", true, $response, $message);
219 $dm = findMailboxDelimeter($imapConnection);
220 for ($i = 0;$i < count($str); $i++) {
221 $mailbox = chop($str[$i]);
222 if (substr(findMailboxName($mailbox), 0, 1) != ".") {
223 $boxes[$g]["RAW"] = $mailbox;
224
225 $mailbox = findMailboxName($mailbox);
226 $periodCount = countCharInString($mailbox, $dm);
227
228 // indent the correct number of spaces.
229 for ($j = 0;$j < $periodCount;$j++)
230 $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . "&nbsp;&nbsp;";
231
232 $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm);
233 $boxes[$g]["UNFORMATTED"] = $mailbox;
234 $boxes[$g]["ID"] = $g;
235 $g++;
236 }
237 }
238
239 $original = $boxes;
240
241 for ($i = 0; $i < count($original); $i++) {
242 $boxes[$i]["UNFORMATTED"] = strtolower($boxes[$i]["UNFORMATTED"]);
243 }
244
245 $boxes = ary_sort($boxes, "UNFORMATTED", 1);
246
247 for ($i = 0; $i < count($original); $i++) {
248 for ($j = 0; $j < count($original); $j++) {
249 if ($boxes[$i]["ID"] == $original[$j]["ID"]) {
250 $boxes[$i]["UNFORMATTED"] = $original[$j]["UNFORMATTED"];
251 $boxes[$i]["FORMATTED"] = $original[$j]["FORMATTED"];
252 $boxes[$i]["RAW"] = $original[$j]["RAW"];
253 }
254 }
255 }
256
257 for ($i = 0; $i < count($boxes); $i++) {
258 if ($boxes[$i]["UNFORMATTED"] == $special_folders[0]) {
259 $boxesnew[0]["FORMATTED"] = $boxes[$i]["FORMATTED"];
260 $boxesnew[0]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
261 $boxesnew[0]["RAW"] = trim($boxes[$i]["RAW"]);
262 $boxes[$i]["USED"] = true;
263 }
264 }
265 if ($list_special_folders_first == true) {
266 for ($i = 0; $i < count($boxes); $i++) {
267 for ($j = 1; $j < count($special_folders); $j++) {
268 if (substr($boxes[$i]["UNFORMATTED"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
269 $pos = count($boxesnew);
270 $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
271 $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
272 $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
273 $boxes[$i]["USED"] = true;
274 }
275 }
276 }
277 }
278 for ($i = 0; $i < count($boxes); $i++) {
279 if (($boxes[$i]["UNFORMATTED"] != $special_folders[0]) &&
280 ($boxes[$i]["UNFORMATTED"] != ".mailboxlist") &&
281 ($boxes[$i]["USED"] == false)) {
282 $pos = count($boxesnew);
283 $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"];
284 $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]);
285 $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]);
286 $boxes[$i]["USED"] = true;
287 }
288 }
289
290 $boxes = $boxesnew;
291 }
292
293 function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) {
294 /** check if they would like to move it to the trash folder or not */
295 if (($move_to_trash == true) && (folderExists($imapConnection, $trash_folder))) {
296 $success = copyMessages($imapConnection, $a, $b, $trash_folder);
297 if ($success == true)
298 setMessageFlag($imapConnection, $a, $b, "Deleted");
299 } else {
300 setMessageFlag($imapConnection, $a, $b, "Deleted");
301 }
302 if ($auto_expunge == true)
303 expungeBox($imapConnection, $mailbox);
304 }
305
306 function stripComments($line) {
307 if (strpos($line, ";")) {
308 $line = substr($line, 0, strpos($line, ";"));
309 }
310
311 if (strpos($line, "(") && strpos($line, ")")) {
312 $full_line = $full_line . substr($line, 0, strpos($line, "("));
313 $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")"));
314 } else {
315 $full_line = $line;
316 }
317 return $full_line;
318 }
319
320 function folderExists($imapConnection, $folderName) {
321 getFolderList($imapConnection, $folders);
322 $found = false;
323 for ($i = 0; ($i < count($folders)) && (!$found); $i++) {
324 if ($folders[$i]["UNFORMATTED"] == $folderName)
325 $found = true;
326 }
327 return $found;
328 }
329 ?>