home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / function.config_load.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.3 KB  |  133 lines

  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.  * @param array Format:
  17.  * <pre>
  18.  * array('file' => required config file name,
  19.  *       'section' => optional config file section to load
  20.  *       'scope' => local/parent/global
  21.  *       'global' => overrides scope, setting to parent if true)
  22.  * </pre>
  23.  * @param Smarty
  24.  */
  25. function smarty_function_config_load($params, &$smarty)
  26. {
  27.         $_file = isset($params['file']) ? $params['file'] : null;
  28.         $_section = isset($params['section']) ? $params['section'] : null;
  29.         $_scope = isset($params['scope']) ? $params['scope'] : 'global';
  30.         $_global = isset($params['global']) ? $params['global'] : false;
  31.  
  32.         if (!isset($_file) || strlen($_file) == 0) {
  33.             $smarty->_syntax_error("missing 'file' attribute in config_load tag", E_USER_ERROR, __FILE__, __LINE__);
  34.         }
  35.  
  36.         if (isset($_scope)) {
  37.             if ($_scope != 'local' &&
  38.                 $_scope != 'parent' &&
  39.                 $_scope != 'global') {
  40.                 $smarty->_syntax_error("invalid 'scope' attribute value", E_USER_ERROR, __FILE__, __LINE__);
  41.             }
  42.         } else {
  43.             if ($_global) {
  44.                 $_scope = 'parent';
  45.             } else {
  46.                 $_scope = 'local';
  47.             }
  48.         }        
  49.             
  50.         if(@is_dir($smarty->config_dir)) {
  51.             $_config_dir = $smarty->config_dir;            
  52.         } else {
  53.             // config_dir not found, try include_path
  54.             $smarty->_get_include_path($smarty->config_dir, $_config_dir);
  55.         }
  56.  
  57.         $_file_path = str_replace('//', '/' ,$_config_dir . '/' . $_file);
  58.         
  59.         // get path to compiled object file
  60.         if(isset($_section)) {
  61.                $_compile_file = $smarty->_get_auto_filename($smarty->compile_dir, $_section . ' ' . $_file);
  62.         } else {
  63.                $_compile_file = $smarty->_get_auto_filename($smarty->compile_dir, $_file);
  64.         }
  65.  
  66.         // need to compile config file?
  67.         if($smarty->force_compile || !file_exists($_compile_file) ||
  68.             ($smarty->compile_check &&
  69.                 file_exists($_file_path) &&
  70.                 ( filemtime($_compile_file) != filemtime($_file_path) ))) {
  71.             $_compile_config = true;
  72.         } else {
  73.             include($_compile_file);
  74.             $_compile_config = empty($_config_vars);
  75.         }
  76.         
  77.         if($_compile_config) {
  78.             if(!is_object($smarty->_conf_obj)) {
  79.                 require_once SMARTY_DIR . $smarty->config_class . '.class.php';
  80.                 $smarty->_conf_obj = new $smarty->config_class($_config_dir);
  81.                 $smarty->_conf_obj->overwrite = $smarty->config_overwrite;
  82.                 $smarty->_conf_obj->booleanize = $smarty->config_booleanize;
  83.                 $smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
  84.                 $smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
  85.                 $smarty->_conf_obj->set_path = $_config_dir;
  86.             }
  87.             if($_config_vars = array_merge($smarty->_conf_obj->get($_file),
  88.                     $smarty->_conf_obj->get($_file, $_section))) {
  89.                 if(function_exists('var_export')) {
  90.                     $_compile_data = '<?php $_config_vars = ' . var_export($_config_vars, true) . '; return true; ?>';                    
  91.                 } else {
  92.                     $_compile_data = '<?php $_config_vars = unserialize(\'' . str_replace('\'','\\\'', serialize($_config_vars)) . '\'); return true; ?>';
  93.                 }
  94.                 $smarty->_write_file($_compile_file, $_compile_data, true);
  95.                 touch($_compile_file,filemtime($_file_path));
  96.             }
  97.         }
  98.  
  99.         if ($smarty->debugging) {
  100.             $debug_start_time = $smarty->_get_microtime();
  101.         }
  102.  
  103.         if ($smarty->caching) {
  104.             $smarty->_cache_info['config'][] = $_file;
  105.         }
  106.  
  107.         $smarty->_config[0]['vars'] = @array_merge($smarty->_config[0]['vars'], $_config_vars);
  108.         $smarty->_config[0]['files'][$_file] = true;
  109.         
  110.         if ($_scope == 'parent') {
  111.                 $smarty->_config[1]['vars'] = @array_merge($smarty->_config[1]['vars'], $_config_vars);
  112.                 $smarty->_config[1]['files'][$_file] = true;
  113.         } else if ($_scope == 'global') {
  114.             for ($i = 1, $for_max = count($smarty->_config); $i < $for_max; $i++) {
  115.                     $smarty->_config[$i]['vars'] = @array_merge($smarty->_config[$i]['vars'], $_config_vars);
  116.                     $smarty->_config[$i]['files'][$_file] = true;
  117.             }
  118.         }
  119.  
  120.         if ($smarty->debugging) {
  121.             $debug_start_time = $smarty->_get_microtime();
  122.             $smarty->_smarty_debug_info[] = array('type'      => 'config',
  123.                                                 'filename'  => $_file.' ['.$_section.'] '.$_scope,
  124.                                                 'depth'     => $smarty->_inclusion_depth,
  125.                                                 'exec_time' => $smarty->_get_microtime() - $debug_start_time);
  126.         }
  127.     
  128. }
  129.  
  130. /* vim: set expandtab: */
  131.  
  132. ?>
  133.