59177427 |
1 | <?php |
8e9e8afa |
2 | /** |
3 | ** imap_mailbox.php |
4 | ** |
5 | ** This impliments all functions that manipulate mailboxes |
245a6892 |
6 | ** |
7 | ** $Id$ |
8e9e8afa |
8 | **/ |
9 | |
f435778e |
10 | if (defined ('imap_mailbox_php')) |
11 | return; |
12 | define ('imap_mailbox_php', true); |
13 | |
0fc2aca0 |
14 | require_once('../src/load_prefs.php'); |
15 | require_once('../functions/array.php'); |
16 | |
8e9e8afa |
17 | /****************************************************************************** |
18 | ** Expunges a mailbox |
19 | ******************************************************************************/ |
a7f3c40d |
20 | function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) { |
8e9e8afa |
21 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
8cf653ad |
22 | $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message); |
8e9e8afa |
23 | } |
24 | |
25 | |
26 | /****************************************************************************** |
27 | ** Checks whether or not the specified mailbox exists |
28 | ******************************************************************************/ |
29 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
146e0c45 |
30 | if (! isset($mailbox)) |
31 | return false; |
26511b3e |
32 | fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n"); |
33 | $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
146e0c45 |
34 | return isset($mbx[0]); |
8e9e8afa |
35 | } |
36 | |
8e9e8afa |
37 | /****************************************************************************** |
38 | ** Selects a mailbox |
39 | ******************************************************************************/ |
04632dbc |
40 | function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) { |
3751dc7f |
41 | global $auto_expunge; |
42 | |
8e9e8afa |
43 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
2752bb16 |
44 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
04632dbc |
45 | if ($recent) { |
46 | for ($i=0; $i<count($read); $i++) { |
47 | if (strpos(strtolower($read[$i]), "recent")) { |
48 | $r = explode(" ", $read[$i]); |
49 | } |
50 | } |
51 | return $r[1]; |
52 | } |
3751dc7f |
53 | if ($auto_expunge) { |
54 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
8cf653ad |
55 | $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b); |
3751dc7f |
56 | } |
8e9e8afa |
57 | } |
58 | |
59 | |
60 | |
61 | /****************************************************************************** |
62 | ** Creates a folder |
63 | ******************************************************************************/ |
64 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
65 | if (strtolower($type) == "noselect") { |
66 | $dm = sqimap_get_delimiter($imap_stream); |
67 | $mailbox = $mailbox.$dm; |
68 | } |
69 | fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n"); |
70 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
71 | |
72 | sqimap_subscribe ($imap_stream, $mailbox); |
73 | } |
74 | |
75 | |
76 | |
77 | /****************************************************************************** |
78 | ** Subscribes to an existing folder |
79 | ******************************************************************************/ |
80 | function sqimap_subscribe ($imap_stream, $mailbox) { |
81 | fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n"); |
82 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
83 | } |
84 | |
85 | |
86 | |
87 | |
88 | /****************************************************************************** |
89 | ** Unsubscribes to an existing folder |
90 | ******************************************************************************/ |
91 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
2752bb16 |
92 | global $imap_server_type; |
cbdc5621 |
93 | |
8e9e8afa |
94 | fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n"); |
95 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
96 | } |
97 | |
98 | |
99 | |
100 | |
101 | /****************************************************************************** |
102 | ** This function simply deletes the given folder |
103 | ******************************************************************************/ |
104 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
105 | fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n"); |
106 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
107 | sqimap_unsubscribe ($imap_stream, $mailbox); |
108 | } |
8d636967 |
109 | |
110 | /*********************************************************************** |
111 | ** Determines if the user is subscribed to the folder or not |
112 | **********************************************************************/ |
113 | function sqimap_mailbox_is_subscribed($imap_stream, $folder) { |
114 | $boxes = sqimap_mailbox_list ($imap_stream); |
115 | foreach ($boxes as $ref) { |
116 | if ($ref['unformatted'] == $folder) |
117 | return true; |
118 | } |
119 | return false; |
120 | } |
121 | |
8e9e8afa |
122 | |
123 | |
124 | /****************************************************************************** |
125 | ** Formats a mailbox into 4 parts for the $boxes array |
5bdd7223 |
126 | ** |
127 | ** The four parts are: |
128 | ** |
129 | ** raw - Raw LIST/LSUB response from the IMAP server |
130 | ** formatted - nicely formatted folder name |
131 | ** unformatted - unformatted, but with delimiter at end removed |
132 | ** unformatted-dm - folder name as it appears in raw response |
18a148f0 |
133 | ** unformatted-disp - unformatted without $folder_prefix |
5bdd7223 |
134 | ** |
8e9e8afa |
135 | ******************************************************************************/ |
16530f8b |
136 | function sqimap_mailbox_parse ($line, $line_lsub, $dm) { |
5bdd7223 |
137 | global $folder_prefix; |
138 | |
139 | // Process each folder line |
8e9e8afa |
140 | for ($g=0; $g < count($line); $g++) { |
5bdd7223 |
141 | |
2752bb16 |
142 | // Store the raw IMAP reply |
a7ea7540 |
143 | if (isset($line[$g])) |
144 | $boxes[$g]["raw"] = $line[$g]; |
145 | else |
146 | $boxes[$g]["raw"] = ""; |
147 | |
5bdd7223 |
148 | |
2752bb16 |
149 | // Count number of delimiters ($dm) in folder name |
6477eb2d |
150 | $mailbox = trim($line_lsub[$g]); |
8e9e8afa |
151 | $dm_count = countCharInString($mailbox, $dm); |
152 | if (substr($mailbox, -1) == $dm) |
5bdd7223 |
153 | $dm_count--; // If name ends in delimiter - decrement count by one |
154 | |
2752bb16 |
155 | // Format folder name, but only if it's a INBOX.* or have |
156 | // a parent. |
157 | $boxesbyname[$mailbox] = $g; |
158 | $parentfolder = readMailboxParent($mailbox, $dm); |
36dfb0c9 |
159 | if((strtolower(substr($mailbox, 0, 5)) == "inbox") || |
160 | (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) || |
161 | (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
2752bb16 |
162 | $indent = $dm_count - (countCharInString($folder_prefix, $dm)); |
b1275991 |
163 | if ($indent > 0) |
0debfed6 |
164 | $boxes[$g]["formatted"] = str_repeat(" ", $indent); |
165 | else |
166 | $boxes[$g]["formatted"] = ''; |
2752bb16 |
167 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
168 | } else { |
169 | $boxes[$g]["formatted"] = $mailbox; |
170 | } |
8e9e8afa |
171 | |
146e0c45 |
172 | $boxes[$g]['unformatted-dm'] = $mailbox; |
8e9e8afa |
173 | if (substr($mailbox, -1) == $dm) |
174 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
146e0c45 |
175 | $boxes[$g]['unformatted'] = $mailbox; |
20eb2621 |
176 | if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) |
631b9da3 |
177 | $mailbox = substr($mailbox, strlen($folder_prefix)); |
178 | $boxes[$g]['unformatted-disp'] = $mailbox; |
146e0c45 |
179 | $boxes[$g]['id'] = $g; |
8e9e8afa |
180 | |
1a7e1e97 |
181 | $boxes[$g]['flags'] = array(); |
182 | if (isset($line[$g])) { |
36dfb0c9 |
183 | ereg("\(([^)]*)\)",$line[$g],$regs); |
1a7e1e97 |
184 | $flags = trim(strtolower(str_replace('\\', '',$regs[1]))); |
185 | if ($flags) |
186 | $boxes[$g]['flags'] = explode(' ', $flags); |
187 | } |
8e9e8afa |
188 | } |
5bdd7223 |
189 | |
8e9e8afa |
190 | return $boxes; |
191 | } |
1071ae95 |
192 | |
193 | /* Apparently you must call a user function with usort instead |
194 | * of calling a built-in directly. Stupid. |
195 | * Patch from dave_michmerhuizen@yahoo.com |
196 | * Allows case insensitivity when sorting folders |
197 | */ |
198 | function user_strcasecmp($a, $b) |
199 | { |
200 | return strcasecmp($a, $b); |
201 | } |
10359c65 |
202 | |
6b8a49c9 |
203 | |
8e9e8afa |
204 | /****************************************************************************** |
205 | ** Returns sorted mailbox lists in several different ways. |
5bdd7223 |
206 | ** See comment on sqimap_mailbox_parse() for info about the returned array. |
8e9e8afa |
207 | ******************************************************************************/ |
208 | function sqimap_mailbox_list ($imap_stream) { |
5bdd7223 |
209 | global $data_dir, $username, $list_special_folders_first; |
6477eb2d |
210 | global $trash_folder, $sent_folder; |
f435778e |
211 | global $move_to_trash, $move_to_sent, $folder_prefix; |
8e9e8afa |
212 | |
1590a668 |
213 | $inbox_in_list = false; |
214 | $inbox_subscribed = false; |
215 | |
0fc2aca0 |
216 | |
8e9e8afa |
217 | |
218 | $dm = sqimap_get_delimiter ($imap_stream); |
219 | |
8e9e8afa |
220 | /** LSUB array **/ |
44a3ca20 |
221 | fputs ($imap_stream, "a001 LSUB \"$folder_prefix\" \"*\"\r\n"); |
8e9e8afa |
222 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
44a3ca20 |
223 | |
224 | // Section about removing the last element was removed |
225 | // We don't return "* OK" anymore from sqimap_read_data |
6477eb2d |
226 | |
44a3ca20 |
227 | $sorted_lsub_ary = array(); |
8e9e8afa |
228 | for ($i=0;$i < count($lsub_ary); $i++) { |
44a3ca20 |
229 | // Workaround for EIMS |
230 | // Doesn't work if the mailbox name is multiple lines |
231 | if (isset($lsub_ary[$i + 1]) && |
232 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
233 | $lsub_ary[$i], $regs)) { |
234 | $i ++; |
235 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . |
236 | '"' . $regs[2]; |
237 | } |
238 | $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]); |
239 | $sorted_lsub_ary[] = $temp_mailbox_name; |
240 | if (strtoupper($temp_mailbox_name) == 'INBOX') |
8e9e8afa |
241 | $inbox_subscribed = true; |
242 | } |
ee62ce13 |
243 | $new_ary = array(); |
244 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
245 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
246 | $new_ary[] = $sorted_lsub_ary[$i]; |
247 | } |
248 | } |
249 | $sorted_lsub_ary = $new_ary; |
8e9e8afa |
250 | if (isset($sorted_lsub_ary)) { |
44a3ca20 |
251 | usort($sorted_lsub_ary, 'user_strcasecmp'); |
10359c65 |
252 | //sort($sorted_lsub_ary); |
8e9e8afa |
253 | } |
254 | |
3cb866d7 |
255 | /** LIST array **/ |
44a3ca20 |
256 | $sorted_list_ary = array(); |
f9b3e5d9 |
257 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
258 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
259 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
260 | else |
261 | $mbx = $sorted_lsub_ary[$i]; |
262 | |
263 | fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n"); |
264 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
44a3ca20 |
265 | // Another workaround for EIMS |
266 | if (isset($read[1]) && |
267 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
268 | $read[0], $regs)) { |
269 | $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . |
270 | '"' . $regs[2]; |
271 | } |
a7ea7540 |
272 | if (isset($sorted_list_ary[$i])) |
273 | $sorted_list_ary[$i] = ""; |
274 | if (isset($read[0])) |
44a3ca20 |
275 | $sorted_list_ary[$i] = $read[0]; |
a7ea7540 |
276 | else |
44a3ca20 |
277 | $sorted_list_ary[$i] = ""; |
278 | if (isset($sorted_list_ary[$i]) && |
279 | strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") |
3cb866d7 |
280 | $inbox_in_list = true; |
f9b3e5d9 |
281 | } |
2752bb16 |
282 | |
44a3ca20 |
283 | /** Just in case they're not subscribed to their inbox, we'll get it |
284 | for them anyway **/ |
8e9e8afa |
285 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
286 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
287 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
44a3ca20 |
288 | // Another workaround for EIMS |
289 | if (isset($inbox_ary[1]) && |
290 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
291 | $inbox_ary[0], $regs)) { |
292 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
293 | '"' . $regs[2]; |
294 | } |
295 | |
296 | $sorted_list_ary[] = $inbox_ary[0]; |
297 | $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]); |
8e9e8afa |
298 | } |
299 | |
f9b3e5d9 |
300 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm); |
301 | |
5bdd7223 |
302 | |
f9b3e5d9 |
303 | /** Now, lets sort for special folders **/ |
5bdd7223 |
304 | |
305 | $boxesnew = Array(); |
306 | |
307 | // Find INBOX |
8e9e8afa |
308 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
309 | if (strtolower($boxes[$i]["unformatted"]) == "inbox") { |
5bdd7223 |
310 | $boxesnew[] = $boxes[$i]; |
283db905 |
311 | $used[$i] = true; |
2752bb16 |
312 | $i = count($boxes); |
8e9e8afa |
313 | } |
314 | } |
aed206bf |
315 | |
8e9e8afa |
316 | if ($list_special_folders_first == true) { |
5bdd7223 |
317 | |
2752bb16 |
318 | // Then list special folders and their subfolders |
245a6892 |
319 | for ($i = 0 ; $i < count($boxes) ; $i++) { |
23ed73eb |
320 | if ($move_to_trash && |
146e0c45 |
321 | eregi('^' . quotemeta($trash_folder) . '(' . |
322 | quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) { |
5bdd7223 |
323 | $boxesnew[] = $boxes[$i]; |
283db905 |
324 | $used[$i] = true; |
1e0628fb |
325 | } |
23ed73eb |
326 | elseif ($move_to_sent && |
146e0c45 |
327 | eregi('^' . quotemeta($sent_folder) . '(' . |
328 | quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) { |
5bdd7223 |
329 | $boxesnew[] = $boxes[$i]; |
283db905 |
330 | $used[$i] = true; |
8e9e8afa |
331 | } |
332 | } |
5bdd7223 |
333 | |
2752bb16 |
334 | // Put INBOX.* folders ahead of the rest |
245a6892 |
335 | for ($i = 0; $i < count($boxes); $i++) { |
146e0c45 |
336 | if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) && |
283db905 |
337 | (!isset($used[$i]) || $used[$i] == false)) { |
2752bb16 |
338 | $boxesnew[] = $boxes[$i]; |
283db905 |
339 | $used[$i] = true; |
2752bb16 |
340 | } |
341 | } |
5bdd7223 |
342 | |
8e9e8afa |
343 | } |
c5eb2c03 |
344 | |
5bdd7223 |
345 | // Rest of the folders |
8e9e8afa |
346 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
347 | if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && |
283db905 |
348 | (!isset($used[$i]) || $used[$i] == false)) { |
5bdd7223 |
349 | $boxesnew[] = $boxes[$i]; |
283db905 |
350 | $used[$i] = true; |
8e9e8afa |
351 | } |
352 | } |
353 | |
354 | return $boxesnew; |
355 | } |
8e9e8afa |
356 | |
357 | /****************************************************************************** |
358 | ** Returns a list of all folders, subscribed or not |
359 | ******************************************************************************/ |
360 | function sqimap_mailbox_list_all ($imap_stream) { |
1e0628fb |
361 | global $list_special_folders_first, $folder_prefix; |
8e9e8afa |
362 | |
8e9e8afa |
363 | $dm = sqimap_get_delimiter ($imap_stream); |
364 | |
365 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
366 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
367 | $g = 0; |
368 | $phase = "inbox"; |
5bdd7223 |
369 | |
8e9e8afa |
370 | for ($i = 0; $i < count($read_ary); $i++) { |
44a3ca20 |
371 | // Another workaround for EIMS |
372 | if (isset($read_ary[$i + 1]) && |
373 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
374 | $read_ary[$i], $regs)) { |
375 | $i ++; |
376 | $read_ary[$i] = $regs[1] . '"' . |
377 | addslashes(trim($read_ary[$i])) . |
378 | '"' . $regs[2]; |
379 | } |
8e9e8afa |
380 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
5bdd7223 |
381 | |
2752bb16 |
382 | // Store the raw IMAP reply |
8e9e8afa |
383 | $boxes[$g]["raw"] = $read_ary[$i]; |
384 | |
2752bb16 |
385 | // Count number of delimiters ($dm) in folder name |
8e9e8afa |
386 | $mailbox = find_mailbox_name($read_ary[$i]); |
387 | $dm_count = countCharInString($mailbox, $dm); |
388 | if (substr($mailbox, -1) == $dm) |
5bdd7223 |
389 | $dm_count--; // If name ends in delimiter - decrement count by one |
2752bb16 |
390 | |
391 | // Format folder name, but only if it's a INBOX.* or have |
392 | // a parent. |
393 | $boxesbyname[$mailbox] = $g; |
394 | $parentfolder = readMailboxParent($mailbox, $dm); |
146e0c45 |
395 | if((eregi('^inbox'.quotemeta($dm), $mailbox)) || |
396 | (ereg('^'.$folder_prefix, $mailbox)) || |
2752bb16 |
397 | ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
0debfed6 |
398 | if ($dm_count) |
399 | $boxes[$g]["formatted"] = str_repeat(" ", $dm_count); |
400 | else |
401 | $boxes[$g]["formatted"] = ''; |
2752bb16 |
402 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
403 | } else { |
404 | $boxes[$g]["formatted"] = $mailbox; |
405 | } |
8e9e8afa |
406 | |
407 | $boxes[$g]["unformatted-dm"] = $mailbox; |
408 | if (substr($mailbox, -1) == $dm) |
409 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
410 | $boxes[$g]["unformatted"] = $mailbox; |
146e0c45 |
411 | $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox); |
8e9e8afa |
412 | $boxes[$g]["id"] = $g; |
413 | |
414 | /** Now lets get the flags for this mailbox **/ |
415 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
416 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
44a3ca20 |
417 | // Another workaround for EIMS |
418 | if (isset($read_mlbx[1]) && |
419 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
420 | $read_mlbx[0], $regs)) { |
421 | $read_mlbx[0] = $regs[1] . '"' . |
422 | addslashes(trim($read_mlbx[1])) . |
423 | '"' . $regs[2]; |
424 | } |
8e9e8afa |
425 | |
426 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
427 | $flags = substr($flags, 0, strpos($flags, ")")); |
146e0c45 |
428 | $flags = str_replace('\\', '', $flags); |
8e9e8afa |
429 | $flags = trim(strtolower($flags)); |
430 | if ($flags) { |
4afa7206 |
431 | $boxes[$g]['flags'] = explode(" ", $flags); |
8e9e8afa |
432 | } |
12d61439 |
433 | else |
434 | { |
435 | $boxes[$g]['flags'] = array(); |
436 | } |
8e9e8afa |
437 | } |
438 | $g++; |
439 | } |
5bdd7223 |
440 | if(is_array($boxes)) { |
591000ec |
441 | $boxes = ary_sort ($boxes, "unformatted", 1); |
442 | } |
5bdd7223 |
443 | |
8e9e8afa |
444 | return $boxes; |
445 | } |
446 | |
0fc2aca0 |
447 | ?> |