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); |
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)); |
aa42fbfb |
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 | |
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; |
aa42fbfb |
88 | } |
8c7dfc99 |
89 | |
2aa12d5e |
90 | // handles logging onto an imap server. |
d92b6f31 |
91 | function loginToImapServer($username, $key, $imapServerAddress, $hide) { |
92 | require("../config/config.php"); |
93 | |
2aa12d5e |
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 |
838f9159 |
103 | fputs($imapConnection, "a001 LOGIN \"$username\" \"$key\"\n"); |
2aa12d5e |
104 | $read = fgets($imapConnection, 1024); |
d92b6f31 |
105 | if ($debug_login == true) { |
106 | echo "SERVER SAYS: $read<BR>"; |
2aa12d5e |
107 | } |
d92b6f31 |
108 | |
f3d17401 |
109 | /** If the login attempt was UNsuccessful, lets see why **/ |
d92b6f31 |
110 | if (substr($read, 0, 7) != "a001 OK") { |
111 | if (!$hide) { |
112 | if (substr($read, 0, 8) == "a001 BAD") { |
113 | echo "Bad request: $read<BR>"; |
114 | exit; |
115 | } |
116 | else if (substr($read, 0, 7) == "a001 NO") { |
f3d17401 |
117 | echo "<HTML><BODY BGCOLOR=FFFFFF><BR>"; |
a4bcd698 |
118 | echo "<TABLE COLS=1 WIDTH=70% NOBORDER BGCOLOR=FFFFFF ALIGN=CENTER>"; |
d92b6f31 |
119 | echo " <TR>"; |
a4bcd698 |
120 | echo " <TD BGCOLOR=\"DCDCDC\">"; |
121 | echo " <FONT FACE=\"Arial,Helvetica\" COLOR=CC0000><B><CENTER>ERROR</CENTER></B></FONT>"; |
d92b6f31 |
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>"; |
124 | echo " </TD></TR>"; |
125 | echo "</TABLE>"; |
126 | echo "</BODY></HTML>"; |
127 | exit; |
128 | } |
129 | else { |
130 | echo "Unknown error: $read<BR>"; |
131 | exit; |
132 | } |
133 | } else { |
134 | exit; |
135 | } |
136 | } |
137 | |
2aa12d5e |
138 | return $imapConnection; |
139 | } |
140 | |
8c7dfc99 |
141 | /** must be sent in the form: user.<USER>.<FOLDER> **/ |
d92b6f31 |
142 | function createFolder($imapConnection, $folder, $type) { |
143 | require ("../config/config.php"); |
144 | |
145 | if (strtolower($type) == "noselect") { |
146 | $dm = findMailboxDelimeter($imapConnection); |
147 | $folder = "$folder$dm"; |
148 | } else { |
149 | $folder = "$folder"; |
150 | } |
8c7dfc99 |
151 | fputs($imapConnection, "1 create \"$folder\"\n"); |
d92b6f31 |
152 | $data = imapReadData($imapConnection, "1", false, $response, $message); |
153 | |
154 | if ($response == "NO") { |
7ce342dc |
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>"; |
d92b6f31 |
158 | exit; |
159 | } else if ($response == "BAD") { |
7ce342dc |
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>"; |
d92b6f31 |
161 | exit; |
162 | } |
0bebd7c9 |
163 | fputs($imapConnection, "1 SUBSCRIBE \"$folder\"\n"); |
164 | $data = imapReadData($imapConnection, "1", true, $response, $message); |
8c7dfc99 |
165 | } |
166 | |
597d8f1d |
167 | function removeFolder($imapConnection, $folder) { |
8c7dfc99 |
168 | fputs($imapConnection, "1 delete \"$folder\"\n"); |
d92b6f31 |
169 | $data = imapReadData($imapConnection, "1", false, $response, $message); |
170 | if ($response == "NO") { |
171 | echo "<FONT FACE=\"Arial,Helvetica\" COLOR=FF0000><B>ERROR</B>: Could not delete the folder $folder.</FONT>"; |
172 | echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\">Probable causes:</FONT><BR>"; |
173 | echo "<FONT FACE=\"Arial,Helvetica\" COLOR=\"$color[8]\"><LI>This folder may contain subfolders. Delete all subfolders first</LI></FONT>"; |
174 | exit; |
175 | } else if ($response == "BAD") { |
176 | 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>"; |
177 | exit; |
178 | } |
8c7dfc99 |
179 | } |
54e3c1d8 |
180 | |
181 | /** Sends back two arrays, boxesFormatted and boxesUnformatted **/ |
7ce342dc |
182 | function getFolderList($imapConnection, &$boxes) { |
183 | require ("../config/config.php"); |
184 | |
0bebd7c9 |
185 | /** First we get the inbox **/ |
186 | fputs($imapConnection, "1 LIST \"\" INBOX\n"); |
d92b6f31 |
187 | $str = imapReadData($imapConnection, "1", true, $response, $message); |
d92b6f31 |
188 | $dm = findMailboxDelimeter($imapConnection); |
7ce342dc |
189 | $g = 0; |
54e3c1d8 |
190 | for ($i = 0;$i < count($str); $i++) { |
191 | $mailbox = chop($str[$i]); |
7ce342dc |
192 | if (substr(findMailboxName($mailbox), 0, 1) != ".") { |
193 | $boxes[$g]["RAW"] = $mailbox; |
d92b6f31 |
194 | |
7ce342dc |
195 | $mailbox = findMailboxName($mailbox); |
196 | $periodCount = countCharInString($mailbox, $dm); |
54e3c1d8 |
197 | |
7ce342dc |
198 | // indent the correct number of spaces. |
199 | for ($j = 0;$j < $periodCount;$j++) |
200 | $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . " "; |
54e3c1d8 |
201 | |
7ce342dc |
202 | $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm); |
203 | $boxes[$g]["UNFORMATTED"] = $mailbox; |
204 | $boxes[$g]["ID"] = $g; |
205 | $g++; |
206 | } |
54e3c1d8 |
207 | } |
7ce342dc |
208 | |
0bebd7c9 |
209 | /** Next, we get all subscribed folders **/ |
210 | fputs($imapConnection, "1 LSUB \"\" *\n"); |
211 | $str = imapReadData($imapConnection, "1", true, $response, $message); |
212 | $dm = findMailboxDelimeter($imapConnection); |
213 | for ($i = 0;$i < count($str); $i++) { |
214 | $mailbox = chop($str[$i]); |
215 | if (substr(findMailboxName($mailbox), 0, 1) != ".") { |
216 | $boxes[$g]["RAW"] = $mailbox; |
217 | |
218 | $mailbox = findMailboxName($mailbox); |
219 | $periodCount = countCharInString($mailbox, $dm); |
220 | |
221 | // indent the correct number of spaces. |
222 | for ($j = 0;$j < $periodCount;$j++) |
223 | $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . " "; |
224 | |
225 | $boxes[$g]["FORMATTED"] = $boxes[$g]["FORMATTED"] . readShortMailboxName($mailbox, $dm); |
226 | $boxes[$g]["UNFORMATTED"] = $mailbox; |
227 | $boxes[$g]["ID"] = $g; |
228 | $g++; |
229 | } |
230 | } |
231 | |
7ce342dc |
232 | $original = $boxes; |
233 | |
234 | for ($i = 0; $i < count($original); $i++) { |
235 | $boxes[$i]["UNFORMATTED"] = strtolower($boxes[$i]["UNFORMATTED"]); |
236 | } |
237 | |
238 | $boxes = ary_sort($boxes, "UNFORMATTED", 1); |
239 | |
240 | for ($i = 0; $i < count($original); $i++) { |
241 | for ($j = 0; $j < count($original); $j++) { |
242 | if ($boxes[$i]["ID"] == $original[$j]["ID"]) { |
243 | $boxes[$i]["UNFORMATTED"] = $original[$j]["UNFORMATTED"]; |
244 | $boxes[$i]["FORMATTED"] = $original[$j]["FORMATTED"]; |
245 | $boxes[$i]["RAW"] = $original[$j]["RAW"]; |
246 | } |
247 | } |
248 | } |
249 | |
250 | for ($i = 0; $i < count($boxes); $i++) { |
251 | if ($boxes[$i]["UNFORMATTED"] == $special_folders[0]) { |
252 | $boxesnew[0]["FORMATTED"] = $boxes[$i]["FORMATTED"]; |
253 | $boxesnew[0]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]); |
254 | $boxesnew[0]["RAW"] = trim($boxes[$i]["RAW"]); |
255 | $boxes[$i]["USED"] = true; |
256 | } |
257 | } |
258 | if ($list_special_folders_first == true) { |
259 | for ($i = 0; $i < count($boxes); $i++) { |
260 | for ($j = 1; $j < count($special_folders); $j++) { |
261 | if (substr($boxes[$i]["UNFORMATTED"], 0, strlen($special_folders[$j])) == $special_folders[$j]) { |
262 | $pos = count($boxesnew); |
263 | $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"]; |
264 | $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]); |
265 | $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]); |
266 | $boxes[$i]["USED"] = true; |
267 | } |
268 | } |
269 | } |
270 | } |
271 | for ($i = 0; $i < count($boxes); $i++) { |
272 | if (($boxes[$i]["UNFORMATTED"] != $special_folders[0]) && |
273 | ($boxes[$i]["UNFORMATTED"] != ".mailboxlist") && |
274 | ($boxes[$i]["USED"] == false)) { |
275 | $pos = count($boxesnew); |
276 | $boxesnew[$pos]["FORMATTED"] = $boxes[$i]["FORMATTED"]; |
277 | $boxesnew[$pos]["RAW"] = trim($boxes[$i]["RAW"]); |
278 | $boxesnew[$pos]["UNFORMATTED"] = trim($boxes[$i]["UNFORMATTED"]); |
279 | $boxes[$i]["USED"] = true; |
280 | } |
281 | } |
282 | |
283 | $boxes = $boxesnew; |
54e3c1d8 |
284 | } |
285 | |
de80e95e |
286 | function deleteMessages($imapConnection, $a, $b, $numMessages, $trash_folder, $move_to_trash, $auto_expunge, $mailbox) { |
287 | /** check if they would like to move it to the trash folder or not */ |
288 | if ($move_to_trash == true) { |
289 | $success = copyMessages($imapConnection, $a, $b, $trash_folder); |
290 | if ($success == true) |
291 | setMessageFlag($imapConnection, $a, $b, "Deleted"); |
292 | } else { |
293 | setMessageFlag($imapConnection, $a, $b, "Deleted"); |
294 | } |
5e3fe357 |
295 | if ($auto_expunge == true) |
296 | expungeBox($imapConnection, $mailbox); |
de80e95e |
297 | } |
5e3fe357 |
298 | |
aceb0d5c |
299 | function stripComments($line) { |
300 | if (strpos($line, ";")) { |
301 | $line = substr($line, 0, strpos($line, ";")); |
302 | } |
303 | |
304 | if (strpos($line, "(") && strpos($line, ")")) { |
305 | $full_line = $full_line . substr($line, 0, strpos($line, "(")); |
306 | $full_line = $full_line . substr($line, strpos($line, ")")+1, strlen($line) - strpos($line, ")")); |
307 | } else { |
308 | $full_line = $line; |
309 | } |
310 | return $full_line; |
311 | } |
3302d0d4 |
312 | ?> |