6 * Copyright (c) 1999-2003 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_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, $c = count($ary)-1; $i < $c; $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;
50 $mbx_parent->mbxs
[] = $mbx;
51 if ($mbx->is_special
&& $specialfirst) {
52 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
57 function sortSpecialMbx($a, $b) {
59 $acmp = '0'. $a->mailboxname_full
;
60 } else if ($a->is_special
) {
61 $acmp = '1'. $a->mailboxname_full
;
63 $acmp = '2' . $a->mailboxname_full
;
66 $bcmp = '0'. $b->mailboxname_full
;
67 }else if ($b->is_special
) {
68 $bcmp = '1' . $b->mailboxname_full
;
70 $bcmp = '2' . $b->mailboxname_full
;
72 if ($acmp == $bcmp) return 0;
73 return ($acmp > $bcmp) ?
1: -1;
76 function find_mailbox_name ($mailbox) {
77 if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
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) {
98 $parts = explode($needle, $haystack);
99 $elem = array_pop($parts);
100 while ($elem == '' && count($parts)) {
101 $elem = array_pop($parts);
103 $ret = join($needle, $parts);
109 function isBoxBelow( $box2, $box1 ) {
110 global $delimiter, $folder_prefix, $imap_server_type;
111 if ( $imap_server_type == 'uw' ) {
113 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
114 if ( $i === false ) {
115 $i = strlen( $box2 );
118 if (substr($box2,0,strlen($box1)) == $box1) {
121 $boxs = $box2 . $delimiter;
122 /* Skip next second delimiter */
123 $i = strpos( $box1, $delimiter );
124 $i = strpos( $box1, $delimiter, $i +
1 );
125 if ( $i === false ) {
126 $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 && $trash_folder && isBoxBelow( $box, $trash_folder )) ||
141 ( $move_to_sent && $sent_folder && isBoxBelow( $box, $sent_folder )) ||
142 ($save_as_draft && $box == $draft_folder ) );
145 $ret = do_hook_function( 'special_mailbox', $box );
150 /* Expunges a mailbox */
151 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
155 $id = sqimap_message_list_squisher($id);
162 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
163 $response, $message, $uid);
166 if (is_array($read)) {
167 foreach ($read as $r) {
168 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
176 /* Checks whether or not the specified mailbox exists */
177 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
178 if (!isset($mailbox)) {
181 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
182 true, $response, $message);
183 return isset($mbx[0]);
186 /* Selects a mailbox */
187 function sqimap_mailbox_select ($imap_stream, $mailbox) {
188 global $auto_expunge;
190 if ($mailbox == 'None') {
194 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
195 true, $response, $message);
197 for ($i = 0, $cnt = count($read); $i < $cnt; $i++
) {
198 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
199 $result[strtoupper($regs[1])] = $regs[2];
200 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
201 $result[strtoupper($regs[2])] = $regs[1];
203 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
204 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
205 $result['PERMANENTFLAGS'] = $regs[1];
206 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
207 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
208 $result['FLAGS'] = $regs[1];
212 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
213 $result['RIGHTS']=$regs[1];
217 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
222 /* Creates a folder */
223 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
225 if (strtolower($type) == 'noselect') {
226 $mailbox .= $delimiter;
229 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
230 true, $response, $message);
231 sqimap_subscribe ($imap_stream, $mailbox);
234 /* Subscribes to an existing folder */
235 function sqimap_subscribe ($imap_stream, $mailbox) {
236 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
237 true, $response, $message);
240 /* Unsubscribes to an existing folder */
241 function sqimap_unsubscribe ($imap_stream, $mailbox) {
242 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
243 true, $response, $message);
246 /* Deletes the given folder */
247 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
248 global $data_dir, $username;
249 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
250 true, $response, $message);
251 sqimap_unsubscribe ($imap_stream, $mailbox);
252 do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
253 removePref($data_dir, $username, "thread_$mailbox");
256 /* Determines if the user is subscribed to the folder or not */
257 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
258 $boxesall = sqimap_mailbox_list ($imap_stream);
259 foreach ($boxesall as $ref) {
260 if ($ref['unformatted'] == $folder) {
267 /* Renames a mailbox */
268 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
269 if ( $old_name != $new_name ) {
270 global $delimiter, $imap_server_type, $data_dir, $username;
271 if ( substr( $old_name, -1 ) == $delimiter ) {
272 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
273 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
274 $postfix = $delimiter;
279 $boxesall = sqimap_mailbox_list($imap_stream);
280 $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
281 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
282 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
283 $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
284 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
285 sqimap_subscribe($imap_stream, $new_name.$postfix);
286 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
287 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
288 $l = strlen( $old_name ) +
1;
291 foreach ($boxesall as $box) {
292 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
293 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
294 if ($imap_server_type == 'cyrus') {
295 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
296 $data = sqimap_run_command($imap_stream, $cmd, true,
297 $response, $message);
299 sqimap_unsubscribe($imap_stream, $box[$p]);
300 $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
301 removePref($data_dir, $username, 'thread_'.$box[$p]);
302 sqimap_subscribe($imap_stream, $new_sub);
303 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
304 do_hook_function('rename_or_delete_folder',
305 $args = array($box[$p], 'rename', $new_sub));
312 * Formats a mailbox into 4 parts for the $boxesall array
314 * The four parts are:
316 * raw - Raw LIST/LSUB response from the IMAP server
317 * formatted - nicely formatted folder name
318 * unformatted - unformatted, but with delimiter at end removed
319 * unformatted-dm - folder name as it appears in raw response
320 * unformatted-disp - unformatted without $folder_prefix
322 function sqimap_mailbox_parse ($line, $line_lsub) {
323 global $folder_prefix, $delimiter;
325 /* Process each folder line */
326 for ($g = 0, $cnt = count($line); $g < $cnt; $g++
) {
327 /* Store the raw IMAP reply */
328 if (isset($line[$g])) {
329 $boxesall[$g]['raw'] = $line[$g];
331 $boxesall[$g]['raw'] = '';
334 /* Count number of delimiters ($delimiter) in folder name */
335 $mailbox = trim($line_lsub[$g]);
336 $dm_count = substr_count($mailbox, $delimiter);
337 if (substr($mailbox, -1) == $delimiter) {
338 /* If name ends in delimiter, decrement count by one */
342 /* Format folder name, but only if it's a INBOX.* or has a parent. */
343 $boxesallbyname[$mailbox] = $g;
344 $parentfolder = readMailboxParent($mailbox, $delimiter);
345 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
346 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
347 (isset($boxesallbyname[$parentfolder]) &&
348 (strlen($parentfolder) > 0) ) ) {
349 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
351 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
353 $boxesall[$g]['formatted'] = '';
355 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
357 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
360 $boxesall[$g]['unformatted-dm'] = $mailbox;
361 if (substr($mailbox, -1) == $delimiter) {
362 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
364 $boxesall[$g]['unformatted'] = $mailbox;
365 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
366 $mailbox = substr($mailbox, strlen($folder_prefix));
368 $boxesall[$g]['unformatted-disp'] = $mailbox;
369 $boxesall[$g]['id'] = $g;
371 $boxesall[$g]['flags'] = array();
372 if (isset($line[$g])) {
373 ereg("\(([^)]*)\)",$line[$g],$regs);
374 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
376 $boxesall[$g]['flags'] = explode(' ', $flags);
384 * Sorting function used to sort mailbox names.
385 * + Original patch from dave_michmerhuizen@yahoo.com
386 * + Allows case insensitivity when sorting folders
387 * + Takes care of the delimiter being sorted to the end, causing
388 * subfolders to be listed in below folders that are prefixed
389 * with their parent folders name.
391 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
392 * Without special sort function: foobar between foo and foo.bar
393 * With special sort function: foobar AFTER foo and foo.bar :)
395 function user_strcasecmp($a, $b) {
398 return strnatcasecmp($a, $b);
400 /* Calculate the length of some strings. */
401 $a_length = strlen($a);
402 $b_length = strlen($b);
403 $min_length = min($a_length, $b_length);
404 $delimiter_length = strlen($delimiter);
406 /* Set the initial result value. */
408 /* Check the strings... */
409 for ($c = 0; $c < $min_length; ++
$c) {
410 $a_del = substr($a, $c, $delimiter_length);
411 $b_del = substr($b, $c, $delimiter_length);
413 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
415 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
417 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
420 $result = strcasecmp($a{$c}, $b{$c});
428 /* If one string is a prefix of the other... */
430 if ($a_length < $b_length) {
432 } else if ($a_length > $b_length) {
441 * Returns list of options (to be echoed into select statement
442 * based on available mailboxes and separators
443 * Caller should surround options with <SELECT..> </SELECT> and
445 * $imap_stream - $imapConnection to query for mailboxes
446 * $show_selected - array containing list of mailboxes to pre-select (0 if none)
447 * $folder_skip - array of folders to keep out of option list (compared in lower)
448 * $boxes - list of already fetched boxes (for places like folder panel, where
449 * you know these options will be shown 3 times in a row.. (most often unset).
451 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0 ) {
452 global $username, $data_dir;
455 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF
);
458 $boxes = sqimap_mailbox_list($imap_stream);
460 foreach ($boxes as $boxes_part) {
461 if (!in_array('noselect', $boxes_part['flags'])) {
462 $box = $boxes_part['unformatted'];
463 $lowerbox = strtolower($box);
465 if ($folder_skip != 0 && in_array($lowerbox, $folder_skip) ) {
468 if ($lowerbox == 'inbox'){
470 } else if ( $shorten_box_names == 2 ) { /* delimited, style = 2 */
471 $box2 = str_replace(' ', '. ', $boxes_part['formatted']);
472 } else if ( $shorten_box_names == 1 ) { /* indent, style = 1 */
473 $box2 = $boxes_part['formatted'];
474 } else { /* default, long names, style = 0 */
475 $box2 = str_replace(' ', ' ', imap_utf7_decode_local($boxes_part['unformatted-disp']));
477 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
478 $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
480 $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
484 return $mbox_options;
488 * Returns sorted mailbox lists in several different ways.
489 * See comment on sqimap_mailbox_parse() for info about the returned array.
491 function sqimap_mailbox_list($imap_stream) {
492 global $default_folder_prefix;
494 if (!isset($boxesnew)) {
495 global $data_dir, $username, $list_special_folders_first,
496 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
497 $move_to_trash, $move_to_sent, $save_as_draft,
498 $delimiter, $noselect_fix_enable;
500 $inbox_in_list = false;
501 $inbox_subscribed = false;
503 require_once(SM_PATH
. 'include/load_prefs.php');
505 if ($noselect_fix_enable) {
506 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
508 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
511 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
512 true, $response, $message);
516 * Section about removing the last element was removed
517 * We don't return "* OK" anymore from sqimap_read_data
520 $sorted_lsub_ary = array();
521 for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++
) {
523 * Workaround for EIMS
524 * Doesn't work if the mailbox name is multiple lines
526 if (isset($lsub_ary[$i +
1]) &&
527 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
528 $lsub_ary[$i], $regs)) {
530 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
532 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
533 $sorted_lsub_ary[] = $temp_mailbox_name;
534 if (strtoupper($temp_mailbox_name) == 'INBOX') {
535 $inbox_subscribed = true;
539 for ($i = 0, $cnt = count($sorted_lsub_ary); $i < $cnt; $i++
) {
540 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
541 $new_ary[] = $sorted_lsub_ary[$i];
544 $sorted_lsub_ary = $new_ary;
545 if (isset($sorted_lsub_ary)) {
546 usort($sorted_lsub_ary, 'user_strcasecmp');
548 $sorted_list_ary = $sorted_lsub_ary;
550 /* $sorted_list_ary = array();
551 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
553 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
554 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
557 $mbx = $sorted_lsub_ary[$i];
560 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
561 true, $response, $message);
563 /* Another workaround for EIMS */
565 if (isset($read[1]) &&
566 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
568 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
571 if (isset($sorted_list_ary[$i])) {
572 $sorted_list_ary[$i] = '';
575 if (isset($read[0])) {
576 $sorted_list_ary[$i] = $read[0];
579 $sorted_list_ary[$i] = '';
582 if (isset($sorted_list_ary[$i]) &&
583 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
584 $inbox_in_list = true;
588 /* $inbox_in_list = true; */
590 * Just in case they're not subscribed to their inbox,
591 * we'll get it for them anyway
593 if (!$inbox_subscribed) {/* || !$inbox_in_list) { */
594 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
595 true, $response, $message);
596 /* Another workaround for EIMS */
597 if (isset($inbox_ary[1]) &&
598 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
599 $inbox_ary[0], $regs)) {
600 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
604 $sorted_list_ary[] = $inbox_ary[0];
605 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
608 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
610 /* Now, lets sort for special folders */
611 $boxesnew = $used = array();
614 $cnt = count($boxesall);
615 for($k = 0; $k < $cnt; $k++
) {
616 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
617 $boxesnew[] = $boxesall[$k];
623 /* List special folders and their subfolders, if requested. */
624 if ($list_special_folders_first) {
625 for($k = 0; $k < $cnt; $k++
) {
626 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
627 $boxesnew[] = $boxesall[$k];
630 $spec_sub = str_replace(' ', '', $boxesall[$k]['formatted']);
631 $spec_sub = preg_replace("/(\*|\[|\]|\(|\)|\?|\+|\{|\}|\^|\\$)/", '\\\\'.'\\1', $spec_sub);
633 /* In case of problems with preg
634 here is a ereg version
635 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) {
637 $match = "?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?";
638 if (!$used[$k] && preg_match($match, $boxesall[$k]['unformatted']) ) {
639 $boxesnew[] = $boxesall[$k];
645 /* Rest of the folders */
646 for($k = 0; $k < $cnt; $k++
) {
648 $boxesnew[] = $boxesall[$k];
656 * Returns a list of all folders, subscribed or not
658 function sqimap_mailbox_list_all($imap_stream) {
659 global $list_special_folders_first, $folder_prefix, $delimiter;
661 $ssid = sqimap_session_id();
662 $lsid = strlen( $ssid );
663 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
664 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
667 $fld_pre_length = strlen($folder_prefix);
669 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++
) {
670 /* Another workaround for EIMS */
671 if (isset($read_ary[$i +
1]) &&
672 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
673 $read_ary[$i], $regs)) {
675 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
677 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
678 /* Store the raw IMAP reply */
679 $boxes[$g]['raw'] = $read_ary[$i];
681 /* Count number of delimiters ($delimiter) in folder name */
682 $mailbox = find_mailbox_name($read_ary[$i]);
683 $dm_count = substr_count($mailbox, $delimiter);
684 if (substr($mailbox, -1) == $delimiter) {
685 /* If name ends in delimiter - decrement count by one */
689 /* Format folder name, but only if it's a INBOX.* or has a parent. */
690 $boxesallbyname[$mailbox] = $g;
691 $parentfolder = readMailboxParent($mailbox, $delimiter);
692 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
693 (ereg('^'.$folder_prefix, $mailbox)) ||
694 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
696 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
698 $boxes[$g]['formatted'] = '';
700 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
702 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
705 $boxes[$g]['unformatted-dm'] = $mailbox;
706 if (substr($mailbox, -1) == $delimiter) {
707 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
709 $boxes[$g]['unformatted'] = $mailbox;
710 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
712 $boxes[$g]['id'] = $g;
714 /* Now lets get the flags for this mailbox */
715 $read_mlbx = $read_ary[$i];
717 // $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
718 // true, $response, $message);
720 /* Another workaround for EIMS */
721 // if (isset($read_mlbx[1]) &&
722 // ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
723 // $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
725 // echo $read_mlbx[0] .' raw 2 <br>';
727 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+
1);
728 $flags = substr($flags, 0, strpos($flags, ')'));
729 $flags = str_replace('\\', '', $flags);
730 $flags = trim(strtolower($flags));
732 $boxes[$g]['flags'] = explode(' ', $flags);
734 $boxes[$g]['flags'] = array();
739 if(is_array($boxes)) {
746 function sqimap_mailbox_tree($imap_stream) {
747 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
748 if (!isset($boxesnew)) {
750 global $data_dir, $username, $list_special_folders_first,
751 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
754 $inbox_in_list = false;
755 $inbox_subscribed = false;
757 require_once(SM_PATH
. 'include/load_prefs.php');
760 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
761 true, $response, $message);
764 * Section about removing the last element was removed
765 * We don't return "* OK" anymore from sqimap_read_data
767 $sorted_lsub_ary = array();
768 $cnt = count($lsub_ary);
769 for ($i = 0; $i < $cnt; $i++
) {
771 * Workaround for EIMS
772 * Doesn't work if the mailbox name is multiple lines
774 if (isset($lsub_ary[$i +
1]) &&
775 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
776 $lsub_ary[$i], $regs)) {
778 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
782 if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
784 $mbx = trim($regs[3]);
785 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
788 $mbx = find_mailbox_name($lsub_ary[$i]);
789 $noselect = check_is_noselect($lsub_ary[$i]);
790 if (substr($mbx, -1) == $delimiter) {
791 $mbx = substr($mbx, 0, strlen($mbx) - 1);
793 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
795 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
797 for($i = 0; $i < $cnt; $i++
) {
798 if ($sorted_lsub_ary[$i]['mbx'] == 'INBOX') {
799 $inbox_in_list = true;
805 * Just in case they're not subscribed to their inbox,
806 * we'll get it for them anyway
808 if (!$inbox_in_list) {
809 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
810 true, $response, $message);
811 /* Another workaround for EIMS */
812 if (isset($inbox_ary[1]) &&
813 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
814 $inbox_ary[0], $regs)) {
815 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
818 $mbx = find_mailbox_name($inbox_ary[0]);
819 if (substr($mbx, -1) == $delimiter) {
820 $mbx = substr($mbx, 0, strlen($mbx) - 1);
822 if ($mbx == 'INBOX') {
823 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
824 sqimap_subscribe($imap_stream, 'INBOX');
829 if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
831 $mbx = trim($regs[3]);
832 if (substr($mbx, -1) == $delimiter) {
833 $mbx = substr($mbx, 0, strlen($mbx) - 1);
835 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
839 for ($i = 0 ; $i < $cnt; $i++
) {
840 $mbx = $sorted_lsub_ary[$i]['mbx'];
841 if (($unseen_notify == 2 && $mbx == 'INBOX') ||
842 ($unseen_notify == 3) ||
843 ($move_to_trash && ($mbx == $trash_folder))) {
844 if($sorted_lsub_ary[$i]['noselect']) {
845 $sorted_lsub_ary[$i]['unseen'] = 0;
847 $sorted_lsub_ary[$i]['unseen'] =
848 sqimap_unseen_messages($imap_stream, $mbx);
850 if (($unseen_type == 2) ||
851 ($move_to_trash && ($mbx == $trash_folder)) ||
852 ($mbx == $trash_folder)) {
853 if($sorted_lsub_ary[$i]['noselect']) {
854 $sorted_lsub_ary[$i]['nummessages'] = 0;
856 $sorted_lsub_ary[$i]['nummessages'] =
857 sqimap_get_num_messages($imap_stream, $mbx);
862 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
868 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
869 global $data_dir, $username, $list_special_folders_first,
870 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
871 $move_to_trash, $move_to_sent, $save_as_draft,
874 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
876 /* create virtual root node */
877 $mailboxes= new mailboxes();
878 $mailboxes->is_root
= true;
880 if (isset($folder_prefix) && $folder_prefix != '') {
881 $start = substr_count($folder_prefix,$delimiter);
882 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
884 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
886 $mailboxes->mailboxname_full
= $folder_prefix;
889 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
893 $cnt = count($mbx_ary);
894 for ($i=0; $i < $cnt; $i++
) {
895 if ($mbx_ary[$i]['mbx'] !='' ) {
896 $mbx = new mailboxes();
897 $mailbox = $mbx_ary[$i]['mbx'];
900 $mbx->is_inbox
= true;
901 $mbx->is_special
= true;
904 $mbx->is_trash
= true;
905 $mbx->is_special
= true;
908 $mbx->is_sent
= true;
909 $mbx->is_special
= true;
912 $mbx->is_draft
= true;
913 $mbx->is_special
= true;
917 if (isset($mbx_ary[$i]['unseen'])) {
918 $mbx->unseen
= $mbx_ary[$i]['unseen'];
920 if (isset($mbx_ary[$i]['nummessages'])) {
921 $mbx->total
= $mbx_ary[$i]['nummessages'];
924 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
926 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
928 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
929 } else { /* mailbox is root folder */
930 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
932 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
933 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);