smarty_internal_config.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Config
  4. *
  5. * @package Smarty
  6. * @subpackage Config
  7. * @author Uwe Tews
  8. */
  9. /**
  10. * Smarty Internal Plugin Config
  11. *
  12. * Main class for config variables
  13. *
  14. * @package Smarty
  15. * @subpackage Config
  16. *
  17. * @property Smarty_Config_Source $source
  18. * @property Smarty_Config_Compiled $compiled
  19. * @ignore
  20. */
  21. class Smarty_Internal_Config {
  22. /**
  23. * Samrty instance
  24. *
  25. * @var Smarty object
  26. */
  27. public $smarty = null;
  28. /**
  29. * Object of config var storage
  30. *
  31. * @var object
  32. */
  33. public $data = null;
  34. /**
  35. * Config resource
  36. * @var string
  37. */
  38. public $config_resource = null;
  39. /**
  40. * Compiled config file
  41. *
  42. * @var string
  43. */
  44. public $compiled_config = null;
  45. /**
  46. * filepath of compiled config file
  47. *
  48. * @var string
  49. */
  50. public $compiled_filepath = null;
  51. /**
  52. * Filemtime of compiled config Filemtime
  53. *
  54. * @var int
  55. */
  56. public $compiled_timestamp = null;
  57. /**
  58. * flag if compiled config file is invalid and must be (re)compiled
  59. * @var bool
  60. */
  61. public $mustCompile = null;
  62. /**
  63. * Config file compiler object
  64. *
  65. * @var Smarty_Internal_Config_File_Compiler object
  66. */
  67. public $compiler_object = null;
  68. /**
  69. * Constructor of config file object
  70. *
  71. * @param string $config_resource config file resource name
  72. * @param Smarty $smarty Smarty instance
  73. * @param object $data object for config vars storage
  74. */
  75. public function __construct($config_resource, $smarty, $data = null)
  76. {
  77. $this->data = $data;
  78. $this->smarty = $smarty;
  79. $this->config_resource = $config_resource;
  80. }
  81. /**
  82. * Returns the compiled filepath
  83. *
  84. * @return string the compiled filepath
  85. */
  86. public function getCompiledFilepath()
  87. {
  88. return $this->compiled_filepath === null ?
  89. ($this->compiled_filepath = $this->buildCompiledFilepath()) :
  90. $this->compiled_filepath;
  91. }
  92. /**
  93. * Get file path.
  94. *
  95. * @return string
  96. */
  97. public function buildCompiledFilepath()
  98. {
  99. $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
  100. $_flag = (int) $this->smarty->config_read_hidden + (int) $this->smarty->config_booleanize * 2
  101. + (int) $this->smarty->config_overwrite * 4;
  102. $_filepath = sha1($this->source->name . $_flag);
  103. // if use_sub_dirs, break file into directories
  104. if ($this->smarty->use_sub_dirs) {
  105. $_filepath = substr($_filepath, 0, 2) . DS
  106. . substr($_filepath, 2, 2) . DS
  107. . substr($_filepath, 4, 2) . DS
  108. . $_filepath;
  109. }
  110. $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
  111. if (isset($_compile_id)) {
  112. $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
  113. }
  114. $_compile_dir = $this->smarty->getCompileDir();
  115. return $_compile_dir . $_filepath . '.' . basename($this->source->name) . '.config' . '.php';
  116. }
  117. /**
  118. * Returns the timpestamp of the compiled file
  119. *
  120. * @return integer the file timestamp
  121. */
  122. public function getCompiledTimestamp()
  123. {
  124. return $this->compiled_timestamp === null
  125. ? ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false)
  126. : $this->compiled_timestamp;
  127. }
  128. /**
  129. * Returns if the current config file must be compiled
  130. *
  131. * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
  132. *
  133. * @return boolean true if the file must be compiled
  134. */
  135. public function mustCompile()
  136. {
  137. return $this->mustCompile === null ?
  138. $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->source->timestamp):
  139. $this->mustCompile;
  140. }
  141. /**
  142. * Returns the compiled config file
  143. *
  144. * It checks if the config file must be compiled or just read the compiled version
  145. *
  146. * @return string the compiled config file
  147. */
  148. public function getCompiledConfig()
  149. {
  150. if ($this->compiled_config === null) {
  151. // see if template needs compiling.
  152. if ($this->mustCompile()) {
  153. $this->compileConfigSource();
  154. } else {
  155. $this->compiled_config = file_get_contents($this->getCompiledFilepath());
  156. }
  157. }
  158. return $this->compiled_config;
  159. }
  160. /**
  161. * Compiles the config files
  162. *
  163. * @throws Exception
  164. */
  165. public function compileConfigSource()
  166. {
  167. // compile template
  168. if (!is_object($this->compiler_object)) {
  169. // load compiler
  170. $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
  171. }
  172. // compile locking
  173. if ($this->smarty->compile_locking) {
  174. if ($saved_timestamp = $this->getCompiledTimestamp()) {
  175. touch($this->getCompiledFilepath());
  176. }
  177. }
  178. // call compiler
  179. try {
  180. $this->compiler_object->compileSource($this);
  181. } catch (Exception $e) {
  182. // restore old timestamp in case of error
  183. if ($this->smarty->compile_locking && $saved_timestamp) {
  184. touch($this->getCompiledFilepath(), $saved_timestamp);
  185. }
  186. throw $e;
  187. }
  188. // compiling succeded
  189. // write compiled template
  190. Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
  191. }
  192. /**
  193. * load config variables
  194. *
  195. * @param mixed $sections array of section names, single section or null
  196. * @param object $scope global,parent or local
  197. */
  198. public function loadConfigVars($sections = null, $scope = 'local')
  199. {
  200. if ($this->data instanceof Smarty_Internal_Template) {
  201. $this->data->properties['file_dependency'][sha1($this->source->filepath)] = array($this->source->filepath, $this->source->timestamp, 'file');
  202. }
  203. if ($this->mustCompile()) {
  204. $this->compileConfigSource();
  205. }
  206. // pointer to scope
  207. if ($scope == 'local') {
  208. $scope_ptr = $this->data;
  209. } elseif ($scope == 'parent') {
  210. if (isset($this->data->parent)) {
  211. $scope_ptr = $this->data->parent;
  212. } else {
  213. $scope_ptr = $this->data;
  214. }
  215. } elseif ($scope == 'root' || $scope == 'global') {
  216. $scope_ptr = $this->data;
  217. while (isset($scope_ptr->parent)) {
  218. $scope_ptr = $scope_ptr->parent;
  219. }
  220. }
  221. $_config_vars = array();
  222. include($this->getCompiledFilepath());
  223. // copy global config vars
  224. foreach ($_config_vars['vars'] as $variable => $value) {
  225. if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
  226. $scope_ptr->config_vars[$variable] = $value;
  227. } else {
  228. $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
  229. }
  230. }
  231. // scan sections
  232. if (!empty($sections)) {
  233. $sections = array_flip((array) $sections);
  234. foreach ($_config_vars['sections'] as $this_section => $dummy) {
  235. if (isset($sections[$this_section])) {
  236. foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
  237. if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
  238. $scope_ptr->config_vars[$variable] = $value;
  239. } else {
  240. $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * set Smarty property in template context
  249. *
  250. * @param string $property_name property name
  251. * @param mixed $value value
  252. * @throws SmartyException if $property_name is not valid
  253. */
  254. public function __set($property_name, $value)
  255. {
  256. switch ($property_name) {
  257. case 'source':
  258. case 'compiled':
  259. $this->$property_name = $value;
  260. return;
  261. }
  262. throw new SmartyException("invalid config property '$property_name'.");
  263. }
  264. /**
  265. * get Smarty property in template context
  266. *
  267. * @param string $property_name property name
  268. * @throws SmartyException if $property_name is not valid
  269. */
  270. public function __get($property_name)
  271. {
  272. switch ($property_name) {
  273. case 'source':
  274. if (empty($this->config_resource)) {
  275. throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\"");
  276. }
  277. $this->source = Smarty_Resource::config($this);
  278. return $this->source;
  279. case 'compiled':
  280. $this->compiled = $this->source->getCompiled($this);
  281. return $this->compiled;
  282. }
  283. throw new SmartyException("config attribute '$property_name' does not exist.");
  284. }
  285. }
  286. ?>