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