- $sorted_lsub_ary = array();
- for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
- /*
- * Workaround for mailboxes returned as literal
- * Doesn't work if the mailbox name is multiple lines
- * (larger then fgets buffer)
- */
- if (isset($lsub_ary[$i + 1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
- if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
- $lsub_ary[$i], $regs)) {
- $i++;
- $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
- }
- }
- $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
- $sorted_lsub_ary[] = $temp_mailbox_name;
- if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
- $inbox_subscribed = true;
- }
- }
- /* remove duplicates */
- $sorted_lsub_ary = array_unique($sorted_lsub_ary);
+/**
+ * Gets the list of mailboxes for sqimap_maolbox_tree and sqimap_mailbox_list
+ *
+ * This is because both of those functions had duplicated logic, but with slightly different
+ * implementations. This will make both use the same implementation, which should make it
+ * easier to maintain and easier to modify in the future
+ * @param stream $imap_stream imap connection resource
+ * @param bool $force force a reload and ignore cache
+ * @param bool $show_only_subscribed controls listing of visible or all folders
+ * @param bool $session_register controls registration of retrieved data in session.
+ * @return object boxesnew - array of mailboxes and their attributes
+ * @since 1.5.1
+ */
+function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=true,$session_register=true) {
+ global $show_only_subscribed_folders,$noselect_fix_enable,$folder_prefix,
+ $list_special_folders_first,$imap_server_type;
+ $inbox_subscribed = false;
+ $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
+
+ if ($show_only_subscribed) { $show_only_subscribed=$show_only_subscribed_folders; }
+
+ //require_once(SM_PATH . 'include/load_prefs.php');
+
+ /**
+ * There are three main listing commands we can use in IMAP:
+ * LSUB shows just the list of subscribed folders
+ * may include flags, but these are not necessarily accurate or authoratative
+ * \NoSelect has special meaning: the folder does not exist -OR- it means this
+ * folder is not subscribed but children may be
+ * [RFC-2060]
+ * LIST this shows every mailbox on the system
+ * flags are always included and are accurate and authoratative
+ * \NoSelect means folder should not be selected
+ * [RFC-2060]
+ * LIST (SUBSCRIBED) implemented with LIST-SUBSCRIBED extension
+ * this is like list but returns only subscribed folders
+ * flag meanings are like LIST, not LSUB
+ * \NonExistent means mailbox doesn't exist
+ * \PlaceHolder means parent is not valid (selectable), but one or more children are
+ * \NoSelect indeed means that the folder should not be selected
+ * IMAPEXT-LIST-EXTENSIONS-04 August 2003 B. Leiba
+ */
+ if (!$show_only_subscribed) {
+ $lsub = 'LIST';
+ $sub_cache_name='list_cache';
+ } elseif ($listsubscribed) {
+ $lsub = 'LIST (SUBSCRIBED)';
+ $sub_cache_name='listsub_cache';
+ } else {
+ $lsub = 'LSUB';
+ $sub_cache_name='lsub_cache';
+ }