It appears gettext doesn't work if the string is passed in single quotes, so this...
[squirrelmail.git] / functions / imap_mailbox.php
index 8d349a196ae01dbd6025a4f93e6c34f88b9196db..18af1782f7a42f8626a45d390e35f31862d99c7e 100755 (executable)
@@ -24,7 +24,7 @@ global $boxesnew;
 */
 
 class mailboxes {
-    var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, 
+    var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
         $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
         $is_trash = false, $is_draft = false,  $mbxs = array(), 
         $unseen = false, $total = false;
@@ -79,8 +79,27 @@ function sortSpecialMbx($a, $b) {
     } else {
         $bcmp = '2' . $b->mailboxname_full;
     }
-    if ($acmp == $bcmp) return 0;
-    return ($acmp > $bcmp) ? 1: -1;
+    return user_strcasecmp($acmp, $bcmp);      
+}
+
+function compact_mailboxes_response($ary)
+{
+    /*
+     * Workaround for mailboxes returned as literal
+     * FIXME : Doesn't work if the mailbox name is multiple lines 
+     * (larger then fgets buffer)
+     */
+    for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
+        if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
+            if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
+                 $ary[$i], $regs)) {
+                $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
+                array_splice($ary, $i+1, 2);
+            }
+        }
+    }
+    /* remove duplicates and ensure array is contiguous */
+    return array_values(array_unique($ary));
 }
 
 /*
@@ -107,30 +126,14 @@ function find_mailbox_name($line)
     return '';
 }
 
-function compact_mailboxes_response($ary)
-{
-    /*
-     * Workaround for mailboxes returned as literal
-     * FIXME : Doesn't work if the mailbox name is multiple lines 
-     * (larger then fgets buffer)
-     */
-    for ($i = 0; $i < count($ary); $i++) {
-        if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
-            if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
-                 $ary[$i], $regs)) {
-                $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
-                array_splice($ary, $i+1, 2);
-            }
-        }
-    }
-    /* remove duplicates and ensure array is contiguous */
-    return array_values(array_unique($ary));
-}
-
 function check_is_noselect ($lsub_line) {
     return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
 }
 
+function check_is_noinferiors ($lsub_line) {
+    return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
+}
+
 /**
  * If $haystack is a full mailbox name, and $needle is the mailbox
  * separator character, returns the second last part of the full
@@ -364,9 +367,9 @@ function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
 }
 
 /*
- * Formats a mailbox into parts for the $boxesall array
+ * Formats a mailbox into parts for the $boxesall array
  *
- * The four parts are:
+ * The parts are:
  *
  *     raw            - Raw LIST/LSUB response from the IMAP server
  *     formatted      - nicely formatted folder name
@@ -387,7 +390,7 @@ function sqimap_mailbox_parse ($line, $line_lsub) {
         }
 
         /* Count number of delimiters ($delimiter) in folder name */
-        $mailbox  = trim($line_lsub[$g]);
+        $mailbox  = /*trim(*/$line_lsub[$g]/*)*/;
         $dm_count = substr_count($mailbox, $delimiter);
         if (substr($mailbox, -1) == $delimiter) {
             /* If name ends in delimiter, decrement count by one */
@@ -509,14 +512,14 @@ function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_sk
                     $box2 = $boxes_part['formatted'];
                     break;
                   default:  /* default, long names, style = 0 */
-                    $box2 = str_replace(' ', '&nbsp;', imap_utf7_decode_local($boxes_part['unformatted-disp']));
+                    $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
                     break;
                 }
             }
             if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
-                $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
+                $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'" SELECTED>'.$box2.'</OPTION>' . "\n";
             } else {
-                $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
+                $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'">'.$box2.'</OPTION>' . "\n";
             }
         }
     }
@@ -632,6 +635,7 @@ function sqimap_mailbox_list($imap_stream) {
             }
         }
     }
+    
     return $boxesnew;
 }
 
@@ -716,11 +720,12 @@ function sqimap_mailbox_tree($imap_stream) {
         $inbox_in_list = false;
         $inbox_subscribed = false;
         $noselect = false;
+        $noinferiors = false;
 
         require_once(SM_PATH . 'include/load_prefs.php');
 
         /* LSUB array */
-        $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
+        $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
                                         true, $response, $message);
         $lsub_ary = compact_mailboxes_response($lsub_ary);
 
@@ -752,11 +757,12 @@ function sqimap_mailbox_tree($imap_stream) {
             // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
             if ($imap_server_type != "uw") {                
                 $noselect = check_is_noselect($lsub_ary[$i]);
+                $noinferiors = check_is_noinferiors($lsub_ary[$i]);
             }
             if (substr($mbx, -1) == $delimiter) {
                 $mbx = substr($mbx, 0, strlen($mbx) - 1);
             }
-            $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect); 
+            $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors); 
         }
         // FIX ME this requires a config setting inside conf.pl instead of checking on server type
         if ($imap_server_type == "uw") {
@@ -775,7 +781,8 @@ function sqimap_mailbox_tree($imap_stream) {
                if ($aServerResponse[$tag] == 'OK') {
                    $sResponse = implode('', $aResponse[$tag]);
                    $noselect = check_is_noselect($sResponse);
-                   $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
+                   $noinferiors = check_is_noinferiors($sResponse);
+                   $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
                }
            }
            $cnt = count($sorted_lsub_ary);
@@ -828,7 +835,7 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
                 the list, despite having "special folders at top" option set.
                 Need a better method than this.
             */
-
+/*
             if ($mailbox == 'INBOX') {
                 $mbx->is_special = true;
             } elseif (stristr($trash_folder , $mailbox)) {
@@ -843,7 +850,7 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
                 case 'INBOX':
                     $mbx->is_inbox = true;
                     $mbx->is_special = true;
-                   $mbx_ary[$i]['noselect'] = false;
+                    $mbx_ary[$i]['noselect'] = false;
                     break;
                 case $trash_folder:
                     $mbx->is_trash = true;
@@ -858,6 +865,13 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
                     $mbx->is_special = true;
                     break;
             }
+*/
+            $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
+            $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
+            $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
+            $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
+            if (!$mbx->is_special)
+                $mbx->is_special = do_hook_function('special_mailbox', $mailbox);
             
             if (isset($mbx_ary[$i]['unseen'])) {
                 $mbx->unseen = $mbx_ary[$i]['unseen'];
@@ -867,6 +881,7 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
             }
 
             $mbx->is_noselect = $mbx_ary[$i]['noselect'];
+            $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
 
             $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
             if ($r_del_pos) {