Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.config_load.php 6.0 KiB

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