Thomas B.
2014-12-29 7c3d8c0a3774ccabf9b2e63f71d0faaf6db704d7
program/lib/Roundcube/rcube_config.php
@@ -39,7 +39,6 @@
     */
    private $legacy_props = array(
        // new name => old name
        'default_folders'      => 'default_imap_folders',
        'mail_pagesize'        => 'pagesize',
        'addressbook_pagesize' => 'pagesize',
        'reply_mode'           => 'top_posting',
@@ -63,7 +62,7 @@
            $this->paths = explode(PATH_SEPARATOR, $paths);
            // make all paths absolute
            foreach ($this->paths as $i => $path) {
                if (!$this->_is_absolute($path)) {
                if (!rcube_utils::is_absolute_path($path)) {
                    if ($realpath = realpath(RCUBE_INSTALL_PATH . $path)) {
                        $this->paths[$i] = unslashify($realpath) . '/';
                    }
@@ -91,6 +90,103 @@
        // but contain information that is used in various
        // locations in the code:
        $this->set('contactlist_fields', array('name', 'firstname', 'surname', 'email'));
    }
    /**
     * @brief Guess the type the string may fit into.
     *
     * Look inside the string to determine what type might be best as a container.
     *
     * @param $value The value to inspect
     *
     * @return The guess at the type.
     */
    private function guess_type($value) {
        $_ = 'string';
        // array requires hint to be passed.
        if (preg_match('/^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/', $value) !== False) {
            $_ = 'double';
        } else if (preg_match('/^\d+$/', $value) !== False) {
            $_ = 'integer';
        } else if (preg_match('/(t(rue)?)|(f(alse)?)/i', $value) !== False) {
            $_ = 'boolean';
        }
        return $_;
    }
    /**
     * @brief Parse environment variable into PHP type.
     *
     * Perform an appropriate parsing of the string to create the desired PHP type.
     *
     * @param $string String to parse into PHP type
     * @param $type Type of value to return
     *
     * @return Appropriately typed interpretation of $string.
     */
    private function parse_env($string, $type) {
        console("parse $string into $type");
        $_ = $string;
        switch ($type) {
        case 'boolean':
            $_ = (boolean) $_;
            break;
        case 'integer':
            $_ = (integer) $_;
            break;
        case 'double':
            $_ = (double) $_;
            break;
        case 'string':
            break;
        case 'array':
            $_ = json_decode($_, true);
            break;
        case 'object':
            $_ = json_decode($_, false);
            break;
        case 'resource':
        case 'NULL':
        default:
            $_ = $this->parse_env($_, $this->guess_type($_));
        }
        return $_;
    }
    /**
     * @brief Get environment variable value.
     *
     * Retrieve an environment variable's value or if it's not found, return the
     * provided default value.
     *
     * @param $varname Environment variable name
     * @param $default_value Default value to return if necessary
     * @param $type Type of value to return
     *
     * @return Value of the environment variable or default if not found.
     */
    private function getenv_default($varname, $default_value, $type = null) {
        $_ = getenv($varname);
        if ($_ === False) {
            $_ = $default_value;
        } else {
            if (is_null($type)) {
                $type = gettype($default_value);
            }
            $_ = $this->parse_env($_, $type);
        }
        return $_;
    }
@@ -142,10 +238,6 @@
        // fix default imap folders encoding
        foreach (array('drafts_mbox', 'junk_mbox', 'sent_mbox', 'trash_mbox') as $folder)
            $this->prop[$folder] = rcube_charset::convert($this->prop[$folder], RCUBE_CHARSET, 'UTF7-IMAP');
        if (!empty($this->prop['default_folders']))
            foreach ($this->prop['default_folders'] as $n => $folder)
                $this->prop['default_folders'][$n] = rcube_charset::convert($folder, RCUBE_CHARSET, 'UTF7-IMAP');
        // set PHP error logging according to config
        if ($this->prop['debug_level'] & 1) {
@@ -243,8 +335,8 @@
     */
    public function resolve_paths($file, $use_env = true)
    {
        $files = array();
        $abs_path = $this->_is_absolute($file);
        $files    = array();
        $abs_path = rcube_utils::is_absolute_path($file);
        foreach ($this->paths as $basepath) {
            $realpath = $abs_path ? $file : realpath($basepath . '/' . $file);
@@ -267,14 +359,6 @@
        }
        return $files;
    }
    /**
     * Determine whether the given file path is absolute or relative
     */
    private function _is_absolute($path)
    {
        return $path[0] == DIRECTORY_SEPARATOR || preg_match('!^[a-z]:[\\\\/]!i', $path);
    }
    /**
@@ -306,6 +390,8 @@
            if ($result && is_string($result))
                $result = explode(',', $result);
        }
        $result = $this->getenv_default('ROUNDCUBE_' . strtoupper($name), $result)
        $plugin = $rcube->plugins->exec_hook('config_get', array(
            'name' => $name, 'default' => $def, 'result' => $result));
@@ -373,9 +459,14 @@
     */
    public function all()
    {
        $props = $this->prop;
   foreach ($props as $prop_name => $prop_value)
            $props[$prop_name] = $this->getenv_default('ROUNDCUBE_' . strtoupper($prop_name), $prop_value);
        $rcube  = rcube::get_instance();
        $plugin = $rcube->plugins->exec_hook('config_get', array(
            'name' => '*', 'result' => $this->prop));
            'name' => '*', 'result' => $props));
        return $plugin['result'];
    }