1a177cc5e4db211bbdd77807c0da76b645985812
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];
39 $no_select_mbx = new mailboxes();
40 if (isset($mbx_parent->mailboxname_full
) && $mbx_parent->mailboxname_full
!= '') {
41 $no_select_mbx->mailboxname_full
= $mbx_parent->mailboxname_full
.$delimiter.$ary[$i];
43 $no_select_mbx->mailboxname_full
= $ary[$i];
45 $no_select_mbx->mailboxname_sub
= $ary[$i];
46 $no_select_mbx->is_noselect
= true;
47 $mbx_parent->mbxs
[] = $no_select_mbx;
51 $mbx_parent->mbxs
[] = $mbx;
52 if ($mbx->is_special
&& $specialfirst) {
53 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
58 function sortSpecialMbx($a, $b) {
60 $acmp = '0'. $a->mailboxname_full
;
61 } else if ($a->is_special
) {
62 $acmp = '1'. $a->mailboxname_full
;
64 $acmp = '2' . $a->mailboxname_full
;
67 $bcmp = '0'. $b->mailboxname_full
;
68 }else if ($b->is_special
) {
69 $bcmp = '1' . $b->mailboxname_full
;
71 $bcmp = '2' . $b->mailboxname_full
;
73 if ($acmp == $bcmp) return 0;
74 return ($acmp > $bcmp) ?
1: -1;
77 function find_mailbox_name ($mailbox) {
78 if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
80 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
82 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
86 function check_is_noselect ($lsub_line) {
87 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\Noselect[^\)]*\)/i", $lsub_line);
91 * If $haystack is a full mailbox name, and $needle is the mailbox
92 * separator character, returns the second last part of the full
93 * mailbox name (i.e. the mailbox's parent mailbox)
95 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 * Check if $subbox is below the specified $parentbox
112 function isBoxBelow( $subbox, $parentbox ) {
115 * Eliminate the obvious mismatch, where the
116 * subfolder path is shorter than that of the potential parent
118 if ( strlen($subbox) < strlen($parentbox) ) {
121 /* check for delimiter */
122 if (!substr($parentbox,-1) == $delimiter) {
123 $parentbox.=$delimiter;
125 if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
132 /* Defines special mailboxes */
133 function isSpecialMailbox( $box ) {
134 global $trash_folder, $sent_folder, $draft_folder,
135 $move_to_trash, $move_to_sent, $save_as_draft;
137 $ret = ( (strtolower($box) == 'inbox') ||
138 isTrashMailbox($box) ||
isSentMailbox($box) ||
isDraftMailbox($box) );
141 $ret = do_hook_function( 'special_mailbox', $box );
146 function isTrashMailbox ($box) {
147 global $trash_folder, $move_to_trash;
148 return $move_to_trash && $trash_folder &&
149 ( $box == $trash_folder ||
isBoxBelow($box, $trash_folder) );
152 function isSentMailbox($box) {
153 global $sent_folder, $move_to_sent;
154 return $move_to_sent && $sent_folder &&
155 ( $box == $sent_folder ||
isBoxBelow($box, $sent_folder) );
158 function isDraftMailbox($box) {
159 global $draft_folder, $save_as_draft;
160 return $save_as_draft &&
161 ( $box == $draft_folder ||
isBoxBelow($box, $draft_folder) );
164 /* Expunges a mailbox */
165 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
169 $id = sqimap_message_list_squisher($id);
176 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
177 $response, $message, $uid);
180 if (is_array($read)) {
181 foreach ($read as $r) {
182 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
190 /* Checks whether or not the specified mailbox exists */
191 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
192 if (!isset($mailbox) ||
empty($mailbox)) {
195 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
196 true, $response, $message);
197 return isset($mbx[0]);
200 /* Selects a mailbox */
201 function sqimap_mailbox_select ($imap_stream, $mailbox) {
202 global $auto_expunge;
204 if ($mailbox == 'None') {
208 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
209 true, $response, $message);
211 for ($i = 0, $cnt = count($read); $i < $cnt; $i++
) {
212 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
213 $result[strtoupper($regs[1])] = $regs[2];
214 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
215 $result[strtoupper($regs[2])] = $regs[1];
217 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
218 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
219 $result['PERMANENTFLAGS'] = $regs[1];
220 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
221 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
222 $result['FLAGS'] = $regs[1];
226 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
227 $result['RIGHTS']=$regs[1];
231 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
236 /* Creates a folder */
237 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
239 if (strtolower($type) == 'noselect') {
240 $mailbox .= $delimiter;
243 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
244 true, $response, $message);
245 sqimap_subscribe ($imap_stream, $mailbox);
248 /* Subscribes to an existing folder */
249 function sqimap_subscribe ($imap_stream, $mailbox) {
250 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
251 true, $response, $message);
254 /* Unsubscribes to an existing folder */
255 function sqimap_unsubscribe ($imap_stream, $mailbox) {
256 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
257 true, $response, $message);
260 /* Deletes the given folder */
261 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
262 global $data_dir, $username;
263 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
264 true, $response, $message);
265 sqimap_unsubscribe ($imap_stream, $mailbox);
266 do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
267 removePref($data_dir, $username, "thread_$mailbox");
270 /* Determines if the user is subscribed to the folder or not */
271 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
272 $boxesall = sqimap_mailbox_list ($imap_stream);
273 foreach ($boxesall as $ref) {
274 if ($ref['unformatted'] == $folder) {
281 /* Renames a mailbox */
282 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
283 if ( $old_name != $new_name ) {
284 global $delimiter, $imap_server_type, $data_dir, $username;
285 if ( substr( $old_name, -1 ) == $delimiter ) {
286 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
287 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
288 $postfix = $delimiter;
293 $boxesall = sqimap_mailbox_list($imap_stream);
294 $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
295 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
296 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
297 $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
298 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
299 sqimap_subscribe($imap_stream, $new_name.$postfix);
300 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
301 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
302 $l = strlen( $old_name ) +
1;
305 foreach ($boxesall as $box) {
306 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
307 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
308 if ($imap_server_type == 'cyrus') {
309 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
310 $data = sqimap_run_command($imap_stream, $cmd, true,
311 $response, $message);
313 sqimap_unsubscribe($imap_stream, $box[$p]);
314 $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
315 removePref($data_dir, $username, 'thread_'.$box[$p]);
316 sqimap_subscribe($imap_stream, $new_sub);
317 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
318 do_hook_function('rename_or_delete_folder',
319 $args = array($box[$p], 'rename', $new_sub));
326 * Formats a mailbox into 4 parts for the $boxesall array
328 * The four parts are:
330 * raw - Raw LIST/LSUB response from the IMAP server
331 * formatted - nicely formatted folder name
332 * unformatted - unformatted, but with delimiter at end removed
333 * unformatted-dm - folder name as it appears in raw response
334 * unformatted-disp - unformatted without $folder_prefix
336 function sqimap_mailbox_parse ($line, $line_lsub) {
337 global $folder_prefix, $delimiter;
339 /* Process each folder line */
340 for ($g = 0, $cnt = count($line); $g < $cnt; ++
$g) {
341 /* Store the raw IMAP reply */
342 if (isset($line[$g])) {
343 $boxesall[$g]['raw'] = $line[$g];
345 $boxesall[$g]['raw'] = '';
348 /* Count number of delimiters ($delimiter) in folder name */
349 $mailbox = trim($line_lsub[$g]);
350 $dm_count = substr_count($mailbox, $delimiter);
351 if (substr($mailbox, -1) == $delimiter) {
352 /* If name ends in delimiter, decrement count by one */
356 /* Format folder name, but only if it's a INBOX.* or has a parent. */
357 $boxesallbyname[$mailbox] = $g;
358 $parentfolder = readMailboxParent($mailbox, $delimiter);
359 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
360 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
361 (isset($boxesallbyname[$parentfolder]) &&
362 (strlen($parentfolder) > 0) ) ) {
363 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
365 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
367 $boxesall[$g]['formatted'] = '';
369 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
371 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
374 $boxesall[$g]['unformatted-dm'] = $mailbox;
375 if (substr($mailbox, -1) == $delimiter) {
376 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
378 $boxesall[$g]['unformatted'] = $mailbox;
379 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
380 $mailbox = substr($mailbox, strlen($folder_prefix));
382 $boxesall[$g]['unformatted-disp'] = $mailbox;
383 $boxesall[$g]['id'] = $g;
385 $boxesall[$g]['flags'] = array();
386 if (isset($line[$g])) {
387 ereg("\(([^)]*)\)",$line[$g],$regs);
388 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
390 $boxesall[$g]['flags'] = explode(' ', $flags);
398 * Sorting function used to sort mailbox names.
399 * + Original patch from dave_michmerhuizen@yahoo.com
400 * + Allows case insensitivity when sorting folders
401 * + Takes care of the delimiter being sorted to the end, causing
402 * subfolders to be listed in below folders that are prefixed
403 * with their parent folders name.
405 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
406 * Without special sort function: foobar between foo and foo.bar
407 * With special sort function: foobar AFTER foo and foo.bar :)
409 function user_strcasecmp($a, $b) {
410 return strnatcasecmp($a, $b);
414 * Returns list of options (to be echoed into select statement
415 * based on available mailboxes and separators
416 * Caller should surround options with <SELECT..> </SELECT> and
418 * $imap_stream - $imapConnection to query for mailboxes
419 * $show_selected - array containing list of mailboxes to pre-select (0 if none)
420 * $folder_skip - array of folders to keep out of option list (compared in lower)
421 * $boxes - list of already fetched boxes (for places like folder panel, where
422 * you know these options will be shown 3 times in a row.. (most often unset).
423 * $flag - flag to check for in mailbox flags, used to filter out mailboxes.
424 * 'noselect' by default to remove unselectable mailboxes.
425 * 'noinferiors' used to filter out folders that can not contain subfolders.
426 * NULL to avoid flag check entirely.
427 * $use_long_format - override folder display preference and always show full folder name.
429 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
430 $flag = 'noselect', $use_long_format = false ) {
431 global $username, $data_dir;
434 if ( $use_long_format ) {
435 $shorten_box_names = 0;
437 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF
);
441 $boxes = sqimap_mailbox_list($imap_stream);
444 foreach ($boxes as $boxes_part) {
445 if ($flag == NULL ||
!in_array($flag, $boxes_part['flags'])) {
446 $box = $boxes_part['unformatted'];
447 $lowerbox = strtolower($box);
449 if ($folder_skip != 0 && in_array($lowerbox, $folder_skip) ) {
452 if ($lowerbox == 'inbox'){
454 } else if ( $shorten_box_names == 2 ) { /* delimited, style = 2 */
455 $box2 = str_replace(' ', '. ', $boxes_part['formatted']);
456 } else if ( $shorten_box_names == 1 ) { /* indent, style = 1 */
457 $box2 = $boxes_part['formatted'];
458 } else { /* default, long names, style = 0 */
459 $box2 = str_replace(' ', ' ', imap_utf7_decode_local($boxes_part['unformatted-disp']));
461 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
462 $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
464 $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
468 return $mbox_options;
472 * Returns sorted mailbox lists in several different ways.
473 * See comment on sqimap_mailbox_parse() for info about the returned array.
475 function sqimap_mailbox_list($imap_stream) {
476 global $default_folder_prefix;
478 if (!isset($boxesnew)) {
479 global $data_dir, $username, $list_special_folders_first,
480 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
481 $move_to_trash, $move_to_sent, $save_as_draft,
482 $delimiter, $noselect_fix_enable;
484 $inbox_in_list = false;
485 $inbox_subscribed = false;
487 require_once(SM_PATH
. 'include/load_prefs.php');
489 if ($noselect_fix_enable) {
490 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
492 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
495 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
496 true, $response, $message);
498 $sorted_lsub_ary = array();
499 for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++
) {
501 * Workaround for mailboxes returned as literal
502 * Doesn't work if the mailbox name is multiple lines
503 * (larger then fgets buffer)
505 if (isset($lsub_ary[$i +
1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
506 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
507 $lsub_ary[$i], $regs)) {
509 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
512 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
513 $sorted_lsub_ary[] = $temp_mailbox_name;
514 if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
515 $inbox_subscribed = true;
518 /* remove duplicates */
519 $sorted_lsub_ary = array_unique($sorted_lsub_ary);
521 /* natural sort mailboxes */
522 if (isset($sorted_lsub_ary)) {
523 usort($sorted_lsub_ary, 'user_strcasecmp');
526 * The LSUB response doesn't provide us information about \Noselect
527 * mail boxes. The LIST response does, that's why we need to do a LIST
528 * call to retrieve the flags for the mailbox
529 * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
530 * in other words, we cannot rely on it.
532 $sorted_list_ary = array();
533 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
534 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
535 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
538 $mbx = $sorted_lsub_ary[$i];
541 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
542 true, $response, $message);
544 /* Another workaround for literals */
546 if (isset($read[1]) && substr($read[1],-3) == "}\r\n") {
547 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
549 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
553 if (isset($read[0])) {
554 $sorted_list_ary[$i] = $read[0];
556 $sorted_list_ary[$i] = '';
561 * Just in case they're not subscribed to their inbox,
562 * we'll get it for them anyway
564 if (!$inbox_subscribed) {
565 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
566 true, $response, $message);
567 /* Another workaround for literals */
568 if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
569 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
570 $inbox_ary[0], $regs)) {
571 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
575 $sorted_list_ary[] = $inbox_ary[0];
576 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
579 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
581 /* Now, lets sort for special folders */
582 $boxesnew = $used = array();
585 $cnt = count($boxesall);
586 $used = array_pad($used,$cnt,false);
587 for($k = 0; $k < $cnt; ++
$k) {
588 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
589 $boxesnew[] = $boxesall[$k];
594 /* List special folders and their subfolders, if requested. */
595 if ($list_special_folders_first) {
596 for($k = 0; $k < $cnt; ++
$k) {
597 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
598 $boxesnew[] = $boxesall[$k];
604 /* Rest of the folders */
605 for($k = 0; $k < $cnt; $k++
) {
607 $boxesnew[] = $boxesall[$k];
615 * Returns a list of all folders, subscribed or not
617 function sqimap_mailbox_list_all($imap_stream) {
618 global $list_special_folders_first, $folder_prefix, $delimiter;
619 $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
622 $fld_pre_length = strlen($folder_prefix);
623 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++
) {
624 /* Another workaround for EIMS */
625 if (isset($read_ary[$i +
1]) &&
626 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
627 $read_ary[$i], $regs)) {
629 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
631 /* Store the raw IMAP reply */
632 $boxes[$g]['raw'] = $read_ary[$i];
634 /* Count number of delimiters ($delimiter) in folder name */
635 $mailbox = find_mailbox_name($read_ary[$i]);
636 $dm_count = substr_count($mailbox, $delimiter);
637 if (substr($mailbox, -1) == $delimiter) {
638 /* If name ends in delimiter - decrement count by one */
642 /* Format folder name, but only if it's a INBOX.* or has a parent. */
643 $boxesallbyname[$mailbox] = $g;
644 $parentfolder = readMailboxParent($mailbox, $delimiter);
645 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
646 (ereg('^'.$folder_prefix, $mailbox)) ||
647 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
649 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
651 $boxes[$g]['formatted'] = '';
653 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
655 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
658 $boxes[$g]['unformatted-dm'] = $mailbox;
659 if (substr($mailbox, -1) == $delimiter) {
660 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
662 $boxes[$g]['unformatted'] = $mailbox;
663 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
665 $boxes[$g]['id'] = $g;
667 /* Now lets get the flags for this mailbox */
668 $read_mlbx = $read_ary[$i];
669 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+
1);
670 $flags = substr($flags, 0, strpos($flags, ')'));
671 $flags = str_replace('\\', '', $flags);
672 $flags = trim(strtolower($flags));
674 $boxes[$g]['flags'] = explode(' ', $flags);
676 $boxes[$g]['flags'] = array();
680 if(is_array($boxes)) {
687 function sqimap_mailbox_tree($imap_stream) {
688 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
689 if (!isset($boxesnew)) {
691 global $data_dir, $username, $list_special_folders_first,
692 $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
696 $inbox_in_list = false;
697 $inbox_subscribed = false;
699 require_once(SM_PATH
. 'include/load_prefs.php');
702 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
703 true, $response, $message);
705 /* Check to see if we have an INBOX */
708 for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++
) {
709 if (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ary[$i])) {
715 if ($has_inbox == false) {
716 $lsub_ibx = sqimap_run_command( $imap_stream, "LSUB \"\" \"INBOX\"", true, $response, $message );
717 if (isset($lsub_ibx[0]) && (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ibx[0]))) {
718 $lsub_ary[] = $lsub_ibx[0];
720 $lsub_ibx = sqimap_run_command( $imap_stream, "LIST \"\" \"INBOX\"", true, $response, $message );
721 if (preg_match("/^\*\s+LIST\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ibx[0])) {
722 sqimap_run_command( $imap_stream, "SUBSCRIBE \"INBOX\"", true, $response, $message );
723 $lsub_ibx[0] = str_replace("LIST","LSUB",$lsub_ibx[0]);
724 $lsub_ary[] = $lsub_ibx[0];
730 * Section about removing the last element was removed
731 * We don't return "* OK" anymore from sqimap_read_data
733 $sorted_lsub_ary = array();
734 $cnt = count($lsub_ary);
736 for ($i = 0; $i < $cnt; $i++
) {
738 * Workaround for EIMS
739 * Doesn't work if the mailbox name is multiple lines
741 if (isset($lsub_ary[$i +
1]) &&
742 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
743 $lsub_ary[$i], $regs)) {
745 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
748 $mbx = find_mailbox_name($lsub_ary[$i]);
750 // Force a list for UW as it returns \NoSelect in LIST and not LSUB //
751 if ($imap_server_type == "uw") {
752 $tmp_str = sqimap_run_command( $imap_stream , "LIST \"\" \"$mbx\"" , true, $response, $message );
753 if (isset($tmp_str[0])) {
754 $lsub_ary[$i] = $tmp_str[0];
757 $noselect = check_is_noselect($lsub_ary[$i]);
758 if (substr($mbx, -1) == $delimiter) {
759 $mbx = substr($mbx, 0, strlen($mbx) - 1);
761 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
763 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
765 for ($i = 0 ; $i < $cnt; $i++
) {
766 $mbx = $sorted_lsub_ary[$i]['mbx'];
767 if (($unseen_notify == 2 && $mbx == 'INBOX') ||
768 ($unseen_notify == 3) ||
769 ($move_to_trash && ($mbx == $trash_folder))) {
770 if( $sorted_lsub_ary[$i]['noselect'] ) {
771 $sorted_lsub_ary[$i]['unseen'] = 0;
773 $sorted_lsub_ary[$i]['unseen'] =
774 sqimap_unseen_messages($imap_stream, $mbx);
776 if (($unseen_type == 2) ||
777 ($move_to_trash && ($mbx == $trash_folder)) ||
778 ($mbx == $trash_folder)) {
779 if($sorted_lsub_ary[$i]['noselect']) {
780 $sorted_lsub_ary[$i]['nummessages'] = 0;
782 $sorted_lsub_ary[$i]['nummessages'] =
783 sqimap_get_num_messages($imap_stream, $mbx);
788 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
794 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
795 global $data_dir, $username, $list_special_folders_first,
796 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
797 $move_to_trash, $move_to_sent, $save_as_draft,
798 $delimiter, $imap_server_type;
800 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
802 /* create virtual root node */
803 $mailboxes= new mailboxes();
804 $mailboxes->is_root
= true;
809 if (isset($folder_prefix) && ($folder_prefix != '')) {
810 $start = substr_count($folder_prefix,$delimiter);
811 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
813 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
815 $mailboxes->mailboxname_full
= $folder_prefix;
818 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
823 $cnt = count($mbx_ary);
824 for ($i=0; $i < $cnt; $i++
) {
825 if ($mbx_ary[$i]['mbx'] !='' ) {
826 $mbx = new mailboxes();
827 $mailbox = $mbx_ary[$i]['mbx'];
830 $mbx->is_inbox
= true;
831 $mbx->is_special
= true;
834 $mbx->is_trash
= true;
835 $mbx->is_special
= true;
838 $mbx->is_sent
= true;
839 $mbx->is_special
= true;
842 $mbx->is_draft
= true;
843 $mbx->is_special
= true;
847 if (isset($mbx_ary[$i]['unseen'])) {
848 $mbx->unseen
= $mbx_ary[$i]['unseen'];
850 if (isset($mbx_ary[$i]['nummessages'])) {
851 $mbx->total
= $mbx_ary[$i]['nummessages'];
854 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
856 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
858 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
859 } else { /* mailbox is root folder */
860 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
862 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
864 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
867 sqimap_utf7_decode_mbx_tree($mailboxes);
871 function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
872 $mbx_tree->mailboxname_sub
=imap_utf7_decode_local($mbx_tree->mailboxname_sub
);
873 if ($mbx_tree->mbxs
) {
874 $iCnt = count($mbx_tree->mbxs
);
875 for ($i=0;$i<$iCnt;++
$i) {
876 $mbxs_tree->mbxs
[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs
[$i]);