From 9d78c68cbf5ce86f02e8fa16814293cdfd204432 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 29 Jul 2015 14:38:21 -0400
Subject: [PATCH] Fix so imap folder attribute comparisons are case-insensitive (#1490466) + make in_array_nocase() much faster for ASCII strings

---
 program/lib/Roundcube/bootstrap.php |  136 ++++++++++++++++++---------------------------
 1 files changed, 55 insertions(+), 81 deletions(-)

diff --git a/program/lib/Roundcube/bootstrap.php b/program/lib/Roundcube/bootstrap.php
index 182ea12..2e5d9e6 100644
--- a/program/lib/Roundcube/bootstrap.php
+++ b/program/lib/Roundcube/bootstrap.php
@@ -1,9 +1,9 @@
 <?php
 
-/*
+/**
  +-----------------------------------------------------------------------+
  | This file is part of the Roundcube PHP suite                          |
- | Copyright (C) 2005-2013, The Roundcube Dev Team                       |
+ | Copyright (C) 2005-2015, The Roundcube Dev Team                       |
  |                                                                       |
  | Licensed under the GNU General Public License version 3 or            |
  | any later version with exceptions for skins & plugins.                |
@@ -17,7 +17,6 @@
  +-----------------------------------------------------------------------+
 */
 
-
 /**
  * Roundcube Framework Initialization
  *
@@ -26,25 +25,25 @@
  */
 
 $config = array(
-    'error_reporting'         => E_ALL &~ (E_NOTICE | E_STRICT),
+    'error_reporting'         => E_ALL & ~E_NOTICE & ~E_STRICT,
     // Some users are not using Installer, so we'll check some
     // critical PHP settings here. Only these, which doesn't provide
     // an error/warning in the logs later. See (#1486307).
     'mbstring.func_overload'  => 0,
-    'magic_quotes_runtime'    => 0,
-    'magic_quotes_sybase'     => 0, // #1488506
+    'magic_quotes_runtime'    => false,
+    'magic_quotes_sybase'     => false, // #1488506
 );
 
 // check these additional ini settings if not called via CLI
 if (php_sapi_name() != 'cli') {
     $config += array(
-        'suhosin.session.encrypt' => 0,
-        'file_uploads'            => 1,
+        'suhosin.session.encrypt' => false,
+        'file_uploads'            => true,
     );
 }
 
 foreach ($config as $optname => $optval) {
-    $ini_optval = filter_var(ini_get($optname), FILTER_VALIDATE_BOOLEAN);
+    $ini_optval = filter_var(ini_get($optname), is_bool($optval) ? FILTER_VALIDATE_BOOLEAN : FILTER_VALIDATE_INT);
     if ($optval != $ini_optval && @ini_set($optname, $optval) === false) {
         $error = "ERROR: Wrong '$optname' option value and it wasn't possible to set it to required value ($optval).\n"
             . "Check your PHP configuration (including php_admin_flag).";
@@ -54,11 +53,11 @@
 }
 
 // framework constants
-define('RCUBE_VERSION', '1.0-git');
+define('RCUBE_VERSION', '1.2-git');
 define('RCUBE_CHARSET', 'UTF-8');
 
 if (!defined('RCUBE_LIB_DIR')) {
-    define('RCUBE_LIB_DIR', dirname(__FILE__).'/');
+    define('RCUBE_LIB_DIR', __DIR__ . '/');
 }
 
 if (!defined('RCUBE_INSTALL_PATH')) {
@@ -78,39 +77,61 @@
 }
 
 // set internal encoding for mbstring extension
-if (extension_loaded('mbstring')) {
+if (function_exists('mb_internal_encoding')) {
     mb_internal_encoding(RCUBE_CHARSET);
-    @mb_regex_encoding(RCUBE_CHARSET);
+}
+if (function_exists('mb_regex_encoding')) {
+    mb_regex_encoding(RCUBE_CHARSET);
+}
+
+// make sure the Roundcube lib directory is in the include_path
+$rcube_path = realpath(RCUBE_LIB_DIR . '..');
+$sep        = PATH_SEPARATOR;
+$regexp     = "!(^|$sep)" . preg_quote($rcube_path, '!') . "($sep|\$)!";
+$path       = ini_get('include_path');
+
+if (!preg_match($regexp, $path)) {
+    set_include_path($path . PATH_SEPARATOR . $rcube_path);
 }
 
 // Register autoloader
 spl_autoload_register('rcube_autoload');
 
 // set PEAR error handling (will also load the PEAR main class)
-PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'rcube_pear_error');
-
+if (class_exists('PEAR')) {
+    @PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'rcube_pear_error');
+}
 
 
 /**
- * Similar function as in_array() but case-insensitive
+ * Similar function as in_array() but case-insensitive with multibyte support.
  *
- * @param string $needle    Needle value
- * @param array  $heystack  Array to search in
+ * @param string $needle   Needle value
+ * @param array  $heystack Array to search in
  *
  * @return boolean True if found, False if not
  */
 function in_array_nocase($needle, $haystack)
 {
-    $needle = mb_strtolower($needle);
-    foreach ((array)$haystack as $value) {
-        if ($needle === mb_strtolower($value)) {
-            return true;
+    // use much faster method for ascii
+    if (is_ascii($needle)) {
+        foreach ((array) $haystack as $value) {
+            if (strcasecmp($value, $needle) === 0) {
+                return true;
+            }
+        }
+    }
+    else {
+        $needle = mb_strtolower($needle);
+        foreach ((array) $haystack as $value) {
+            if ($needle === mb_strtolower($value)) {
+                return true;
+            }
         }
     }
 
     return false;
 }
-
 
 /**
  * Parse a human readable string for a number of bytes.
@@ -146,7 +167,6 @@
     return floatval($bytes);
 }
 
-
 /**
  * Make sure the string ends with a slash
  */
@@ -155,7 +175,6 @@
   return unslashify($str).'/';
 }
 
-
 /**
  * Remove slashes at the end of the string
  */
@@ -163,7 +182,6 @@
 {
   return preg_replace('/\/+$/', '', $str);
 }
-
 
 /**
  * Returns number of seconds for a specified offset string.
@@ -197,7 +215,6 @@
     return $amount;
 }
 
-
 /**
  * Create a unix timestamp with a specified offset from now.
  *
@@ -210,7 +227,6 @@
 {
     return time() + get_offset_sec($offset_str) * $factor;
 }
-
 
 /**
  * Truncate string if it is longer than the allowed length.
@@ -242,7 +258,6 @@
     return $str;
 }
 
-
 /**
  * Get all keys from array (recursive).
  *
@@ -266,7 +281,6 @@
     return $keys;
 }
 
-
 /**
  * Remove all non-ascii and non-word chars except ., -, _
  */
@@ -275,7 +289,6 @@
     $allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : '');
     return preg_replace("/[^$allowed]/i", $replace_with, $str);
 }
-
 
 /**
  * Check if a string contains only ascii characters
@@ -290,7 +303,6 @@
     $regexp = $control_chars ? '/[^\x00-\x7F]/' : '/[^\x20-\x7E]/';
     return preg_match($regexp, $str) ? false : true;
 }
-
 
 /**
  * Compose a valid representation of name and e-mail address
@@ -316,7 +328,6 @@
     return $email;
 }
 
-
 /**
  * Format e-mail address
  *
@@ -339,7 +350,6 @@
     return $email;
 }
 
-
 /**
  * Fix version number so it can be used correctly in version_compare()
  *
@@ -355,50 +365,13 @@
         $version);
 }
 
-
-/**
- * mbstring replacement functions
- */
-if (!extension_loaded('mbstring'))
-{
-    function mb_strlen($str)
-    {
-        return strlen($str);
-    }
-
-    function mb_strtolower($str)
-    {
-        return strtolower($str);
-    }
-
-    function mb_strtoupper($str)
-    {
-        return strtoupper($str);
-    }
-
-    function mb_substr($str, $start, $len=null)
-    {
-        return substr($str, $start, $len);
-    }
-
-    function mb_strpos($haystack, $needle, $offset=0)
-    {
-        return strpos($haystack, $needle, $offset);
-    }
-
-    function mb_strrpos($haystack, $needle, $offset=0)
-    {
-        return strrpos($haystack, $needle, $offset);
-    }
-}
-
 /**
  * intl replacement functions
  */
 
 if (!function_exists('idn_to_utf8'))
 {
-    function idn_to_utf8($domain, $flags=null)
+    function idn_to_utf8($domain)
     {
         static $idn, $loaded;
 
@@ -420,7 +393,7 @@
 
 if (!function_exists('idn_to_ascii'))
 {
-    function idn_to_ascii($domain, $flags=null)
+    function idn_to_ascii($domain)
     {
         static $idn, $loaded;
 
@@ -454,16 +427,14 @@
             '/Net_(.+)/',
             '/Auth_(.+)/',
             '/^html_.+/',
-            '/^rcube(.*)/',
-            '/^utf8$/',
+            '/^rcube(.*)/'
         ),
         array(
             'Mail/\\1',
             'Net/\\1',
             'Auth/\\1',
             'Roundcube/html',
-            'Roundcube/rcube\\1',
-            'utf8.class',
+            'Roundcube/rcube\\1'
         ),
         $classname
     );
@@ -482,8 +453,11 @@
  */
 function rcube_pear_error($err)
 {
-    error_log(sprintf("%s (%s): %s",
-        $err->getMessage(),
-        $err->getCode(),
-        $err->getUserinfo()), 0);
+    $msg = sprintf("ERROR: %s (%s)", $err->getMessage(), $err->getCode());
+
+    if ($info = $err->getUserinfo()) {
+        $msg .= ': ' . $info;
+    }
+
+    error_log($msg, 0);
 }

--
Gitblit v1.9.1