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