Code

Updated smarty to 3.0.8
[gosa.git] / gosa-core / include / smarty / sysplugins / smarty_internal_config.php
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, $data = null)
16     {
17         $this->data = $data;
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 SmartyException ("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 SmartyException("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 SmartyException("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         $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
139         $_flag = (int)$this->smarty->config_read_hidden + (int)$this->smarty->config_booleanize * 2 +
140         (int)$this->smarty->config_overwrite * 4;
141         $_filepath = sha1($this->config_resource_name . $_flag); 
142         // if use_sub_dirs, break file into directories
143         if ($this->smarty->use_sub_dirs) {
144             $_filepath = substr($_filepath, 0, 2) . DS
145              . substr($_filepath, 2, 2) . DS
146              . substr($_filepath, 4, 2) . DS
147              . $_filepath;
148         } 
149         $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
150         if (isset($_compile_id)) {
151             $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
152         } 
153         $_compile_dir = $this->smarty->compile_dir;
154         if (substr($_compile_dir, -1) != DS) {
155             $_compile_dir .= DS;
156         } 
157         return $_compile_dir . $_filepath . '.' . basename($this->config_resource_name) . '.config' . '.php';
158     } 
159     /**
160      * Returns the timpestamp of the compiled file
161      * 
162      * @return integer the file timestamp
163      */
164     public function getCompiledTimestamp ()
165     {
166         return $this->compiled_timestamp === null ?
167         ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false) :
168         $this->compiled_timestamp;
169     } 
170     /**
171      * Returns if the current config file must be compiled 
172      * 
173      * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
174      * 
175      * @return boolean true if the file must be compiled
176      */
177     public function mustCompile ()
178     {
179         return $this->mustCompile === null ?
180         $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp () === false || $this->smarty->compile_check && $this->getCompiledTimestamp () < $this->getTimestamp ()):
181         $this->mustCompile;
182     } 
183     /**
184      * Returns the compiled config file 
185      * 
186      * It checks if the config file must be compiled or just read the compiled version
187      * 
188      * @return string the compiled config file
189      */
190     public function getCompiledConfig ()
191     {
192         if ($this->compiled_config === null) {
193             // see if template needs compiling.
194             if ($this->mustCompile()) {
195                 $this->compileConfigSource();
196             } else {
197                 $this->compiled_config = file_get_contents($this->getCompiledFilepath());
198             } 
199         } 
200         return $this->compiled_config;
201     } 
203     /**
204      * Compiles the config files
205      */
206     public function compileConfigSource ()
207     { 
208         // compile template
209         if (!is_object($this->compiler_object)) {
210             // load compiler
211             $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
212         } 
213         // compile locking
214         if ($this->smarty->compile_locking) {
215             if ($saved_timestamp = $this->getCompiledTimestamp()) {
216                 touch($this->getCompiledFilepath());
217             } 
218         } 
219         // call compiler
220         try {
221             $this->compiler_object->compileSource($this);
222         } 
223         catch (Exception $e) {
224             // restore old timestamp in case of error
225             if ($this->smarty->compile_locking && $saved_timestamp) {
226                 touch($this->getCompiledFilepath(), $saved_timestamp);
227             } 
228             throw $e;
229         } 
230         // compiling succeded
231         // write compiled template
232         Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
233     } 
235     /*
236      * load config variables
237     *
238     * @param mixed $sections array of section names, single section or null
239     * @param object $scope global,parent or local
240     */
241     public function loadConfigVars ($sections = null, $scope = 'local')
242     {
243         if ($this->data instanceof Smarty_Internal_Template) {
244             $this->data->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp(),'file');
245         } 
246         if ($this->mustCompile()) {
247             $this->compileConfigSource();
248         }
249         // pointer to scope
250         if ($scope == 'local') {
251                 $scope_ptr = $this->data;
252         } elseif ($scope == 'parent') {
253                 if (isset($this->data->parent)) {
254                         $scope_ptr = $this->data->parent;
255                 } else {
256                         $scope_ptr = $this->data;
257                 }                       
258         } elseif ($scope == 'root' || $scope == 'global') {
259                 $scope_ptr = $this->data;
260                 while (isset($scope_ptr->parent)) {
261                         $scope_ptr = $scope_ptr->parent;
262                 } 
263         }
264         $_config_vars = array();
265         include($this->getCompiledFilepath ());
266         // copy global config vars
267         foreach ($_config_vars['vars'] as $variable => $value) {
268             if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
269                 $scope_ptr->config_vars[$variable] = $value;
270             } else {
271                 $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);
272             } 
273         } 
274         // scan sections
275         if(!empty($sections)) {
276           foreach ($_config_vars['sections'] as $this_section => $dummy) {
277               if (in_array($this_section, (array)$sections)) {
278                   foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
279                       if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
280                           $scope_ptr->config_vars[$variable] = $value;
281                       } else {
282                           $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);
283                       } 
284                   } 
285               } 
286           }
287         }
288     } 
289
290 ?>