@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * eg. $t->assign('name','mickey'); |
175 | 175 | * |
176 | 176 | * @access public |
177 | - * @param mixed $variable_name Name of template variable or associative array name/value |
|
177 | + * @param string $variable Name of template variable or associative array name/value |
|
178 | 178 | * @param mixed $value value assigned to this variable. Not set if variable_name is an associative array |
179 | 179 | */ |
180 | 180 | public function assign( $variable, $value = null ){ |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | * |
253 | 253 | * @access public |
254 | 254 | * @param string $tpl_name Name of template (set the same of draw) |
255 | - * @param int $expiration_time Set after how many seconds the cache expire and must be regenerated |
|
255 | + * @param int $expire_time Set after how many seconds the cache expire and must be regenerated |
|
256 | 256 | * @param string $cache_id Suffix to be used when writing file to cache (optional) |
257 | 257 | * @return string it return the HTML or null if the cache must be recreated |
258 | 258 | */ |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * @access protected |
300 | 300 | * @param string $tpl_name template name to check |
301 | 301 | * @throws RainTpl_NotFoundException |
302 | - * @return bool return true if the template has changed |
|
302 | + * @return boolean|null return true if the template has changed |
|
303 | 303 | */ |
304 | 304 | protected function check_template( $tpl_name ){ |
305 | 305 |
@@ -14,1045 +14,1045 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class RainTPL{ |
16 | 16 | |
17 | - // ------------------------- |
|
18 | - // CONFIGURATION |
|
19 | - // ------------------------- |
|
20 | - |
|
21 | - /** |
|
22 | - * Template directory |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - static $tpl_dir = 'tpl/'; |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * Cache directory |
|
31 | - * |
|
32 | - * Is the directory where RainTPL will compile the template and save the cache |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - static $cache_dir = 'tmp/'; |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * Template base URL |
|
41 | - * |
|
42 | - * RainTPL will add this URL to the relative paths of element selected in $path_replace_list. |
|
43 | - * |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - static $base_url = null; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Template extension |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - static $tpl_ext = "html"; |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * Should the path be replaced |
|
59 | - * |
|
60 | - * Path replace is a cool features that replace all relative paths of images (<img src="...">), stylesheet (<link href="...">), script (<script src="...">) and link (<a href="...">) |
|
61 | - * Set true to enable the path replace. |
|
62 | - * |
|
63 | - * @var boolean |
|
64 | - */ |
|
65 | - static $path_replace = true; |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * You can set what the path_replace method will replace. |
|
70 | - * Avaible options: a, img, link, script, input |
|
71 | - * |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - static $path_replace_list = array( 'a', 'img', 'link', 'script', 'input' ); |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * You can define in the black list what string are disabled into the template tags |
|
79 | - * |
|
80 | - * @var array |
|
81 | - */ |
|
82 | - static $black_list = array( '\$this', 'raintpl::', 'self::', '_SESSION', '_SERVER', '_ENV', 'eval', 'exec', 'unlink', 'rmdir' ); |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * Check template |
|
87 | - * |
|
88 | - * true: checks template update time, if changed it compile them |
|
89 | - * false: loads the compiled template. Set false if server doesn't have write permission for cache_directory. |
|
90 | - * |
|
91 | - * @var bool |
|
92 | - */ |
|
93 | - static $check_template_update = true; |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * PHP tags <? ?> |
|
98 | - * |
|
99 | - * True: php tags are enabled into the template |
|
100 | - * False: php tags are disabled into the template and rendered as html |
|
101 | - * |
|
102 | - * @var bool |
|
103 | - */ |
|
104 | - static $php_enabled = false; |
|
105 | - |
|
106 | - |
|
107 | - /** |
|
108 | - * Debug mode flag |
|
109 | - * |
|
110 | - * True: debug mode is used, syntax errors are displayed directly in template. Execution of script is not terminated. |
|
111 | - * False: exception is thrown on found error. |
|
112 | - * |
|
113 | - * @var bool |
|
114 | - */ |
|
115 | - static $debug = false; |
|
116 | - |
|
117 | - // ------------------------- |
|
118 | - |
|
119 | - |
|
120 | - // ------------------------- |
|
121 | - // RAINTPL VARIABLES |
|
122 | - // ------------------------- |
|
123 | - |
|
124 | - /** |
|
125 | - * Is the array where RainTPL keep the variables assigned |
|
126 | - * |
|
127 | - * @var array |
|
128 | - */ |
|
129 | - public $var = array(); |
|
130 | - |
|
131 | - /** |
|
132 | - * variables to keep the template directories and info |
|
133 | - * |
|
134 | - * @var array |
|
135 | - */ |
|
136 | - protected $tpl = array(); // |
|
137 | - |
|
138 | - /** |
|
139 | - * static cache enabled / disabled |
|
140 | - * |
|
141 | - * @var bool |
|
142 | - */ |
|
143 | - protected $cache = false; |
|
144 | - |
|
145 | - /** |
|
146 | - * identify only one cache |
|
147 | - * |
|
148 | - * @var string |
|
149 | - */ |
|
150 | - protected $cache_id = ''; |
|
151 | - |
|
152 | - /** |
|
153 | - * takes all the config to create the md5 of the file |
|
154 | - * |
|
155 | - * @var array the file |
|
156 | - */ |
|
157 | - protected static $config_name_sum = array(); |
|
158 | - |
|
159 | - // ------------------------- |
|
160 | - |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * default cache expire time = hour |
|
165 | - * |
|
166 | - * @const int |
|
167 | - */ |
|
168 | - const CACHE_EXPIRE_TIME = 3600; |
|
169 | - |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * Assign variable |
|
174 | - * eg. $t->assign('name','mickey'); |
|
175 | - * |
|
176 | - * @access public |
|
177 | - * @param mixed $variable_name Name of template variable or associative array name/value |
|
178 | - * @param mixed $value value assigned to this variable. Not set if variable_name is an associative array |
|
179 | - */ |
|
180 | - public function assign( $variable, $value = null ){ |
|
181 | - if( is_array( $variable ) ) |
|
182 | - $this->var += $variable; |
|
183 | - else |
|
184 | - $this->var[ $variable ] = $value; |
|
185 | - } |
|
186 | - |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * Draw the template |
|
191 | - * eg. $html = $tpl->draw( 'demo', TRUE ); // return template in string |
|
192 | - * or $tpl->draw( $tpl_name ); // echo the template |
|
193 | - * |
|
194 | - * @access public |
|
195 | - * @param string $tpl_name template to load |
|
196 | - * @param boolean $return_string true=return a string, false=echo the template |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function draw( $tpl_name, $return_string = false ){ |
|
200 | - |
|
201 | - try { |
|
202 | - // compile the template if necessary and set the template filepath |
|
203 | - $this->check_template( $tpl_name ); |
|
204 | - } catch (RainTpl_Exception $e) { |
|
205 | - $output = $this->printDebug($e); |
|
206 | - die($output); |
|
207 | - } |
|
208 | - |
|
209 | - // Cache is off and, return_string is false |
|
210 | - // Rain just echo the template |
|
211 | - |
|
212 | - if( !$this->cache && !$return_string ){ |
|
213 | - extract( $this->var ); |
|
214 | - include $this->tpl['compiled_filename']; |
|
215 | - unset( $this->tpl ); |
|
216 | - } |
|
217 | - |
|
218 | - |
|
219 | - // cache or return_string are enabled |
|
220 | - // rain get the output buffer to save the output in the cache or to return it as string |
|
221 | - |
|
222 | - else{ |
|
223 | - |
|
224 | - //---------------------- |
|
225 | - // get the output buffer |
|
226 | - //---------------------- |
|
227 | - ob_start(); |
|
228 | - extract( $this->var ); |
|
229 | - include $this->tpl['compiled_filename']; |
|
230 | - $raintpl_contents = ob_get_clean(); |
|
231 | - //---------------------- |
|
232 | - |
|
233 | - |
|
234 | - // save the output in the cache |
|
235 | - if( $this->cache ) |
|
236 | - file_put_contents( $this->tpl['cache_filename'], "<?php if(!class_exists('raintpl')){exit;}?>" . $raintpl_contents ); |
|
237 | - |
|
238 | - // free memory |
|
239 | - unset( $this->tpl ); |
|
240 | - |
|
241 | - // return or print the template |
|
242 | - if( $return_string ) return $raintpl_contents; else echo $raintpl_contents; |
|
243 | - |
|
244 | - } |
|
245 | - |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * If exists a valid cache for this template it returns the cache |
|
252 | - * |
|
253 | - * @access public |
|
254 | - * @param string $tpl_name Name of template (set the same of draw) |
|
255 | - * @param int $expiration_time Set after how many seconds the cache expire and must be regenerated |
|
256 | - * @param string $cache_id Suffix to be used when writing file to cache (optional) |
|
257 | - * @return string it return the HTML or null if the cache must be recreated |
|
258 | - */ |
|
259 | - public function cache( $tpl_name, $expire_time = self::CACHE_EXPIRE_TIME, $cache_id = '' ){ |
|
260 | - |
|
261 | - // set the cache_id |
|
262 | - $this->cache_id = $cache_id; |
|
263 | - |
|
264 | - if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl['cache_filename'] ) && ( time() - filemtime( $this->tpl['cache_filename'] ) < $expire_time ) ) |
|
265 | - return substr( file_get_contents( $this->tpl['cache_filename'] ), 43 ); |
|
266 | - else{ |
|
267 | - //delete the cache of the selected template |
|
268 | - if (file_exists($this->tpl['cache_filename'])) |
|
269 | - unlink($this->tpl['cache_filename'] ); |
|
270 | - $this->cache = true; |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Configure the settings of RainTPL |
|
278 | - * |
|
279 | - * @access public |
|
280 | - * @static |
|
281 | - * @param array|string $setting array of settings or setting name |
|
282 | - * @param mixed $value content to set in the setting (optional) |
|
283 | - */ |
|
284 | - public static function configure( $setting, $value = null ){ |
|
285 | - if( is_array( $setting ) ) |
|
286 | - foreach( $setting as $key => $value ) |
|
287 | - self::configure( $key, $value ); |
|
288 | - else if( property_exists( __CLASS__, $setting ) ){ |
|
289 | - self::$$setting = $value; |
|
290 | - self::$config_name_sum[ $setting ] = $value; // take trace of all config |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Check if has to compile the template |
|
298 | - * |
|
299 | - * @access protected |
|
300 | - * @param string $tpl_name template name to check |
|
301 | - * @throws RainTpl_NotFoundException |
|
302 | - * @return bool return true if the template has changed |
|
303 | - */ |
|
304 | - protected function check_template( $tpl_name ){ |
|
305 | - |
|
306 | - if( !isset($this->tpl['checked']) ){ |
|
307 | - |
|
308 | - $tpl_basename = basename( $tpl_name ); // template basename |
|
309 | - $tpl_basedir = strpos($tpl_name,"/") ? dirname($tpl_name) . '/' : null; // template basedirectory |
|
310 | - $tpl_dir = PATH . self::$tpl_dir . $tpl_basedir; // template directory |
|
311 | - $this->tpl['tpl_filename'] = $tpl_dir . $tpl_basename . '.' . self::$tpl_ext; // template filename |
|
312 | - $temp_compiled_filename = PATH . self::$cache_dir . $tpl_basename . "." . md5( $tpl_dir . serialize(self::$config_name_sum)); |
|
313 | - $this->tpl['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename |
|
314 | - $this->tpl['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id . '.rtpl.php'; // static cache filename |
|
315 | - |
|
316 | - // if the template doesn't exsist throw an error |
|
317 | - if( self::$check_template_update && !file_exists( $this->tpl['tpl_filename'] ) ){ |
|
318 | - $e = new RainTpl_NotFoundException( 'Template '. $tpl_basename .' not found!' ); |
|
319 | - throw $e->setTemplateFile($this->tpl['tpl_filename']); |
|
320 | - } |
|
321 | - |
|
322 | - // file doesn't exsist, or the template was updated, Rain will compile the template |
|
323 | - if( !file_exists( $this->tpl['compiled_filename'] ) || ( self::$check_template_update && filemtime($this->tpl['compiled_filename']) < filemtime( $this->tpl['tpl_filename'] ) ) ){ |
|
324 | - $this->compileFile( $tpl_basedir, $this->tpl['tpl_filename'], PATH . self::$cache_dir, $this->tpl['compiled_filename'] ); |
|
325 | - return true; |
|
326 | - } |
|
327 | - $this->tpl['checked'] = true; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - |
|
333 | - /** |
|
334 | - * execute stripslaches() on the xml block. Invoqued by preg_replace_callback function below |
|
335 | - * |
|
336 | - * @access protected |
|
337 | - * @param string $capture |
|
338 | - * @return string |
|
339 | - */ |
|
340 | - protected function xml_reSubstitution($capture) { |
|
341 | - return "<?php echo '<?xml ".stripslashes($capture[1])." ?>'; ?>"; |
|
342 | - } |
|
343 | - |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * Compile and write the compiled template file |
|
348 | - * |
|
349 | - * @access protected |
|
350 | - * @param string $tpl_basedir |
|
351 | - * @param string $tpl_filename |
|
352 | - * @param string $cache_dir |
|
353 | - * @param string $compiled_filename |
|
354 | - * @throws RainTpl_Exception |
|
355 | - * @return void |
|
356 | - */ |
|
357 | - protected function compileFile( $tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename ){ |
|
358 | - |
|
359 | - //read template file |
|
360 | - $this->tpl['source'] = $template_code = file_get_contents( $tpl_filename ); |
|
361 | - |
|
362 | - //xml substitution |
|
363 | - $template_code = preg_replace( "/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code ); |
|
364 | - |
|
365 | - //disable php tag |
|
366 | - if( !self::$php_enabled ) |
|
367 | - $template_code = str_replace( array("<?","?>"), array("<?","?>"), $template_code ); |
|
368 | - |
|
369 | - //xml re-substitution |
|
370 | - $template_code = preg_replace_callback ( "/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code ); |
|
371 | - |
|
372 | - //compile template |
|
373 | - $template_compiled = "<?php if(!class_exists('raintpl')){exit;}?>" . $this->compileTemplate( $template_code, $tpl_basedir ); |
|
374 | - |
|
375 | - |
|
376 | - // fix the php-eating-newline-after-closing-tag-problem |
|
377 | - $template_compiled = str_replace( "?>\n", "?>\n\n", $template_compiled ); |
|
378 | - |
|
379 | - // create directories |
|
380 | - if( !is_dir( $cache_dir ) ) |
|
381 | - mkdir( $cache_dir, 0755, true ); |
|
382 | - |
|
383 | - if( !is_writable( $cache_dir ) ) |
|
384 | - throw new RainTpl_Exception ('Cache directory ' . $cache_dir . ' doesn\'t have write permission. Set write permission or set RAINTPL_CHECK_TEMPLATE_UPDATE to false. More details on http://www.raintpl.com/Documentation/Documentation-for-PHP-developers/Configuration/'); |
|
385 | - |
|
386 | - //write compiled file |
|
387 | - file_put_contents( $compiled_filename, $template_compiled ); |
|
388 | - } |
|
389 | - |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * Compile template |
|
394 | - * |
|
395 | - * @access protected |
|
396 | - * @param string $template_code |
|
397 | - * @param string $tpl_basedir |
|
398 | - * @return string |
|
399 | - */ |
|
400 | - protected function compileTemplate( $template_code, $tpl_basedir ){ |
|
401 | - |
|
402 | - //tag list |
|
403 | - $tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})', |
|
404 | - 'loop_close' => '(\{\/loop\})', |
|
405 | - 'if' => '(\{if(?: condition){0,1}="[^"]*"\})', |
|
406 | - 'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})', |
|
407 | - 'else' => '(\{else\})', |
|
408 | - 'if_close' => '(\{\/if\})', |
|
409 | - 'function' => '(\{function="[^"]*"\})', |
|
410 | - 'noparse' => '(\{noparse\})', |
|
411 | - 'noparse_close'=> '(\{\/noparse\})', |
|
412 | - 'ignore' => '(\{ignore\}|\{\*)', |
|
413 | - 'ignore_close' => '(\{\/ignore\}|\*\})', |
|
414 | - 'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})', |
|
415 | - 'template_info'=> '(\{\$template_info\})', |
|
416 | - 'function' => '(\{function="(\w*?)(?:.*?)"\})' |
|
417 | - ); |
|
418 | - |
|
419 | - $tag_regexp = "/" . join( "|", $tag_regexp ) . "/"; |
|
420 | - |
|
421 | - //split the code with the tags regexp |
|
422 | - $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
423 | - |
|
424 | - //path replace (src of img, background and href of link) |
|
425 | - $template_code = $this->path_replace( $template_code, $tpl_basedir ); |
|
426 | - |
|
427 | - //compile the code |
|
428 | - $compiled_code = $this->compileCode( $template_code ); |
|
17 | + // ------------------------- |
|
18 | + // CONFIGURATION |
|
19 | + // ------------------------- |
|
20 | + |
|
21 | + /** |
|
22 | + * Template directory |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + static $tpl_dir = 'tpl/'; |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * Cache directory |
|
31 | + * |
|
32 | + * Is the directory where RainTPL will compile the template and save the cache |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + static $cache_dir = 'tmp/'; |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * Template base URL |
|
41 | + * |
|
42 | + * RainTPL will add this URL to the relative paths of element selected in $path_replace_list. |
|
43 | + * |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + static $base_url = null; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Template extension |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + static $tpl_ext = "html"; |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * Should the path be replaced |
|
59 | + * |
|
60 | + * Path replace is a cool features that replace all relative paths of images (<img src="...">), stylesheet (<link href="...">), script (<script src="...">) and link (<a href="...">) |
|
61 | + * Set true to enable the path replace. |
|
62 | + * |
|
63 | + * @var boolean |
|
64 | + */ |
|
65 | + static $path_replace = true; |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * You can set what the path_replace method will replace. |
|
70 | + * Avaible options: a, img, link, script, input |
|
71 | + * |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + static $path_replace_list = array( 'a', 'img', 'link', 'script', 'input' ); |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * You can define in the black list what string are disabled into the template tags |
|
79 | + * |
|
80 | + * @var array |
|
81 | + */ |
|
82 | + static $black_list = array( '\$this', 'raintpl::', 'self::', '_SESSION', '_SERVER', '_ENV', 'eval', 'exec', 'unlink', 'rmdir' ); |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * Check template |
|
87 | + * |
|
88 | + * true: checks template update time, if changed it compile them |
|
89 | + * false: loads the compiled template. Set false if server doesn't have write permission for cache_directory. |
|
90 | + * |
|
91 | + * @var bool |
|
92 | + */ |
|
93 | + static $check_template_update = true; |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * PHP tags <? ?> |
|
98 | + * |
|
99 | + * True: php tags are enabled into the template |
|
100 | + * False: php tags are disabled into the template and rendered as html |
|
101 | + * |
|
102 | + * @var bool |
|
103 | + */ |
|
104 | + static $php_enabled = false; |
|
105 | + |
|
106 | + |
|
107 | + /** |
|
108 | + * Debug mode flag |
|
109 | + * |
|
110 | + * True: debug mode is used, syntax errors are displayed directly in template. Execution of script is not terminated. |
|
111 | + * False: exception is thrown on found error. |
|
112 | + * |
|
113 | + * @var bool |
|
114 | + */ |
|
115 | + static $debug = false; |
|
116 | + |
|
117 | + // ------------------------- |
|
118 | + |
|
119 | + |
|
120 | + // ------------------------- |
|
121 | + // RAINTPL VARIABLES |
|
122 | + // ------------------------- |
|
123 | + |
|
124 | + /** |
|
125 | + * Is the array where RainTPL keep the variables assigned |
|
126 | + * |
|
127 | + * @var array |
|
128 | + */ |
|
129 | + public $var = array(); |
|
130 | + |
|
131 | + /** |
|
132 | + * variables to keep the template directories and info |
|
133 | + * |
|
134 | + * @var array |
|
135 | + */ |
|
136 | + protected $tpl = array(); // |
|
137 | + |
|
138 | + /** |
|
139 | + * static cache enabled / disabled |
|
140 | + * |
|
141 | + * @var bool |
|
142 | + */ |
|
143 | + protected $cache = false; |
|
144 | + |
|
145 | + /** |
|
146 | + * identify only one cache |
|
147 | + * |
|
148 | + * @var string |
|
149 | + */ |
|
150 | + protected $cache_id = ''; |
|
151 | + |
|
152 | + /** |
|
153 | + * takes all the config to create the md5 of the file |
|
154 | + * |
|
155 | + * @var array the file |
|
156 | + */ |
|
157 | + protected static $config_name_sum = array(); |
|
158 | + |
|
159 | + // ------------------------- |
|
160 | + |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * default cache expire time = hour |
|
165 | + * |
|
166 | + * @const int |
|
167 | + */ |
|
168 | + const CACHE_EXPIRE_TIME = 3600; |
|
169 | + |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * Assign variable |
|
174 | + * eg. $t->assign('name','mickey'); |
|
175 | + * |
|
176 | + * @access public |
|
177 | + * @param mixed $variable_name Name of template variable or associative array name/value |
|
178 | + * @param mixed $value value assigned to this variable. Not set if variable_name is an associative array |
|
179 | + */ |
|
180 | + public function assign( $variable, $value = null ){ |
|
181 | + if( is_array( $variable ) ) |
|
182 | + $this->var += $variable; |
|
183 | + else |
|
184 | + $this->var[ $variable ] = $value; |
|
185 | + } |
|
186 | + |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * Draw the template |
|
191 | + * eg. $html = $tpl->draw( 'demo', TRUE ); // return template in string |
|
192 | + * or $tpl->draw( $tpl_name ); // echo the template |
|
193 | + * |
|
194 | + * @access public |
|
195 | + * @param string $tpl_name template to load |
|
196 | + * @param boolean $return_string true=return a string, false=echo the template |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function draw( $tpl_name, $return_string = false ){ |
|
200 | + |
|
201 | + try { |
|
202 | + // compile the template if necessary and set the template filepath |
|
203 | + $this->check_template( $tpl_name ); |
|
204 | + } catch (RainTpl_Exception $e) { |
|
205 | + $output = $this->printDebug($e); |
|
206 | + die($output); |
|
207 | + } |
|
208 | + |
|
209 | + // Cache is off and, return_string is false |
|
210 | + // Rain just echo the template |
|
211 | + |
|
212 | + if( !$this->cache && !$return_string ){ |
|
213 | + extract( $this->var ); |
|
214 | + include $this->tpl['compiled_filename']; |
|
215 | + unset( $this->tpl ); |
|
216 | + } |
|
217 | + |
|
218 | + |
|
219 | + // cache or return_string are enabled |
|
220 | + // rain get the output buffer to save the output in the cache or to return it as string |
|
221 | + |
|
222 | + else{ |
|
223 | + |
|
224 | + //---------------------- |
|
225 | + // get the output buffer |
|
226 | + //---------------------- |
|
227 | + ob_start(); |
|
228 | + extract( $this->var ); |
|
229 | + include $this->tpl['compiled_filename']; |
|
230 | + $raintpl_contents = ob_get_clean(); |
|
231 | + //---------------------- |
|
232 | + |
|
233 | + |
|
234 | + // save the output in the cache |
|
235 | + if( $this->cache ) |
|
236 | + file_put_contents( $this->tpl['cache_filename'], "<?php if(!class_exists('raintpl')){exit;}?>" . $raintpl_contents ); |
|
237 | + |
|
238 | + // free memory |
|
239 | + unset( $this->tpl ); |
|
240 | + |
|
241 | + // return or print the template |
|
242 | + if( $return_string ) return $raintpl_contents; else echo $raintpl_contents; |
|
243 | + |
|
244 | + } |
|
245 | + |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * If exists a valid cache for this template it returns the cache |
|
252 | + * |
|
253 | + * @access public |
|
254 | + * @param string $tpl_name Name of template (set the same of draw) |
|
255 | + * @param int $expiration_time Set after how many seconds the cache expire and must be regenerated |
|
256 | + * @param string $cache_id Suffix to be used when writing file to cache (optional) |
|
257 | + * @return string it return the HTML or null if the cache must be recreated |
|
258 | + */ |
|
259 | + public function cache( $tpl_name, $expire_time = self::CACHE_EXPIRE_TIME, $cache_id = '' ){ |
|
260 | + |
|
261 | + // set the cache_id |
|
262 | + $this->cache_id = $cache_id; |
|
263 | + |
|
264 | + if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl['cache_filename'] ) && ( time() - filemtime( $this->tpl['cache_filename'] ) < $expire_time ) ) |
|
265 | + return substr( file_get_contents( $this->tpl['cache_filename'] ), 43 ); |
|
266 | + else{ |
|
267 | + //delete the cache of the selected template |
|
268 | + if (file_exists($this->tpl['cache_filename'])) |
|
269 | + unlink($this->tpl['cache_filename'] ); |
|
270 | + $this->cache = true; |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Configure the settings of RainTPL |
|
278 | + * |
|
279 | + * @access public |
|
280 | + * @static |
|
281 | + * @param array|string $setting array of settings or setting name |
|
282 | + * @param mixed $value content to set in the setting (optional) |
|
283 | + */ |
|
284 | + public static function configure( $setting, $value = null ){ |
|
285 | + if( is_array( $setting ) ) |
|
286 | + foreach( $setting as $key => $value ) |
|
287 | + self::configure( $key, $value ); |
|
288 | + else if( property_exists( __CLASS__, $setting ) ){ |
|
289 | + self::$$setting = $value; |
|
290 | + self::$config_name_sum[ $setting ] = $value; // take trace of all config |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Check if has to compile the template |
|
298 | + * |
|
299 | + * @access protected |
|
300 | + * @param string $tpl_name template name to check |
|
301 | + * @throws RainTpl_NotFoundException |
|
302 | + * @return bool return true if the template has changed |
|
303 | + */ |
|
304 | + protected function check_template( $tpl_name ){ |
|
305 | + |
|
306 | + if( !isset($this->tpl['checked']) ){ |
|
307 | + |
|
308 | + $tpl_basename = basename( $tpl_name ); // template basename |
|
309 | + $tpl_basedir = strpos($tpl_name,"/") ? dirname($tpl_name) . '/' : null; // template basedirectory |
|
310 | + $tpl_dir = PATH . self::$tpl_dir . $tpl_basedir; // template directory |
|
311 | + $this->tpl['tpl_filename'] = $tpl_dir . $tpl_basename . '.' . self::$tpl_ext; // template filename |
|
312 | + $temp_compiled_filename = PATH . self::$cache_dir . $tpl_basename . "." . md5( $tpl_dir . serialize(self::$config_name_sum)); |
|
313 | + $this->tpl['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename |
|
314 | + $this->tpl['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id . '.rtpl.php'; // static cache filename |
|
315 | + |
|
316 | + // if the template doesn't exsist throw an error |
|
317 | + if( self::$check_template_update && !file_exists( $this->tpl['tpl_filename'] ) ){ |
|
318 | + $e = new RainTpl_NotFoundException( 'Template '. $tpl_basename .' not found!' ); |
|
319 | + throw $e->setTemplateFile($this->tpl['tpl_filename']); |
|
320 | + } |
|
321 | + |
|
322 | + // file doesn't exsist, or the template was updated, Rain will compile the template |
|
323 | + if( !file_exists( $this->tpl['compiled_filename'] ) || ( self::$check_template_update && filemtime($this->tpl['compiled_filename']) < filemtime( $this->tpl['tpl_filename'] ) ) ){ |
|
324 | + $this->compileFile( $tpl_basedir, $this->tpl['tpl_filename'], PATH . self::$cache_dir, $this->tpl['compiled_filename'] ); |
|
325 | + return true; |
|
326 | + } |
|
327 | + $this->tpl['checked'] = true; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + |
|
333 | + /** |
|
334 | + * execute stripslaches() on the xml block. Invoqued by preg_replace_callback function below |
|
335 | + * |
|
336 | + * @access protected |
|
337 | + * @param string $capture |
|
338 | + * @return string |
|
339 | + */ |
|
340 | + protected function xml_reSubstitution($capture) { |
|
341 | + return "<?php echo '<?xml ".stripslashes($capture[1])." ?>'; ?>"; |
|
342 | + } |
|
343 | + |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * Compile and write the compiled template file |
|
348 | + * |
|
349 | + * @access protected |
|
350 | + * @param string $tpl_basedir |
|
351 | + * @param string $tpl_filename |
|
352 | + * @param string $cache_dir |
|
353 | + * @param string $compiled_filename |
|
354 | + * @throws RainTpl_Exception |
|
355 | + * @return void |
|
356 | + */ |
|
357 | + protected function compileFile( $tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename ){ |
|
358 | + |
|
359 | + //read template file |
|
360 | + $this->tpl['source'] = $template_code = file_get_contents( $tpl_filename ); |
|
361 | + |
|
362 | + //xml substitution |
|
363 | + $template_code = preg_replace( "/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code ); |
|
364 | + |
|
365 | + //disable php tag |
|
366 | + if( !self::$php_enabled ) |
|
367 | + $template_code = str_replace( array("<?","?>"), array("<?","?>"), $template_code ); |
|
368 | + |
|
369 | + //xml re-substitution |
|
370 | + $template_code = preg_replace_callback ( "/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code ); |
|
371 | + |
|
372 | + //compile template |
|
373 | + $template_compiled = "<?php if(!class_exists('raintpl')){exit;}?>" . $this->compileTemplate( $template_code, $tpl_basedir ); |
|
374 | + |
|
375 | + |
|
376 | + // fix the php-eating-newline-after-closing-tag-problem |
|
377 | + $template_compiled = str_replace( "?>\n", "?>\n\n", $template_compiled ); |
|
378 | + |
|
379 | + // create directories |
|
380 | + if( !is_dir( $cache_dir ) ) |
|
381 | + mkdir( $cache_dir, 0755, true ); |
|
382 | + |
|
383 | + if( !is_writable( $cache_dir ) ) |
|
384 | + throw new RainTpl_Exception ('Cache directory ' . $cache_dir . ' doesn\'t have write permission. Set write permission or set RAINTPL_CHECK_TEMPLATE_UPDATE to false. More details on http://www.raintpl.com/Documentation/Documentation-for-PHP-developers/Configuration/'); |
|
385 | + |
|
386 | + //write compiled file |
|
387 | + file_put_contents( $compiled_filename, $template_compiled ); |
|
388 | + } |
|
389 | + |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * Compile template |
|
394 | + * |
|
395 | + * @access protected |
|
396 | + * @param string $template_code |
|
397 | + * @param string $tpl_basedir |
|
398 | + * @return string |
|
399 | + */ |
|
400 | + protected function compileTemplate( $template_code, $tpl_basedir ){ |
|
401 | + |
|
402 | + //tag list |
|
403 | + $tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})', |
|
404 | + 'loop_close' => '(\{\/loop\})', |
|
405 | + 'if' => '(\{if(?: condition){0,1}="[^"]*"\})', |
|
406 | + 'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})', |
|
407 | + 'else' => '(\{else\})', |
|
408 | + 'if_close' => '(\{\/if\})', |
|
409 | + 'function' => '(\{function="[^"]*"\})', |
|
410 | + 'noparse' => '(\{noparse\})', |
|
411 | + 'noparse_close'=> '(\{\/noparse\})', |
|
412 | + 'ignore' => '(\{ignore\}|\{\*)', |
|
413 | + 'ignore_close' => '(\{\/ignore\}|\*\})', |
|
414 | + 'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})', |
|
415 | + 'template_info'=> '(\{\$template_info\})', |
|
416 | + 'function' => '(\{function="(\w*?)(?:.*?)"\})' |
|
417 | + ); |
|
418 | + |
|
419 | + $tag_regexp = "/" . join( "|", $tag_regexp ) . "/"; |
|
420 | + |
|
421 | + //split the code with the tags regexp |
|
422 | + $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
423 | + |
|
424 | + //path replace (src of img, background and href of link) |
|
425 | + $template_code = $this->path_replace( $template_code, $tpl_basedir ); |
|
426 | + |
|
427 | + //compile the code |
|
428 | + $compiled_code = $this->compileCode( $template_code ); |
|
429 | 429 | |
430 | - //return the compiled code |
|
431 | - return $compiled_code; |
|
432 | - |
|
433 | - } |
|
430 | + //return the compiled code |
|
431 | + return $compiled_code; |
|
432 | + |
|
433 | + } |
|
434 | 434 | |
435 | 435 | |
436 | 436 | |
437 | - /** |
|
438 | - * Compile the code |
|
439 | - * |
|
440 | - * @access protected |
|
441 | - * @param string $parsed_code |
|
442 | - * @throws RainTpl_SyntaxException |
|
443 | - * @return string |
|
444 | - */ |
|
445 | - protected function compileCode( $parsed_code ){ |
|
437 | + /** |
|
438 | + * Compile the code |
|
439 | + * |
|
440 | + * @access protected |
|
441 | + * @param string $parsed_code |
|
442 | + * @throws RainTpl_SyntaxException |
|
443 | + * @return string |
|
444 | + */ |
|
445 | + protected function compileCode( $parsed_code ){ |
|
446 | 446 | |
447 | - //variables initialization |
|
448 | - $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null; |
|
449 | - $loop_level = 0; |
|
447 | + //variables initialization |
|
448 | + $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null; |
|
449 | + $loop_level = 0; |
|
450 | 450 | |
451 | - //read all parsed code |
|
452 | - while( $html = array_shift( $parsed_code ) ){ |
|
451 | + //read all parsed code |
|
452 | + while( $html = array_shift( $parsed_code ) ){ |
|
453 | 453 | |
454 | - //close ignore tag |
|
455 | - if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) ) |
|
456 | - $ignore_is_open = false; |
|
454 | + //close ignore tag |
|
455 | + if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) ) |
|
456 | + $ignore_is_open = false; |
|
457 | 457 | |
458 | - //code between tag ignore id deleted |
|
459 | - elseif( $ignore_is_open ){ |
|
460 | - //ignore the code |
|
461 | - } |
|
458 | + //code between tag ignore id deleted |
|
459 | + elseif( $ignore_is_open ){ |
|
460 | + //ignore the code |
|
461 | + } |
|
462 | 462 | |
463 | - //close no parse tag |
|
464 | - elseif( strpos( $html, '{/noparse}' ) !== FALSE ) |
|
465 | - $comment_is_open = false; |
|
463 | + //close no parse tag |
|
464 | + elseif( strpos( $html, '{/noparse}' ) !== FALSE ) |
|
465 | + $comment_is_open = false; |
|
466 | 466 | |
467 | - //code between tag noparse is not compiled |
|
468 | - elseif( $comment_is_open ) |
|
469 | - $compiled_code .= $html; |
|
467 | + //code between tag noparse is not compiled |
|
468 | + elseif( $comment_is_open ) |
|
469 | + $compiled_code .= $html; |
|
470 | 470 | |
471 | - //ignore |
|
472 | - elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE ) |
|
473 | - $ignore_is_open = true; |
|
471 | + //ignore |
|
472 | + elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE ) |
|
473 | + $ignore_is_open = true; |
|
474 | 474 | |
475 | - //noparse |
|
476 | - elseif( strpos( $html, '{noparse}' ) !== FALSE ) |
|
477 | - $comment_is_open = true; |
|
475 | + //noparse |
|
476 | + elseif( strpos( $html, '{noparse}' ) !== FALSE ) |
|
477 | + $comment_is_open = true; |
|
478 | 478 | |
479 | - //include tag |
|
480 | - elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){ |
|
479 | + //include tag |
|
480 | + elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){ |
|
481 | 481 | |
482 | - //variables substitution |
|
483 | - $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level ); |
|
482 | + //variables substitution |
|
483 | + $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level ); |
|
484 | 484 | |
485 | - // if the cache is active |
|
486 | - if( isset($code[ 2 ]) ){ |
|
485 | + // if the cache is active |
|
486 | + if( isset($code[ 2 ]) ){ |
|
487 | 487 | |
488 | - //dynamic include |
|
489 | - $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
490 | - 'if( $cache = $tpl->cache( $template = basename("'.$include_var.'") ) )' . |
|
491 | - ' echo $cache;' . |
|
492 | - 'else{' . |
|
493 | - ' $tpl_dir_temp = self::$tpl_dir;' . |
|
494 | - ' $tpl->assign( $this->var );' . |
|
495 | - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
496 | - ' $tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
497 | - '} ?>'; |
|
498 | - } |
|
499 | - else{ |
|
488 | + //dynamic include |
|
489 | + $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
490 | + 'if( $cache = $tpl->cache( $template = basename("'.$include_var.'") ) )' . |
|
491 | + ' echo $cache;' . |
|
492 | + 'else{' . |
|
493 | + ' $tpl_dir_temp = self::$tpl_dir;' . |
|
494 | + ' $tpl->assign( $this->var );' . |
|
495 | + ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
496 | + ' $tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
497 | + '} ?>'; |
|
498 | + } |
|
499 | + else{ |
|
500 | 500 | |
501 | - //dynamic include |
|
502 | - $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
503 | - '$tpl_dir_temp = self::$tpl_dir;' . |
|
504 | - '$tpl->assign( $this->var );' . |
|
505 | - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
506 | - '$tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
507 | - '?>'; |
|
501 | + //dynamic include |
|
502 | + $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
503 | + '$tpl_dir_temp = self::$tpl_dir;' . |
|
504 | + '$tpl->assign( $this->var );' . |
|
505 | + ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
506 | + '$tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
507 | + '?>'; |
|
508 | 508 | |
509 | 509 | |
510 | - } |
|
510 | + } |
|
511 | 511 | |
512 | - } |
|
512 | + } |
|
513 | 513 | |
514 | - //loop |
|
515 | - elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){ |
|
514 | + //loop |
|
515 | + elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){ |
|
516 | 516 | |
517 | - //increase the loop counter |
|
518 | - $loop_level++; |
|
517 | + //increase the loop counter |
|
518 | + $loop_level++; |
|
519 | 519 | |
520 | - //replace the variable in the loop |
|
521 | - $var = $this->var_replace( '$' . $code[ 1 ], $tag_left_delimiter=null, $tag_right_delimiter=null, $php_left_delimiter=null, $php_right_delimiter=null, $loop_level-1 ); |
|
520 | + //replace the variable in the loop |
|
521 | + $var = $this->var_replace( '$' . $code[ 1 ], $tag_left_delimiter=null, $tag_right_delimiter=null, $php_left_delimiter=null, $php_right_delimiter=null, $loop_level-1 ); |
|
522 | 522 | |
523 | - //loop variables |
|
524 | - $counter = "\$counter$loop_level"; // count iteration |
|
525 | - $key = "\$key$loop_level"; // key |
|
526 | - $value = "\$value$loop_level"; // value |
|
523 | + //loop variables |
|
524 | + $counter = "\$counter$loop_level"; // count iteration |
|
525 | + $key = "\$key$loop_level"; // key |
|
526 | + $value = "\$value$loop_level"; // value |
|
527 | 527 | |
528 | - //loop code |
|
529 | - $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>"; |
|
528 | + //loop code |
|
529 | + $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>"; |
|
530 | 530 | |
531 | - } |
|
531 | + } |
|
532 | 532 | |
533 | - //close loop tag |
|
534 | - elseif( strpos( $html, '{/loop}' ) !== FALSE ) { |
|
533 | + //close loop tag |
|
534 | + elseif( strpos( $html, '{/loop}' ) !== FALSE ) { |
|
535 | 535 | |
536 | - //iterator |
|
537 | - $counter = "\$counter$loop_level"; |
|
536 | + //iterator |
|
537 | + $counter = "\$counter$loop_level"; |
|
538 | 538 | |
539 | - //decrease the loop counter |
|
540 | - $loop_level--; |
|
539 | + //decrease the loop counter |
|
540 | + $loop_level--; |
|
541 | 541 | |
542 | - //close loop code |
|
543 | - $compiled_code .= "<?php } ?>"; |
|
542 | + //close loop code |
|
543 | + $compiled_code .= "<?php } ?>"; |
|
544 | 544 | |
545 | - } |
|
545 | + } |
|
546 | 546 | |
547 | - //if |
|
548 | - elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
547 | + //if |
|
548 | + elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
549 | 549 | |
550 | - //increase open if counter (for intendation) |
|
551 | - $open_if++; |
|
550 | + //increase open if counter (for intendation) |
|
551 | + $open_if++; |
|
552 | 552 | |
553 | - //tag |
|
554 | - $tag = $code[ 0 ]; |
|
553 | + //tag |
|
554 | + $tag = $code[ 0 ]; |
|
555 | 555 | |
556 | - //condition attribute |
|
557 | - $condition = $code[ 1 ]; |
|
556 | + //condition attribute |
|
557 | + $condition = $code[ 1 ]; |
|
558 | 558 | |
559 | - // check if there's any function disabled by black_list |
|
560 | - $this->function_check( $tag ); |
|
559 | + // check if there's any function disabled by black_list |
|
560 | + $this->function_check( $tag ); |
|
561 | 561 | |
562 | - //variable substitution into condition (no delimiter into the condition) |
|
563 | - $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
562 | + //variable substitution into condition (no delimiter into the condition) |
|
563 | + $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
564 | 564 | |
565 | - //if code |
|
566 | - $compiled_code .= "<?php if( $parsed_condition ){ ?>"; |
|
565 | + //if code |
|
566 | + $compiled_code .= "<?php if( $parsed_condition ){ ?>"; |
|
567 | 567 | |
568 | - } |
|
568 | + } |
|
569 | 569 | |
570 | - //elseif |
|
571 | - elseif( preg_match( '/\{elseif(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
570 | + //elseif |
|
571 | + elseif( preg_match( '/\{elseif(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
572 | 572 | |
573 | - //tag |
|
574 | - $tag = $code[ 0 ]; |
|
573 | + //tag |
|
574 | + $tag = $code[ 0 ]; |
|
575 | 575 | |
576 | - //condition attribute |
|
577 | - $condition = $code[ 1 ]; |
|
576 | + //condition attribute |
|
577 | + $condition = $code[ 1 ]; |
|
578 | 578 | |
579 | - //variable substitution into condition (no delimiter into the condition) |
|
580 | - $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
579 | + //variable substitution into condition (no delimiter into the condition) |
|
580 | + $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
581 | 581 | |
582 | - //elseif code |
|
583 | - $compiled_code .= "<?php }elseif( $parsed_condition ){ ?>"; |
|
584 | - } |
|
582 | + //elseif code |
|
583 | + $compiled_code .= "<?php }elseif( $parsed_condition ){ ?>"; |
|
584 | + } |
|
585 | 585 | |
586 | - //else |
|
587 | - elseif( strpos( $html, '{else}' ) !== FALSE ) { |
|
586 | + //else |
|
587 | + elseif( strpos( $html, '{else}' ) !== FALSE ) { |
|
588 | 588 | |
589 | - //else code |
|
590 | - $compiled_code .= '<?php }else{ ?>'; |
|
589 | + //else code |
|
590 | + $compiled_code .= '<?php }else{ ?>'; |
|
591 | 591 | |
592 | - } |
|
592 | + } |
|
593 | 593 | |
594 | - //close if tag |
|
595 | - elseif( strpos( $html, '{/if}' ) !== FALSE ) { |
|
594 | + //close if tag |
|
595 | + elseif( strpos( $html, '{/if}' ) !== FALSE ) { |
|
596 | 596 | |
597 | - //decrease if counter |
|
598 | - $open_if--; |
|
597 | + //decrease if counter |
|
598 | + $open_if--; |
|
599 | 599 | |
600 | - // close if code |
|
601 | - $compiled_code .= '<?php } ?>'; |
|
600 | + // close if code |
|
601 | + $compiled_code .= '<?php } ?>'; |
|
602 | 602 | |
603 | - } |
|
603 | + } |
|
604 | 604 | |
605 | - //function |
|
606 | - elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){ |
|
605 | + //function |
|
606 | + elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){ |
|
607 | 607 | |
608 | - //tag |
|
609 | - $tag = $code[ 0 ]; |
|
608 | + //tag |
|
609 | + $tag = $code[ 0 ]; |
|
610 | 610 | |
611 | - //function |
|
612 | - $function = $code[ 1 ]; |
|
611 | + //function |
|
612 | + $function = $code[ 1 ]; |
|
613 | 613 | |
614 | - // check if there's any function disabled by black_list |
|
615 | - $this->function_check( $tag ); |
|
614 | + // check if there's any function disabled by black_list |
|
615 | + $this->function_check( $tag ); |
|
616 | 616 | |
617 | - if( empty( $code[ 2 ] ) ) |
|
618 | - $parsed_function = $function . "()"; |
|
619 | - else |
|
620 | - // parse the function |
|
621 | - $parsed_function = $function . $this->var_replace( $code[ 2 ], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
617 | + if( empty( $code[ 2 ] ) ) |
|
618 | + $parsed_function = $function . "()"; |
|
619 | + else |
|
620 | + // parse the function |
|
621 | + $parsed_function = $function . $this->var_replace( $code[ 2 ], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
622 | 622 | |
623 | - //if code |
|
624 | - $compiled_code .= "<?php echo $parsed_function; ?>"; |
|
625 | - } |
|
623 | + //if code |
|
624 | + $compiled_code .= "<?php echo $parsed_function; ?>"; |
|
625 | + } |
|
626 | 626 | |
627 | - // show all vars |
|
628 | - elseif ( strpos( $html, '{$template_info}' ) !== FALSE ) { |
|
627 | + // show all vars |
|
628 | + elseif ( strpos( $html, '{$template_info}' ) !== FALSE ) { |
|
629 | 629 | |
630 | - //tag |
|
631 | - $tag = '{$template_info}'; |
|
630 | + //tag |
|
631 | + $tag = '{$template_info}'; |
|
632 | 632 | |
633 | - //if code |
|
634 | - $compiled_code .= '<?php echo "<pre>"; print_r( $this->var ); echo "</pre>"; ?>'; |
|
635 | - } |
|
633 | + //if code |
|
634 | + $compiled_code .= '<?php echo "<pre>"; print_r( $this->var ); echo "</pre>"; ?>'; |
|
635 | + } |
|
636 | 636 | |
637 | 637 | |
638 | - //all html code |
|
639 | - else{ |
|
638 | + //all html code |
|
639 | + else{ |
|
640 | 640 | |
641 | - //variables substitution (es. {$title}) |
|
642 | - $html = $this->var_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
643 | - //const substitution (es. {#CONST#}) |
|
644 | - $html = $this->const_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
645 | - //functions substitution (es. {"string"|functions}) |
|
646 | - $compiled_code .= $this->func_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
647 | - } |
|
648 | - } |
|
641 | + //variables substitution (es. {$title}) |
|
642 | + $html = $this->var_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
643 | + //const substitution (es. {#CONST#}) |
|
644 | + $html = $this->const_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
645 | + //functions substitution (es. {"string"|functions}) |
|
646 | + $compiled_code .= $this->func_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
647 | + } |
|
648 | + } |
|
649 | 649 | |
650 | - if( $open_if > 0 ) { |
|
651 | - $e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $this->tpl['tpl_filename'] . ' template'); |
|
652 | - throw $e->setTemplateFile($this->tpl['tpl_filename']); |
|
653 | - } |
|
654 | - return $compiled_code; |
|
655 | - } |
|
650 | + if( $open_if > 0 ) { |
|
651 | + $e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $this->tpl['tpl_filename'] . ' template'); |
|
652 | + throw $e->setTemplateFile($this->tpl['tpl_filename']); |
|
653 | + } |
|
654 | + return $compiled_code; |
|
655 | + } |
|
656 | 656 | |
657 | 657 | |
658 | 658 | |
659 | - /** |
|
660 | - * Reduce a path |
|
661 | - * |
|
662 | - * eg. www/library/../filepath//file => www/filepath/file |
|
663 | - * |
|
664 | - * @param string $path |
|
665 | - * @return string |
|
666 | - */ |
|
667 | - protected function reduce_path( $path ){ |
|
668 | - $path = str_replace( "://", "@not_replace@", $path ); |
|
669 | - $path = str_replace( "//", "/", $path ); |
|
670 | - $path = str_replace( "@not_replace@", "://", $path ); |
|
671 | - return preg_replace('/\w+\/\.\.\//', '', $path ); |
|
672 | - } |
|
659 | + /** |
|
660 | + * Reduce a path |
|
661 | + * |
|
662 | + * eg. www/library/../filepath//file => www/filepath/file |
|
663 | + * |
|
664 | + * @param string $path |
|
665 | + * @return string |
|
666 | + */ |
|
667 | + protected function reduce_path( $path ){ |
|
668 | + $path = str_replace( "://", "@not_replace@", $path ); |
|
669 | + $path = str_replace( "//", "/", $path ); |
|
670 | + $path = str_replace( "@not_replace@", "://", $path ); |
|
671 | + return preg_replace('/\w+\/\.\.\//', '', $path ); |
|
672 | + } |
|
673 | 673 | |
674 | 674 | |
675 | 675 | |
676 | - /** |
|
677 | - * replace the path of image src, link href and a href |
|
678 | - * |
|
679 | - * url => template_dir/url |
|
680 | - * url# => url |
|
681 | - * http://url => http://url |
|
682 | - * |
|
683 | - * @access protected |
|
684 | - * @param string $html |
|
685 | - * @param string $tpl_basedir |
|
686 | - * @return string html substitution |
|
687 | - */ |
|
688 | - protected function path_replace( $html, $tpl_basedir ){ |
|
676 | + /** |
|
677 | + * replace the path of image src, link href and a href |
|
678 | + * |
|
679 | + * url => template_dir/url |
|
680 | + * url# => url |
|
681 | + * http://url => http://url |
|
682 | + * |
|
683 | + * @access protected |
|
684 | + * @param string $html |
|
685 | + * @param string $tpl_basedir |
|
686 | + * @return string html substitution |
|
687 | + */ |
|
688 | + protected function path_replace( $html, $tpl_basedir ){ |
|
689 | 689 | |
690 | - if( self::$path_replace ){ |
|
690 | + if( self::$path_replace ){ |
|
691 | 691 | |
692 | - $tpl_dir = self::$base_url . PATH . self::$tpl_dir . $tpl_basedir; |
|
692 | + $tpl_dir = self::$base_url . PATH . self::$tpl_dir . $tpl_basedir; |
|
693 | 693 | |
694 | - // reduce the path |
|
695 | - $path = $this->reduce_path($tpl_dir); |
|
694 | + // reduce the path |
|
695 | + $path = $this->reduce_path($tpl_dir); |
|
696 | 696 | |
697 | - $exp = $sub = array(); |
|
697 | + $exp = $sub = array(); |
|
698 | 698 | |
699 | - if( in_array( "img", self::$path_replace_list ) ){ |
|
700 | - $exp = array( '/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ); |
|
701 | - $sub = array( '<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"' ); |
|
702 | - } |
|
699 | + if( in_array( "img", self::$path_replace_list ) ){ |
|
700 | + $exp = array( '/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ); |
|
701 | + $sub = array( '<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"' ); |
|
702 | + } |
|
703 | 703 | |
704 | - if( in_array( "script", self::$path_replace_list ) ){ |
|
705 | - $exp = array_merge( $exp , array( '/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
706 | - $sub = array_merge( $sub , array( '<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"' ) ); |
|
707 | - } |
|
704 | + if( in_array( "script", self::$path_replace_list ) ){ |
|
705 | + $exp = array_merge( $exp , array( '/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
706 | + $sub = array_merge( $sub , array( '<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"' ) ); |
|
707 | + } |
|
708 | 708 | |
709 | - if( in_array( "link", self::$path_replace_list ) ){ |
|
710 | - $exp = array_merge( $exp , array( '/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
711 | - $sub = array_merge( $sub , array( '<link$1href=@$2://$3@', '<link$1href=@$2@' , '<link$1href="' . $path . '$2"', '<link$1href="$2"' ) ); |
|
712 | - } |
|
709 | + if( in_array( "link", self::$path_replace_list ) ){ |
|
710 | + $exp = array_merge( $exp , array( '/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
711 | + $sub = array_merge( $sub , array( '<link$1href=@$2://$3@', '<link$1href=@$2@' , '<link$1href="' . $path . '$2"', '<link$1href="$2"' ) ); |
|
712 | + } |
|
713 | 713 | |
714 | - if( in_array( "a", self::$path_replace_list ) ){ |
|
715 | - $exp = array_merge( $exp , array( '/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
716 | - $sub = array_merge( $sub , array( '<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"' ) ); |
|
717 | - } |
|
714 | + if( in_array( "a", self::$path_replace_list ) ){ |
|
715 | + $exp = array_merge( $exp , array( '/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
716 | + $sub = array_merge( $sub , array( '<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"' ) ); |
|
717 | + } |
|
718 | 718 | |
719 | - if( in_array( "input", self::$path_replace_list ) ){ |
|
720 | - $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
721 | - $sub = array_merge( $sub , array( '<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"' ) ); |
|
722 | - } |
|
719 | + if( in_array( "input", self::$path_replace_list ) ){ |
|
720 | + $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
721 | + $sub = array_merge( $sub , array( '<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"' ) ); |
|
722 | + } |
|
723 | 723 | |
724 | - return preg_replace( $exp, $sub, $html ); |
|
724 | + return preg_replace( $exp, $sub, $html ); |
|
725 | 725 | |
726 | - } |
|
727 | - else |
|
728 | - return $html; |
|
726 | + } |
|
727 | + else |
|
728 | + return $html; |
|
729 | 729 | |
730 | - } |
|
730 | + } |
|
731 | 731 | |
732 | 732 | |
733 | 733 | |
734 | - /** |
|
735 | - * replace constants |
|
736 | - * |
|
737 | - * @access public |
|
738 | - * @param string $html |
|
739 | - * @param string $tag_left_delimiter |
|
740 | - * @param string $tag_right_delimiter |
|
741 | - * @param string $php_left_delimiter (optional) |
|
742 | - * @param string $php_right_delimiter (optional) |
|
743 | - * @param string $loop_level (optional) |
|
744 | - * @param string $echo (optional) |
|
745 | - * @return string |
|
746 | - */ |
|
747 | - public function const_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
748 | - // const |
|
749 | - return preg_replace( '/\{\#(\w+)\#{0,1}\}/', $php_left_delimiter . ( $echo ? " echo " : null ) . '\\1' . $php_right_delimiter, $html ); |
|
750 | - } |
|
734 | + /** |
|
735 | + * replace constants |
|
736 | + * |
|
737 | + * @access public |
|
738 | + * @param string $html |
|
739 | + * @param string $tag_left_delimiter |
|
740 | + * @param string $tag_right_delimiter |
|
741 | + * @param string $php_left_delimiter (optional) |
|
742 | + * @param string $php_right_delimiter (optional) |
|
743 | + * @param string $loop_level (optional) |
|
744 | + * @param string $echo (optional) |
|
745 | + * @return string |
|
746 | + */ |
|
747 | + public function const_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
748 | + // const |
|
749 | + return preg_replace( '/\{\#(\w+)\#{0,1}\}/', $php_left_delimiter . ( $echo ? " echo " : null ) . '\\1' . $php_right_delimiter, $html ); |
|
750 | + } |
|
751 | 751 | |
752 | 752 | |
753 | 753 | |
754 | - /** |
|
755 | - * replace functions/modifiers on constants and strings |
|
756 | - * |
|
757 | - * @access public |
|
758 | - * @param string $html |
|
759 | - * @param string $tag_left_delimiter |
|
760 | - * @param string $tag_right_delimiter |
|
761 | - * @param string $php_left_delimiter (optional) |
|
762 | - * @param string $php_right_delimiter (optional) |
|
763 | - * @param string $loop_level (optional) |
|
764 | - * @param string $echo (optional) |
|
765 | - * @return string |
|
766 | - */ |
|
767 | - public function func_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
754 | + /** |
|
755 | + * replace functions/modifiers on constants and strings |
|
756 | + * |
|
757 | + * @access public |
|
758 | + * @param string $html |
|
759 | + * @param string $tag_left_delimiter |
|
760 | + * @param string $tag_right_delimiter |
|
761 | + * @param string $php_left_delimiter (optional) |
|
762 | + * @param string $php_right_delimiter (optional) |
|
763 | + * @param string $loop_level (optional) |
|
764 | + * @param string $echo (optional) |
|
765 | + * @return string |
|
766 | + */ |
|
767 | + public function func_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
768 | 768 | |
769 | - preg_match_all( '/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $html, $matches ); |
|
769 | + preg_match_all( '/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $html, $matches ); |
|
770 | 770 | |
771 | - for( $i=0, $n=count($matches[0]); $i<$n; $i++ ){ |
|
771 | + for( $i=0, $n=count($matches[0]); $i<$n; $i++ ){ |
|
772 | 772 | |
773 | - //complete tag ex: {$news.title|substr:0,100} |
|
774 | - $tag = $matches[ 0 ][ $i ]; |
|
773 | + //complete tag ex: {$news.title|substr:0,100} |
|
774 | + $tag = $matches[ 0 ][ $i ]; |
|
775 | 775 | |
776 | - //variable name ex: news.title |
|
777 | - $var = $matches[ 1 ][ $i ]; |
|
776 | + //variable name ex: news.title |
|
777 | + $var = $matches[ 1 ][ $i ]; |
|
778 | 778 | |
779 | - //function and parameters associate to the variable ex: substr:0,100 |
|
780 | - $extra_var = $matches[ 2 ][ $i ]; |
|
779 | + //function and parameters associate to the variable ex: substr:0,100 |
|
780 | + $extra_var = $matches[ 2 ][ $i ]; |
|
781 | 781 | |
782 | - // check if there's any function disabled by black_list |
|
783 | - $this->function_check( $tag ); |
|
782 | + // check if there's any function disabled by black_list |
|
783 | + $this->function_check( $tag ); |
|
784 | 784 | |
785 | - $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
785 | + $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
786 | 786 | |
787 | 787 | |
788 | - // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
789 | - $is_init_variable = preg_match( "/^(\s*?)\=[^=](.*?)$/", $extra_var ); |
|
788 | + // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
789 | + $is_init_variable = preg_match( "/^(\s*?)\=[^=](.*?)$/", $extra_var ); |
|
790 | 790 | |
791 | - //function associate to variable |
|
792 | - $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
791 | + //function associate to variable |
|
792 | + $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
793 | 793 | |
794 | - //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
795 | - $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
794 | + //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
795 | + $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
796 | 796 | |
797 | - //variable name |
|
798 | - $var_name = $temp[ 0 ]; |
|
797 | + //variable name |
|
798 | + $var_name = $temp[ 0 ]; |
|
799 | 799 | |
800 | - //variable path |
|
801 | - $variable_path = substr( $var, strlen( $var_name ) ); |
|
800 | + //variable path |
|
801 | + $variable_path = substr( $var, strlen( $var_name ) ); |
|
802 | 802 | |
803 | - //parentesis transform [ e ] in [" e in "] |
|
804 | - $variable_path = str_replace( '[', '["', $variable_path ); |
|
805 | - $variable_path = str_replace( ']', '"]', $variable_path ); |
|
803 | + //parentesis transform [ e ] in [" e in "] |
|
804 | + $variable_path = str_replace( '[', '["', $variable_path ); |
|
805 | + $variable_path = str_replace( ']', '"]', $variable_path ); |
|
806 | 806 | |
807 | - //transform .$variable in ["$variable"] |
|
808 | - $variable_path = preg_replace('/\.\$(\w+)/', '["$\\1"]', $variable_path ); |
|
807 | + //transform .$variable in ["$variable"] |
|
808 | + $variable_path = preg_replace('/\.\$(\w+)/', '["$\\1"]', $variable_path ); |
|
809 | 809 | |
810 | - //transform [variable] in ["variable"] |
|
811 | - $variable_path = preg_replace('/\.(\w+)/', '["\\1"]', $variable_path ); |
|
810 | + //transform [variable] in ["variable"] |
|
811 | + $variable_path = preg_replace('/\.(\w+)/', '["\\1"]', $variable_path ); |
|
812 | 812 | |
813 | - //if there's a function |
|
814 | - if( $function_var ){ |
|
813 | + //if there's a function |
|
814 | + if( $function_var ){ |
|
815 | 815 | |
816 | - // check if there's a function or a static method and separate, function by parameters |
|
817 | - $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
816 | + // check if there's a function or a static method and separate, function by parameters |
|
817 | + $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
818 | 818 | |
819 | - // get the position of the first : |
|
820 | - if( $dot_position = strpos( $function_var, ":" ) ){ |
|
819 | + // get the position of the first : |
|
820 | + if( $dot_position = strpos( $function_var, ":" ) ){ |
|
821 | 821 | |
822 | - // get the function and the parameters |
|
823 | - $function = substr( $function_var, 0, $dot_position ); |
|
824 | - $params = substr( $function_var, $dot_position+1 ); |
|
822 | + // get the function and the parameters |
|
823 | + $function = substr( $function_var, 0, $dot_position ); |
|
824 | + $params = substr( $function_var, $dot_position+1 ); |
|
825 | 825 | |
826 | - } |
|
827 | - else{ |
|
826 | + } |
|
827 | + else{ |
|
828 | 828 | |
829 | - //get the function |
|
830 | - $function = str_replace( "@double_dot@", "::", $function_var ); |
|
831 | - $params = null; |
|
829 | + //get the function |
|
830 | + $function = str_replace( "@double_dot@", "::", $function_var ); |
|
831 | + $params = null; |
|
832 | 832 | |
833 | - } |
|
833 | + } |
|
834 | 834 | |
835 | - // replace back the @double_dot@ with :: |
|
836 | - $function = str_replace( "@double_dot@", "::", $function ); |
|
837 | - $params = str_replace( "@double_dot@", "::", $params ); |
|
835 | + // replace back the @double_dot@ with :: |
|
836 | + $function = str_replace( "@double_dot@", "::", $function ); |
|
837 | + $params = str_replace( "@double_dot@", "::", $params ); |
|
838 | 838 | |
839 | 839 | |
840 | - } |
|
841 | - else |
|
842 | - $function = $params = null; |
|
840 | + } |
|
841 | + else |
|
842 | + $function = $params = null; |
|
843 | 843 | |
844 | - $php_var = $var_name . $variable_path; |
|
844 | + $php_var = $var_name . $variable_path; |
|
845 | 845 | |
846 | - // compile the variable for php |
|
847 | - if( isset( $function ) ){ |
|
848 | - if( $php_var ) |
|
849 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
850 | - else |
|
851 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $params ) )" : "$function()" ) . $php_right_delimiter; |
|
852 | - } |
|
853 | - else |
|
854 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
846 | + // compile the variable for php |
|
847 | + if( isset( $function ) ){ |
|
848 | + if( $php_var ) |
|
849 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
850 | + else |
|
851 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $params ) )" : "$function()" ) . $php_right_delimiter; |
|
852 | + } |
|
853 | + else |
|
854 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
855 | 855 | |
856 | - $html = str_replace( $tag, $php_var, $html ); |
|
856 | + $html = str_replace( $tag, $php_var, $html ); |
|
857 | 857 | |
858 | - } |
|
858 | + } |
|
859 | 859 | |
860 | - return $html; |
|
860 | + return $html; |
|
861 | 861 | |
862 | - } |
|
862 | + } |
|
863 | 863 | |
864 | 864 | |
865 | 865 | |
866 | - /** |
|
867 | - * replace variables |
|
868 | - * |
|
869 | - * @access public |
|
870 | - * @param string $html |
|
871 | - * @param string $tag_left_delimiter |
|
872 | - * @param string $tag_right_delimiter |
|
873 | - * @param string $php_left_delimiter (optional) |
|
874 | - * @param string $php_right_delimiter (optional) |
|
875 | - * @param string $loop_level (optional) |
|
876 | - * @param string $echo (optional) |
|
877 | - * @return string |
|
878 | - */ |
|
879 | - public function var_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
866 | + /** |
|
867 | + * replace variables |
|
868 | + * |
|
869 | + * @access public |
|
870 | + * @param string $html |
|
871 | + * @param string $tag_left_delimiter |
|
872 | + * @param string $tag_right_delimiter |
|
873 | + * @param string $php_left_delimiter (optional) |
|
874 | + * @param string $php_right_delimiter (optional) |
|
875 | + * @param string $loop_level (optional) |
|
876 | + * @param string $echo (optional) |
|
877 | + * @return string |
|
878 | + */ |
|
879 | + public function var_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
880 | 880 | |
881 | - //all variables |
|
882 | - if( preg_match_all( '/' . $tag_left_delimiter . '\$(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)' . $tag_right_delimiter . '/', $html, $matches ) ){ |
|
881 | + //all variables |
|
882 | + if( preg_match_all( '/' . $tag_left_delimiter . '\$(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)' . $tag_right_delimiter . '/', $html, $matches ) ){ |
|
883 | 883 | |
884 | - for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++ ) |
|
885 | - $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]); |
|
884 | + for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++ ) |
|
885 | + $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]); |
|
886 | 886 | |
887 | - foreach( $parsed as $tag => $array ){ |
|
887 | + foreach( $parsed as $tag => $array ){ |
|
888 | 888 | |
889 | - //variable name ex: news.title |
|
890 | - $var = $array['var']; |
|
889 | + //variable name ex: news.title |
|
890 | + $var = $array['var']; |
|
891 | 891 | |
892 | - //function and parameters associate to the variable ex: substr:0,100 |
|
893 | - $extra_var = $array['extra_var']; |
|
892 | + //function and parameters associate to the variable ex: substr:0,100 |
|
893 | + $extra_var = $array['extra_var']; |
|
894 | 894 | |
895 | - // check if there's any function disabled by black_list |
|
896 | - $this->function_check( $tag ); |
|
895 | + // check if there's any function disabled by black_list |
|
896 | + $this->function_check( $tag ); |
|
897 | 897 | |
898 | - $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
898 | + $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
899 | 899 | |
900 | - // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
901 | - $is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var ); |
|
900 | + // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
901 | + $is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var ); |
|
902 | 902 | |
903 | - //function associate to variable |
|
904 | - $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
903 | + //function associate to variable |
|
904 | + $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
905 | 905 | |
906 | - //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
907 | - $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
906 | + //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
907 | + $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
908 | 908 | |
909 | - //variable name |
|
910 | - $var_name = $temp[ 0 ]; |
|
909 | + //variable name |
|
910 | + $var_name = $temp[ 0 ]; |
|
911 | 911 | |
912 | - //variable path |
|
913 | - $variable_path = substr( $var, strlen( $var_name ) ); |
|
912 | + //variable path |
|
913 | + $variable_path = substr( $var, strlen( $var_name ) ); |
|
914 | 914 | |
915 | - //parentesis transform [ e ] in [" e in "] |
|
916 | - $variable_path = str_replace( '[', '["', $variable_path ); |
|
917 | - $variable_path = str_replace( ']', '"]', $variable_path ); |
|
915 | + //parentesis transform [ e ] in [" e in "] |
|
916 | + $variable_path = str_replace( '[', '["', $variable_path ); |
|
917 | + $variable_path = str_replace( ']', '"]', $variable_path ); |
|
918 | 918 | |
919 | - //transform .$variable in ["$variable"] and .variable in ["variable"] |
|
920 | - $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path ); |
|
919 | + //transform .$variable in ["$variable"] and .variable in ["variable"] |
|
920 | + $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path ); |
|
921 | 921 | |
922 | - // if is an assignment also assign the variable to $this->var['value'] |
|
923 | - if( $is_init_variable ) |
|
924 | - $extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var; |
|
922 | + // if is an assignment also assign the variable to $this->var['value'] |
|
923 | + if( $is_init_variable ) |
|
924 | + $extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var; |
|
925 | 925 | |
926 | 926 | |
927 | 927 | |
928 | - //if there's a function |
|
929 | - if( $function_var ){ |
|
928 | + //if there's a function |
|
929 | + if( $function_var ){ |
|
930 | 930 | |
931 | - // check if there's a function or a static method and separate, function by parameters |
|
932 | - $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
931 | + // check if there's a function or a static method and separate, function by parameters |
|
932 | + $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
933 | 933 | |
934 | 934 | |
935 | - // get the position of the first : |
|
936 | - if( $dot_position = strpos( $function_var, ":" ) ){ |
|
935 | + // get the position of the first : |
|
936 | + if( $dot_position = strpos( $function_var, ":" ) ){ |
|
937 | 937 | |
938 | - // get the function and the parameters |
|
939 | - $function = substr( $function_var, 0, $dot_position ); |
|
940 | - $params = substr( $function_var, $dot_position+1 ); |
|
938 | + // get the function and the parameters |
|
939 | + $function = substr( $function_var, 0, $dot_position ); |
|
940 | + $params = substr( $function_var, $dot_position+1 ); |
|
941 | 941 | |
942 | - } |
|
943 | - else{ |
|
942 | + } |
|
943 | + else{ |
|
944 | 944 | |
945 | - //get the function |
|
946 | - $function = str_replace( "@double_dot@", "::", $function_var ); |
|
947 | - $params = null; |
|
945 | + //get the function |
|
946 | + $function = str_replace( "@double_dot@", "::", $function_var ); |
|
947 | + $params = null; |
|
948 | 948 | |
949 | - } |
|
949 | + } |
|
950 | 950 | |
951 | - // replace back the @double_dot@ with :: |
|
952 | - $function = str_replace( "@double_dot@", "::", $function ); |
|
953 | - $params = str_replace( "@double_dot@", "::", $params ); |
|
954 | - } |
|
955 | - else |
|
956 | - $function = $params = null; |
|
951 | + // replace back the @double_dot@ with :: |
|
952 | + $function = str_replace( "@double_dot@", "::", $function ); |
|
953 | + $params = str_replace( "@double_dot@", "::", $params ); |
|
954 | + } |
|
955 | + else |
|
956 | + $function = $params = null; |
|
957 | 957 | |
958 | - //if it is inside a loop |
|
959 | - if( $loop_level ){ |
|
960 | - //verify the variable name |
|
961 | - if( $var_name == 'key' ) |
|
962 | - $php_var = '$key' . $loop_level; |
|
963 | - elseif( $var_name == 'value' ) |
|
964 | - $php_var = '$value' . $loop_level . $variable_path; |
|
965 | - elseif( $var_name == 'counter' ) |
|
966 | - $php_var = '$counter' . $loop_level; |
|
967 | - else |
|
968 | - $php_var = '$' . $var_name . $variable_path; |
|
969 | - }else |
|
970 | - $php_var = '$' . $var_name . $variable_path; |
|
971 | - |
|
972 | - // compile the variable for php |
|
973 | - if( isset( $function ) ) |
|
974 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
975 | - else |
|
976 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
977 | - |
|
978 | - $html = str_replace( $tag, $php_var, $html ); |
|
979 | - |
|
980 | - |
|
981 | - } |
|
982 | - } |
|
983 | - |
|
984 | - return $html; |
|
985 | - } |
|
986 | - |
|
987 | - |
|
988 | - |
|
989 | - /** |
|
990 | - * Check if function is in black list (sandbox) |
|
991 | - * |
|
992 | - * @access protected |
|
993 | - * @param string $code |
|
994 | - * @throws RainTpl_SyntaxException |
|
995 | - * @return void |
|
996 | - */ |
|
997 | - protected function function_check( $code ){ |
|
998 | - |
|
999 | - $preg = '#(\W|\s)' . implode( '(\W|\s)|(\W|\s)', self::$black_list ) . '(\W|\s)#'; |
|
1000 | - |
|
1001 | - // check if the function is in the black list (or not in white list) |
|
1002 | - if( count(self::$black_list) && preg_match( $preg, $code, $match ) ){ |
|
1003 | - |
|
1004 | - // find the line of the error |
|
1005 | - $line = 0; |
|
1006 | - $rows=explode("\n",$this->tpl['source']); |
|
1007 | - while( !strpos($rows[$line],$code) ) |
|
1008 | - $line++; |
|
1009 | - |
|
1010 | - // stop the execution of the script |
|
1011 | - $e = new RainTpl_SyntaxException('Unallowed syntax in ' . $this->tpl['tpl_filename'] . ' template'); |
|
1012 | - throw $e->setTemplateFile($this->tpl['tpl_filename']) |
|
1013 | - ->setTag($code) |
|
1014 | - ->setTemplateLine($line); |
|
1015 | - } |
|
1016 | - |
|
1017 | - } |
|
1018 | - |
|
1019 | - |
|
1020 | - |
|
1021 | - /** |
|
1022 | - * Prints debug info about exception or passes it further if debug is disabled. |
|
1023 | - * |
|
1024 | - * @access protected |
|
1025 | - * @param RainTpl_Exception $e |
|
1026 | - * @return string |
|
1027 | - */ |
|
1028 | - protected function printDebug(RainTpl_Exception $e){ |
|
1029 | - if (!self::$debug) { |
|
1030 | - throw $e; |
|
1031 | - } |
|
1032 | - $output = sprintf('<h2>Exception: %s</h2><h3>%s</h3><p>template: %s</p>', |
|
1033 | - get_class($e), |
|
1034 | - $e->getMessage(), |
|
1035 | - $e->getTemplateFile() |
|
1036 | - ); |
|
1037 | - if ($e instanceof RainTpl_SyntaxException) { |
|
1038 | - if (null !== $e->getTemplateLine()) { |
|
1039 | - $output .= '<p>line: ' . $e->getTemplateLine() . '</p>'; |
|
1040 | - } |
|
1041 | - if (null !== $e->getTag()) { |
|
1042 | - $output .= '<p>in tag: ' . htmlspecialchars($e->getTag()) . '</p>'; |
|
1043 | - } |
|
1044 | - if (null !== $e->getTemplateLine() && null !== $e->getTag()) { |
|
1045 | - $rows=explode("\n", htmlspecialchars($this->tpl['source'])); |
|
1046 | - $rows[$e->getTemplateLine()] = '<font color=red>' . $rows[$e->getTemplateLine()] . '</font>'; |
|
1047 | - $output .= '<h3>template code</h3>' . implode('<br />', $rows) . '</pre>'; |
|
1048 | - } |
|
1049 | - } |
|
1050 | - $output .= sprintf('<h3>trace</h3><p>In %s on line %d</p><pre>%s</pre>', |
|
1051 | - $e->getFile(), $e->getLine(), |
|
1052 | - nl2br(htmlspecialchars($e->getTraceAsString())) |
|
1053 | - ); |
|
1054 | - return $output; |
|
1055 | - } |
|
958 | + //if it is inside a loop |
|
959 | + if( $loop_level ){ |
|
960 | + //verify the variable name |
|
961 | + if( $var_name == 'key' ) |
|
962 | + $php_var = '$key' . $loop_level; |
|
963 | + elseif( $var_name == 'value' ) |
|
964 | + $php_var = '$value' . $loop_level . $variable_path; |
|
965 | + elseif( $var_name == 'counter' ) |
|
966 | + $php_var = '$counter' . $loop_level; |
|
967 | + else |
|
968 | + $php_var = '$' . $var_name . $variable_path; |
|
969 | + }else |
|
970 | + $php_var = '$' . $var_name . $variable_path; |
|
971 | + |
|
972 | + // compile the variable for php |
|
973 | + if( isset( $function ) ) |
|
974 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
975 | + else |
|
976 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
977 | + |
|
978 | + $html = str_replace( $tag, $php_var, $html ); |
|
979 | + |
|
980 | + |
|
981 | + } |
|
982 | + } |
|
983 | + |
|
984 | + return $html; |
|
985 | + } |
|
986 | + |
|
987 | + |
|
988 | + |
|
989 | + /** |
|
990 | + * Check if function is in black list (sandbox) |
|
991 | + * |
|
992 | + * @access protected |
|
993 | + * @param string $code |
|
994 | + * @throws RainTpl_SyntaxException |
|
995 | + * @return void |
|
996 | + */ |
|
997 | + protected function function_check( $code ){ |
|
998 | + |
|
999 | + $preg = '#(\W|\s)' . implode( '(\W|\s)|(\W|\s)', self::$black_list ) . '(\W|\s)#'; |
|
1000 | + |
|
1001 | + // check if the function is in the black list (or not in white list) |
|
1002 | + if( count(self::$black_list) && preg_match( $preg, $code, $match ) ){ |
|
1003 | + |
|
1004 | + // find the line of the error |
|
1005 | + $line = 0; |
|
1006 | + $rows=explode("\n",$this->tpl['source']); |
|
1007 | + while( !strpos($rows[$line],$code) ) |
|
1008 | + $line++; |
|
1009 | + |
|
1010 | + // stop the execution of the script |
|
1011 | + $e = new RainTpl_SyntaxException('Unallowed syntax in ' . $this->tpl['tpl_filename'] . ' template'); |
|
1012 | + throw $e->setTemplateFile($this->tpl['tpl_filename']) |
|
1013 | + ->setTag($code) |
|
1014 | + ->setTemplateLine($line); |
|
1015 | + } |
|
1016 | + |
|
1017 | + } |
|
1018 | + |
|
1019 | + |
|
1020 | + |
|
1021 | + /** |
|
1022 | + * Prints debug info about exception or passes it further if debug is disabled. |
|
1023 | + * |
|
1024 | + * @access protected |
|
1025 | + * @param RainTpl_Exception $e |
|
1026 | + * @return string |
|
1027 | + */ |
|
1028 | + protected function printDebug(RainTpl_Exception $e){ |
|
1029 | + if (!self::$debug) { |
|
1030 | + throw $e; |
|
1031 | + } |
|
1032 | + $output = sprintf('<h2>Exception: %s</h2><h3>%s</h3><p>template: %s</p>', |
|
1033 | + get_class($e), |
|
1034 | + $e->getMessage(), |
|
1035 | + $e->getTemplateFile() |
|
1036 | + ); |
|
1037 | + if ($e instanceof RainTpl_SyntaxException) { |
|
1038 | + if (null !== $e->getTemplateLine()) { |
|
1039 | + $output .= '<p>line: ' . $e->getTemplateLine() . '</p>'; |
|
1040 | + } |
|
1041 | + if (null !== $e->getTag()) { |
|
1042 | + $output .= '<p>in tag: ' . htmlspecialchars($e->getTag()) . '</p>'; |
|
1043 | + } |
|
1044 | + if (null !== $e->getTemplateLine() && null !== $e->getTag()) { |
|
1045 | + $rows=explode("\n", htmlspecialchars($this->tpl['source'])); |
|
1046 | + $rows[$e->getTemplateLine()] = '<font color=red>' . $rows[$e->getTemplateLine()] . '</font>'; |
|
1047 | + $output .= '<h3>template code</h3>' . implode('<br />', $rows) . '</pre>'; |
|
1048 | + } |
|
1049 | + } |
|
1050 | + $output .= sprintf('<h3>trace</h3><p>In %s on line %d</p><pre>%s</pre>', |
|
1051 | + $e->getFile(), $e->getLine(), |
|
1052 | + nl2br(htmlspecialchars($e->getTraceAsString())) |
|
1053 | + ); |
|
1054 | + return $output; |
|
1055 | + } |
|
1056 | 1056 | } |
1057 | 1057 | |
1058 | 1058 | |
@@ -1061,32 +1061,32 @@ discard block |
||
1061 | 1061 | * Basic Rain tpl exception. |
1062 | 1062 | */ |
1063 | 1063 | class RainTpl_Exception extends Exception{ |
1064 | - /** |
|
1065 | - * Path of template file with error. |
|
1066 | - */ |
|
1067 | - protected $templateFile = ''; |
|
1068 | - |
|
1069 | - /** |
|
1070 | - * Returns path of template file with error. |
|
1071 | - * |
|
1072 | - * @return string |
|
1073 | - */ |
|
1074 | - public function getTemplateFile() |
|
1075 | - { |
|
1076 | - return $this->templateFile; |
|
1077 | - } |
|
1078 | - |
|
1079 | - /** |
|
1080 | - * Sets path of template file with error. |
|
1081 | - * |
|
1082 | - * @param string $templateFile |
|
1083 | - * @return RainTpl_Exception |
|
1084 | - */ |
|
1085 | - public function setTemplateFile($templateFile) |
|
1086 | - { |
|
1087 | - $this->templateFile = (string) $templateFile; |
|
1088 | - return $this; |
|
1089 | - } |
|
1064 | + /** |
|
1065 | + * Path of template file with error. |
|
1066 | + */ |
|
1067 | + protected $templateFile = ''; |
|
1068 | + |
|
1069 | + /** |
|
1070 | + * Returns path of template file with error. |
|
1071 | + * |
|
1072 | + * @return string |
|
1073 | + */ |
|
1074 | + public function getTemplateFile() |
|
1075 | + { |
|
1076 | + return $this->templateFile; |
|
1077 | + } |
|
1078 | + |
|
1079 | + /** |
|
1080 | + * Sets path of template file with error. |
|
1081 | + * |
|
1082 | + * @param string $templateFile |
|
1083 | + * @return RainTpl_Exception |
|
1084 | + */ |
|
1085 | + public function setTemplateFile($templateFile) |
|
1086 | + { |
|
1087 | + $this->templateFile = (string) $templateFile; |
|
1088 | + return $this; |
|
1089 | + } |
|
1090 | 1090 | } |
1091 | 1091 | |
1092 | 1092 | |
@@ -1101,64 +1101,64 @@ discard block |
||
1101 | 1101 | * Exception thrown when syntax error occurs. |
1102 | 1102 | */ |
1103 | 1103 | class RainTpl_SyntaxException extends RainTpl_Exception{ |
1104 | - /** |
|
1105 | - * Line in template file where error has occured. |
|
1106 | - * |
|
1107 | - * @var int | null |
|
1108 | - */ |
|
1109 | - protected $templateLine = null; |
|
1110 | - |
|
1111 | - /** |
|
1112 | - * Tag which caused an error. |
|
1113 | - * |
|
1114 | - * @var string | null |
|
1115 | - */ |
|
1116 | - protected $tag = null; |
|
1117 | - |
|
1118 | - /** |
|
1119 | - * Returns line in template file where error has occured |
|
1120 | - * or null if line is not defined. |
|
1121 | - * |
|
1122 | - * @return int | null |
|
1123 | - */ |
|
1124 | - public function getTemplateLine() |
|
1125 | - { |
|
1126 | - return $this->templateLine; |
|
1127 | - } |
|
1128 | - |
|
1129 | - /** |
|
1130 | - * Sets line in template file where error has occured. |
|
1131 | - * |
|
1132 | - * @param int $templateLine |
|
1133 | - * @return RainTpl_SyntaxException |
|
1134 | - */ |
|
1135 | - public function setTemplateLine($templateLine) |
|
1136 | - { |
|
1137 | - $this->templateLine = (int) $templateLine; |
|
1138 | - return $this; |
|
1139 | - } |
|
1140 | - |
|
1141 | - /** |
|
1142 | - * Returns tag which caused an error. |
|
1143 | - * |
|
1144 | - * @return string |
|
1145 | - */ |
|
1146 | - public function getTag() |
|
1147 | - { |
|
1148 | - return $this->tag; |
|
1149 | - } |
|
1150 | - |
|
1151 | - /** |
|
1152 | - * Sets tag which caused an error. |
|
1153 | - * |
|
1154 | - * @param string $tag |
|
1155 | - * @return RainTpl_SyntaxException |
|
1156 | - */ |
|
1157 | - public function setTag($tag) |
|
1158 | - { |
|
1159 | - $this->tag = (string) $tag; |
|
1160 | - return $this; |
|
1161 | - } |
|
1104 | + /** |
|
1105 | + * Line in template file where error has occured. |
|
1106 | + * |
|
1107 | + * @var int | null |
|
1108 | + */ |
|
1109 | + protected $templateLine = null; |
|
1110 | + |
|
1111 | + /** |
|
1112 | + * Tag which caused an error. |
|
1113 | + * |
|
1114 | + * @var string | null |
|
1115 | + */ |
|
1116 | + protected $tag = null; |
|
1117 | + |
|
1118 | + /** |
|
1119 | + * Returns line in template file where error has occured |
|
1120 | + * or null if line is not defined. |
|
1121 | + * |
|
1122 | + * @return int | null |
|
1123 | + */ |
|
1124 | + public function getTemplateLine() |
|
1125 | + { |
|
1126 | + return $this->templateLine; |
|
1127 | + } |
|
1128 | + |
|
1129 | + /** |
|
1130 | + * Sets line in template file where error has occured. |
|
1131 | + * |
|
1132 | + * @param int $templateLine |
|
1133 | + * @return RainTpl_SyntaxException |
|
1134 | + */ |
|
1135 | + public function setTemplateLine($templateLine) |
|
1136 | + { |
|
1137 | + $this->templateLine = (int) $templateLine; |
|
1138 | + return $this; |
|
1139 | + } |
|
1140 | + |
|
1141 | + /** |
|
1142 | + * Returns tag which caused an error. |
|
1143 | + * |
|
1144 | + * @return string |
|
1145 | + */ |
|
1146 | + public function getTag() |
|
1147 | + { |
|
1148 | + return $this->tag; |
|
1149 | + } |
|
1150 | + |
|
1151 | + /** |
|
1152 | + * Sets tag which caused an error. |
|
1153 | + * |
|
1154 | + * @param string $tag |
|
1155 | + * @return RainTpl_SyntaxException |
|
1156 | + */ |
|
1157 | + public function setTag($tag) |
|
1158 | + { |
|
1159 | + $this->tag = (string) $tag; |
|
1160 | + return $this; |
|
1161 | + } |
|
1162 | 1162 | } |
1163 | 1163 | |
1164 | 1164 | // -- end |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | /** |
13 | 13 | * RainTPL |
14 | 14 | */ |
15 | -class RainTPL{ |
|
15 | +class RainTPL { |
|
16 | 16 | |
17 | 17 | // ------------------------- |
18 | 18 | // CONFIGURATION |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * |
72 | 72 | * @var array |
73 | 73 | */ |
74 | - static $path_replace_list = array( 'a', 'img', 'link', 'script', 'input' ); |
|
74 | + static $path_replace_list = array('a', 'img', 'link', 'script', 'input'); |
|
75 | 75 | |
76 | 76 | |
77 | 77 | /** |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * |
80 | 80 | * @var array |
81 | 81 | */ |
82 | - static $black_list = array( '\$this', 'raintpl::', 'self::', '_SESSION', '_SERVER', '_ENV', 'eval', 'exec', 'unlink', 'rmdir' ); |
|
82 | + static $black_list = array('\$this', 'raintpl::', 'self::', '_SESSION', '_SERVER', '_ENV', 'eval', 'exec', 'unlink', 'rmdir'); |
|
83 | 83 | |
84 | 84 | |
85 | 85 | /** |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | * |
134 | 134 | * @var array |
135 | 135 | */ |
136 | - protected $tpl = array(); // |
|
136 | + protected $tpl = array(); // |
|
137 | 137 | |
138 | 138 | /** |
139 | 139 | * static cache enabled / disabled |
@@ -177,11 +177,11 @@ discard block |
||
177 | 177 | * @param mixed $variable_name Name of template variable or associative array name/value |
178 | 178 | * @param mixed $value value assigned to this variable. Not set if variable_name is an associative array |
179 | 179 | */ |
180 | - public function assign( $variable, $value = null ){ |
|
181 | - if( is_array( $variable ) ) |
|
180 | + public function assign($variable, $value = null) { |
|
181 | + if (is_array($variable)) |
|
182 | 182 | $this->var += $variable; |
183 | 183 | else |
184 | - $this->var[ $variable ] = $value; |
|
184 | + $this->var[$variable] = $value; |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | |
@@ -196,11 +196,11 @@ discard block |
||
196 | 196 | * @param boolean $return_string true=return a string, false=echo the template |
197 | 197 | * @return string |
198 | 198 | */ |
199 | - public function draw( $tpl_name, $return_string = false ){ |
|
199 | + public function draw($tpl_name, $return_string = false) { |
|
200 | 200 | |
201 | 201 | try { |
202 | 202 | // compile the template if necessary and set the template filepath |
203 | - $this->check_template( $tpl_name ); |
|
203 | + $this->check_template($tpl_name); |
|
204 | 204 | } catch (RainTpl_Exception $e) { |
205 | 205 | $output = $this->printDebug($e); |
206 | 206 | die($output); |
@@ -209,37 +209,37 @@ discard block |
||
209 | 209 | // Cache is off and, return_string is false |
210 | 210 | // Rain just echo the template |
211 | 211 | |
212 | - if( !$this->cache && !$return_string ){ |
|
213 | - extract( $this->var ); |
|
212 | + if (!$this->cache && !$return_string) { |
|
213 | + extract($this->var); |
|
214 | 214 | include $this->tpl['compiled_filename']; |
215 | - unset( $this->tpl ); |
|
215 | + unset($this->tpl); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | |
219 | 219 | // cache or return_string are enabled |
220 | 220 | // rain get the output buffer to save the output in the cache or to return it as string |
221 | 221 | |
222 | - else{ |
|
222 | + else { |
|
223 | 223 | |
224 | 224 | //---------------------- |
225 | 225 | // get the output buffer |
226 | 226 | //---------------------- |
227 | 227 | ob_start(); |
228 | - extract( $this->var ); |
|
228 | + extract($this->var); |
|
229 | 229 | include $this->tpl['compiled_filename']; |
230 | 230 | $raintpl_contents = ob_get_clean(); |
231 | 231 | //---------------------- |
232 | 232 | |
233 | 233 | |
234 | 234 | // save the output in the cache |
235 | - if( $this->cache ) |
|
236 | - file_put_contents( $this->tpl['cache_filename'], "<?php if(!class_exists('raintpl')){exit;}?>" . $raintpl_contents ); |
|
235 | + if ($this->cache) |
|
236 | + file_put_contents($this->tpl['cache_filename'], "<?php if(!class_exists('raintpl')){exit;}?>" . $raintpl_contents); |
|
237 | 237 | |
238 | 238 | // free memory |
239 | - unset( $this->tpl ); |
|
239 | + unset($this->tpl); |
|
240 | 240 | |
241 | 241 | // return or print the template |
242 | - if( $return_string ) return $raintpl_contents; else echo $raintpl_contents; |
|
242 | + if ($return_string) return $raintpl_contents; else echo $raintpl_contents; |
|
243 | 243 | |
244 | 244 | } |
245 | 245 | |
@@ -256,17 +256,17 @@ discard block |
||
256 | 256 | * @param string $cache_id Suffix to be used when writing file to cache (optional) |
257 | 257 | * @return string it return the HTML or null if the cache must be recreated |
258 | 258 | */ |
259 | - public function cache( $tpl_name, $expire_time = self::CACHE_EXPIRE_TIME, $cache_id = '' ){ |
|
259 | + public function cache($tpl_name, $expire_time = self::CACHE_EXPIRE_TIME, $cache_id = '') { |
|
260 | 260 | |
261 | 261 | // set the cache_id |
262 | 262 | $this->cache_id = $cache_id; |
263 | 263 | |
264 | - if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl['cache_filename'] ) && ( time() - filemtime( $this->tpl['cache_filename'] ) < $expire_time ) ) |
|
265 | - return substr( file_get_contents( $this->tpl['cache_filename'] ), 43 ); |
|
266 | - else{ |
|
264 | + if (!$this->check_template($tpl_name) && file_exists($this->tpl['cache_filename']) && (time()-filemtime($this->tpl['cache_filename']) < $expire_time)) |
|
265 | + return substr(file_get_contents($this->tpl['cache_filename']), 43); |
|
266 | + else { |
|
267 | 267 | //delete the cache of the selected template |
268 | 268 | if (file_exists($this->tpl['cache_filename'])) |
269 | - unlink($this->tpl['cache_filename'] ); |
|
269 | + unlink($this->tpl['cache_filename']); |
|
270 | 270 | $this->cache = true; |
271 | 271 | } |
272 | 272 | } |
@@ -281,13 +281,13 @@ discard block |
||
281 | 281 | * @param array|string $setting array of settings or setting name |
282 | 282 | * @param mixed $value content to set in the setting (optional) |
283 | 283 | */ |
284 | - public static function configure( $setting, $value = null ){ |
|
285 | - if( is_array( $setting ) ) |
|
286 | - foreach( $setting as $key => $value ) |
|
287 | - self::configure( $key, $value ); |
|
288 | - else if( property_exists( __CLASS__, $setting ) ){ |
|
284 | + public static function configure($setting, $value = null) { |
|
285 | + if (is_array($setting)) |
|
286 | + foreach ($setting as $key => $value) |
|
287 | + self::configure($key, $value); |
|
288 | + else if (property_exists(__CLASS__, $setting)) { |
|
289 | 289 | self::$$setting = $value; |
290 | - self::$config_name_sum[ $setting ] = $value; // take trace of all config |
|
290 | + self::$config_name_sum[$setting] = $value; // take trace of all config |
|
291 | 291 | } |
292 | 292 | } |
293 | 293 | |
@@ -301,27 +301,27 @@ discard block |
||
301 | 301 | * @throws RainTpl_NotFoundException |
302 | 302 | * @return bool return true if the template has changed |
303 | 303 | */ |
304 | - protected function check_template( $tpl_name ){ |
|
304 | + protected function check_template($tpl_name) { |
|
305 | 305 | |
306 | - if( !isset($this->tpl['checked']) ){ |
|
306 | + if (!isset($this->tpl['checked'])) { |
|
307 | 307 | |
308 | - $tpl_basename = basename( $tpl_name ); // template basename |
|
309 | - $tpl_basedir = strpos($tpl_name,"/") ? dirname($tpl_name) . '/' : null; // template basedirectory |
|
310 | - $tpl_dir = PATH . self::$tpl_dir . $tpl_basedir; // template directory |
|
311 | - $this->tpl['tpl_filename'] = $tpl_dir . $tpl_basename . '.' . self::$tpl_ext; // template filename |
|
312 | - $temp_compiled_filename = PATH . self::$cache_dir . $tpl_basename . "." . md5( $tpl_dir . serialize(self::$config_name_sum)); |
|
313 | - $this->tpl['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename |
|
314 | - $this->tpl['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id . '.rtpl.php'; // static cache filename |
|
308 | + $tpl_basename = basename($tpl_name); // template basename |
|
309 | + $tpl_basedir = strpos($tpl_name, "/") ? dirname($tpl_name) . '/' : null; // template basedirectory |
|
310 | + $tpl_dir = PATH . self::$tpl_dir . $tpl_basedir; // template directory |
|
311 | + $this->tpl['tpl_filename'] = $tpl_dir . $tpl_basename . '.' . self::$tpl_ext; // template filename |
|
312 | + $temp_compiled_filename = PATH . self::$cache_dir . $tpl_basename . "." . md5($tpl_dir . serialize(self::$config_name_sum)); |
|
313 | + $this->tpl['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename |
|
314 | + $this->tpl['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id . '.rtpl.php'; // static cache filename |
|
315 | 315 | |
316 | 316 | // if the template doesn't exsist throw an error |
317 | - if( self::$check_template_update && !file_exists( $this->tpl['tpl_filename'] ) ){ |
|
318 | - $e = new RainTpl_NotFoundException( 'Template '. $tpl_basename .' not found!' ); |
|
317 | + if (self::$check_template_update && !file_exists($this->tpl['tpl_filename'])) { |
|
318 | + $e = new RainTpl_NotFoundException('Template ' . $tpl_basename . ' not found!'); |
|
319 | 319 | throw $e->setTemplateFile($this->tpl['tpl_filename']); |
320 | 320 | } |
321 | 321 | |
322 | 322 | // file doesn't exsist, or the template was updated, Rain will compile the template |
323 | - if( !file_exists( $this->tpl['compiled_filename'] ) || ( self::$check_template_update && filemtime($this->tpl['compiled_filename']) < filemtime( $this->tpl['tpl_filename'] ) ) ){ |
|
324 | - $this->compileFile( $tpl_basedir, $this->tpl['tpl_filename'], PATH . self::$cache_dir, $this->tpl['compiled_filename'] ); |
|
323 | + if (!file_exists($this->tpl['compiled_filename']) || (self::$check_template_update && filemtime($this->tpl['compiled_filename']) < filemtime($this->tpl['tpl_filename']))) { |
|
324 | + $this->compileFile($tpl_basedir, $this->tpl['tpl_filename'], PATH . self::$cache_dir, $this->tpl['compiled_filename']); |
|
325 | 325 | return true; |
326 | 326 | } |
327 | 327 | $this->tpl['checked'] = true; |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | * @return string |
339 | 339 | */ |
340 | 340 | protected function xml_reSubstitution($capture) { |
341 | - return "<?php echo '<?xml ".stripslashes($capture[1])." ?>'; ?>"; |
|
341 | + return "<?php echo '<?xml " . stripslashes($capture[1]) . " ?>'; ?>"; |
|
342 | 342 | } |
343 | 343 | |
344 | 344 | |
@@ -354,37 +354,37 @@ discard block |
||
354 | 354 | * @throws RainTpl_Exception |
355 | 355 | * @return void |
356 | 356 | */ |
357 | - protected function compileFile( $tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename ){ |
|
357 | + protected function compileFile($tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename) { |
|
358 | 358 | |
359 | 359 | //read template file |
360 | - $this->tpl['source'] = $template_code = file_get_contents( $tpl_filename ); |
|
360 | + $this->tpl['source'] = $template_code = file_get_contents($tpl_filename); |
|
361 | 361 | |
362 | 362 | //xml substitution |
363 | - $template_code = preg_replace( "/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code ); |
|
363 | + $template_code = preg_replace("/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code); |
|
364 | 364 | |
365 | 365 | //disable php tag |
366 | - if( !self::$php_enabled ) |
|
367 | - $template_code = str_replace( array("<?","?>"), array("<?","?>"), $template_code ); |
|
366 | + if (!self::$php_enabled) |
|
367 | + $template_code = str_replace(array("<?", "?>"), array("<?", "?>"), $template_code); |
|
368 | 368 | |
369 | 369 | //xml re-substitution |
370 | - $template_code = preg_replace_callback ( "/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code ); |
|
370 | + $template_code = preg_replace_callback("/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code); |
|
371 | 371 | |
372 | 372 | //compile template |
373 | - $template_compiled = "<?php if(!class_exists('raintpl')){exit;}?>" . $this->compileTemplate( $template_code, $tpl_basedir ); |
|
373 | + $template_compiled = "<?php if(!class_exists('raintpl')){exit;}?>" . $this->compileTemplate($template_code, $tpl_basedir); |
|
374 | 374 | |
375 | 375 | |
376 | 376 | // fix the php-eating-newline-after-closing-tag-problem |
377 | - $template_compiled = str_replace( "?>\n", "?>\n\n", $template_compiled ); |
|
377 | + $template_compiled = str_replace("?>\n", "?>\n\n", $template_compiled); |
|
378 | 378 | |
379 | 379 | // create directories |
380 | - if( !is_dir( $cache_dir ) ) |
|
381 | - mkdir( $cache_dir, 0755, true ); |
|
380 | + if (!is_dir($cache_dir)) |
|
381 | + mkdir($cache_dir, 0755, true); |
|
382 | 382 | |
383 | - if( !is_writable( $cache_dir ) ) |
|
384 | - throw new RainTpl_Exception ('Cache directory ' . $cache_dir . ' doesn\'t have write permission. Set write permission or set RAINTPL_CHECK_TEMPLATE_UPDATE to false. More details on http://www.raintpl.com/Documentation/Documentation-for-PHP-developers/Configuration/'); |
|
383 | + if (!is_writable($cache_dir)) |
|
384 | + throw new RainTpl_Exception('Cache directory ' . $cache_dir . ' doesn\'t have write permission. Set write permission or set RAINTPL_CHECK_TEMPLATE_UPDATE to false. More details on http://www.raintpl.com/Documentation/Documentation-for-PHP-developers/Configuration/'); |
|
385 | 385 | |
386 | 386 | //write compiled file |
387 | - file_put_contents( $compiled_filename, $template_compiled ); |
|
387 | + file_put_contents($compiled_filename, $template_compiled); |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | |
@@ -397,10 +397,10 @@ discard block |
||
397 | 397 | * @param string $tpl_basedir |
398 | 398 | * @return string |
399 | 399 | */ |
400 | - protected function compileTemplate( $template_code, $tpl_basedir ){ |
|
400 | + protected function compileTemplate($template_code, $tpl_basedir) { |
|
401 | 401 | |
402 | 402 | //tag list |
403 | - $tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})', |
|
403 | + $tag_regexp = array('loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})', |
|
404 | 404 | 'loop_close' => '(\{\/loop\})', |
405 | 405 | 'if' => '(\{if(?: condition){0,1}="[^"]*"\})', |
406 | 406 | 'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})', |
@@ -416,16 +416,16 @@ discard block |
||
416 | 416 | 'function' => '(\{function="(\w*?)(?:.*?)"\})' |
417 | 417 | ); |
418 | 418 | |
419 | - $tag_regexp = "/" . join( "|", $tag_regexp ) . "/"; |
|
419 | + $tag_regexp = "/" . join("|", $tag_regexp) . "/"; |
|
420 | 420 | |
421 | 421 | //split the code with the tags regexp |
422 | - $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
422 | + $template_code = preg_split($tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
|
423 | 423 | |
424 | 424 | //path replace (src of img, background and href of link) |
425 | - $template_code = $this->path_replace( $template_code, $tpl_basedir ); |
|
425 | + $template_code = $this->path_replace($template_code, $tpl_basedir); |
|
426 | 426 | |
427 | 427 | //compile the code |
428 | - $compiled_code = $this->compileCode( $template_code ); |
|
428 | + $compiled_code = $this->compileCode($template_code); |
|
429 | 429 | |
430 | 430 | //return the compiled code |
431 | 431 | return $compiled_code; |
@@ -442,68 +442,68 @@ discard block |
||
442 | 442 | * @throws RainTpl_SyntaxException |
443 | 443 | * @return string |
444 | 444 | */ |
445 | - protected function compileCode( $parsed_code ){ |
|
445 | + protected function compileCode($parsed_code) { |
|
446 | 446 | |
447 | 447 | //variables initialization |
448 | 448 | $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null; |
449 | 449 | $loop_level = 0; |
450 | 450 | |
451 | 451 | //read all parsed code |
452 | - while( $html = array_shift( $parsed_code ) ){ |
|
452 | + while ($html = array_shift($parsed_code)) { |
|
453 | 453 | |
454 | 454 | //close ignore tag |
455 | - if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) ) |
|
455 | + if (!$comment_is_open && (strpos($html, '{/ignore}') !== FALSE || strpos($html, '*}') !== FALSE)) |
|
456 | 456 | $ignore_is_open = false; |
457 | 457 | |
458 | 458 | //code between tag ignore id deleted |
459 | - elseif( $ignore_is_open ){ |
|
459 | + elseif ($ignore_is_open) { |
|
460 | 460 | //ignore the code |
461 | 461 | } |
462 | 462 | |
463 | 463 | //close no parse tag |
464 | - elseif( strpos( $html, '{/noparse}' ) !== FALSE ) |
|
464 | + elseif (strpos($html, '{/noparse}') !== FALSE) |
|
465 | 465 | $comment_is_open = false; |
466 | 466 | |
467 | 467 | //code between tag noparse is not compiled |
468 | - elseif( $comment_is_open ) |
|
468 | + elseif ($comment_is_open) |
|
469 | 469 | $compiled_code .= $html; |
470 | 470 | |
471 | 471 | //ignore |
472 | - elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE ) |
|
472 | + elseif (strpos($html, '{ignore}') !== FALSE || strpos($html, '{*') !== FALSE) |
|
473 | 473 | $ignore_is_open = true; |
474 | 474 | |
475 | 475 | //noparse |
476 | - elseif( strpos( $html, '{noparse}' ) !== FALSE ) |
|
476 | + elseif (strpos($html, '{noparse}') !== FALSE) |
|
477 | 477 | $comment_is_open = true; |
478 | 478 | |
479 | 479 | //include tag |
480 | - elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){ |
|
480 | + elseif (preg_match('/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code)) { |
|
481 | 481 | |
482 | 482 | //variables substitution |
483 | - $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level ); |
|
483 | + $include_var = $this->var_replace($code[1], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".', $php_right_delimiter = '."', $loop_level); |
|
484 | 484 | |
485 | 485 | // if the cache is active |
486 | - if( isset($code[ 2 ]) ){ |
|
486 | + if (isset($code[2])) { |
|
487 | 487 | |
488 | 488 | //dynamic include |
489 | - $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
490 | - 'if( $cache = $tpl->cache( $template = basename("'.$include_var.'") ) )' . |
|
489 | + $compiled_code .= '<?php $tpl = new ' . get_class($this) . ';' . |
|
490 | + 'if( $cache = $tpl->cache( $template = basename("' . $include_var . '") ) )' . |
|
491 | 491 | ' echo $cache;' . |
492 | 492 | 'else{' . |
493 | 493 | ' $tpl_dir_temp = self::$tpl_dir;' . |
494 | 494 | ' $tpl->assign( $this->var );' . |
495 | - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
496 | - ' $tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
495 | + (!$loop_level ? null : '$tpl->assign( "key", $key' . $loop_level . ' ); $tpl->assign( "value", $value' . $loop_level . ' );') . |
|
496 | + ' $tpl->draw( dirname("' . $include_var . '") . ( substr("' . $include_var . '",-1,1) != "/" ? "/" : "" ) . basename("' . $include_var . '") );' . |
|
497 | 497 | '} ?>'; |
498 | 498 | } |
499 | - else{ |
|
499 | + else { |
|
500 | 500 | |
501 | 501 | //dynamic include |
502 | - $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
502 | + $compiled_code .= '<?php $tpl = new ' . get_class($this) . ';' . |
|
503 | 503 | '$tpl_dir_temp = self::$tpl_dir;' . |
504 | 504 | '$tpl->assign( $this->var );' . |
505 | - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
506 | - '$tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
505 | + (!$loop_level ? null : '$tpl->assign( "key", $key' . $loop_level . ' ); $tpl->assign( "value", $value' . $loop_level . ' );') . |
|
506 | + '$tpl->draw( dirname("' . $include_var . '") . ( substr("' . $include_var . '",-1,1) != "/" ? "/" : "" ) . basename("' . $include_var . '") );' . |
|
507 | 507 | '?>'; |
508 | 508 | |
509 | 509 | |
@@ -512,26 +512,26 @@ discard block |
||
512 | 512 | } |
513 | 513 | |
514 | 514 | //loop |
515 | - elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){ |
|
515 | + elseif (preg_match('/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code)) { |
|
516 | 516 | |
517 | 517 | //increase the loop counter |
518 | 518 | $loop_level++; |
519 | 519 | |
520 | 520 | //replace the variable in the loop |
521 | - $var = $this->var_replace( '$' . $code[ 1 ], $tag_left_delimiter=null, $tag_right_delimiter=null, $php_left_delimiter=null, $php_right_delimiter=null, $loop_level-1 ); |
|
521 | + $var = $this->var_replace('$' . $code[1], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level-1); |
|
522 | 522 | |
523 | 523 | //loop variables |
524 | - $counter = "\$counter$loop_level"; // count iteration |
|
525 | - $key = "\$key$loop_level"; // key |
|
526 | - $value = "\$value$loop_level"; // value |
|
524 | + $counter = "\$counter$loop_level"; // count iteration |
|
525 | + $key = "\$key$loop_level"; // key |
|
526 | + $value = "\$value$loop_level"; // value |
|
527 | 527 | |
528 | 528 | //loop code |
529 | - $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>"; |
|
529 | + $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>"; |
|
530 | 530 | |
531 | 531 | } |
532 | 532 | |
533 | 533 | //close loop tag |
534 | - elseif( strpos( $html, '{/loop}' ) !== FALSE ) { |
|
534 | + elseif (strpos($html, '{/loop}') !== FALSE) { |
|
535 | 535 | |
536 | 536 | //iterator |
537 | 537 | $counter = "\$counter$loop_level"; |
@@ -540,114 +540,114 @@ discard block |
||
540 | 540 | $loop_level--; |
541 | 541 | |
542 | 542 | //close loop code |
543 | - $compiled_code .= "<?php } ?>"; |
|
543 | + $compiled_code .= "<?php } ?>"; |
|
544 | 544 | |
545 | 545 | } |
546 | 546 | |
547 | 547 | //if |
548 | - elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
548 | + elseif (preg_match('/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code)) { |
|
549 | 549 | |
550 | 550 | //increase open if counter (for intendation) |
551 | 551 | $open_if++; |
552 | 552 | |
553 | 553 | //tag |
554 | - $tag = $code[ 0 ]; |
|
554 | + $tag = $code[0]; |
|
555 | 555 | |
556 | 556 | //condition attribute |
557 | - $condition = $code[ 1 ]; |
|
557 | + $condition = $code[1]; |
|
558 | 558 | |
559 | 559 | // check if there's any function disabled by black_list |
560 | - $this->function_check( $tag ); |
|
560 | + $this->function_check($tag); |
|
561 | 561 | |
562 | 562 | //variable substitution into condition (no delimiter into the condition) |
563 | - $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
563 | + $parsed_condition = $this->var_replace($condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level); |
|
564 | 564 | |
565 | 565 | //if code |
566 | - $compiled_code .= "<?php if( $parsed_condition ){ ?>"; |
|
566 | + $compiled_code .= "<?php if( $parsed_condition ){ ?>"; |
|
567 | 567 | |
568 | 568 | } |
569 | 569 | |
570 | 570 | //elseif |
571 | - elseif( preg_match( '/\{elseif(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
571 | + elseif (preg_match('/\{elseif(?: condition){0,1}="([^"]*)"\}/', $html, $code)) { |
|
572 | 572 | |
573 | 573 | //tag |
574 | - $tag = $code[ 0 ]; |
|
574 | + $tag = $code[0]; |
|
575 | 575 | |
576 | 576 | //condition attribute |
577 | - $condition = $code[ 1 ]; |
|
577 | + $condition = $code[1]; |
|
578 | 578 | |
579 | 579 | //variable substitution into condition (no delimiter into the condition) |
580 | - $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
580 | + $parsed_condition = $this->var_replace($condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level); |
|
581 | 581 | |
582 | 582 | //elseif code |
583 | - $compiled_code .= "<?php }elseif( $parsed_condition ){ ?>"; |
|
583 | + $compiled_code .= "<?php }elseif( $parsed_condition ){ ?>"; |
|
584 | 584 | } |
585 | 585 | |
586 | 586 | //else |
587 | - elseif( strpos( $html, '{else}' ) !== FALSE ) { |
|
587 | + elseif (strpos($html, '{else}') !== FALSE) { |
|
588 | 588 | |
589 | 589 | //else code |
590 | - $compiled_code .= '<?php }else{ ?>'; |
|
590 | + $compiled_code .= '<?php }else{ ?>'; |
|
591 | 591 | |
592 | 592 | } |
593 | 593 | |
594 | 594 | //close if tag |
595 | - elseif( strpos( $html, '{/if}' ) !== FALSE ) { |
|
595 | + elseif (strpos($html, '{/if}') !== FALSE) { |
|
596 | 596 | |
597 | 597 | //decrease if counter |
598 | 598 | $open_if--; |
599 | 599 | |
600 | 600 | // close if code |
601 | - $compiled_code .= '<?php } ?>'; |
|
601 | + $compiled_code .= '<?php } ?>'; |
|
602 | 602 | |
603 | 603 | } |
604 | 604 | |
605 | 605 | //function |
606 | - elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){ |
|
606 | + elseif (preg_match('/\{function="(\w*)(.*?)"\}/', $html, $code)) { |
|
607 | 607 | |
608 | 608 | //tag |
609 | - $tag = $code[ 0 ]; |
|
609 | + $tag = $code[0]; |
|
610 | 610 | |
611 | 611 | //function |
612 | - $function = $code[ 1 ]; |
|
612 | + $function = $code[1]; |
|
613 | 613 | |
614 | 614 | // check if there's any function disabled by black_list |
615 | - $this->function_check( $tag ); |
|
615 | + $this->function_check($tag); |
|
616 | 616 | |
617 | - if( empty( $code[ 2 ] ) ) |
|
617 | + if (empty($code[2])) |
|
618 | 618 | $parsed_function = $function . "()"; |
619 | 619 | else |
620 | 620 | // parse the function |
621 | - $parsed_function = $function . $this->var_replace( $code[ 2 ], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
621 | + $parsed_function = $function . $this->var_replace($code[2], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level); |
|
622 | 622 | |
623 | 623 | //if code |
624 | - $compiled_code .= "<?php echo $parsed_function; ?>"; |
|
624 | + $compiled_code .= "<?php echo $parsed_function; ?>"; |
|
625 | 625 | } |
626 | 626 | |
627 | 627 | // show all vars |
628 | - elseif ( strpos( $html, '{$template_info}' ) !== FALSE ) { |
|
628 | + elseif (strpos($html, '{$template_info}') !== FALSE) { |
|
629 | 629 | |
630 | 630 | //tag |
631 | - $tag = '{$template_info}'; |
|
631 | + $tag = '{$template_info}'; |
|
632 | 632 | |
633 | 633 | //if code |
634 | - $compiled_code .= '<?php echo "<pre>"; print_r( $this->var ); echo "</pre>"; ?>'; |
|
634 | + $compiled_code .= '<?php echo "<pre>"; print_r( $this->var ); echo "</pre>"; ?>'; |
|
635 | 635 | } |
636 | 636 | |
637 | 637 | |
638 | 638 | //all html code |
639 | - else{ |
|
639 | + else { |
|
640 | 640 | |
641 | 641 | //variables substitution (es. {$title}) |
642 | - $html = $this->var_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
642 | + $html = $this->var_replace($html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true); |
|
643 | 643 | //const substitution (es. {#CONST#}) |
644 | - $html = $this->const_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
644 | + $html = $this->const_replace($html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true); |
|
645 | 645 | //functions substitution (es. {"string"|functions}) |
646 | - $compiled_code .= $this->func_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
646 | + $compiled_code .= $this->func_replace($html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true); |
|
647 | 647 | } |
648 | 648 | } |
649 | 649 | |
650 | - if( $open_if > 0 ) { |
|
650 | + if ($open_if > 0) { |
|
651 | 651 | $e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $this->tpl['tpl_filename'] . ' template'); |
652 | 652 | throw $e->setTemplateFile($this->tpl['tpl_filename']); |
653 | 653 | } |
@@ -664,11 +664,11 @@ discard block |
||
664 | 664 | * @param string $path |
665 | 665 | * @return string |
666 | 666 | */ |
667 | - protected function reduce_path( $path ){ |
|
668 | - $path = str_replace( "://", "@not_replace@", $path ); |
|
669 | - $path = str_replace( "//", "/", $path ); |
|
670 | - $path = str_replace( "@not_replace@", "://", $path ); |
|
671 | - return preg_replace('/\w+\/\.\.\//', '', $path ); |
|
667 | + protected function reduce_path($path) { |
|
668 | + $path = str_replace("://", "@not_replace@", $path); |
|
669 | + $path = str_replace("//", "/", $path); |
|
670 | + $path = str_replace("@not_replace@", "://", $path); |
|
671 | + return preg_replace('/\w+\/\.\.\//', '', $path); |
|
672 | 672 | } |
673 | 673 | |
674 | 674 | |
@@ -685,9 +685,9 @@ discard block |
||
685 | 685 | * @param string $tpl_basedir |
686 | 686 | * @return string html substitution |
687 | 687 | */ |
688 | - protected function path_replace( $html, $tpl_basedir ){ |
|
688 | + protected function path_replace($html, $tpl_basedir) { |
|
689 | 689 | |
690 | - if( self::$path_replace ){ |
|
690 | + if (self::$path_replace) { |
|
691 | 691 | |
692 | 692 | $tpl_dir = self::$base_url . PATH . self::$tpl_dir . $tpl_basedir; |
693 | 693 | |
@@ -696,32 +696,32 @@ discard block |
||
696 | 696 | |
697 | 697 | $exp = $sub = array(); |
698 | 698 | |
699 | - if( in_array( "img", self::$path_replace_list ) ){ |
|
700 | - $exp = array( '/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ); |
|
701 | - $sub = array( '<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"' ); |
|
699 | + if (in_array("img", self::$path_replace_list)) { |
|
700 | + $exp = array('/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i'); |
|
701 | + $sub = array('<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"'); |
|
702 | 702 | } |
703 | 703 | |
704 | - if( in_array( "script", self::$path_replace_list ) ){ |
|
705 | - $exp = array_merge( $exp , array( '/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
706 | - $sub = array_merge( $sub , array( '<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"' ) ); |
|
704 | + if (in_array("script", self::$path_replace_list)) { |
|
705 | + $exp = array_merge($exp, array('/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i')); |
|
706 | + $sub = array_merge($sub, array('<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"')); |
|
707 | 707 | } |
708 | 708 | |
709 | - if( in_array( "link", self::$path_replace_list ) ){ |
|
710 | - $exp = array_merge( $exp , array( '/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
711 | - $sub = array_merge( $sub , array( '<link$1href=@$2://$3@', '<link$1href=@$2@' , '<link$1href="' . $path . '$2"', '<link$1href="$2"' ) ); |
|
709 | + if (in_array("link", self::$path_replace_list)) { |
|
710 | + $exp = array_merge($exp, array('/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i')); |
|
711 | + $sub = array_merge($sub, array('<link$1href=@$2://$3@', '<link$1href=@$2@', '<link$1href="' . $path . '$2"', '<link$1href="$2"')); |
|
712 | 712 | } |
713 | 713 | |
714 | - if( in_array( "a", self::$path_replace_list ) ){ |
|
715 | - $exp = array_merge( $exp , array( '/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
716 | - $sub = array_merge( $sub , array( '<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"' ) ); |
|
714 | + if (in_array("a", self::$path_replace_list)) { |
|
715 | + $exp = array_merge($exp, array('/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i')); |
|
716 | + $sub = array_merge($sub, array('<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"')); |
|
717 | 717 | } |
718 | 718 | |
719 | - if( in_array( "input", self::$path_replace_list ) ){ |
|
720 | - $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
721 | - $sub = array_merge( $sub , array( '<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"' ) ); |
|
719 | + if (in_array("input", self::$path_replace_list)) { |
|
720 | + $exp = array_merge($exp, array('/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i')); |
|
721 | + $sub = array_merge($sub, array('<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"')); |
|
722 | 722 | } |
723 | 723 | |
724 | - return preg_replace( $exp, $sub, $html ); |
|
724 | + return preg_replace($exp, $sub, $html); |
|
725 | 725 | |
726 | 726 | } |
727 | 727 | else |
@@ -744,9 +744,9 @@ discard block |
||
744 | 744 | * @param string $echo (optional) |
745 | 745 | * @return string |
746 | 746 | */ |
747 | - public function const_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
747 | + public function const_replace($html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null) { |
|
748 | 748 | // const |
749 | - return preg_replace( '/\{\#(\w+)\#{0,1}\}/', $php_left_delimiter . ( $echo ? " echo " : null ) . '\\1' . $php_right_delimiter, $html ); |
|
749 | + return preg_replace('/\{\#(\w+)\#{0,1}\}/', $php_left_delimiter . ($echo ? " echo " : null) . '\\1' . $php_right_delimiter, $html); |
|
750 | 750 | } |
751 | 751 | |
752 | 752 | |
@@ -764,77 +764,77 @@ discard block |
||
764 | 764 | * @param string $echo (optional) |
765 | 765 | * @return string |
766 | 766 | */ |
767 | - public function func_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
767 | + public function func_replace($html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null) { |
|
768 | 768 | |
769 | - preg_match_all( '/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $html, $matches ); |
|
769 | + preg_match_all('/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $html, $matches); |
|
770 | 770 | |
771 | - for( $i=0, $n=count($matches[0]); $i<$n; $i++ ){ |
|
771 | + for ($i = 0, $n = count($matches[0]); $i < $n; $i++) { |
|
772 | 772 | |
773 | 773 | //complete tag ex: {$news.title|substr:0,100} |
774 | - $tag = $matches[ 0 ][ $i ]; |
|
774 | + $tag = $matches[0][$i]; |
|
775 | 775 | |
776 | 776 | //variable name ex: news.title |
777 | - $var = $matches[ 1 ][ $i ]; |
|
777 | + $var = $matches[1][$i]; |
|
778 | 778 | |
779 | 779 | //function and parameters associate to the variable ex: substr:0,100 |
780 | - $extra_var = $matches[ 2 ][ $i ]; |
|
780 | + $extra_var = $matches[2][$i]; |
|
781 | 781 | |
782 | 782 | // check if there's any function disabled by black_list |
783 | - $this->function_check( $tag ); |
|
783 | + $this->function_check($tag); |
|
784 | 784 | |
785 | - $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
785 | + $extra_var = $this->var_replace($extra_var, null, null, null, null, $loop_level); |
|
786 | 786 | |
787 | 787 | |
788 | 788 | // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
789 | - $is_init_variable = preg_match( "/^(\s*?)\=[^=](.*?)$/", $extra_var ); |
|
789 | + $is_init_variable = preg_match("/^(\s*?)\=[^=](.*?)$/", $extra_var); |
|
790 | 790 | |
791 | 791 | //function associate to variable |
792 | - $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
792 | + $function_var = ($extra_var and $extra_var[0] == '|') ? substr($extra_var, 1) : null; |
|
793 | 793 | |
794 | 794 | //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
795 | - $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
795 | + $temp = preg_split("/\.|\[|\-\>/", $var); |
|
796 | 796 | |
797 | 797 | //variable name |
798 | - $var_name = $temp[ 0 ]; |
|
798 | + $var_name = $temp[0]; |
|
799 | 799 | |
800 | 800 | //variable path |
801 | - $variable_path = substr( $var, strlen( $var_name ) ); |
|
801 | + $variable_path = substr($var, strlen($var_name)); |
|
802 | 802 | |
803 | 803 | //parentesis transform [ e ] in [" e in "] |
804 | - $variable_path = str_replace( '[', '["', $variable_path ); |
|
805 | - $variable_path = str_replace( ']', '"]', $variable_path ); |
|
804 | + $variable_path = str_replace('[', '["', $variable_path); |
|
805 | + $variable_path = str_replace(']', '"]', $variable_path); |
|
806 | 806 | |
807 | 807 | //transform .$variable in ["$variable"] |
808 | - $variable_path = preg_replace('/\.\$(\w+)/', '["$\\1"]', $variable_path ); |
|
808 | + $variable_path = preg_replace('/\.\$(\w+)/', '["$\\1"]', $variable_path); |
|
809 | 809 | |
810 | 810 | //transform [variable] in ["variable"] |
811 | - $variable_path = preg_replace('/\.(\w+)/', '["\\1"]', $variable_path ); |
|
811 | + $variable_path = preg_replace('/\.(\w+)/', '["\\1"]', $variable_path); |
|
812 | 812 | |
813 | 813 | //if there's a function |
814 | - if( $function_var ){ |
|
814 | + if ($function_var) { |
|
815 | 815 | |
816 | 816 | // check if there's a function or a static method and separate, function by parameters |
817 | - $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
817 | + $function_var = str_replace("::", "@double_dot@", $function_var); |
|
818 | 818 | |
819 | 819 | // get the position of the first : |
820 | - if( $dot_position = strpos( $function_var, ":" ) ){ |
|
820 | + if ($dot_position = strpos($function_var, ":")) { |
|
821 | 821 | |
822 | 822 | // get the function and the parameters |
823 | - $function = substr( $function_var, 0, $dot_position ); |
|
824 | - $params = substr( $function_var, $dot_position+1 ); |
|
823 | + $function = substr($function_var, 0, $dot_position); |
|
824 | + $params = substr($function_var, $dot_position+1); |
|
825 | 825 | |
826 | 826 | } |
827 | - else{ |
|
827 | + else { |
|
828 | 828 | |
829 | 829 | //get the function |
830 | - $function = str_replace( "@double_dot@", "::", $function_var ); |
|
830 | + $function = str_replace("@double_dot@", "::", $function_var); |
|
831 | 831 | $params = null; |
832 | 832 | |
833 | 833 | } |
834 | 834 | |
835 | 835 | // replace back the @double_dot@ with :: |
836 | - $function = str_replace( "@double_dot@", "::", $function ); |
|
837 | - $params = str_replace( "@double_dot@", "::", $params ); |
|
836 | + $function = str_replace("@double_dot@", "::", $function); |
|
837 | + $params = str_replace("@double_dot@", "::", $params); |
|
838 | 838 | |
839 | 839 | |
840 | 840 | } |
@@ -844,16 +844,16 @@ discard block |
||
844 | 844 | $php_var = $var_name . $variable_path; |
845 | 845 | |
846 | 846 | // compile the variable for php |
847 | - if( isset( $function ) ){ |
|
848 | - if( $php_var ) |
|
849 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
847 | + if (isset($function)) { |
|
848 | + if ($php_var) |
|
849 | + $php_var = $php_left_delimiter . (!$is_init_variable && $echo ? 'echo ' : null) . ($params ? "( $function( $php_var, $params ) )" : "$function( $php_var )") . $php_right_delimiter; |
|
850 | 850 | else |
851 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $params ) )" : "$function()" ) . $php_right_delimiter; |
|
851 | + $php_var = $php_left_delimiter . (!$is_init_variable && $echo ? 'echo ' : null) . ($params ? "( $function( $params ) )" : "$function()") . $php_right_delimiter; |
|
852 | 852 | } |
853 | 853 | else |
854 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
854 | + $php_var = $php_left_delimiter . (!$is_init_variable && $echo ? 'echo ' : null) . $php_var . $extra_var . $php_right_delimiter; |
|
855 | 855 | |
856 | - $html = str_replace( $tag, $php_var, $html ); |
|
856 | + $html = str_replace($tag, $php_var, $html); |
|
857 | 857 | |
858 | 858 | } |
859 | 859 | |
@@ -876,15 +876,15 @@ discard block |
||
876 | 876 | * @param string $echo (optional) |
877 | 877 | * @return string |
878 | 878 | */ |
879 | - public function var_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
879 | + public function var_replace($html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null) { |
|
880 | 880 | |
881 | 881 | //all variables |
882 | - if( preg_match_all( '/' . $tag_left_delimiter . '\$(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)' . $tag_right_delimiter . '/', $html, $matches ) ){ |
|
882 | + if (preg_match_all('/' . $tag_left_delimiter . '\$(\w+(?:\.\${0,1}[A-Za-z0-9_]+)*(?:(?:\[\${0,1}[A-Za-z0-9_]+\])|(?:\-\>\${0,1}[A-Za-z0-9_]+))*)(.*?)' . $tag_right_delimiter . '/', $html, $matches)) { |
|
883 | 883 | |
884 | - for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++ ) |
|
885 | - $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]); |
|
884 | + for ($parsed = array(), $i = 0, $n = count($matches[0]); $i < $n; $i++) |
|
885 | + $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i], 'extra_var'=>$matches[2][$i]); |
|
886 | 886 | |
887 | - foreach( $parsed as $tag => $array ){ |
|
887 | + foreach ($parsed as $tag => $array) { |
|
888 | 888 | |
889 | 889 | //variable name ex: news.title |
890 | 890 | $var = $array['var']; |
@@ -893,89 +893,89 @@ discard block |
||
893 | 893 | $extra_var = $array['extra_var']; |
894 | 894 | |
895 | 895 | // check if there's any function disabled by black_list |
896 | - $this->function_check( $tag ); |
|
896 | + $this->function_check($tag); |
|
897 | 897 | |
898 | - $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
898 | + $extra_var = $this->var_replace($extra_var, null, null, null, null, $loop_level); |
|
899 | 899 | |
900 | 900 | // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
901 | - $is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var ); |
|
901 | + $is_init_variable = preg_match("/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var); |
|
902 | 902 | |
903 | 903 | //function associate to variable |
904 | - $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
904 | + $function_var = ($extra_var and $extra_var[0] == '|') ? substr($extra_var, 1) : null; |
|
905 | 905 | |
906 | 906 | //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
907 | - $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
907 | + $temp = preg_split("/\.|\[|\-\>/", $var); |
|
908 | 908 | |
909 | 909 | //variable name |
910 | - $var_name = $temp[ 0 ]; |
|
910 | + $var_name = $temp[0]; |
|
911 | 911 | |
912 | 912 | //variable path |
913 | - $variable_path = substr( $var, strlen( $var_name ) ); |
|
913 | + $variable_path = substr($var, strlen($var_name)); |
|
914 | 914 | |
915 | 915 | //parentesis transform [ e ] in [" e in "] |
916 | - $variable_path = str_replace( '[', '["', $variable_path ); |
|
917 | - $variable_path = str_replace( ']', '"]', $variable_path ); |
|
916 | + $variable_path = str_replace('[', '["', $variable_path); |
|
917 | + $variable_path = str_replace(']', '"]', $variable_path); |
|
918 | 918 | |
919 | 919 | //transform .$variable in ["$variable"] and .variable in ["variable"] |
920 | - $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path ); |
|
920 | + $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path); |
|
921 | 921 | |
922 | 922 | // if is an assignment also assign the variable to $this->var['value'] |
923 | - if( $is_init_variable ) |
|
923 | + if ($is_init_variable) |
|
924 | 924 | $extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var; |
925 | 925 | |
926 | 926 | |
927 | 927 | |
928 | 928 | //if there's a function |
929 | - if( $function_var ){ |
|
929 | + if ($function_var) { |
|
930 | 930 | |
931 | 931 | // check if there's a function or a static method and separate, function by parameters |
932 | - $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
932 | + $function_var = str_replace("::", "@double_dot@", $function_var); |
|
933 | 933 | |
934 | 934 | |
935 | 935 | // get the position of the first : |
936 | - if( $dot_position = strpos( $function_var, ":" ) ){ |
|
936 | + if ($dot_position = strpos($function_var, ":")) { |
|
937 | 937 | |
938 | 938 | // get the function and the parameters |
939 | - $function = substr( $function_var, 0, $dot_position ); |
|
940 | - $params = substr( $function_var, $dot_position+1 ); |
|
939 | + $function = substr($function_var, 0, $dot_position); |
|
940 | + $params = substr($function_var, $dot_position+1); |
|
941 | 941 | |
942 | 942 | } |
943 | - else{ |
|
943 | + else { |
|
944 | 944 | |
945 | 945 | //get the function |
946 | - $function = str_replace( "@double_dot@", "::", $function_var ); |
|
946 | + $function = str_replace("@double_dot@", "::", $function_var); |
|
947 | 947 | $params = null; |
948 | 948 | |
949 | 949 | } |
950 | 950 | |
951 | 951 | // replace back the @double_dot@ with :: |
952 | - $function = str_replace( "@double_dot@", "::", $function ); |
|
953 | - $params = str_replace( "@double_dot@", "::", $params ); |
|
952 | + $function = str_replace("@double_dot@", "::", $function); |
|
953 | + $params = str_replace("@double_dot@", "::", $params); |
|
954 | 954 | } |
955 | 955 | else |
956 | 956 | $function = $params = null; |
957 | 957 | |
958 | 958 | //if it is inside a loop |
959 | - if( $loop_level ){ |
|
959 | + if ($loop_level) { |
|
960 | 960 | //verify the variable name |
961 | - if( $var_name == 'key' ) |
|
961 | + if ($var_name == 'key') |
|
962 | 962 | $php_var = '$key' . $loop_level; |
963 | - elseif( $var_name == 'value' ) |
|
963 | + elseif ($var_name == 'value') |
|
964 | 964 | $php_var = '$value' . $loop_level . $variable_path; |
965 | - elseif( $var_name == 'counter' ) |
|
965 | + elseif ($var_name == 'counter') |
|
966 | 966 | $php_var = '$counter' . $loop_level; |
967 | 967 | else |
968 | 968 | $php_var = '$' . $var_name . $variable_path; |
969 | - }else |
|
969 | + } else |
|
970 | 970 | $php_var = '$' . $var_name . $variable_path; |
971 | 971 | |
972 | 972 | // compile the variable for php |
973 | - if( isset( $function ) ) |
|
974 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
973 | + if (isset($function)) |
|
974 | + $php_var = $php_left_delimiter . (!$is_init_variable && $echo ? 'echo ' : null) . ($params ? "( $function( $php_var, $params ) )" : "$function( $php_var )") . $php_right_delimiter; |
|
975 | 975 | else |
976 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
976 | + $php_var = $php_left_delimiter . (!$is_init_variable && $echo ? 'echo ' : null) . $php_var . $extra_var . $php_right_delimiter; |
|
977 | 977 | |
978 | - $html = str_replace( $tag, $php_var, $html ); |
|
978 | + $html = str_replace($tag, $php_var, $html); |
|
979 | 979 | |
980 | 980 | |
981 | 981 | } |
@@ -994,17 +994,17 @@ discard block |
||
994 | 994 | * @throws RainTpl_SyntaxException |
995 | 995 | * @return void |
996 | 996 | */ |
997 | - protected function function_check( $code ){ |
|
997 | + protected function function_check($code) { |
|
998 | 998 | |
999 | - $preg = '#(\W|\s)' . implode( '(\W|\s)|(\W|\s)', self::$black_list ) . '(\W|\s)#'; |
|
999 | + $preg = '#(\W|\s)' . implode('(\W|\s)|(\W|\s)', self::$black_list) . '(\W|\s)#'; |
|
1000 | 1000 | |
1001 | 1001 | // check if the function is in the black list (or not in white list) |
1002 | - if( count(self::$black_list) && preg_match( $preg, $code, $match ) ){ |
|
1002 | + if (count(self::$black_list) && preg_match($preg, $code, $match)) { |
|
1003 | 1003 | |
1004 | 1004 | // find the line of the error |
1005 | 1005 | $line = 0; |
1006 | - $rows=explode("\n",$this->tpl['source']); |
|
1007 | - while( !strpos($rows[$line],$code) ) |
|
1006 | + $rows = explode("\n", $this->tpl['source']); |
|
1007 | + while (!strpos($rows[$line], $code)) |
|
1008 | 1008 | $line++; |
1009 | 1009 | |
1010 | 1010 | // stop the execution of the script |
@@ -1025,7 +1025,7 @@ discard block |
||
1025 | 1025 | * @param RainTpl_Exception $e |
1026 | 1026 | * @return string |
1027 | 1027 | */ |
1028 | - protected function printDebug(RainTpl_Exception $e){ |
|
1028 | + protected function printDebug(RainTpl_Exception $e) { |
|
1029 | 1029 | if (!self::$debug) { |
1030 | 1030 | throw $e; |
1031 | 1031 | } |
@@ -1042,7 +1042,7 @@ discard block |
||
1042 | 1042 | $output .= '<p>in tag: ' . htmlspecialchars($e->getTag()) . '</p>'; |
1043 | 1043 | } |
1044 | 1044 | if (null !== $e->getTemplateLine() && null !== $e->getTag()) { |
1045 | - $rows=explode("\n", htmlspecialchars($this->tpl['source'])); |
|
1045 | + $rows = explode("\n", htmlspecialchars($this->tpl['source'])); |
|
1046 | 1046 | $rows[$e->getTemplateLine()] = '<font color=red>' . $rows[$e->getTemplateLine()] . '</font>'; |
1047 | 1047 | $output .= '<h3>template code</h3>' . implode('<br />', $rows) . '</pre>'; |
1048 | 1048 | } |
@@ -1060,7 +1060,7 @@ discard block |
||
1060 | 1060 | /** |
1061 | 1061 | * Basic Rain tpl exception. |
1062 | 1062 | */ |
1063 | -class RainTpl_Exception extends Exception{ |
|
1063 | +class RainTpl_Exception extends Exception { |
|
1064 | 1064 | /** |
1065 | 1065 | * Path of template file with error. |
1066 | 1066 | */ |
@@ -1094,13 +1094,13 @@ discard block |
||
1094 | 1094 | /** |
1095 | 1095 | * Exception thrown when template file does not exists. |
1096 | 1096 | */ |
1097 | -class RainTpl_NotFoundException extends RainTpl_Exception{ |
|
1097 | +class RainTpl_NotFoundException extends RainTpl_Exception { |
|
1098 | 1098 | } |
1099 | 1099 | |
1100 | 1100 | /** |
1101 | 1101 | * Exception thrown when syntax error occurs. |
1102 | 1102 | */ |
1103 | -class RainTpl_SyntaxException extends RainTpl_Exception{ |
|
1103 | +class RainTpl_SyntaxException extends RainTpl_Exception { |
|
1104 | 1104 | /** |
1105 | 1105 | * Line in template file where error has occured. |
1106 | 1106 | * |
@@ -89,39 +89,39 @@ discard block |
||
89 | 89 | if (!function_exists('gd_info')) return ''; |
90 | 90 | |
91 | 91 | // We hash the input string. |
92 | - $hash=hash('sha1',$text.$this->salt).hash('md5',$text.$this->salt); |
|
93 | - $hash=$hash.strrev($hash); # more data to make graphics |
|
94 | - $hashlen=strlen($hash); |
|
92 | + $hash = hash('sha1', $text . $this->salt) . hash('md5', $text . $this->salt); |
|
93 | + $hash = $hash . strrev($hash); # more data to make graphics |
|
94 | + $hashlen = strlen($hash); |
|
95 | 95 | |
96 | 96 | // We convert the hash into an array of integers. |
97 | - $this->VALUES=array(); |
|
98 | - for($i=0; $i<$hashlen; $i=$i+2){ array_push($this->VALUES,hexdec(substr($hash,$i,2))); } |
|
99 | - $this->VALUES_INDEX=0; // to walk the array. |
|
97 | + $this->VALUES = array(); |
|
98 | + for ($i = 0; $i < $hashlen; $i = $i+2) { array_push($this->VALUES, hexdec(substr($hash, $i, 2))); } |
|
99 | + $this->VALUES_INDEX = 0; // to walk the array. |
|
100 | 100 | |
101 | 101 | // Then use these integers to drive the creation of an image. |
102 | - $image = imagecreatetruecolor($this->width,$this->height); |
|
102 | + $image = imagecreatetruecolor($this->width, $this->height); |
|
103 | 103 | |
104 | - $r0 = $this->getInt();$r=$r0; |
|
105 | - $g0 = $this->getInt();$g=$g0; |
|
106 | - $b0 = $this->getInt();$b=$b0; |
|
104 | + $r0 = $this->getInt(); $r = $r0; |
|
105 | + $g0 = $this->getInt(); $g = $g0; |
|
106 | + $b0 = $this->getInt(); $b = $b0; |
|
107 | 107 | |
108 | 108 | // First, create an image with a specific gradient background. |
109 | - $op='v'; if (($this->getInt()%2)==0) { $op='h'; }; |
|
110 | - $image = $this->degrade($image,$op,array($r0,$g0,$b0),array(0,0,0)); |
|
109 | + $op = 'v'; if (($this->getInt() % 2) == 0) { $op = 'h'; }; |
|
110 | + $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0)); |
|
111 | 111 | |
112 | - for($i=0; $i<7; $i=$i+1) |
|
112 | + for ($i = 0; $i < 7; $i = $i+1) |
|
113 | 113 | { |
114 | - $action=$this->getInt(); |
|
115 | - $color = imagecolorallocate($image, $r,$g,$b); |
|
116 | - $r = ($r0 + $this->getInt()/25)%256; |
|
117 | - $g = ($g0 + $this->getInt()/25)%256; |
|
118 | - $b = ($b0 + $this->getInt()/25)%256; |
|
119 | - $r0=$r; $g0=$g; $b0=$b; |
|
120 | - $this->drawshape($image,$action,$color); |
|
114 | + $action = $this->getInt(); |
|
115 | + $color = imagecolorallocate($image, $r, $g, $b); |
|
116 | + $r = ($r0+$this->getInt() / 25) % 256; |
|
117 | + $g = ($g0+$this->getInt() / 25) % 256; |
|
118 | + $b = ($b0+$this->getInt() / 25) % 256; |
|
119 | + $r0 = $r; $g0 = $g; $b0 = $b; |
|
120 | + $this->drawshape($image, $action, $color); |
|
121 | 121 | } |
122 | 122 | |
123 | - $color = imagecolorallocate($image,$this->getInt(),$this->getInt(),$this->getInt()); |
|
124 | - $this->drawshape($image,$this->getInt(),$color); |
|
123 | + $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt()); |
|
124 | + $this->drawshape($image, $this->getInt(), $color); |
|
125 | 125 | ob_start(); |
126 | 126 | imagepng($image); |
127 | 127 | $imagedata = ob_get_contents(); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | */ |
140 | 140 | private function getInt() |
141 | 141 | { |
142 | - $v= $this->VALUES[$this->VALUES_INDEX]; |
|
142 | + $v = $this->VALUES[$this->VALUES_INDEX]; |
|
143 | 143 | $this->VALUES_INDEX++; |
144 | 144 | $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array |
145 | 145 | return $v; |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | */ |
154 | 154 | private function getX() |
155 | 155 | { |
156 | - return $this->width*$this->getInt()/256; |
|
156 | + return $this->width * $this->getInt() / 256; |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | /** |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | */ |
165 | 165 | private function getY() |
166 | 166 | { |
167 | - return $this->height*$this->getInt()/256; |
|
167 | + return $this->height * $this->getInt() / 256; |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -180,22 +180,22 @@ discard block |
||
180 | 180 | * @param array $color2 |
181 | 181 | * @return resource |
182 | 182 | */ |
183 | - private function degrade($img,$direction,$color1,$color2) |
|
183 | + private function degrade($img, $direction, $color1, $color2) |
|
184 | 184 | { |
185 | - if($direction=='h') { $size = imagesx($img); $sizeinv = imagesy($img); } |
|
186 | - else { $size = imagesy($img); $sizeinv = imagesx($img);} |
|
185 | + if ($direction == 'h') { $size = imagesx($img); $sizeinv = imagesy($img); } |
|
186 | + else { $size = imagesy($img); $sizeinv = imagesx($img); } |
|
187 | 187 | $diffs = array( |
188 | - (($color2[0]-$color1[0])/$size), |
|
189 | - (($color2[1]-$color1[1])/$size), |
|
190 | - (($color2[2]-$color1[2])/$size) |
|
188 | + (($color2[0]-$color1[0]) / $size), |
|
189 | + (($color2[1]-$color1[1]) / $size), |
|
190 | + (($color2[2]-$color1[2]) / $size) |
|
191 | 191 | ); |
192 | - for($i=0;$i<$size;$i++) |
|
192 | + for ($i = 0; $i < $size; $i++) |
|
193 | 193 | { |
194 | - $r = $color1[0]+($diffs[0]*$i); |
|
195 | - $g = $color1[1]+($diffs[1]*$i); |
|
196 | - $b = $color1[2]+($diffs[2]*$i); |
|
197 | - if($direction=='h') { imageline($img,$i,0,$i,$sizeinv,imagecolorallocate($img,$r,$g,$b)); } |
|
198 | - else { imageline($img,0,$i,$sizeinv,$i,imagecolorallocate($img,$r,$g,$b)); } |
|
194 | + $r = $color1[0]+($diffs[0] * $i); |
|
195 | + $g = $color1[1]+($diffs[1] * $i); |
|
196 | + $b = $color1[2]+($diffs[2] * $i); |
|
197 | + if ($direction == 'h') { imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b)); } |
|
198 | + else { imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b)); } |
|
199 | 199 | } |
200 | 200 | return $img; |
201 | 201 | } |
@@ -209,24 +209,24 @@ discard block |
||
209 | 209 | * @param int $color |
210 | 210 | * @return void |
211 | 211 | */ |
212 | - private function drawshape($image,$action,$color) |
|
212 | + private function drawshape($image, $action, $color) |
|
213 | 213 | { |
214 | - switch($action%7) |
|
214 | + switch ($action % 7) |
|
215 | 215 | { |
216 | 216 | case 0: |
217 | - ImageFilledRectangle ($image,$this->getX(),$this->getY(),$this->getX(),$this->getY(),$color); |
|
217 | + ImageFilledRectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color); |
|
218 | 218 | break; |
219 | 219 | case 1: |
220 | 220 | case 2: |
221 | - ImageFilledEllipse ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color); |
|
221 | + ImageFilledEllipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color); |
|
222 | 222 | break; |
223 | 223 | case 3: |
224 | - $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(),$this->getX(), $this->getY()); |
|
225 | - ImageFilledPolygon ($image, $points, 4, $color); |
|
224 | + $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY()); |
|
225 | + ImageFilledPolygon($image, $points, 4, $color); |
|
226 | 226 | break; |
227 | 227 | default: |
228 | - $start=$this->getInt()*360/256; $end=$start+$this->getInt()*180/256; |
|
229 | - ImageFilledArc ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(),$start,$end,$color,IMG_ARC_PIE); |
|
228 | + $start = $this->getInt() * 360 / 256; $end = $start+$this->getInt() * 180 / 256; |
|
229 | + ImageFilledArc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE); |
|
230 | 230 | } |
231 | 231 | } |
232 | 232 | } |