]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/sysplugins/smarty_internal_config.php
reverting tinymce changes, updating smarty to 3.1.19
[friendica.git] / library / Smarty / libs / sysplugins / smarty_internal_config.php
1 <?php
2 /**
3  * Smarty Internal Plugin Config
4  *
5  * @package    Smarty
6  * @subpackage Config
7  * @author     Uwe Tews
8  */
9
10 /**
11  * Smarty Internal Plugin Config
12  * Main class for config variables
13  *
14  * @package    Smarty
15  * @subpackage Config
16  * @ignore
17  */
18 class Smarty_Internal_Config
19 {
20     /**
21      * Smarty instance
22      *
23      * @var Smarty object
24      */
25     public $smarty = null;
26     /**
27      * Object of config var storage
28      *
29      * @var object
30      */
31     public $data = null;
32     /**
33      * Config resource
34      *
35      * @var string
36      */
37     public $config_resource = null;
38     /**
39      * Compiled config file
40      *
41      * @var string
42      */
43     public $compiled_config = null;
44     /**
45      * filepath of compiled config file
46      *
47      * @var string
48      */
49     public $compiled_filepath = null;
50     /**
51      * Filemtime of compiled config Filemtime
52      *
53      * @var int
54      */
55     public $compiled_timestamp = null;
56     /**
57      * flag if compiled config file is invalid and must be (re)compiled
58      *
59      * @var bool
60      */
61     public $mustCompile = null;
62     /**
63      * Config file compiler object
64      *
65      * @var Smarty_Internal_Config_File_Compiler object
66      */
67     public $compiler_object = null;
68
69     /**
70      * Constructor of config file object
71      *
72      * @param string $config_resource config file resource name
73      * @param Smarty $smarty          Smarty instance
74      * @param object $data            object for config vars storage
75      */
76     public function __construct($config_resource, $smarty, $data = null)
77     {
78         $this->data = $data;
79         $this->smarty = $smarty;
80         $this->config_resource = $config_resource;
81     }
82
83     /**
84      * Returns the compiled  filepath
85      *
86      * @return string the compiled filepath
87      */
88     public function getCompiledFilepath()
89     {
90         return $this->compiled_filepath === null ?
91             ($this->compiled_filepath = $this->buildCompiledFilepath()) :
92             $this->compiled_filepath;
93     }
94
95     /**
96      * Get file path.
97      *
98      * @return string
99      */
100     public function buildCompiledFilepath()
101     {
102         $_compile_id = isset($this->smarty->compile_id) ? preg_replace('![^\w\|]+!', '_', $this->smarty->compile_id) : null;
103         $_flag = (int) $this->smarty->config_read_hidden + (int) $this->smarty->config_booleanize * 2
104             + (int) $this->smarty->config_overwrite * 4;
105         $_filepath = sha1(realpath($this->source->filepath) . $_flag);
106         // if use_sub_dirs, break file into directories
107         if ($this->smarty->use_sub_dirs) {
108             $_filepath = substr($_filepath, 0, 2) . DS
109                 . substr($_filepath, 2, 2) . DS
110                 . substr($_filepath, 4, 2) . DS
111                 . $_filepath;
112         }
113         $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
114         if (isset($_compile_id)) {
115             $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
116         }
117         $_compile_dir = $this->smarty->getCompileDir();
118
119         return $_compile_dir . $_filepath . '.' . basename($this->source->name) . '.config' . '.php';
120     }
121
122     /**
123      * Returns the timestamp of the compiled file
124      *
125      * @return integer the file timestamp
126      */
127     public function getCompiledTimestamp()
128     {
129         return $this->compiled_timestamp === null
130             ? ($this->compiled_timestamp = (file_exists($this->getCompiledFilepath())) ? filemtime($this->getCompiledFilepath()) : false)
131             : $this->compiled_timestamp;
132     }
133
134     /**
135      * Returns if the current config file must be compiled
136      * It does compare the timestamps of config source and the compiled config and checks the force compile configuration
137      *
138      * @return boolean true if the file must be compiled
139      */
140     public function mustCompile()
141     {
142         return $this->mustCompile === null ?
143             $this->mustCompile = ($this->smarty->force_compile || $this->getCompiledTimestamp() === false || $this->smarty->compile_check && $this->getCompiledTimestamp() < $this->source->timestamp) :
144             $this->mustCompile;
145     }
146
147     /**
148      * Returns the compiled config file
149      * It checks if the config file must be compiled or just read the compiled version
150      *
151      * @return string the compiled config file
152      */
153     public function getCompiledConfig()
154     {
155         if ($this->compiled_config === null) {
156             // see if template needs compiling.
157             if ($this->mustCompile()) {
158                 $this->compileConfigSource();
159             } else {
160                 $this->compiled_config = file_get_contents($this->getCompiledFilepath());
161             }
162         }
163
164         return $this->compiled_config;
165     }
166
167     /**
168      * Compiles the config files
169      *
170      * @throws Exception
171      */
172     public function compileConfigSource()
173     {
174         // compile template
175         if (!is_object($this->compiler_object)) {
176             // load compiler
177             $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
178         }
179         // compile locking
180         if ($this->smarty->compile_locking) {
181             if ($saved_timestamp = $this->getCompiledTimestamp()) {
182                 touch($this->getCompiledFilepath());
183             }
184         }
185         // call compiler
186         try {
187             $this->compiler_object->compileSource($this);
188         }
189         catch (Exception $e) {
190             // restore old timestamp in case of error
191             if ($this->smarty->compile_locking && $saved_timestamp) {
192                 touch($this->getCompiledFilepath(), $saved_timestamp);
193             }
194             throw $e;
195         }
196         // compiling succeeded
197         // write compiled template
198         Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
199     }
200
201     /**
202      * load config variables
203      *
204      * @param mixed         $sections array of section names, single section or null
205      * @param string $scope    global,parent or local
206      *
207      * @throws Exception
208      */
209     public function loadConfigVars($sections = null, $scope = 'local')
210     {
211         if ($this->data instanceof Smarty_Internal_Template) {
212             $this->data->properties['file_dependency'][sha1($this->source->filepath)] = array($this->source->filepath, $this->source->timestamp, 'file');
213         }
214         if ($this->mustCompile()) {
215             $this->compileConfigSource();
216         }
217         // pointer to scope
218         if ($scope == 'local') {
219             $scope_ptr = $this->data;
220         } elseif ($scope == 'parent') {
221             if (isset($this->data->parent)) {
222                 $scope_ptr = $this->data->parent;
223             } else {
224                 $scope_ptr = $this->data;
225             }
226         } elseif ($scope == 'root' || $scope == 'global') {
227             $scope_ptr = $this->data;
228             while (isset($scope_ptr->parent)) {
229                 $scope_ptr = $scope_ptr->parent;
230             }
231         }
232         $_config_vars = array();
233         include($this->getCompiledFilepath());
234         // copy global config vars
235         foreach ($_config_vars['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         // scan sections
243         if (!empty($sections)) {
244             foreach ((array) $sections as $this_section) {
245                 if (isset($_config_vars['sections'][$this_section])) {
246                     foreach ($_config_vars['sections'][$this_section]['vars'] as $variable => $value) {
247                         if ($this->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
248                             $scope_ptr->config_vars[$variable] = $value;
249                         } else {
250                             $scope_ptr->config_vars[$variable] = array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
251                         }
252                     }
253                 }
254             }
255         }
256     }
257
258     /**
259      * set Smarty property in template context
260      *
261      * @param  string $property_name property name
262      * @param  mixed  $value         value
263      *
264      * @throws SmartyException if $property_name is not valid
265      */
266     public function __set($property_name, $value)
267     {
268         switch ($property_name) {
269             case 'source':
270             case 'compiled':
271                 $this->$property_name = $value;
272
273                 return;
274         }
275
276         throw new SmartyException("invalid config property '$property_name'.");
277     }
278
279     /**
280      * get Smarty property in template context
281      *
282      * @param  string $property_name property name
283      *
284      * @return \Smarty_Config_Source|\Smarty_Template_Compiled
285      * @throws SmartyException if $property_name is not valid
286      */
287     public function __get($property_name)
288     {
289         switch ($property_name) {
290             case 'source':
291                 if (empty($this->config_resource)) {
292                     throw new SmartyException("Unable to parse resource name \"{$this->config_resource}\"");
293                 }
294                 $this->source = Smarty_Resource::config($this);
295
296                 return $this->source;
297
298             case 'compiled':
299                 $this->compiled = $this->source->getCompiled($this);
300
301                 return $this->compiled;
302         }
303
304         throw new SmartyException("config attribute '$property_name' does not exist.");
305     }
306 }