- $boxes[$g]['unformatted'] = $mailbox;
- $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
- $boxes[$g]['id'] = $g;
-
- if (isset($line[$g]))
- ereg("\(([^)]*)\)",$line[$g],$regs);
- $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
- if ($flags) {
- $boxes[$g]['flags'] = explode(' ', $flags);
- }
- else
- $boxes[$g]['flags'] = array();
- }
-
- return $boxes;
- }
-
- /* Apparently you must call a user function with usort instead
- * of calling a built-in directly. Stupid.
- * Patch from dave_michmerhuizen@yahoo.com
- * Allows case insensitivity when sorting folders
- */
- function user_strcasecmp($a, $b)
- {
- return strcasecmp($a, $b);
- }
-
-
- /******************************************************************************
- ** Returns sorted mailbox lists in several different ways.
- ** See comment on sqimap_mailbox_parse() for info about the returned array.
- ******************************************************************************/
- function sqimap_mailbox_list ($imap_stream) {
- global $load_prefs_php, $prefs_php, $config_php;
- global $data_dir, $username, $list_special_folders_first;
- global $trash_folder, $sent_folder;
- global $move_to_trash, $move_to_sent;
-
- $inbox_in_list = false;
- $inbox_subscribed = false;
-
- if (!isset($load_prefs_php)) include "../src/load_prefs.php";
- else global $folder_prefix;
- if (!function_exists ("ary_sort")) include "../functions/array.php";
-
- $dm = sqimap_get_delimiter ($imap_stream);
-
- /** LSUB array **/
- $inbox_subscribed = false;
- fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
- $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
-
- /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
- /** LUKE: This introduced errors.. do a check first **/
- if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
- unset($lsub_ary[count($lsub_ary)-1]);
- }
-
- for ($i=0;$i < count($lsub_ary); $i++) {
- $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
- if ($sorted_lsub_ary[$i] == "INBOX")
+ }
+ $boxes[$g]['unformatted'] = $mailbox;
+ if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
+ $mailbox = substr($mailbox, strlen($folder_prefix));
+ }
+ $boxes[$g]['unformatted-disp'] = $mailbox;
+ $boxes[$g]['id'] = $g;
+
+ $boxes[$g]['flags'] = array();
+ if (isset($line[$g])) {
+ ereg("\(([^)]*)\)",$line[$g],$regs);
+ $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
+ if ($flags) {
+ $boxes[$g]['flags'] = explode(' ', $flags);
+ }
+ }
+ }
+
+ return $boxes;
+}
+
+/**
+ * Sorting function used to sort mailbox names.
+ * + Original patch from dave_michmerhuizen@yahoo.com
+ * + Allows case insensitivity when sorting folders
+ * + Takes care of the delimiter being sorted to the end, causing
+ * subfolders to be listed in below folders that are prefixed
+ * with their parent folders name.
+ * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
+ * Without special sort function: foobar between foo and foo.bar
+ * With special sort function: foobar AFTER foo and foo.bar :)
+ */
+function user_strcasecmp($a, $b) {
+ global $delimiter;
+
+ /* Calculate the length of some strings. */
+ $a_length = strlen($a);
+ $b_length = strlen($b);
+ $min_length = min($a_length, $b_length);
+ $delimiter_length = strlen($delimiter);
+
+ /* Set the initial result value. */
+ $result = 0;
+
+ /* Check the strings... */
+ for ($c = 0; $c < $min_length; ++$c) {
+ $a_del = substr($a, $c, $delimiter_length);
+ $b_del = substr($b, $c, $delimiter_length);
+
+ if (($a_del == $delimiter) && ($b_del == $delimiter)) {
+ $result = 0;
+ } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
+ $result = 1;
+ } else if (($a_del != $delimiter) && ($b_del != $delimiter)) {
+ $result = -1;
+ } else {
+ $result = strcasecmp($a{$c}, $b{$c});
+ }
+
+ if ($result != 0) {
+ break;
+ }
+ }
+
+ /* If one string is a prefix of the other... */
+ if ($result == 0) {
+ if ($a_length < $b_length) {
+ $result = -1;
+ } else if ($a_length > $b_length) {
+ $result = 1;
+ }
+ }
+
+ return ($result);
+}
+
+
+/******************************************************************************
+ ** Returns sorted mailbox lists in several different ways.
+ ** See comment on sqimap_mailbox_parse() for info about the returned array.
+ ******************************************************************************/
+function sqimap_mailbox_list ($imap_stream) {
+ global $data_dir, $username, $list_special_folders_first;
+ global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
+ global $move_to_trash, $move_to_sent, $save_as_draft;
+ global $delimiter;
+
+ $inbox_in_list = false;
+ $inbox_subscribed = false;
+
+ require_once('../src/load_prefs.php');
+ require_once('../functions/array.php');
+
+ /** LSUB array **/
+ fputs ($imap_stream, sqimap_session_id() .
+ " LSUB \"$folder_prefix\" \"*\"\r\n");
+ $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
+ true, $response, $message);
+
+ /* Section about removing the last element was removed */
+ /* We don't return "* OK" anymore from sqimap_read_data */
+
+ $sorted_lsub_ary = array();
+ for ($i=0;$i < count($lsub_ary); $i++) {
+ /* Workaround for EIMS */
+ /* Doesn't work if the mailbox name is multiple lines */
+ if (isset($lsub_ary[$i + 1]) &&
+ 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 (strtoupper($temp_mailbox_name) == 'INBOX') {