function.config_load.php 5.9 KB

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