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