59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
4 | * imap_mailbox.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This impliments all functions that manipulate mailboxes |
10 | * |
11 | * $Id$ |
12 | */ |
f435778e |
13 | |
90de1755 |
14 | /* |
15 | Defines Special Mail Boxes |
16 | */ |
17 | |
18 | function isSpecialMailbox( $box ) { |
19 | |
20 | global $trash_folder, $sent_folder, $draft_folder, |
21 | $move_to_trash, $move_to_sent, $save_as_draft, |
72b9aff9 |
22 | $delimiter, $folder_prefix, $imap_server_type; |
90de1755 |
23 | |
7980d569 |
24 | if ( $move_to_sent ) { |
72b9aff9 |
25 | if ( $imap_server_type == 'uw' ) { |
26 | $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) ); |
27 | if ( $i === FALSE ) { |
28 | $i = strlen( $box ); |
29 | } |
30 | } else { |
25dc0ad7 |
31 | // Skip next second delimiter |
32 | $i = strpos( $sent_folder, $delimiter ); |
33 | $i = strpos( $sent_folder, $delimiter, $i+ 1 ); |
34 | if ( $i === FALSE ) { |
35 | $i = strlen( $box ); |
36 | } |
4d6bd355 |
37 | } |
7980d569 |
38 | } |
72b9aff9 |
39 | |
90de1755 |
40 | $ret = ( (strtolower($box) == 'inbox') || |
41 | ($box == $trash_folder && |
42 | $move_to_trash) || |
dc7a6d34 |
43 | ( substr( $sent_folder, 0, $i ) == substr( $box, 0, $i ) && |
90de1755 |
44 | $move_to_sent) || |
45 | ($box == $draft_folder && |
46 | $save_as_draft) ); |
47 | |
48 | return( $ret ); |
49 | |
50 | } |
51 | |
35586184 |
52 | /************************* |
53 | ** Expunges a mailbox ** |
54 | *************************/ |
90de1755 |
55 | function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE) |
43b698c7 |
56 | { |
1c72b151 |
57 | $read = sqimap_run_command($imap_stream, 'EXPUNGE', |
58 | $handle_errors, $response, $message); |
43b698c7 |
59 | } |
60 | |
61 | |
62 | /****************************************************************************** |
63 | ** Checks whether or not the specified mailbox exists |
64 | ******************************************************************************/ |
65 | function sqimap_mailbox_exists ($imap_stream, $mailbox) |
66 | { |
67 | if (! isset($mailbox)) { |
68 | return false; |
69 | } |
1c72b151 |
70 | $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"", |
90de1755 |
71 | TRUE, $response, $message); |
43b698c7 |
72 | return isset($mbx[0]); |
73 | } |
74 | |
75 | /****************************************************************************** |
76 | ** Selects a mailbox |
77 | ******************************************************************************/ |
78 | function sqimap_mailbox_select ($imap_stream, $mailbox, |
90de1755 |
79 | $hide=TRUE, $recent=false) |
43b698c7 |
80 | { |
81 | global $auto_expunge; |
82 | |
83 | if ( $mailbox == 'None' ) { |
84 | return; |
85 | } |
86 | |
1c72b151 |
87 | $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"", |
90de1755 |
88 | TRUE, $response, $message); |
43b698c7 |
89 | if ($recent) { |
90 | for ($i=0; $i<count($read); $i++) { |
bccadd02 |
91 | if (strpos(strtolower($read[$i]), 'recent')) { |
43b698c7 |
92 | $r = explode(' ', $read[$i]); |
04632dbc |
93 | } |
43b698c7 |
94 | } |
95 | return $r[1]; |
96 | } |
97 | if ($auto_expunge) { |
1a16af11 |
98 | $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', |
1c72b151 |
99 | false, $a, $b); |
43b698c7 |
100 | } |
101 | } |
102 | |
103 | |
104 | |
105 | /****************************************************************************** |
106 | ** Creates a folder |
107 | ******************************************************************************/ |
108 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) |
109 | { |
110 | global $delimiter; |
111 | if (strtolower($type) == 'noselect') { |
112 | $mailbox = $mailbox.$delimiter; |
113 | } |
1c72b151 |
114 | $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"", |
90de1755 |
115 | TRUE, $response, $message); |
43b698c7 |
116 | |
117 | sqimap_subscribe ($imap_stream, $mailbox); |
118 | } |
119 | |
120 | |
121 | |
122 | /****************************************************************************** |
123 | ** Subscribes to an existing folder |
124 | ******************************************************************************/ |
125 | function sqimap_subscribe ($imap_stream, $mailbox) |
126 | { |
1a16af11 |
127 | $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"", |
90de1755 |
128 | TRUE, $response, $message); |
43b698c7 |
129 | } |
130 | |
131 | |
132 | |
133 | /****************************************************************************** |
134 | ** Unsubscribes to an existing folder |
135 | ******************************************************************************/ |
136 | function sqimap_unsubscribe ($imap_stream, $mailbox) |
137 | { |
138 | global $imap_server_type; |
139 | |
1a16af11 |
140 | $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"", |
90de1755 |
141 | TRUE, $response, $message); |
43b698c7 |
142 | } |
143 | |
144 | |
145 | |
146 | /****************************************************************************** |
147 | ** This function simply deletes the given folder |
148 | ******************************************************************************/ |
149 | function sqimap_mailbox_delete ($imap_stream, $mailbox) |
150 | { |
1c72b151 |
151 | $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"", |
90de1755 |
152 | TRUE, $response, $message); |
43b698c7 |
153 | sqimap_unsubscribe ($imap_stream, $mailbox); |
154 | } |
155 | |
156 | /*********************************************************************** |
157 | ** Determines if the user is subscribed to the folder or not |
158 | **********************************************************************/ |
159 | function sqimap_mailbox_is_subscribed($imap_stream, $folder) |
160 | { |
161 | $boxes = sqimap_mailbox_list ($imap_stream); |
162 | foreach ($boxes as $ref) { |
163 | if ($ref['unformatted'] == $folder) { |
90de1755 |
164 | return TRUE; |
43b698c7 |
165 | } |
166 | } |
167 | return false; |
168 | } |
169 | |
1c52ba77 |
170 | /* |
171 | Renames a mailbox |
172 | */ |
173 | function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) { |
43b698c7 |
174 | |
1c52ba77 |
175 | if ( $old_name <> $new_name ) { |
176 | |
177 | global $delimiter; |
178 | |
179 | if ( substr( $old_name, -1 ) == $delimiter ) { |
180 | $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 ); |
181 | $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 ); |
182 | $postfix = $delimiter; |
183 | $boxes = sqimap_mailbox_list($imap_stream); |
184 | } else { |
185 | $postfix = ''; |
186 | $boxes = FALSE; |
187 | } |
188 | |
1c72b151 |
189 | $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"'; |
190 | $data = sqimap_run_command($imap_stream, $cmd, |
1c52ba77 |
191 | TRUE, $response, $message); |
192 | sqimap_unsubscribe($imap_stream, $old_name.$postfix); |
193 | sqimap_subscribe($imap_stream, $new_name.$postfix); |
194 | |
195 | if ( $boxes ) { |
196 | // Sub-unsub subfolders |
197 | $l = strlen( $old_name ) + 1; |
198 | $p = 'unformatted'; |
199 | foreach ( $boxes as $box ) { |
200 | if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) { |
201 | sqimap_unsubscribe($imap_stream, $box[$p]); |
202 | sqimap_subscribe($imap_stream, |
203 | $new_name . $delimiter . substr( $box[$p], $l ) ); |
204 | } |
205 | } |
206 | } |
207 | |
208 | } |
209 | |
210 | } |
43b698c7 |
211 | |
212 | /****************************************************************************** |
213 | ** Formats a mailbox into 4 parts for the $boxes array |
214 | ** |
215 | ** The four parts are: |
216 | ** |
217 | ** raw - Raw LIST/LSUB response from the IMAP server |
218 | ** formatted - nicely formatted folder name |
219 | ** unformatted - unformatted, but with delimiter at end removed |
220 | ** unformatted-dm - folder name as it appears in raw response |
221 | ** unformatted-disp - unformatted without $folder_prefix |
222 | ** |
223 | ******************************************************************************/ |
224 | function sqimap_mailbox_parse ($line, $line_lsub) |
225 | { |
226 | global $folder_prefix, $delimiter; |
227 | |
228 | /* Process each folder line */ |
229 | for ($g=0; $g < count($line); $g++) { |
230 | |
231 | /* Store the raw IMAP reply */ |
232 | if (isset($line[$g])) { |
a7ea7540 |
233 | $boxes[$g]["raw"] = $line[$g]; |
43b698c7 |
234 | } |
235 | else { |
a7ea7540 |
236 | $boxes[$g]["raw"] = ""; |
43b698c7 |
237 | } |
238 | |
239 | |
240 | /* Count number of delimiters ($delimiter) in folder name */ |
241 | $mailbox = trim($line_lsub[$g]); |
602e1431 |
242 | $dm_count = substr_count($mailbox, $delimiter); |
43b698c7 |
243 | if (substr($mailbox, -1) == $delimiter) { |
244 | /* If name ends in delimiter - decrement count by one */ |
245 | $dm_count--; |
246 | } |
247 | |
248 | /* Format folder name, but only if it's a INBOX.* or have */ |
249 | /* a parent. */ |
250 | $boxesbyname[$mailbox] = $g; |
251 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
252 | if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") || |
253 | (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) || |
254 | ( isset($boxesbyname[$parentfolder]) && |
255 | (strlen($parentfolder) > 0) ) ) { |
602e1431 |
256 | $indent = $dm_count - ( substr_count($folder_prefix, $delimiter)); |
43b698c7 |
257 | if ($indent > 0) { |
90de1755 |
258 | $boxes[$g]['formatted'] = str_repeat(" ", $indent); |
43b698c7 |
259 | } |
260 | else { |
90de1755 |
261 | $boxes[$g]['formatted'] = ''; |
43b698c7 |
262 | } |
90de1755 |
263 | $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); |
43b698c7 |
264 | } |
265 | else { |
90de1755 |
266 | $boxes[$g]['formatted'] = $mailbox; |
43b698c7 |
267 | } |
90de1755 |
268 | |
43b698c7 |
269 | $boxes[$g]['unformatted-dm'] = $mailbox; |
270 | if (substr($mailbox, -1) == $delimiter) { |
8e9e8afa |
271 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
43b698c7 |
272 | } |
273 | $boxes[$g]['unformatted'] = $mailbox; |
274 | if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) { |
631b9da3 |
275 | $mailbox = substr($mailbox, strlen($folder_prefix)); |
43b698c7 |
276 | } |
277 | $boxes[$g]['unformatted-disp'] = $mailbox; |
278 | $boxes[$g]['id'] = $g; |
90de1755 |
279 | |
43b698c7 |
280 | $boxes[$g]['flags'] = array(); |
281 | if (isset($line[$g])) { |
36dfb0c9 |
282 | ereg("\(([^)]*)\)",$line[$g],$regs); |
1a7e1e97 |
283 | $flags = trim(strtolower(str_replace('\\', '',$regs[1]))); |
43b698c7 |
284 | if ($flags) { |
285 | $boxes[$g]['flags'] = explode(' ', $flags); |
286 | } |
287 | } |
288 | } |
90de1755 |
289 | |
43b698c7 |
290 | return $boxes; |
291 | } |
292 | |
a3439b27 |
293 | /** |
294 | * Sorting function used to sort mailbox names. |
295 | * + Original patch from dave_michmerhuizen@yahoo.com |
296 | * + Allows case insensitivity when sorting folders |
297 | * + Takes care of the delimiter being sorted to the end, causing |
298 | * subfolders to be listed in below folders that are prefixed |
299 | * with their parent folders name. |
300 | * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar |
301 | * Without special sort function: foobar between foo and foo.bar |
302 | * With special sort function: foobar AFTER foo and foo.bar :) |
43b698c7 |
303 | */ |
a3439b27 |
304 | function user_strcasecmp($a, $b) { |
305 | global $delimiter; |
306 | |
307 | /* Calculate the length of some strings. */ |
308 | $a_length = strlen($a); |
309 | $b_length = strlen($b); |
310 | $min_length = min($a_length, $b_length); |
311 | $delimiter_length = strlen($delimiter); |
312 | |
313 | /* Set the initial result value. */ |
314 | $result = 0; |
315 | |
316 | /* Check the strings... */ |
317 | for ($c = 0; $c < $min_length; ++$c) { |
318 | $a_del = substr($a, $c, $delimiter_length); |
319 | $b_del = substr($b, $c, $delimiter_length); |
320 | |
321 | if (($a_del == $delimiter) && ($b_del == $delimiter)) { |
322 | $result = 0; |
323 | } else if (($a_del == $delimiter) && ($b_del != $delimiter)) { |
a3439b27 |
324 | $result = -1; |
c64c33f4 |
325 | } else if (($a_del != $delimiter) && ($b_del == $delimiter)) { |
326 | $result = 1; |
a3439b27 |
327 | } else { |
328 | $result = strcasecmp($a{$c}, $b{$c}); |
329 | } |
330 | |
331 | if ($result != 0) { |
332 | break; |
333 | } |
334 | } |
90de1755 |
335 | |
a3439b27 |
336 | /* If one string is a prefix of the other... */ |
337 | if ($result == 0) { |
338 | if ($a_length < $b_length) { |
339 | $result = -1; |
340 | } else if ($a_length > $b_length) { |
341 | $result = 1; |
342 | } |
343 | } |
344 | |
345 | return ($result); |
43b698c7 |
346 | } |
347 | |
348 | |
349 | /****************************************************************************** |
350 | ** Returns sorted mailbox lists in several different ways. |
351 | ** See comment on sqimap_mailbox_parse() for info about the returned array. |
352 | ******************************************************************************/ |
a3439b27 |
353 | function sqimap_mailbox_list ($imap_stream) { |
90de1755 |
354 | global $data_dir, $username, $list_special_folders_first, |
355 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
356 | $move_to_trash, $move_to_sent, $save_as_draft, |
357 | $delimiter; |
358 | |
43b698c7 |
359 | $inbox_in_list = false; |
360 | $inbox_subscribed = false; |
90de1755 |
361 | |
43b698c7 |
362 | require_once('../src/load_prefs.php'); |
363 | require_once('../functions/array.php'); |
90de1755 |
364 | |
43b698c7 |
365 | /** LSUB array **/ |
1c72b151 |
366 | $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"", |
90de1755 |
367 | TRUE, $response, $message); |
368 | |
43b698c7 |
369 | /* Section about removing the last element was removed */ |
370 | /* We don't return "* OK" anymore from sqimap_read_data */ |
90de1755 |
371 | |
43b698c7 |
372 | $sorted_lsub_ary = array(); |
373 | for ($i=0;$i < count($lsub_ary); $i++) { |
374 | /* Workaround for EIMS */ |
375 | /* Doesn't work if the mailbox name is multiple lines */ |
376 | if (isset($lsub_ary[$i + 1]) && |
90de1755 |
377 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
378 | $lsub_ary[$i], $regs)) { |
379 | $i ++; |
380 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . |
381 | '"' . $regs[2]; |
382 | } |
383 | $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]); |
384 | $sorted_lsub_ary[] = $temp_mailbox_name; |
385 | if (strtoupper($temp_mailbox_name) == 'INBOX') { |
90de1755 |
386 | $inbox_subscribed = TRUE; |
43b698c7 |
387 | } |
388 | } |
389 | $new_ary = array(); |
390 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
391 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
ee62ce13 |
392 | $new_ary[] = $sorted_lsub_ary[$i]; |
43b698c7 |
393 | } |
394 | } |
395 | $sorted_lsub_ary = $new_ary; |
396 | if (isset($sorted_lsub_ary)) { |
397 | usort($sorted_lsub_ary, 'user_strcasecmp'); |
90de1755 |
398 | } |
399 | |
43b698c7 |
400 | /** LIST array **/ |
401 | $sorted_list_ary = array(); |
402 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
403 | if (substr($sorted_lsub_ary[$i], -1) == $delimiter) { |
f9b3e5d9 |
404 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
43b698c7 |
405 | } |
406 | else { |
f9b3e5d9 |
407 | $mbx = $sorted_lsub_ary[$i]; |
43b698c7 |
408 | } |
90de1755 |
409 | |
1c72b151 |
410 | $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"", |
90de1755 |
411 | TRUE, $response, $message); |
43b698c7 |
412 | /* Another workaround for EIMS */ |
90de1755 |
413 | if (isset($read[1]) && |
414 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
415 | $read[0], $regs)) { |
416 | $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . |
90de1755 |
417 | '"' . $regs[2]; |
43b698c7 |
418 | } |
90de1755 |
419 | |
43b698c7 |
420 | if (isset($sorted_list_ary[$i])) { |
90de1755 |
421 | $sorted_list_ary[$i] = ''; |
43b698c7 |
422 | } |
90de1755 |
423 | |
43b698c7 |
424 | if (isset($read[0])) { |
44a3ca20 |
425 | $sorted_list_ary[$i] = $read[0]; |
43b698c7 |
426 | } |
427 | else { |
90de1755 |
428 | $sorted_list_ary[$i] = ''; |
43b698c7 |
429 | } |
90de1755 |
430 | |
431 | if (isset($sorted_list_ary[$i]) && |
432 | strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') { |
433 | $inbox_in_list = TRUE; |
43b698c7 |
434 | } |
435 | } |
90de1755 |
436 | |
43b698c7 |
437 | /** |
438 | * Just in case they're not subscribed to their inbox, |
439 | * we'll get it for them anyway |
440 | */ |
441 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
1c72b151 |
442 | $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"", |
90de1755 |
443 | TRUE, $response, $message); |
43b698c7 |
444 | /* Another workaround for EIMS */ |
445 | if (isset($inbox_ary[1]) && |
90de1755 |
446 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
447 | $inbox_ary[0], $regs)) { |
448 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
449 | '"' . $regs[2]; |
450 | } |
90de1755 |
451 | |
43b698c7 |
452 | $sorted_list_ary[] = $inbox_ary[0]; |
453 | $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]); |
454 | } |
90de1755 |
455 | |
43b698c7 |
456 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary); |
90de1755 |
457 | |
43b698c7 |
458 | /** Now, lets sort for special folders **/ |
90de1755 |
459 | $boxesnew = $used = array(); |
460 | |
43b698c7 |
461 | /* Find INBOX */ |
90de1755 |
462 | foreach ( $boxes as $k => $box ) { |
463 | if ( strtolower($box['unformatted']) == 'inbox') { |
464 | $boxesnew[] = $box; |
465 | $used[$k] = TRUE; |
466 | } else { |
467 | $used[$k] = FALSE; |
43b698c7 |
468 | } |
469 | } |
90de1755 |
470 | |
43b698c7 |
471 | /* List special folders and their subfolders, if requested. */ |
90de1755 |
472 | if ($list_special_folders_first == TRUE) { |
473 | |
474 | foreach ( $boxes as $k => $box ) { |
475 | if ( !$used[$k] && |
476 | isSpecialMailbox( $box['unformatted'] ) ) { |
477 | $boxesnew[] = $box; |
478 | $used[$k] = TRUE; |
2752bb16 |
479 | } |
43b698c7 |
480 | } |
90de1755 |
481 | |
43b698c7 |
482 | } |
90de1755 |
483 | |
43b698c7 |
484 | /* Rest of the folders */ |
90de1755 |
485 | foreach ( $boxes as $k => $box ) { |
486 | if ( !$used[$k] ) { |
487 | $boxesnew[] = $box; |
43b698c7 |
488 | } |
489 | } |
90de1755 |
490 | |
491 | return( $boxesnew ); |
43b698c7 |
492 | } |
493 | |
90de1755 |
494 | /* |
495 | * Returns a list of all folders, subscribed or not |
496 | */ |
43b698c7 |
497 | function sqimap_mailbox_list_all ($imap_stream) |
498 | { |
499 | global $list_special_folders_first, $folder_prefix; |
500 | global $delimiter; |
90de1755 |
501 | |
502 | if (!function_exists('ary_sort')) { |
43b698c7 |
503 | include_once('../functions/array.php'); |
504 | } |
90de1755 |
505 | |
43b698c7 |
506 | $ssid = sqimap_session_id(); |
90de1755 |
507 | $lsid = strlen( $ssid ); |
43b698c7 |
508 | fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n"); |
90de1755 |
509 | $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message); |
43b698c7 |
510 | $g = 0; |
90de1755 |
511 | $phase = 'inbox'; |
512 | |
43b698c7 |
513 | for ($i = 0; $i < count($read_ary); $i++) { |
514 | /* Another workaround for EIMS */ |
515 | if (isset($read_ary[$i + 1]) && |
90de1755 |
516 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
517 | $read_ary[$i], $regs)) { |
518 | $i ++; |
90de1755 |
519 | $read_ary[$i] = $regs[1] . '"' . |
1c52ba77 |
520 | addslashes(trim($read_ary[$i])) . |
521 | '"' . $regs[2]; |
43b698c7 |
522 | } |
90de1755 |
523 | if (substr($read_ary[$i], 0, $lsid) != $ssid ) { |
524 | |
43b698c7 |
525 | /* Store the raw IMAP reply */ |
8e9e8afa |
526 | $boxes[$g]["raw"] = $read_ary[$i]; |
1c52ba77 |
527 | |
43b698c7 |
528 | /* Count number of delimiters ($delimiter) in folder name */ |
8e9e8afa |
529 | $mailbox = find_mailbox_name($read_ary[$i]); |
602e1431 |
530 | $dm_count = substr_count($mailbox, $delimiter); |
43b698c7 |
531 | if (substr($mailbox, -1) == $delimiter) { |
0cfd745a |
532 | /* If name ends in delimiter - decrement count by one */ |
90de1755 |
533 | $dm_count--; |
43b698c7 |
534 | } |
90de1755 |
535 | |
43b698c7 |
536 | /* Format folder name, but only if it's a INBOX.* or have */ |
537 | /* a parent. */ |
2752bb16 |
538 | $boxesbyname[$mailbox] = $g; |
525b7ae6 |
539 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
90de1755 |
540 | if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) || |
146e0c45 |
541 | (ereg('^'.$folder_prefix, $mailbox)) || |
2752bb16 |
542 | ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
43b698c7 |
543 | if ($dm_count) { |
90de1755 |
544 | $boxes[$g]['formatted'] = str_repeat(' ', $dm_count); |
43b698c7 |
545 | } |
546 | else { |
90de1755 |
547 | $boxes[$g]['formatted'] = ''; |
43b698c7 |
548 | } |
90de1755 |
549 | $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); |
2752bb16 |
550 | } |
43b698c7 |
551 | else { |
90de1755 |
552 | $boxes[$g]['formatted'] = $mailbox; |
43b698c7 |
553 | } |
90de1755 |
554 | |
8e9e8afa |
555 | $boxes[$g]["unformatted-dm"] = $mailbox; |
43b698c7 |
556 | if (substr($mailbox, -1) == $delimiter) { |
557 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
558 | } |
90de1755 |
559 | $boxes[$g]['unformatted'] = $mailbox; |
560 | $boxes[$g]['unformatted-disp'] = |
0cfd745a |
561 | ereg_replace('^' . $folder_prefix, '', $mailbox); |
90de1755 |
562 | $boxes[$g]['id'] = $g; |
563 | |
8e9e8afa |
564 | /** Now lets get the flags for this mailbox **/ |
1c72b151 |
565 | $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"", |
90de1755 |
566 | TRUE, $response, $message); |
567 | |
43b698c7 |
568 | /* Another workaround for EIMS */ |
569 | if (isset($read_mlbx[1]) && |
90de1755 |
570 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
571 | $read_mlbx[0], $regs)) { |
1c52ba77 |
572 | $read_mlbx[0] = $regs[1] . '"' . |
573 | addslashes(trim($read_mlbx[1])) . |
574 | '"' . $regs[2]; |
43b698c7 |
575 | } |
90de1755 |
576 | |
577 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1); |
578 | $flags = substr($flags, 0, strpos($flags, ')')); |
146e0c45 |
579 | $flags = str_replace('\\', '', $flags); |
8e9e8afa |
580 | $flags = trim(strtolower($flags)); |
581 | if ($flags) { |
1c52ba77 |
582 | $boxes[$g]['flags'] = explode(' ', $flags); |
90de1755 |
583 | } else { |
43b698c7 |
584 | $boxes[$g]['flags'] = array(); |
12d61439 |
585 | } |
43b698c7 |
586 | } |
587 | $g++; |
588 | } |
589 | if(is_array($boxes)) { |
1c52ba77 |
590 | $boxes = ary_sort ($boxes, 'unformatted', 1); |
43b698c7 |
591 | } |
90de1755 |
592 | |
43b698c7 |
593 | return $boxes; |
594 | } |
5bdd7223 |
595 | |
f7b1b3b1 |
596 | ?> |