6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This impliments all functions that manipulate mailboxes
13 require_once(SM_PATH
. 'functions/imap_utf7_encode_local.php');
14 require_once(SM_PATH
. 'functions/imap_utf7_decode_local.php');
18 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
19 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
20 $is_trash = false, $is_draft = false, $mbxs = array(),
21 $unseen = false, $total = false;
23 function addMbx($mbx, $delimiter, $start, $specialfirst) {
24 $ary = explode($delimiter, $mbx->mailboxname_full
);
26 for ($i=$start; $i < (count($ary) -1); $i++
) {
27 $mbx_childs = &$mbx_parent->mbxs
;
30 foreach ($mbx_childs as $key => $parent) {
31 if ($parent->mailboxname_sub
== $ary[$i]) {
32 $mbx_parent = &$mbx_parent->mbxs
[$key];
38 $no_select_mbx = new mailboxes();
39 if (isset($mbx_parent->mailboxname_full
) && $mbx_parent->mailboxname_full
!= '') {
40 $no_select_mbx->mailboxname_full
= $mbx_parent->mailboxname_full
.$delimiter.$ary[$i];
42 $no_select_mbx->mailboxname_full
= $ary[$i];
44 $no_select_mbx->mailboxname_sub
= $ary[$i];
45 $no_select_mbx->is_noselect
= true;
46 $mbx_parent->mbxs
[] = $no_select_mbx;
51 $mbx_parent->mbxs
[] = $mbx;
52 if ($mbx->is_special
&& $specialfirst) {
53 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
59 function sortSpecialMbx($a, $b) {
61 $acmp = '0'. $a->mailboxname_full
;
62 } else if ($a->is_special
) {
63 $acmp = '1'. $a->mailboxname_full
;
65 $acmp = '2' . $a->mailboxname_full
;
68 $bcmp = '0'. $b->mailboxname_full
;
69 }else if ($b->is_special
) {
70 $bcmp = '1' . $b->mailboxname_full
;
72 $bcmp = '2' . $b->mailboxname_full
;
74 if ($acmp == $bcmp) return 0;
75 return ($acmp>$bcmp) ?
1: -1;
78 function find_mailbox_name ($mailbox) {
79 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
81 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
85 function check_is_noselect ($lsub_line) {
86 return preg_match("/^\* LSUB \([^\)]*\\Noselect[^\)]*\)/i", $lsub_line);
90 * If $haystack is a full mailbox name, and $needle is the mailbox
91 * separator character, returns the second last part of the full
92 * mailbox name (i.e. the mailbox's parent mailbox)
94 function readMailboxParent($haystack, $needle) {
99 $parts = explode($needle, $haystack);
100 $elem = array_pop($parts);
101 while ($elem == '' && count($parts)) {
102 $elem = array_pop($parts);
104 $ret = join($needle, $parts);
110 function isBoxBelow( $box2, $box1 ) {
111 global $delimiter, $folder_prefix, $imap_server_type;
113 if ( $imap_server_type == 'uw' ) {
115 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
116 if ( $i === false ) {
117 $i = strlen( $box2 );
120 $boxs = $box2 . $delimiter;
121 /* Skip next second delimiter */
122 $i = strpos( $box1, $delimiter );
123 $i = strpos( $box1, $delimiter, $i +
1 );
124 if ( $i === false ) {
125 $i = strlen( $box2 );
131 return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
134 /* Defines special mailboxes */
135 function isSpecialMailbox( $box ) {
136 global $trash_folder, $sent_folder, $draft_folder,
137 $move_to_trash, $move_to_sent, $save_as_draft;
139 $ret = ( (strtolower($box) == 'inbox') ||
140 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
141 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
142 ($save_as_draft && $box == $draft_folder ) );
145 $ret = do_hook_function( 'special_mailbox', $box );
151 /* Expunges a mailbox */
152 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
156 $id = sqimap_message_list_squisher($id);
163 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
164 $response, $message, $uid);
167 if ( is_array( $read ) ) {
168 foreach ($read as $r) {
169 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
177 /* Checks whether or not the specified mailbox exists */
178 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
179 if (! isset($mailbox)) {
182 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
183 true, $response, $message);
184 return isset($mbx[0]);
187 /* Selects a mailbox */
188 function sqimap_mailbox_select ($imap_stream, $mailbox) {
189 global $auto_expunge;
191 if ( $mailbox == 'None' ) {
195 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
196 true, $response, $message);
198 for ($i=0; $i<count($read); $i++
) {
199 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
200 $result[strtoupper($regs[1])] = $regs[2];
201 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
202 $result[strtoupper($regs[2])] = $regs[1];
204 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
205 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
206 $result['PERMANENTFLAGS'] = $regs[1];
208 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
209 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
210 $result['FLAGS'] = $regs[1];
214 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
215 $result['RIGHTS']=$regs[1];
219 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
224 /* Creates a folder */
225 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
227 if (strtolower($type) == 'noselect') {
228 $mailbox .= $delimiter;
230 $mailbox = imap_utf7_encode_local($mailbox);
231 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
232 true, $response, $message);
233 sqimap_subscribe ($imap_stream, $mailbox);
236 /* Subscribes to an existing folder */
237 function sqimap_subscribe ($imap_stream, $mailbox) {
238 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
239 true, $response, $message);
242 /* Unsubscribes to an existing folder */
243 function sqimap_unsubscribe ($imap_stream, $mailbox) {
244 global $imap_server_type;
245 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
246 true, $response, $message);
249 /* Deletes the given folder */
250 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
251 global $data_dir, $username;
252 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
253 true, $response, $message);
254 sqimap_unsubscribe ($imap_stream, $mailbox);
255 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
256 removePref($data_dir, $username, "thread_$mailbox");
259 /* Determines if the user is subscribed to the folder or not */
260 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
261 $boxesall = sqimap_mailbox_list ($imap_stream);
262 foreach ($boxesall as $ref) {
263 if ($ref['unformatted'] == $folder) {
270 /* Renames a mailbox */
271 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
272 if ( $old_name != $new_name ) {
273 global $delimiter, $imap_server_type, $data_dir, $username;
274 if ( substr( $old_name, -1 ) == $delimiter ) {
275 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
276 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
277 $postfix = $delimiter;
281 $boxesall = sqimap_mailbox_list($imap_stream);
282 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
283 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
284 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
285 $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
286 removePref($data_dir, $username, "thread_".$old_name.$postfix);
287 sqimap_subscribe($imap_stream, $new_name.$postfix);
288 setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
289 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
290 $l = strlen( $old_name ) +
1;
292 foreach ( $boxesall as $box ) {
293 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
294 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
295 if ($imap_server_type == 'cyrus') {
296 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
297 $data = sqimap_run_command($imap_stream, $cmd, true,
298 $response, $message);
300 sqimap_unsubscribe($imap_stream, $box[$p]);
301 $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
302 removePref($data_dir, $username, "thread_".$box[$p]);
303 sqimap_subscribe($imap_stream, $new_sub);
304 setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
305 do_hook_function("rename_or_delete_folder",
306 $args = array($box[$p], 'rename', $new_sub));
313 * Formats a mailbox into 4 parts for the $boxesall array
315 * The four parts are:
317 * raw - Raw LIST/LSUB response from the IMAP server
318 * formatted - nicely formatted folder name
319 * unformatted - unformatted, but with delimiter at end removed
320 * unformatted-dm - folder name as it appears in raw response
321 * unformatted-disp - unformatted without $folder_prefix
323 function sqimap_mailbox_parse ($line, $line_lsub) {
324 global $folder_prefix, $delimiter;
326 /* Process each folder line */
327 for ($g=0; $g < count($line); $g++
) {
329 /* Store the raw IMAP reply */
330 if (isset($line[$g])) {
331 $boxesall[$g]["raw"] = $line[$g];
334 $boxesall[$g]["raw"] = '';
338 /* Count number of delimiters ($delimiter) in folder name */
339 $mailbox = trim($line_lsub[$g]);
340 $dm_count = substr_count($mailbox, $delimiter);
341 if (substr($mailbox, -1) == $delimiter) {
342 /* If name ends in delimiter, decrement count by one */
346 /* Format folder name, but only if it's a INBOX.* or has a parent. */
347 $boxesallbyname[$mailbox] = $g;
348 $parentfolder = readMailboxParent($mailbox, $delimiter);
349 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
350 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
351 ( isset($boxesallbyname[$parentfolder]) &&
352 (strlen($parentfolder) > 0) ) ) {
353 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
355 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
358 $boxesall[$g]['formatted'] = '';
360 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
363 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
366 $boxesall[$g]['unformatted-dm'] = $mailbox;
367 if (substr($mailbox, -1) == $delimiter) {
368 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
370 $boxesall[$g]['unformatted'] = $mailbox;
371 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
372 $mailbox = substr($mailbox, strlen($folder_prefix));
374 $boxesall[$g]['unformatted-disp'] = $mailbox;
375 $boxesall[$g]['id'] = $g;
377 $boxesall[$g]['flags'] = array();
378 if (isset($line[$g])) {
379 ereg("\(([^)]*)\)",$line[$g],$regs);
380 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
382 $boxesall[$g]['flags'] = explode(' ', $flags);
391 * Sorting function used to sort mailbox names.
392 * + Original patch from dave_michmerhuizen@yahoo.com
393 * + Allows case insensitivity when sorting folders
394 * + Takes care of the delimiter being sorted to the end, causing
395 * subfolders to be listed in below folders that are prefixed
396 * with their parent folders name.
398 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
399 * Without special sort function: foobar between foo and foo.bar
400 * With special sort function: foobar AFTER foo and foo.bar :)
402 function user_strcasecmp($a, $b) {
405 /* Calculate the length of some strings. */
406 $a_length = strlen($a);
407 $b_length = strlen($b);
408 $min_length = min($a_length, $b_length);
409 $delimiter_length = strlen($delimiter);
411 /* Set the initial result value. */
414 /* Check the strings... */
415 for ($c = 0; $c < $min_length; ++
$c) {
416 $a_del = substr($a, $c, $delimiter_length);
417 $b_del = substr($b, $c, $delimiter_length);
419 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
421 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
423 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
426 $result = strcasecmp($a{$c}, $b{$c});
434 /* If one string is a prefix of the other... */
436 if ($a_length < $b_length) {
438 } else if ($a_length > $b_length) {
448 * Returns sorted mailbox lists in several different ways.
449 * See comment on sqimap_mailbox_parse() for info about the returned array.
451 function sqimap_mailbox_list($imap_stream) {
452 global $default_folder_prefix;
454 if ( !isset( $boxesnew ) ) {
456 global $data_dir, $username, $list_special_folders_first,
457 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
458 $move_to_trash, $move_to_sent, $save_as_draft,
459 $delimiter, $noselect_fix_enable;
461 $inbox_in_list = false;
462 $inbox_subscribed = false;
464 require_once(SM_PATH
. 'include/load_prefs.php');
465 require_once(SM_PATH
. 'functions/array.php');
467 if ($noselect_fix_enable) {
468 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
471 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
474 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
475 true, $response, $message);
479 * Section about removing the last element was removed
480 * We don't return "* OK" anymore from sqimap_read_data
483 $sorted_lsub_ary = array();
484 for ($i=0;$i < count($lsub_ary); $i++
) {
486 * Workaround for EIMS
487 * Doesn't work if the mailbox name is multiple lines
489 if (isset($lsub_ary[$i +
1]) &&
490 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
491 $lsub_ary[$i], $regs)) {
493 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
495 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
496 $sorted_lsub_ary[] = $temp_mailbox_name;
497 if (strtoupper($temp_mailbox_name) == 'INBOX') {
498 $inbox_subscribed = true;
502 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
503 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
504 $new_ary[] = $sorted_lsub_ary[$i];
507 $sorted_lsub_ary = $new_ary;
508 if (isset($sorted_lsub_ary)) {
509 usort($sorted_lsub_ary, 'user_strcasecmp');
513 $sorted_list_ary = array();
514 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
515 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
516 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
519 $mbx = $sorted_lsub_ary[$i];
522 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
523 true, $response, $message);
524 /* Another workaround for EIMS */
525 if (isset($read[1]) &&
526 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
528 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
531 if (isset($sorted_list_ary[$i])) {
532 $sorted_list_ary[$i] = '';
535 if (isset($read[0])) {
536 $sorted_list_ary[$i] = $read[0];
539 $sorted_list_ary[$i] = '';
542 if (isset($sorted_list_ary[$i]) &&
543 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
544 $inbox_in_list = true;
549 * Just in case they're not subscribed to their inbox,
550 * we'll get it for them anyway
552 if (!$inbox_subscribed ||
!$inbox_in_list) {
553 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
554 true, $response, $message);
555 /* Another workaround for EIMS */
556 if (isset($inbox_ary[1]) &&
557 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
558 $inbox_ary[0], $regs)) {
559 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
563 $sorted_list_ary[] = $inbox_ary[0];
564 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
567 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
569 /* Now, lets sort for special folders */
570 $boxesnew = $used = array();
573 foreach ( $boxesall as $k => $box ) {
574 if ( strtolower($box['unformatted']) == 'inbox') {
581 /* List special folders and their subfolders, if requested. */
582 if ($list_special_folders_first) {
583 foreach ( $boxesall as $k => $box ) {
584 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
588 $spec_sub = str_replace(' ', '', $box['formatted']);
589 $spec_sub = preg_replace("/(\*|\[|\]|\(|\)|\?|\+|\{|\}|\^|\\$)/", '\\\\'.'\\1', $spec_sub);
591 /* In case of problems with preg
592 here is a ereg version
593 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
595 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
602 /* Rest of the folders */
603 foreach ( $boxesall as $k => $box ) {
613 * Returns a list of all folders, subscribed or not
615 function sqimap_mailbox_list_all($imap_stream) {
616 global $list_special_folders_first, $folder_prefix, $delimiter;
618 require_once(SM_PATH
. 'functions/array.php');
620 $ssid = sqimap_session_id();
621 $lsid = strlen( $ssid );
622 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
623 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
627 for ($i = 0; $i < count($read_ary); $i++
) {
628 /* Another workaround for EIMS */
629 if (isset($read_ary[$i +
1]) &&
630 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
631 $read_ary[$i], $regs)) {
633 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
635 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
637 /* Store the raw IMAP reply */
638 $boxes[$g]['raw'] = $read_ary[$i];
640 /* Count number of delimiters ($delimiter) in folder name */
641 $mailbox = find_mailbox_name($read_ary[$i]);
642 $dm_count = substr_count($mailbox, $delimiter);
643 if (substr($mailbox, -1) == $delimiter) {
644 /* If name ends in delimiter - decrement count by one */
648 /* Format folder name, but only if it's a INBOX.* or has a parent. */
649 $boxesallbyname[$mailbox] = $g;
650 $parentfolder = readMailboxParent($mailbox, $delimiter);
651 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
652 (ereg('^'.$folder_prefix, $mailbox)) ||
653 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
655 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
658 $boxes[$g]['formatted'] = '';
660 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
663 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
666 $boxes[$g]['unformatted-dm'] = $mailbox;
667 if (substr($mailbox, -1) == $delimiter) {
668 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
670 $boxes[$g]['unformatted'] = $mailbox;
671 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
672 $boxes[$g]['id'] = $g;
674 /* Now lets get the flags for this mailbox */
675 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
676 true, $response, $message);
678 /* Another workaround for EIMS */
679 if (isset($read_mlbx[1]) &&
680 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
681 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
684 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
685 $flags = substr($flags, 0, strpos($flags, ')'));
686 $flags = str_replace('\\', '', $flags);
687 $flags = trim(strtolower($flags));
689 $boxes[$g]['flags'] = explode(' ', $flags);
691 $boxes[$g]['flags'] = array();
696 if(is_array($boxes)) {
697 $boxes = ary_sort ($boxes, 'unformatted', 1);
703 function sqimap_mailbox_tree($imap_stream) {
704 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
705 if ( !isset( $boxesnew ) ) {
707 global $data_dir, $username, $list_special_folders_first,
708 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
711 $inbox_in_list = false;
712 $inbox_subscribed = false;
714 require_once(SM_PATH
. 'include/load_prefs.php');
715 require_once(SM_PATH
. 'functions/array.php');
718 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
719 true, $response, $message);
722 * Section about removing the last element was removed
723 * We don't return "* OK" anymore from sqimap_read_data
725 $sorted_lsub_ary = array();
726 $cnt = count($lsub_ary);
727 for ($i=0;$i < $cnt; $i++
) {
729 * Workaround for EIMS
730 * Doesn't work if the mailbox name is multiple lines
732 if (isset($lsub_ary[$i +
1]) &&
733 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
734 $lsub_ary[$i], $regs)) {
736 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
739 // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
741 // $mbx = trim($regs[3]);
742 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
744 $mbx = find_mailbox_name($lsub_ary[$i]);
745 $noselect = check_is_noselect($lsub_ary[$i]);
746 if (substr($mbx, -1) == $delimiter) {
747 $mbx = substr($mbx, 0, strlen($mbx) - 1);
749 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
751 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
753 foreach ($sorted_lsub_ary as $mbx) {
754 if ($mbx['mbx'] == 'INBOX') {
755 $inbox_in_list = true;
761 * Just in case they're not subscribed to their inbox,
762 * we'll get it for them anyway
764 if (!$inbox_in_list) {
765 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
766 true, $response, $message);
767 /* Another workaround for EIMS */
768 if (isset($inbox_ary[1]) &&
769 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
770 $inbox_ary[0], $regs)) {
771 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
774 $mbx = find_mailbox_name($inbox_ary[0]);
775 if (substr($mbx, -1) == $delimiter) {
776 $mbx = substr($mbx, 0, strlen($mbx) - 1);
778 if ( $mbx == 'INBOX') {
779 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
780 sqimap_subscribe($imap_stream, 'INBOX');
783 // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
785 // $mbx = trim($regs[3]);
786 // if (substr($mbx, -1) == $delimiter) {
787 // $mbx = substr($mbx, 0, strlen($mbx) - 1);
789 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
792 $cnt = count($sorted_lsub_ary);
793 for ($i=0 ; $i < $cnt; $i++
) {
794 $mbx = $sorted_lsub_ary[$i]['mbx'];
795 if (($unseen_notify == 2 && $mbx == 'INBOX')
796 ||
$unseen_notify == 3
797 ||
($move_to_trash && ($mbx == $trash_folder))) {
798 $sorted_lsub_ary[$i]['unseen'] =
799 $sorted_lsub_ary[$i]['noselect'] ?
800 0 : sqimap_unseen_messages($imap_stream, $mbx);
801 if ($unseen_type == 2 ||
($move_to_trash
802 && ($mbx == $trash_folder) )) {
803 $sorted_lsub_ary[$i]['nummessages'] =
804 $sorted_lsub_ary[$i]['noselect'] ?
805 0 : sqimap_get_num_messages($imap_stream, $mbx);
807 if ($mbx == $trash_folder) {
808 $sorted_lsub_ary[$i]['nummessages'] =
809 $sorted_lsub_ary[$i]['noselect'] ?
810 0 : sqimap_get_num_messages($imap_stream, $mbx);
814 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
820 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
821 global $data_dir, $username, $list_special_folders_first,
822 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
823 $move_to_trash, $move_to_sent, $save_as_draft,
826 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
828 /* create virtual root node */
829 $mailboxes= new mailboxes();
830 $mailboxes->is_root
= true;
832 if (isset($folder_prefix) && $folder_prefix != '') {
833 $start = substr_count($folder_prefix,$delimiter);
834 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
836 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
838 $mailboxes->mailboxname_full
= $folder_prefix;
841 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
843 $cnt = count($mbx_ary);
844 for ($i=0; $i < $cnt; $i++
) {
845 if ($mbx_ary[$i]['mbx'] !='' ) {
846 $mbx = new mailboxes();
847 $mailbox = $mbx_ary[$i]['mbx'];
850 $mbx->is_inbox
= true;
851 $mbx->is_special
= true;
854 $mbx->is_trash
= true;
855 $mbx->is_special
= true;
858 $mbx->is_sent
= true;
859 $mbx->is_special
= true;
862 $mbx->is_draft
= true;
863 $mbx->is_special
= true;
867 if (isset($mbx_ary[$i]['unseen'])) {
868 $mbx->unseen
= $mbx_ary[$i]['unseen'];
870 if (isset($mbx_ary[$i]['nummessages'])) {
871 $mbx->total
= $mbx_ary[$i]['nummessages'];
874 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
876 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
878 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
879 } else { /* mailbox is root folder */
880 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
882 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
883 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);