smarty/plugins/function.config_load.php
changeset 0 c63de9eb7045
equal deleted inserted replaced
-1:000000000000 0:c63de9eb7045
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  * @package Smarty
       
     5  * @subpackage plugins
       
     6  */
       
     7 
       
     8 /**
       
     9  * Smarty {config_load} function plugin
       
    10  *
       
    11  * Type:     function<br>
       
    12  * Name:     config_load<br>
       
    13  * Purpose:  load config file vars
       
    14  * @link http://smarty.php.net/manual/en/language.function.config.load.php {config_load}
       
    15  *       (Smarty online manual)
       
    16  * @author Monte Ohrt <monte at ohrt dot com>
       
    17  * @author messju mohr <messju at lammfellpuschen dot de> (added use of resources)
       
    18  * @param array Format:
       
    19  * <pre>
       
    20  * array('file' => required config file name,
       
    21  *       'section' => optional config file section to load
       
    22  *       'scope' => local/parent/global
       
    23  *       'global' => overrides scope, setting to parent if true)
       
    24  * </pre>
       
    25  * @param Smarty
       
    26  */
       
    27 function smarty_function_config_load($params, &$smarty)
       
    28 {
       
    29         if ($smarty->debugging) {
       
    30             $_params = array();
       
    31             require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
       
    32             $_debug_start_time = smarty_core_get_microtime($_params, $smarty);
       
    33         }
       
    34 
       
    35         $_file = isset($params['file']) ? $smarty->_dequote($params['file']) : null;
       
    36         $_section = isset($params['section']) ? $smarty->_dequote($params['section']) : null;
       
    37         $_scope = isset($params['scope']) ? $smarty->_dequote($params['scope']) : 'global';
       
    38         $_global = isset($params['global']) ? $smarty->_dequote($params['global']) : false;
       
    39 
       
    40         if (!isset($_file) || strlen($_file) == 0) {
       
    41             $smarty->trigger_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
       
    42         }
       
    43 
       
    44         if (isset($_scope)) {
       
    45             if ($_scope != 'local' &&
       
    46                 $_scope != 'parent' &&
       
    47                 $_scope != 'global') {
       
    48                 $smarty->trigger_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
       
    49             }
       
    50         } else {
       
    51             if ($_global) {
       
    52                 $_scope = 'parent';
       
    53             } else {
       
    54                 $_scope = 'local';
       
    55             }
       
    56         }
       
    57 
       
    58         $_params = array('resource_name' => $_file,
       
    59                          'resource_base_path' => $smarty->config_dir,
       
    60                          'get_source' => false);
       
    61         $smarty->_parse_resource_name($_params);
       
    62         $_file_path = $_params['resource_type'] . ':' . $_params['resource_name'];
       
    63         if (isset($_section))
       
    64             $_compile_file = $smarty->_get_compile_path($_file_path.'|'.$_section);
       
    65         else
       
    66             $_compile_file = $smarty->_get_compile_path($_file_path);
       
    67 
       
    68         if($smarty->force_compile || !file_exists($_compile_file)) {
       
    69             $_compile = true;
       
    70         } elseif ($smarty->compile_check) {
       
    71             $_params = array('resource_name' => $_file,
       
    72                              'resource_base_path' => $smarty->config_dir,
       
    73                              'get_source' => false);
       
    74             $_compile = $smarty->_fetch_resource_info($_params) &&
       
    75                 $_params['resource_timestamp'] > filemtime($_compile_file);
       
    76         } else {
       
    77             $_compile = false;
       
    78         }
       
    79 
       
    80         if($_compile) {
       
    81             // compile config file
       
    82             if(!is_object($smarty->_conf_obj)) {
       
    83                 require_once SMARTY_DIR . $smarty->config_class . '.class.php';
       
    84                 $smarty->_conf_obj = new $smarty->config_class();
       
    85                 $smarty->_conf_obj->overwrite = $smarty->config_overwrite;
       
    86                 $smarty->_conf_obj->booleanize = $smarty->config_booleanize;
       
    87                 $smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
       
    88                 $smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
       
    89             }
       
    90 
       
    91             $_params = array('resource_name' => $_file,
       
    92                              'resource_base_path' => $smarty->config_dir,
       
    93                              $_params['get_source'] = true);
       
    94             if (!$smarty->_fetch_resource_info($_params)) {
       
    95                 return;
       
    96             }
       
    97             $smarty->_conf_obj->set_file_contents($_file, $_params['source_content']);
       
    98             $_config_vars = array_merge($smarty->_conf_obj->get($_file),
       
    99                     $smarty->_conf_obj->get($_file, $_section));
       
   100             if(function_exists('var_export')) {
       
   101                 $_output = '<?php $_config_vars = ' . var_export($_config_vars, true) . '; ?>';
       
   102             } else {
       
   103                 $_output = '<?php $_config_vars = unserialize(\'' . strtr(serialize($_config_vars),array('\''=>'\\\'', '\\'=>'\\\\')) . '\'); ?>';
       
   104             }
       
   105             $_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output, 'resource_timestamp' => $_params['resource_timestamp']));
       
   106             require_once(SMARTY_CORE_DIR . 'core.write_compiled_resource.php');
       
   107             smarty_core_write_compiled_resource($_params, $smarty);
       
   108         } else {
       
   109             include($_compile_file);
       
   110         }
       
   111 
       
   112         if ($smarty->caching) {
       
   113             $smarty->_cache_info['config'][$_file] = true;
       
   114         }
       
   115 
       
   116         $smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
       
   117         $smarty->_config[0]['files'][$_file] = true;
       
   118 
       
   119         if ($_scope == 'parent') {
       
   120                 $smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
       
   121                 $smarty->_config[1]['files'][$_file] = true;
       
   122         } else if ($_scope == 'global') {
       
   123             for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
       
   124                 $smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
       
   125                 $smarty->_config[$i]['files'][$_file] = true;
       
   126             }
       
   127         }
       
   128 
       
   129         if ($smarty->debugging) {
       
   130             $_params = array();
       
   131             require_once(SMARTY_CORE_DIR . 'core.get_microtime.php');
       
   132             $smarty->_smarty_debug_info[] = array('type'      => 'config',
       
   133                                                 'filename'  => $_file.' ['.$_section.'] '.$_scope,
       
   134                                                 'depth'     => $smarty->_inclusion_depth,
       
   135                                                 'exec_time' => smarty_core_get_microtime($_params, $smarty) - $_debug_start_time);
       
   136         }
       
   137 
       
   138 }
       
   139 
       
   140 /* vim: set expandtab: */
       
   141 
       
   142 ?>