From d08333ea578e3b6c6ab42bed05f808a2b7b93cf1 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Thu, 05 May 2011 08:46:54 -0400
Subject: [PATCH] - Fix problems with subfolders of INBOX folder on some IMAP servers (#1487725)                                                             - Fix handling of folders that doesn't belong to any namespace (#1487637)

---
 program/include/main.inc |   48 ++++++++++++++++++++++++++++++++++--------------
 1 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/program/include/main.inc b/program/include/main.inc
index c13d91f..baaca65 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -26,7 +26,7 @@
  * @author Thomas Bruederli <roundcube@gmail.com>
  */
 
-require_once 'lib/utf7.inc';
+require_once 'utf7.inc';
 require_once INSTALL_PATH . 'program/include/rcube_shared.inc';
 
 // define constannts for input reading
@@ -1228,17 +1228,20 @@
   else
     $list = $RCMAIL->imap->list_mailboxes();
 
-  foreach ($list as $folder)
+  $delimiter = $RCMAIL->imap->get_hierarchy_delimiter();
+
+  foreach ($list as $folder) {
     if (empty($p['exceptions']) || !in_array($folder, $p['exceptions']))
-      rcmail_build_folder_tree($a_mailboxes, $folder, $RCMAIL->imap->get_hierarchy_delimiter());
+      rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter);
+  }
 
   $select = new html_select($p);
-  
+
   if ($p['noselection'])
     $select->add($p['noselection'], '');
-    
+
   rcmail_render_folder_tree_select($a_mailboxes, $mbox, $p['maxlength'], $select, $p['realnames']);
-  
+
   return $select;
 }
 
@@ -1251,6 +1254,17 @@
 function rcmail_build_folder_tree(&$arrFolders, $folder, $delm='/', $path='')
 {
   global $RCMAIL;
+
+  // Handle namespace prefix
+  $prefix = '';
+  if (!$path) {
+    $n_folder = $folder;
+    $folder = $RCMAIL->imap->mod_mailbox($folder);
+
+    if ($n_folder != $folder) {
+      $prefix = substr($n_folder, 0, -strlen($folder));
+    }
+  }
 
   $pos = strpos($folder, $delm);
 
@@ -1272,14 +1286,14 @@
     $virtual = false;
   }
 
-  $path .= $currentFolder;
-
-  // Check \Noselect option (if options are in cache)
-  if (!$virtual && ($opts = $RCMAIL->imap->mailbox_options($path))) {
-    $virtual = in_array('\\Noselect', $opts);
-  }
+  $path .= $prefix.$currentFolder;
 
   if (!isset($arrFolders[$currentFolder])) {
+    // Check \Noselect option (if options are in cache)
+    if (!$virtual && ($opts = $RCMAIL->imap->mailbox_options($path))) {
+      $virtual = in_array('\\Noselect', $opts);
+    }
+
     $arrFolders[$currentFolder] = array(
       'id' => $path,
       'name' => rcube_charset_convert($currentFolder, 'UTF7-IMAP'),
@@ -1673,14 +1687,20 @@
 {
   // %n - host
   $n = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']);
-  // %d - domain name without first part, e.g. %d=mail.domain.tld, %m=domain.tld
+  // %d - domain name without first part, e.g. %n=mail.domain.tld, %d=domain.tld
   $d = preg_replace('/^[^\.]+\./', '', $n);
   // %h - IMAP host
   $h = $_SESSION['imap_host'] ? $_SESSION['imap_host'] : $host;
   // %z - IMAP domain without first part, e.g. %h=imap.domain.tld, %z=domain.tld
   $z = preg_replace('/^[^\.]+\./', '', $h);
+  // %s - domain name after the '@' from e-mail address provided at login screen. Returns FALSE if an invalid email is provided
+  if ( strpos($name, '%s') !== false ){
+    $user_email = rcube_idn_convert(get_input_value('_user', RCUBE_INPUT_POST), true);
+    if ( preg_match('/(.*)@([a-z0-9\.\-\[\]\:]+)/i', $user_email, $s) < 1 || filter_var($s[1]."@".$s[2], FILTER_VALIDATE_EMAIL) === false )
+      return false;
+  }
 
-  $name = str_replace(array('%n', '%d', '%h', '%z'), array($n, $d, $h, $z), $name);
+  $name = str_replace(array('%n', '%d', '%h', '%z', '%s'), array($n, $d, $h, $z, $s[2]), $name);
   return $name;
 }
 

--
Gitblit v1.9.1