Aleksander Machniak
2015-06-28 8447bae77c19a2350bd48b0f0c5b3a56a35c7af9
Require Mbstring and OpenSSL extensions (#1490415) - remove redundant code
10 files modified
1 files deleted
423 ■■■■■ changed files
CHANGELOG 1 ●●●● patch | view | raw | blame | history
INSTALL 5 ●●●●● patch | view | raw | blame | history
composer.json-dist 3 ●●●● patch | view | raw | blame | history
installer/check.php 6 ●●●●● patch | view | raw | blame | history
program/include/iniset.php 9 ●●●●● patch | view | raw | blame | history
program/lib/Roundcube/bootstrap.php 36 ●●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube.php 113 ●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_mime.php 4 ●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_utils.php 27 ●●●●● patch | view | raw | blame | history
program/lib/des.inc 218 ●●●●● patch | view | raw | blame | history
tests/Framework/Utils.php 1 ●●●● patch | view | raw | blame | history
CHANGELOG
@@ -1,6 +1,7 @@
CHANGELOG Roundcube Webmail
===========================
- Require Mbstring and OpenSSL extensions (#1490415)
- Get rid of Mail_mimeDecode package dependency (#1490416)
- Add --config and --type options to moduserprefs.sh script (#1490051)
- Implemented memcache_debug and apc_debug options
INSTALL
@@ -12,10 +12,9 @@
* The Apache, Lighttpd, Cherokee or Hiawatha web server
* .htaccess support allowing overrides for DirectoryIndex
* PHP Version 5.3.7 or greater including
   - PCRE, DOM, JSON, Session, Sockets (required)
   - PCRE, DOM, JSON, Session, Sockets, OpenSSL, Mbstring (required)
   - PHP Data Objects (PDO) with driver for either MySQL, PostgreSQL or SQLite (required)
   - Libiconv, Zip (recommended)
   - OpenSSL, Fileinfo, Mcrypt, mbstring (optional)
   - Libiconv, Zip, Fileinfo (recommended)
* PEAR packages distributed with Roundcube or external:
   - Mail_Mime 1.8.1 or newer
   - Net_SMTP (latest from https://github.com/pear/Net_SMTP/)
composer.json-dist
@@ -23,8 +23,7 @@
        "pear-pear.php.net/net_idna2": ">=0.1.1",
        "pear-pear.php.net/net_sieve": ">=1.3.2",
        "pear/mail_mime": ">=1.8.9",
        "pear/net_smtp": "dev-master",
        "patchwork/utf8": "1.2.x"
        "pear/net_smtp": "dev-master"
    },
    "require-dev": {
        "pear-pear.php.net/crypt_gpg": "*",
installer/check.php
@@ -15,14 +15,13 @@
    'XML'       => 'xml',
    'JSON'      => 'json',
    'PDO'       => 'PDO',
    'Multibyte' => 'mbstring',
    'OpenSSL'   => 'openssl',
);
$optional_php_exts = array(
    'FileInfo'  => 'fileinfo',
    'Libiconv'  => 'iconv',
    'Multibyte' => 'mbstring',
    'OpenSSL'   => 'openssl',
    'Mcrypt'    => 'mcrypt',
    'Intl'      => 'intl',
    'Exif'      => 'exif',
    'LDAP'      => 'ldap',
@@ -63,7 +62,6 @@
    'FileInfo'  => 'http://www.php.net/manual/en/book.fileinfo.php',
    'Libiconv'  => 'http://www.php.net/manual/en/book.iconv.php',
    'Multibyte' => 'http://www.php.net/manual/en/book.mbstring.php',
    'Mcrypt'    => 'http://www.php.net/manual/en/book.mcrypt.php',
    'OpenSSL'   => 'http://www.php.net/manual/en/book.openssl.php',
    'JSON'      => 'http://www.php.net/manual/en/book.json.php',
    'DOM'       => 'http://www.php.net/manual/en/book.dom.php',
program/include/iniset.php
@@ -68,15 +68,6 @@
// backward compatybility (to be removed)
require_once INSTALL_PATH . 'program/include/bc.php';
// load the UTF-8 portability layers from Patchwork
// don't load mbstring layer as it conflicts with Roundcube Framework (#1490280)
if (!function_exists('iconv')) {
    \Patchwork\Utf8\Bootup::initIconv();
}
if (!function_exists('utf8_encode')) {
    \Patchwork\Utf8\Bootup::initUtf8Encode();
}
/**
 * PHP5 autoloader routine for dynamic class loading
 */
program/lib/Roundcube/bootstrap.php
@@ -356,42 +356,6 @@
}
/**
 * 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
 */
program/lib/Roundcube/rcube.php
@@ -827,40 +827,12 @@
        // Add a single canary byte to the end of the clear text, which
        // will help find out how much of padding will need to be removed
        // upon decryption; see http://php.net/mcrypt_generic#68082.
        $clear = pack("a*H2", $clear, "80");
        $ckey  = $this->config->get_crypto_key($key);
        if (function_exists('openssl_encrypt')) {
            $method = 'DES-EDE3-CBC';
            $opts   = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true;
            $iv     = $this->create_iv(openssl_cipher_iv_length($method));
            $cipher = $iv . openssl_encrypt($clear, $method, $ckey, $opts, $iv);
        }
        else if (function_exists('mcrypt_module_open') &&
            ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))
        ) {
            $iv = $this->create_iv(mcrypt_enc_get_iv_size($td));
            mcrypt_generic_init($td, $ckey, $iv);
            $cipher = $iv . mcrypt_generic($td, $clear);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
        }
        else {
            @include_once 'des.inc';
            if (function_exists('des')) {
                $des_iv_size = 8;
                $iv = $this->create_iv($des_iv_size);
                $cipher = $iv . des($ckey, $clear, 1, 1, $iv);
            }
            else {
                self::raise_error(array(
                    'code' => 500, 'type' => 'php',
                    'file' => __FILE__, 'line' => __LINE__,
                    'message' => "Could not perform encryption; make sure OpenSSL or Mcrypt or lib/des.inc is available"
                    ), true, true);
            }
        }
        $clear  = pack("a*H2", $clear, "80");
        $ckey   = $this->config->get_crypto_key($key);
        $method = 'DES-EDE3-CBC';
        $opts   = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true;
        $iv     = rcube_utils::random_bytes(openssl_cipher_iv_length($method), true);
        $cipher = $iv . openssl_encrypt($clear, $method, $ckey, $opts, $iv);
        return $base64 ? base64_encode($cipher) : $cipher;
    }
@@ -883,79 +855,24 @@
        $cipher = $base64 ? base64_decode($cipher) : $cipher;
        $ckey   = $this->config->get_crypto_key($key);
        if (function_exists('openssl_decrypt')) {
            $method  = 'DES-EDE3-CBC';
            $opts    = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true;
            $iv_size = openssl_cipher_iv_length($method);
            $iv      = substr($cipher, 0, $iv_size);
        $method  = 'DES-EDE3-CBC';
        $opts    = defined('OPENSSL_RAW_DATA') ? OPENSSL_RAW_DATA : true;
        $iv_size = openssl_cipher_iv_length($method);
        $iv      = substr($cipher, 0, $iv_size);
            // session corruption? (#1485970)
            if (strlen($iv) < $iv_size) {
                return '';
            }
            $cipher = substr($cipher, $iv_size);
            $clear  = openssl_decrypt($cipher, $method, $ckey, $opts, $iv);
        // session corruption? (#1485970)
        if (strlen($iv) < $iv_size) {
            return '';
        }
        else if (function_exists('mcrypt_module_open') &&
            ($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, ""))
        ) {
            $iv_size = mcrypt_enc_get_iv_size($td);
            $iv      = substr($cipher, 0, $iv_size);
            // session corruption? (#1485970)
            if (strlen($iv) < $iv_size) {
                return '';
            }
            $cipher = substr($cipher, $iv_size);
            mcrypt_generic_init($td, $ckey, $iv);
            $clear = mdecrypt_generic($td, $cipher);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
        }
        else {
            @include_once 'des.inc';
            if (function_exists('des')) {
                $des_iv_size = 8;
                $iv     = substr($cipher, 0, $des_iv_size);
                $cipher = substr($cipher, $des_iv_size);
                $clear  = des($ckey, $cipher, 0, 1, $iv);
            }
            else {
                self::raise_error(array(
                    'code' => 500, 'type' => 'php',
                    'file' => __FILE__, 'line' => __LINE__,
                    'message' => "Could not perform decryption; make sure OpenSSL or Mcrypt or lib/des.inc is available"
                    ), true, true);
            }
        }
        $cipher = substr($cipher, $iv_size);
        $clear  = openssl_decrypt($cipher, $method, $ckey, $opts, $iv);
        // Trim PHP's padding and the canary byte; see note in
        // rcube::encrypt() and http://php.net/mcrypt_generic#68082
        $clear = substr(rtrim($clear, "\0"), 0, -1);
        return $clear;
    }
    /**
     * Generates encryption initialization vector (IV)
     *
     * @param int $size Vector size
     *
     * @return string Vector string
     */
    private function create_iv($size)
    {
        // mcrypt_create_iv() can be slow when system lacks entrophy
        // we'll generate IV vector manually
        $iv = '';
        for ($i = 0; $i < $size; $i++) {
            $iv .= chr(mt_rand(0, 255));
        }
        return $iv;
    }
    /**
program/lib/Roundcube/rcube_mime.php
@@ -567,7 +567,7 @@
        // Note: Never try to use iconv instead of mbstring functions here
        //       Iconv's substr/strlen are 100x slower (#1489113)
        if ($charset && $charset != RCUBE_CHARSET && function_exists('mb_internal_encoding')) {
        if ($charset && $charset != RCUBE_CHARSET) {
            mb_internal_encoding($charset);
        }
@@ -661,7 +661,7 @@
            }
        }
        if ($charset && $charset != RCUBE_CHARSET && function_exists('mb_internal_encoding')) {
        if ($charset && $charset != RCUBE_CHARSET) {
            mb_internal_encoding(RCUBE_CHARSET);
        }
program/lib/Roundcube/rcube_utils.php
@@ -1123,28 +1123,25 @@
    /**
     * Generate a ramdom string
     *
     * @param int String length
     * @param int  $length String length
     * @param bool $raw    Return RAW data instead of hex
     *
     * @return string The generated random string
     */
    public static function random_bytes($length)
    public static function random_bytes($length, $raw = false)
    {
        if (function_exists('openssl_random_pseudo_bytes')) {
            $random = openssl_random_pseudo_bytes(ceil($length / 2));
            $random = bin2hex($random);
        $rlen   = $raw ? $length : ceil($length / 2);
        $random = openssl_random_pseudo_bytes($rlen);
            // if the length wasn't even...
            if ($length < strlen($random)) {
                $random = substr($random, 0, $length);
            }
        if ($raw) {
            return $random;
        }
        else {
            $alpha  = 'ABCDEFGHIJKLMNOPQERSTUVXYZabcdefghijklmnopqrtsuvwxyz0123456789+*%&?!$-_=';
            $random = '';
            for ($i = 0; $i < $length; $i++) {
                $random .= $alpha[rand(0, strlen($alpha)-1)];
            }
        $random = bin2hex($random);
        // if the length wasn't even...
        if ($length < strlen($random)) {
            $random = substr($random, 0, $length);
        }
        return $random;
program/lib/des.inc
File was deleted
tests/Framework/Utils.php
@@ -426,6 +426,7 @@
    function test_random_bytes()
    {
        $this->assertSame(15, strlen(rcube_utils::random_bytes(15)));
        $this->assertSame(15, strlen(rcube_utils::random_bytes(15, true)));
        $this->assertSame(1, strlen(rcube_utils::random_bytes(1)));
        $this->assertSame(0, strlen(rcube_utils::random_bytes(0)));
        $this->assertSame(0, strlen(rcube_utils::random_bytes(-1)));