PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/smarty/sysplugins/smarty_internal_config.php

https://github.com/tedkulp/cmsmadesimple-2-0
PHP | 270 lines | 248 code | 5 blank | 17 comment | 8 complexity | 769d17354bfab38fe15ea063f71f29dc MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Config
  4. *
  5. * Main class for config variables
  6. *
  7. * @ignore
  8. * @package Smarty
  9. * @subpackage Config
  10. * @author Uwe Tews
  11. */
  12. class Smarty_Internal_Config {
  13. static $config_objects = array();
  14. public function __construct($config_resource, $smarty, $template = null)
  15. {
  16. $this->template = $template;
  17. $this->smarty = $smarty;
  18. $this->config_resource = $config_resource;
  19. $this->config_resource_type = null;
  20. $this->config_resource_name = null;
  21. $this->config_filepath = null;
  22. $this->config_timestamp = null;
  23. $this->config_source = null;
  24. $this->compiled_config = null;
  25. $this->compiled_filepath = null;
  26. $this->compiled_timestamp = null;
  27. $this->mustCompile = null;
  28. $this->compiler_object = null;
  29. // parse config resource name
  30. if (!$this->parseConfigResourceName ($config_resource)) {
  31. throw new Exception ("Unable to parse config resource '{$config_resource}'");
  32. }
  33. }
  34. public function getConfigFilepath ()
  35. {
  36. return $this->config_filepath === null ?
  37. $this->config_filepath = $this->buildConfigFilepath() :
  38. $this->config_filepath;
  39. }
  40. public function getTimestamp ()
  41. {
  42. return $this->config_timestamp === null ?
  43. $this->config_timestamp = filemtime($this->getConfigFilepath()) :
  44. $this->config_timestamp;
  45. }
  46. private function parseConfigResourceName($config_resource)
  47. {
  48. if (empty($config_resource))
  49. return false;
  50. if (strpos($config_resource, ':') === false) {
  51. // no resource given, use default
  52. $this->config_resource_type = $this->smarty->default_config_type;
  53. $this->config_resource_name = $config_resource;
  54. } else {
  55. // get type and name from path
  56. list($this->config_resource_type, $this->config_resource_name) = explode(':', $config_resource, 2);
  57. if (strlen($this->config_resource_type) == 1) {
  58. // 1 char is not resource type, but part of filepath
  59. $this->config_resource_type = $this->smarty->default_config_type;
  60. $this->config_resource_name = $config_resource;
  61. } else {
  62. $this->config_resource_type = strtolower($this->config_resource_type);
  63. }
  64. }
  65. return true;
  66. }
  67. /*
  68. * get system filepath to config
  69. */
  70. public function buildConfigFilepath ()
  71. {
  72. foreach((array)$this->smarty->config_dir as $_config_dir) {
  73. if (strpos('/\\', substr($_config_dir, -1)) === false) {
  74. $_config_dir .= DS;
  75. }
  76. $_filepath = $_config_dir . $this->config_resource_name;
  77. if (file_exists($_filepath))
  78. return $_filepath;
  79. }
  80. // check for absolute path
  81. if (file_exists($this->config_resource_name))
  82. return $this->config_resource_name;
  83. // no tpl file found
  84. throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
  85. return false;
  86. }
  87. /**
  88. * Read config file source
  89. *
  90. * @return string content of source file
  91. */
  92. /**
  93. * Returns the template source code
  94. *
  95. * The template source is being read by the actual resource handler
  96. *
  97. * @return string the template source
  98. */
  99. public function getConfigSource ()
  100. {
  101. if ($this->config_source === null) {
  102. if ($this->readConfigSource($this) === false) {
  103. throw new Exception("Unable to load config file \"{$this->config_resource_name}\"");
  104. }
  105. }
  106. return $this->config_source;
  107. }
  108. public function readConfigSource()
  109. {
  110. // read source file
  111. if (file_exists($this->getConfigFilepath())) {
  112. $this->config_source = file_get_contents($this->getConfigFilepath());
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * Returns the compiled filepath
  120. *
  121. * @return string the compiled filepath
  122. */
  123. public function getCompiledFilepath ()
  124. {
  125. return $this->compiled_filepath === null ?
  126. ($this->compiled_filepath = $this->buildCompiledFilepath()) :
  127. $this->compiled_filepath;
  128. }
  129. public function buildCompiledFilepath()
  130. {
  131. $_flag = (int)$this->smarty->config_read_hidden + (int)$this->smarty->config_booleanize * 2 +
  132. (int)$this->smarty->config_overwrite * 4;
  133. $_filepath = sha1($this->config_resource_name . $_flag);
  134. // if use_sub_dirs, break file into directories
  135. if ($this->smarty->use_sub_dirs) {
  136. $_filepath = substr($_filepath, 0, 2) . DS
  137. . substr($_filepath, 2, 2) . DS
  138. . substr($_filepath, 4, 2) . DS
  139. . $_filepath;
  140. }
  141. $_compile_dir = $this->smarty->compile_dir;
  142. if (substr($_compile_dir, -1) != DS) {
  143. $_compile_dir .= DS;
  144. }
  145. return $_compile_dir . $_filepath . '.' . basename($this->config_resource_name) . '.config' . '.php';
  146. }
  147. /**
  148. * Returns the timpestamp of the compiled file
  149. *
  150. * @return integer the file timestamp
  151. */
  152. public function getCompiledTimestamp ()
  153. {
  154. return $this->compiled_timestamp === null ?
  155. ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false) :
  156. $this->compiled_timestamp;
  157. }
  158. /**
  159. * Returns if the current config file must be compiled
  160. *
  161. * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
  162. *
  163. * @return boolean true if the file must be compiled
  164. */
  165. public function mustCompile ()
  166. {
  167. return $this->mustCompile === null ?
  168. $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->getTimestamp ()):
  169. $this->mustCompile;
  170. }
  171. /**
  172. * Returns the compiled config file
  173. *
  174. * It checks if the config file must be compiled or just read the compiled version
  175. *
  176. * @return string the compiled config file
  177. */
  178. public function getCompiledConfig ()
  179. {
  180. if ($this->compiled_config === null) {
  181. // see if template needs compiling.
  182. if ($this->mustCompile()) {
  183. $this->compileConfigSource();
  184. } else {
  185. $this->compiled_config = file_get_contents($this->getCompiledFilepath());
  186. }
  187. }
  188. return $this->compiled_config;
  189. }
  190. /**
  191. * Compiles the config files
  192. */
  193. public function compileConfigSource ()
  194. {
  195. // compile template
  196. if (!is_object($this->compiler_object)) {
  197. // load compiler
  198. $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
  199. }
  200. // compile locking
  201. if ($this->smarty->compile_locking) {
  202. if ($saved_timestamp = $this->getCompiledTimestamp()) {
  203. touch($this->getCompiledFilepath());
  204. }
  205. }
  206. // call compiler
  207. try {
  208. $this->compiler_object->compileSource($this);
  209. }
  210. catch (Exception $e) {
  211. // restore old timestamp in case of error
  212. if ($this->smarty->compile_locking && $saved_timestamp) {
  213. touch($this->getCompiledFilepath(), $saved_timestamp);
  214. }
  215. throw $e;
  216. }
  217. // compiling succeded
  218. // write compiled template
  219. Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
  220. }
  221. /*
  222. * load config variables
  223. *
  224. * @param mixed $sections array of section names, single section or null
  225. * @param object $scope global,parent or local
  226. */
  227. public function loadConfigVars ($sections = null, $scope)
  228. {
  229. if (isset($this->template)) {
  230. $this->template->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
  231. } else {
  232. $this->smarty->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
  233. }
  234. if ($this->mustCompile()) {
  235. $this->compileConfigSource();
  236. }
  237. include($this->getCompiledFilepath ());
  238. // copy global config vars
  239. foreach ($_config_vars['vars'] as $variable => $value) {
  240. if ($this->smarty->config_overwrite || !isset($scope->config_vars[$variable])) {
  241. $scope->config_vars[$variable] = $value;
  242. } else {
  243. $scope->config_vars[$variable] = array_merge((array)$scope->config_vars[$variable], (array)$value);
  244. }
  245. }
  246. // scan sections
  247. foreach ($_config_vars['sections'] as $this_section => $dummy) {
  248. if ($sections == null || in_array($this_section, (array)$sections)) {
  249. foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
  250. if ($this->smarty->config_overwrite || !isset($scope->config_vars[$variable])) {
  251. $scope->config_vars[$variable] = $value;
  252. } else {
  253. $scope->config_vars[$variable] = array_merge((array)$scope->config_vars[$variable], (array)$value);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. ?>