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