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