2c161eaeae92bc31d1af90ce8fff534c2dfd4a69
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 (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
81 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
83 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
87 function check_is_noselect ($lsub_line) {
88 return preg_match("/^\* LSUB \([^\)]*\\Noselect[^\)]*\)/i", $lsub_line);
92 * If $haystack is a full mailbox name, and $needle is the mailbox
93 * separator character, returns the second last part of the full
94 * mailbox name (i.e. the mailbox's parent mailbox)
96 function readMailboxParent($haystack, $needle) {
101 $parts = explode($needle, $haystack);
102 $elem = array_pop($parts);
103 while ($elem == '' && count($parts)) {
104 $elem = array_pop($parts);
106 $ret = join($needle, $parts);
112 function isBoxBelow( $box2, $box1 ) {
113 global $delimiter, $folder_prefix, $imap_server_type;
115 if ( $imap_server_type == 'uw' ) {
117 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
118 if ( $i === false ) {
119 $i = strlen( $box2 );
122 $boxs = $box2 . $delimiter;
123 /* Skip next second delimiter */
124 $i = strpos( $box1, $delimiter );
125 $i = strpos( $box1, $delimiter, $i +
1 );
126 if ( $i === false ) {
127 $i = strlen( $box2 );
133 return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
136 /* Defines special mailboxes */
137 function isSpecialMailbox( $box ) {
138 global $trash_folder, $sent_folder, $draft_folder,
139 $move_to_trash, $move_to_sent, $save_as_draft;
141 $ret = ( (strtolower($box) == 'inbox') ||
142 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
143 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
144 ($save_as_draft && $box == $draft_folder ) );
147 $ret = do_hook_function( 'special_mailbox', $box );
153 /* Expunges a mailbox */
154 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
158 $id = sqimap_message_list_squisher($id);
165 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
166 $response, $message, $uid);
169 if ( is_array( $read ) ) {
170 foreach ($read as $r) {
171 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
179 /* Checks whether or not the specified mailbox exists */
180 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
181 if (! isset($mailbox)) {
184 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
185 true, $response, $message);
186 return isset($mbx[0]);
189 /* Selects a mailbox */
190 function sqimap_mailbox_select ($imap_stream, $mailbox) {
191 global $auto_expunge;
193 if ( $mailbox == 'None' ) {
197 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
198 true, $response, $message);
200 for ($i=0; $i<count($read); $i++
) {
201 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
202 $result[strtoupper($regs[1])] = $regs[2];
203 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
204 $result[strtoupper($regs[2])] = $regs[1];
206 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
207 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
208 $result['PERMANENTFLAGS'] = $regs[1];
210 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
211 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
212 $result['FLAGS'] = $regs[1];
216 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
217 $result['RIGHTS']=$regs[1];
221 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
226 /* Creates a folder */
227 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
229 if (strtolower($type) == 'noselect') {
230 $mailbox .= $delimiter;
232 $mailbox = imap_utf7_encode_local($mailbox);
233 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
234 true, $response, $message);
235 sqimap_subscribe ($imap_stream, $mailbox);
238 /* Subscribes to an existing folder */
239 function sqimap_subscribe ($imap_stream, $mailbox) {
240 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
241 true, $response, $message);
244 /* Unsubscribes to an existing folder */
245 function sqimap_unsubscribe ($imap_stream, $mailbox) {
246 global $imap_server_type;
247 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
248 true, $response, $message);
251 /* Deletes the given folder */
252 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
253 global $data_dir, $username;
254 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
255 true, $response, $message);
256 sqimap_unsubscribe ($imap_stream, $mailbox);
257 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
258 removePref($data_dir, $username, "thread_$mailbox");
261 /* Determines if the user is subscribed to the folder or not */
262 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
263 $boxesall = sqimap_mailbox_list ($imap_stream);
264 foreach ($boxesall as $ref) {
265 if ($ref['unformatted'] == $folder) {
272 /* Renames a mailbox */
273 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
274 if ( $old_name != $new_name ) {
275 global $delimiter, $imap_server_type, $data_dir, $username;
276 if ( substr( $old_name, -1 ) == $delimiter ) {
277 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
278 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
279 $postfix = $delimiter;
283 $boxesall = sqimap_mailbox_list($imap_stream);
284 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
285 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
286 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
287 $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
288 removePref($data_dir, $username, "thread_".$old_name.$postfix);
289 sqimap_subscribe($imap_stream, $new_name.$postfix);
290 setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
291 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
292 $l = strlen( $old_name ) +
1;
294 foreach ( $boxesall as $box ) {
295 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
296 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
297 if ($imap_server_type == 'cyrus') {
298 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
299 $data = sqimap_run_command($imap_stream, $cmd, true,
300 $response, $message);
302 sqimap_unsubscribe($imap_stream, $box[$p]);
303 $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
304 removePref($data_dir, $username, "thread_".$box[$p]);
305 sqimap_subscribe($imap_stream, $new_sub);
306 setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
307 do_hook_function("rename_or_delete_folder",
308 $args = array($box[$p], 'rename', $new_sub));
315 * Formats a mailbox into 4 parts for the $boxesall array
317 * The four parts are:
319 * raw - Raw LIST/LSUB response from the IMAP server
320 * formatted - nicely formatted folder name
321 * unformatted - unformatted, but with delimiter at end removed
322 * unformatted-dm - folder name as it appears in raw response
323 * unformatted-disp - unformatted without $folder_prefix
325 function sqimap_mailbox_parse ($line, $line_lsub) {
326 global $folder_prefix, $delimiter;
328 /* Process each folder line */
329 for ($g=0; $g < count($line); $g++
) {
331 /* Store the raw IMAP reply */
332 if (isset($line[$g])) {
333 $boxesall[$g]["raw"] = $line[$g];
336 $boxesall[$g]["raw"] = '';
340 /* Count number of delimiters ($delimiter) in folder name */
341 $mailbox = trim($line_lsub[$g]);
342 $dm_count = substr_count($mailbox, $delimiter);
343 if (substr($mailbox, -1) == $delimiter) {
344 /* If name ends in delimiter, decrement count by one */
348 /* Format folder name, but only if it's a INBOX.* or has a parent. */
349 $boxesallbyname[$mailbox] = $g;
350 $parentfolder = readMailboxParent($mailbox, $delimiter);
351 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
352 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
353 ( isset($boxesallbyname[$parentfolder]) &&
354 (strlen($parentfolder) > 0) ) ) {
355 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
357 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
360 $boxesall[$g]['formatted'] = '';
362 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
365 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
368 $boxesall[$g]['unformatted-dm'] = $mailbox;
369 if (substr($mailbox, -1) == $delimiter) {
370 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
372 $boxesall[$g]['unformatted'] = $mailbox;
373 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
374 $mailbox = substr($mailbox, strlen($folder_prefix));
376 $boxesall[$g]['unformatted-disp'] = $mailbox;
377 $boxesall[$g]['id'] = $g;
379 $boxesall[$g]['flags'] = array();
380 if (isset($line[$g])) {
381 ereg("\(([^)]*)\)",$line[$g],$regs);
382 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
384 $boxesall[$g]['flags'] = explode(' ', $flags);
393 * Sorting function used to sort mailbox names.
394 * + Original patch from dave_michmerhuizen@yahoo.com
395 * + Allows case insensitivity when sorting folders
396 * + Takes care of the delimiter being sorted to the end, causing
397 * subfolders to be listed in below folders that are prefixed
398 * with their parent folders name.
400 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
401 * Without special sort function: foobar between foo and foo.bar
402 * With special sort function: foobar AFTER foo and foo.bar :)
404 function user_strcasecmp($a, $b) {
407 return strnatcasecmp($a, $b);
409 /* Calculate the length of some strings. */
410 $a_length = strlen($a);
411 $b_length = strlen($b);
412 $min_length = min($a_length, $b_length);
413 $delimiter_length = strlen($delimiter);
415 /* Set the initial result value. */
417 /* Check the strings... */
418 for ($c = 0; $c < $min_length; ++
$c) {
419 $a_del = substr($a, $c, $delimiter_length);
420 $b_del = substr($b, $c, $delimiter_length);
422 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
424 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
426 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
429 $result = strcasecmp($a{$c}, $b{$c});
437 /* If one string is a prefix of the other... */
439 if ($a_length < $b_length) {
441 } else if ($a_length > $b_length) {
450 * Returns list of options (to be echoed into select statement
451 * based on available mailboxes and separators
452 * Caller should surround options with <SELECT..> </SELECT> and
454 * $imap_stream - $imapConnection to query for mailboxes
455 * $show_selected - array containing list of mailboxes to pre-select (0 if none)
456 * $folder_skip - array of folders to keep out of option list (compared in lower)
457 * $boxes - list of already fetched boxes (for places like folder panel, where
458 * you know these options will be shown 3 times in a row.. (most often unset).
460 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0 ) {
461 global $username, $data_dir;
464 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF
);
467 $boxes = sqimap_mailbox_list($imap_stream);
469 foreach ($boxes as $boxes_part) {
470 if (!in_array('noselect', $boxes_part['flags'])) {
471 $box = $boxes_part['unformatted'];
472 $lowerbox = strtolower($box);
474 if ( $folder_skip != 0 && in_array($lowerbox, $folder_skip) ) {
477 if ($lowerbox == 'inbox'){
479 } else if ( $shorten_box_names == 2 ) { /* delimited, style = 2 */
480 $box2 = str_replace(' ', '. ', $boxes_part['formatted']);
481 } else if ( $shorten_box_names == 1 ) { /* indent, style = 1 */
482 $box2 = $boxes_part['formatted'];
483 } else { /* default, long names, style = 0 */
484 $box2 = str_replace(' ', ' ', imap_utf7_decode_local($boxes_part['unformatted-disp']));
486 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
487 $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
489 $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
493 return $mbox_options;
497 * Returns sorted mailbox lists in several different ways.
498 * See comment on sqimap_mailbox_parse() for info about the returned array.
500 function sqimap_mailbox_list($imap_stream) {
501 global $default_folder_prefix;
503 if ( !isset( $boxesnew ) ) {
505 global $data_dir, $username, $list_special_folders_first,
506 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
507 $move_to_trash, $move_to_sent, $save_as_draft,
508 $delimiter, $noselect_fix_enable;
510 $inbox_in_list = false;
511 $inbox_subscribed = false;
513 require_once(SM_PATH
. 'include/load_prefs.php');
514 require_once(SM_PATH
. 'functions/array.php');
516 if ($noselect_fix_enable) {
517 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
520 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
523 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
524 true, $response, $message);
528 * Section about removing the last element was removed
529 * We don't return "* OK" anymore from sqimap_read_data
532 $sorted_lsub_ary = array();
533 for ($i=0;$i < count($lsub_ary); $i++
) {
535 * Workaround for EIMS
536 * Doesn't work if the mailbox name is multiple lines
538 if (isset($lsub_ary[$i +
1]) &&
539 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
540 $lsub_ary[$i], $regs)) {
542 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
544 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
545 $sorted_lsub_ary[] = $temp_mailbox_name;
546 if (strtoupper($temp_mailbox_name) == 'INBOX') {
547 $inbox_subscribed = true;
551 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
552 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
553 $new_ary[] = $sorted_lsub_ary[$i];
556 $sorted_lsub_ary = $new_ary;
557 if (isset($sorted_lsub_ary)) {
558 usort($sorted_lsub_ary, 'user_strcasecmp');
560 $sorted_list_ary = $sorted_lsub_ary;
562 // $sorted_list_ary = array();
563 // for ($i=0; $i < count($sorted_lsub_ary); $i++) {
565 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
566 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
569 $mbx = $sorted_lsub_ary[$i];
572 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
573 true, $response, $message);
574 /* Another workaround for EIMS */
575 if (isset($read[1]) &&
576 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
578 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
581 if (isset($sorted_list_ary[$i])) {
582 $sorted_list_ary[$i] = '';
585 if (isset($read[0])) {
586 $sorted_list_ary[$i] = $read[0];
589 $sorted_list_ary[$i] = '';
592 if (isset($sorted_list_ary[$i]) &&
593 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
594 $inbox_in_list = true;
597 // $inbox_in_list = true;
599 * Just in case they're not subscribed to their inbox,
600 * we'll get it for them anyway
602 if (!$inbox_subscribed) {// || !$inbox_in_list) {
603 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
604 true, $response, $message);
605 /* Another workaround for EIMS */
606 if (isset($inbox_ary[1]) &&
607 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
608 $inbox_ary[0], $regs)) {
609 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
613 $sorted_list_ary[] = $inbox_ary[0];
614 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
617 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
619 /* Now, lets sort for special folders */
620 $boxesnew = $used = array();
623 foreach ( $boxesall as $k => $box ) {
624 if ( strtolower($box['unformatted']) == 'inbox') {
631 /* List special folders and their subfolders, if requested. */
632 if ($list_special_folders_first) {
633 foreach ( $boxesall as $k => $box ) {
634 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
638 $spec_sub = str_replace(' ', '', $box['formatted']);
639 $spec_sub = preg_replace("/(\*|\[|\]|\(|\)|\?|\+|\{|\}|\^|\\$)/", '\\\\'.'\\1', $spec_sub);
641 /* In case of problems with preg
642 here is a ereg version
643 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
645 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
652 /* Rest of the folders */
653 foreach ( $boxesall as $k => $box ) {
663 * Returns a list of all folders, subscribed or not
665 function sqimap_mailbox_list_all($imap_stream) {
666 global $list_special_folders_first, $folder_prefix, $delimiter;
668 require_once(SM_PATH
. 'functions/array.php');
670 $ssid = sqimap_session_id();
671 $lsid = strlen( $ssid );
672 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
673 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
676 $fld_pre_length = strlen($folder_prefix);
678 for ($i = 0; $i < count($read_ary); $i++
) {
679 /* Another workaround for EIMS */
680 if (isset($read_ary[$i +
1]) &&
681 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
682 $read_ary[$i], $regs)) {
684 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
686 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
687 /* Store the raw IMAP reply */
688 $boxes[$g]['raw'] = $read_ary[$i];
690 /* Count number of delimiters ($delimiter) in folder name */
691 $mailbox = find_mailbox_name($read_ary[$i]);
692 $dm_count = substr_count($mailbox, $delimiter);
693 if (substr($mailbox, -1) == $delimiter) {
694 /* If name ends in delimiter - decrement count by one */
698 /* Format folder name, but only if it's a INBOX.* or has a parent. */
699 $boxesallbyname[$mailbox] = $g;
700 $parentfolder = readMailboxParent($mailbox, $delimiter);
701 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
702 (ereg('^'.$folder_prefix, $mailbox)) ||
703 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
705 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
708 $boxes[$g]['formatted'] = '';
710 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
713 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
716 $boxes[$g]['unformatted-dm'] = $mailbox;
717 if (substr($mailbox, -1) == $delimiter) {
718 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
720 $boxes[$g]['unformatted'] = $mailbox;
721 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
723 $boxes[$g]['id'] = $g;
725 /* Now lets get the flags for this mailbox */
726 $read_mlbx = $read_ary[$i];
728 // $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
729 // true, $response, $message);
731 /* Another workaround for EIMS */
732 // if (isset($read_mlbx[1]) &&
733 // ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
734 // $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
736 // echo $read_mlbx[0] .' raw 2 <br>';
738 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+
1);
739 $flags = substr($flags, 0, strpos($flags, ')'));
740 $flags = str_replace('\\', '', $flags);
741 $flags = trim(strtolower($flags));
743 $boxes[$g]['flags'] = explode(' ', $flags);
745 $boxes[$g]['flags'] = array();
750 if(is_array($boxes)) {
751 $boxes = ary_sort ($boxes, 'unformatted', 1);
757 function sqimap_mailbox_tree($imap_stream) {
758 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
759 if ( !isset( $boxesnew ) ) {
761 global $data_dir, $username, $list_special_folders_first,
762 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
765 $inbox_in_list = false;
766 $inbox_subscribed = false;
768 require_once(SM_PATH
. 'include/load_prefs.php');
769 require_once(SM_PATH
. 'functions/array.php');
772 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
773 true, $response, $message);
776 * Section about removing the last element was removed
777 * We don't return "* OK" anymore from sqimap_read_data
779 $sorted_lsub_ary = array();
780 $cnt = count($lsub_ary);
781 for ($i=0;$i < $cnt; $i++
) {
783 * Workaround for EIMS
784 * Doesn't work if the mailbox name is multiple lines
786 if (isset($lsub_ary[$i +
1]) &&
787 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
788 $lsub_ary[$i], $regs)) {
790 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
793 // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
795 // $mbx = trim($regs[3]);
796 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
798 $mbx = find_mailbox_name($lsub_ary[$i]);
799 $noselect = check_is_noselect($lsub_ary[$i]);
800 if (substr($mbx, -1) == $delimiter) {
801 $mbx = substr($mbx, 0, strlen($mbx) - 1);
803 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
805 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
807 foreach ($sorted_lsub_ary as $mbx) {
808 if ($mbx['mbx'] == 'INBOX') {
809 $inbox_in_list = true;
815 * Just in case they're not subscribed to their inbox,
816 * we'll get it for them anyway
818 if (!$inbox_in_list) {
819 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
820 true, $response, $message);
821 /* Another workaround for EIMS */
822 if (isset($inbox_ary[1]) &&
823 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
824 $inbox_ary[0], $regs)) {
825 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
828 $mbx = find_mailbox_name($inbox_ary[0]);
829 if (substr($mbx, -1) == $delimiter) {
830 $mbx = substr($mbx, 0, strlen($mbx) - 1);
832 if ( $mbx == 'INBOX') {
833 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
834 sqimap_subscribe($imap_stream, 'INBOX');
837 // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
839 // $mbx = trim($regs[3]);
840 // if (substr($mbx, -1) == $delimiter) {
841 // $mbx = substr($mbx, 0, strlen($mbx) - 1);
843 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
846 $cnt = count($sorted_lsub_ary);
847 for ($i=0 ; $i < $cnt; $i++
) {
848 $mbx = $sorted_lsub_ary[$i]['mbx'];
849 if (($unseen_notify == 2 && $mbx == 'INBOX')
850 ||
$unseen_notify == 3
851 ||
($move_to_trash && ($mbx == $trash_folder))) {
852 $sorted_lsub_ary[$i]['unseen'] =
853 $sorted_lsub_ary[$i]['noselect'] ?
854 0 : sqimap_unseen_messages($imap_stream, $mbx);
855 if ($unseen_type == 2 ||
($move_to_trash
856 && ($mbx == $trash_folder) )) {
857 $sorted_lsub_ary[$i]['nummessages'] =
858 $sorted_lsub_ary[$i]['noselect'] ?
859 0 : sqimap_get_num_messages($imap_stream, $mbx);
861 if ($mbx == $trash_folder) {
862 $sorted_lsub_ary[$i]['nummessages'] =
863 $sorted_lsub_ary[$i]['noselect'] ?
864 0 : sqimap_get_num_messages($imap_stream, $mbx);
868 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
874 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
875 global $data_dir, $username, $list_special_folders_first,
876 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
877 $move_to_trash, $move_to_sent, $save_as_draft,
880 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
882 /* create virtual root node */
883 $mailboxes= new mailboxes();
884 $mailboxes->is_root
= true;
886 if (isset($folder_prefix) && $folder_prefix != '') {
887 $start = substr_count($folder_prefix,$delimiter);
888 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
890 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
892 $mailboxes->mailboxname_full
= $folder_prefix;
895 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
897 $cnt = count($mbx_ary);
898 for ($i=0; $i < $cnt; $i++
) {
899 if ($mbx_ary[$i]['mbx'] !='' ) {
900 $mbx = new mailboxes();
901 $mailbox = $mbx_ary[$i]['mbx'];
904 $mbx->is_inbox
= true;
905 $mbx->is_special
= true;
908 $mbx->is_trash
= true;
909 $mbx->is_special
= true;
912 $mbx->is_sent
= true;
913 $mbx->is_special
= true;
916 $mbx->is_draft
= true;
917 $mbx->is_special
= true;
921 if (isset($mbx_ary[$i]['unseen'])) {
922 $mbx->unseen
= $mbx_ary[$i]['unseen'];
924 if (isset($mbx_ary[$i]['nummessages'])) {
925 $mbx->total
= $mbx_ary[$i]['nummessages'];
928 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
930 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
932 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
933 } else { /* mailbox is root folder */
934 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
936 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
937 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);