smarty_internal_config.php 9.5 KB

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