Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {config_load} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     config_load
Chris PeBenito 696b41
 * Purpose:  load config file vars
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.config.load.php {config_load}
Chris PeBenito 696b41
 *       (Smarty online manual)
Chris PeBenito 696b41
 * @param array Format:
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * array('file' => required config file name,
Chris PeBenito 696b41
 *       'section' => optional config file section to load
Chris PeBenito 696b41
 *       'scope' => local/parent/global
Chris PeBenito 696b41
 *       'global' => overrides scope, setting to parent if true)
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_config_load($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
        if ($smarty->debugging) {
Chris PeBenito 696b41
            $_params = array();
Chris PeBenito 696b41
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
Chris PeBenito 696b41
            $_debug_start_time = smarty_core_get_microtime($_params, $smarty);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $_file = isset($params['file']) ? $smarty->_dequote($params['file']) : null;
Chris PeBenito 696b41
        $_section = isset($params['section']) ? $smarty->_dequote($params['section']) : null;
Chris PeBenito 696b41
        $_scope = isset($params['scope']) ? $smarty->_dequote($params['scope']) : 'global';
Chris PeBenito 696b41
        $_global = isset($params['global']) ? $smarty->_dequote($params['global']) : false;
Chris PeBenito 696b41
Chris PeBenito 696b41
        if (!isset($_file) || strlen($_file) == 0) {
Chris PeBenito 696b41
            $smarty->trigger_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if (isset($_scope)) {
Chris PeBenito 696b41
            if ($_scope != 'local' &&
Chris PeBenito 696b41
                $_scope != 'parent' &&
Chris PeBenito 696b41
                $_scope != 'global') {
Chris PeBenito 696b41
                $smarty->trigger_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            if ($_global) {
Chris PeBenito 696b41
                $_scope = 'parent';
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                $_scope = 'local';
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $_params = array('resource_name' => $_file,
Chris PeBenito 696b41
                         'resource_base_path' => $smarty->config_dir,
Chris PeBenito 696b41
                         'get_source' => false);
Chris PeBenito 696b41
        $smarty->_parse_resource_name($_params);
Chris PeBenito 696b41
        $_file_path = $_params['resource_type'] . ':' . $_params['resource_name'];
Chris PeBenito 696b41
        if (isset($_section))
Chris PeBenito 696b41
            $_compile_file = $smarty->_get_compile_path($_file_path.'|'.$_section);
Chris PeBenito 696b41
        else
Chris PeBenito 696b41
            $_compile_file = $smarty->_get_compile_path($_file_path);
Chris PeBenito 696b41
Chris PeBenito 696b41
        if($smarty->force_compile || !file_exists($_compile_file)) {
Chris PeBenito 696b41
            $_compile = true;
Chris PeBenito 696b41
        } elseif ($smarty->compile_check) {
Chris PeBenito 696b41
            $_params = array('resource_name' => $_file,
Chris PeBenito 696b41
                             'resource_base_path' => $smarty->config_dir,
Chris PeBenito 696b41
                             'get_source' => false);
Chris PeBenito 696b41
            $_compile = $smarty->_fetch_resource_info($_params) &&
Chris PeBenito 696b41
                $_params['resource_timestamp'] > filemtime($_compile_file);
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            $_compile = false;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if($_compile) {
Chris PeBenito 696b41
            // compile config file
Chris PeBenito 696b41
            if(!is_object($smarty->_conf_obj)) {
Chris PeBenito 696b41
                require_once SMARTY_DIR . $smarty->config_class . '.class.php';
Chris PeBenito 696b41
                $smarty->_conf_obj = new $smarty->config_class();
Chris PeBenito 696b41
                $smarty->_conf_obj->overwrite = $smarty->config_overwrite;
Chris PeBenito 696b41
                $smarty->_conf_obj->booleanize = $smarty->config_booleanize;
Chris PeBenito 696b41
                $smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
Chris PeBenito 696b41
                $smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
Chris PeBenito 696b41
            $_params = array('resource_name' => $_file,
Chris PeBenito 696b41
                             'resource_base_path' => $smarty->config_dir,
Chris PeBenito 696b41
                             $_params['get_source'] = true);
Chris PeBenito 696b41
            if (!$smarty->_fetch_resource_info($_params)) {
Chris PeBenito 696b41
                return;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
Chris PeBenito 696b41
            $_config_vars = array_merge($smarty->_conf_obj->get($_file),
Chris PeBenito 696b41
                    $smarty->_conf_obj->get($_file, $_section));
Chris PeBenito 696b41
            if(function_exists('var_export')) {
Chris PeBenito 696b41
                $_output = '';
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                $_output = ''\\\'', '\\'=>'\\\\')) . '\'); ?>';
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output, 'resource_timestamp' => $_params['resource_timestamp']));
Chris PeBenito 696b41
            require_once(SMARTY_CORE_DIR . 'core.write_compiled_resource.php');
Chris PeBenito 696b41
            smarty_core_write_compiled_resource($_params, $smarty);
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            include($_compile_file);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if ($smarty->caching) {
Chris PeBenito 696b41
            $smarty->_cache_info['config'][$_file] = true;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
Chris PeBenito 696b41
        $smarty->_config[0]['files'][$_file] = true;
Chris PeBenito 696b41
Chris PeBenito 696b41
        if ($_scope == 'parent') {
Chris PeBenito 696b41
                $smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
Chris PeBenito 696b41
                $smarty->_config[1]['files'][$_file] = true;
Chris PeBenito 696b41
        } else if ($_scope == 'global') {
Chris PeBenito 696b41
            for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
Chris PeBenito 696b41
                $smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
Chris PeBenito 696b41
                $smarty->_config[$i]['files'][$_file] = true;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        if ($smarty->debugging) {
Chris PeBenito 696b41
            $_params = array();
Chris PeBenito 696b41
            require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
Chris PeBenito 696b41
            $smarty->_smarty_debug_info[] = array('type'      => 'config',
Chris PeBenito 696b41
                                                'filename'  => $_file.' ['.$_section.'] '.$_scope,
Chris PeBenito 696b41
                                                'depth'     => $smarty->_inclusion_depth,
Chris PeBenito 696b41
                                                'exec_time' => smarty_core_get_microtime($_params, $smarty) - $_debug_start_time);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>