Code

Updated smarty
[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, $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->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         // call compiler
209         if ($this->compiler_object->compileSource($this)) {
210             // compiling succeded
211             // write compiled template
212             Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty); 
213             // make template and compiled file timestamp match
214             touch($this->getCompiledFilepath(), $this->getTimestamp());
215         } else {
216             // error compiling template
217             throw new Exception("Error compiling template {$this->getConfigFilepath ()}");
218             return false;
219         } 
220     } 
222     /*
223      * load config variables
224     *
225     * @param mixed $sections array of section names, single section or null
226     * @param object $scope global,parent or local
227     */
228     public function loadConfigVars ($sections = null, $scope)
229     {
230         if (isset($this->template)) {
231             $this->template->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
232         } else {
233             $this->smarty->properties['file_dependency'][sha1($this->getConfigFilepath())] = array($this->getConfigFilepath(), $this->getTimestamp());
234         } 
235         $config_data = unserialize($this->getCompiledConfig()); 
236         // var_dump($config_data);
237         // copy global config vars
238         foreach ($config_data['vars'] as $variable => $value) {
239             if ($this->smarty->config_overwrite || !isset($scope->config_vars[$variable])) {
240                 $scope->config_vars[$variable] = $value;
241             } else {
242                 $scope->config_vars[$variable] = array_merge((array)$scope->config_vars[$variable], (array)$value);
243             } 
244         } 
245         // scan sections
246         foreach ($config_data['sections'] as $this_section => $dummy) {
247             if ($sections == null || in_array($this_section, (array)$sections)) {
248                 foreach ($config_data['sections'][$this_section]['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             } 
256         } 
257     } 
258
260 ?>