@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | break; |
449 | 449 | case 'mssql': |
450 | 450 | $sql = "SELECT name FROM sysobjects " |
451 | - . "WHERE type = 'U' ORDER BY name"; |
|
451 | + . "WHERE type = 'U' ORDER BY name"; |
|
452 | 452 | break; |
453 | 453 | case 'mysql': |
454 | 454 | $sql = 'SHOW TABLES'; |
@@ -458,22 +458,22 @@ discard block |
||
458 | 458 | break; |
459 | 459 | case 'pgsql': |
460 | 460 | $sql = "SELECT c.relname AS table_name " |
461 | - . "FROM pg_class c, pg_user u " |
|
462 | - . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' " |
|
463 | - . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " |
|
464 | - . "AND c.relname !~ '^(pg_|sql_)' " |
|
465 | - . "UNION " |
|
466 | - . "SELECT c.relname AS table_name " |
|
467 | - . "FROM pg_class c " |
|
468 | - . "WHERE c.relkind = 'r' " |
|
469 | - . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " |
|
470 | - . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) " |
|
471 | - . "AND c.relname !~ '^pg_'"; |
|
461 | + . "FROM pg_class c, pg_user u " |
|
462 | + . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' " |
|
463 | + . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " |
|
464 | + . "AND c.relname !~ '^(pg_|sql_)' " |
|
465 | + . "UNION " |
|
466 | + . "SELECT c.relname AS table_name " |
|
467 | + . "FROM pg_class c " |
|
468 | + . "WHERE c.relkind = 'r' " |
|
469 | + . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) " |
|
470 | + . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) " |
|
471 | + . "AND c.relname !~ '^pg_'"; |
|
472 | 472 | break; |
473 | 473 | case 'sqlite': |
474 | 474 | $sql = "SELECT name FROM sqlite_master WHERE type='table' " |
475 | - . "UNION ALL SELECT name FROM sqlite_temp_master " |
|
476 | - . "WHERE type='table' ORDER BY name"; |
|
475 | + . "UNION ALL SELECT name FROM sqlite_temp_master " |
|
476 | + . "WHERE type='table' ORDER BY name"; |
|
477 | 477 | break; |
478 | 478 | default: |
479 | 479 | throw new Exception( |
@@ -14,1046 +14,1046 @@ 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_basename, $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_basename |
|
351 | - * @param string $tpl_basedir |
|
352 | - * @param string $tpl_filename |
|
353 | - * @param string $cache_dir |
|
354 | - * @param string $compiled_filename |
|
355 | - * @throws RainTpl_Exception |
|
356 | - * @return void |
|
357 | - */ |
|
358 | - protected function compileFile( $tpl_basename, $tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename ){ |
|
359 | - |
|
360 | - //read template file |
|
361 | - $this->tpl['source'] = $template_code = file_get_contents( $tpl_filename ); |
|
362 | - |
|
363 | - //xml substitution |
|
364 | - $template_code = preg_replace( "/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code ); |
|
365 | - |
|
366 | - //disable php tag |
|
367 | - if( !self::$php_enabled ) |
|
368 | - $template_code = str_replace( array("<?","?>"), array("<?","?>"), $template_code ); |
|
369 | - |
|
370 | - //xml re-substitution |
|
371 | - $template_code = preg_replace_callback ( "/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code ); |
|
372 | - |
|
373 | - //compile template |
|
374 | - $template_compiled = "<?php if(!class_exists('raintpl')){exit;}?>" . $this->compileTemplate( $template_code, $tpl_basedir ); |
|
375 | - |
|
376 | - |
|
377 | - // fix the php-eating-newline-after-closing-tag-problem |
|
378 | - $template_compiled = str_replace( "?>\n", "?>\n\n", $template_compiled ); |
|
379 | - |
|
380 | - // create directories |
|
381 | - if( !is_dir( $cache_dir ) ) |
|
382 | - mkdir( $cache_dir, 0755, true ); |
|
383 | - |
|
384 | - if( !is_writable( $cache_dir ) ) |
|
385 | - 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/'); |
|
386 | - |
|
387 | - //write compiled file |
|
388 | - file_put_contents( $compiled_filename, $template_compiled ); |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - |
|
393 | - /** |
|
394 | - * Compile template |
|
395 | - * |
|
396 | - * @access protected |
|
397 | - * @param string $template_code |
|
398 | - * @param string $tpl_basedir |
|
399 | - * @return string |
|
400 | - */ |
|
401 | - protected function compileTemplate( $template_code, $tpl_basedir ){ |
|
402 | - |
|
403 | - //tag list |
|
404 | - $tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})', |
|
405 | - 'loop_close' => '(\{\/loop\})', |
|
406 | - 'if' => '(\{if(?: condition){0,1}="[^"]*"\})', |
|
407 | - 'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})', |
|
408 | - 'else' => '(\{else\})', |
|
409 | - 'if_close' => '(\{\/if\})', |
|
410 | - 'function' => '(\{function="[^"]*"\})', |
|
411 | - 'noparse' => '(\{noparse\})', |
|
412 | - 'noparse_close'=> '(\{\/noparse\})', |
|
413 | - 'ignore' => '(\{ignore\}|\{\*)', |
|
414 | - 'ignore_close' => '(\{\/ignore\}|\*\})', |
|
415 | - 'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})', |
|
416 | - 'template_info'=> '(\{\$template_info\})', |
|
417 | - 'function' => '(\{function="(\w*?)(?:.*?)"\})' |
|
418 | - ); |
|
419 | - |
|
420 | - $tag_regexp = "/" . join( "|", $tag_regexp ) . "/"; |
|
421 | - |
|
422 | - //split the code with the tags regexp |
|
423 | - $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
424 | - |
|
425 | - //path replace (src of img, background and href of link) |
|
426 | - $template_code = $this->path_replace( $template_code, $tpl_basedir ); |
|
427 | - |
|
428 | - //compile the code |
|
429 | - $compiled_code = $this->compileCode( $template_code ); |
|
430 | - |
|
431 | - //return the compiled code |
|
432 | - return $compiled_code; |
|
433 | - |
|
434 | - } |
|
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_basename, $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_basename |
|
351 | + * @param string $tpl_basedir |
|
352 | + * @param string $tpl_filename |
|
353 | + * @param string $cache_dir |
|
354 | + * @param string $compiled_filename |
|
355 | + * @throws RainTpl_Exception |
|
356 | + * @return void |
|
357 | + */ |
|
358 | + protected function compileFile( $tpl_basename, $tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename ){ |
|
359 | + |
|
360 | + //read template file |
|
361 | + $this->tpl['source'] = $template_code = file_get_contents( $tpl_filename ); |
|
362 | + |
|
363 | + //xml substitution |
|
364 | + $template_code = preg_replace( "/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code ); |
|
365 | + |
|
366 | + //disable php tag |
|
367 | + if( !self::$php_enabled ) |
|
368 | + $template_code = str_replace( array("<?","?>"), array("<?","?>"), $template_code ); |
|
369 | + |
|
370 | + //xml re-substitution |
|
371 | + $template_code = preg_replace_callback ( "/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code ); |
|
372 | + |
|
373 | + //compile template |
|
374 | + $template_compiled = "<?php if(!class_exists('raintpl')){exit;}?>" . $this->compileTemplate( $template_code, $tpl_basedir ); |
|
375 | + |
|
376 | + |
|
377 | + // fix the php-eating-newline-after-closing-tag-problem |
|
378 | + $template_compiled = str_replace( "?>\n", "?>\n\n", $template_compiled ); |
|
379 | + |
|
380 | + // create directories |
|
381 | + if( !is_dir( $cache_dir ) ) |
|
382 | + mkdir( $cache_dir, 0755, true ); |
|
383 | + |
|
384 | + if( !is_writable( $cache_dir ) ) |
|
385 | + 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/'); |
|
386 | + |
|
387 | + //write compiled file |
|
388 | + file_put_contents( $compiled_filename, $template_compiled ); |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + |
|
393 | + /** |
|
394 | + * Compile template |
|
395 | + * |
|
396 | + * @access protected |
|
397 | + * @param string $template_code |
|
398 | + * @param string $tpl_basedir |
|
399 | + * @return string |
|
400 | + */ |
|
401 | + protected function compileTemplate( $template_code, $tpl_basedir ){ |
|
402 | + |
|
403 | + //tag list |
|
404 | + $tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})', |
|
405 | + 'loop_close' => '(\{\/loop\})', |
|
406 | + 'if' => '(\{if(?: condition){0,1}="[^"]*"\})', |
|
407 | + 'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})', |
|
408 | + 'else' => '(\{else\})', |
|
409 | + 'if_close' => '(\{\/if\})', |
|
410 | + 'function' => '(\{function="[^"]*"\})', |
|
411 | + 'noparse' => '(\{noparse\})', |
|
412 | + 'noparse_close'=> '(\{\/noparse\})', |
|
413 | + 'ignore' => '(\{ignore\}|\{\*)', |
|
414 | + 'ignore_close' => '(\{\/ignore\}|\*\})', |
|
415 | + 'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})', |
|
416 | + 'template_info'=> '(\{\$template_info\})', |
|
417 | + 'function' => '(\{function="(\w*?)(?:.*?)"\})' |
|
418 | + ); |
|
419 | + |
|
420 | + $tag_regexp = "/" . join( "|", $tag_regexp ) . "/"; |
|
421 | + |
|
422 | + //split the code with the tags regexp |
|
423 | + $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
424 | + |
|
425 | + //path replace (src of img, background and href of link) |
|
426 | + $template_code = $this->path_replace( $template_code, $tpl_basedir ); |
|
427 | + |
|
428 | + //compile the code |
|
429 | + $compiled_code = $this->compileCode( $template_code ); |
|
430 | + |
|
431 | + //return the compiled code |
|
432 | + return $compiled_code; |
|
433 | + |
|
434 | + } |
|
435 | 435 | |
436 | 436 | |
437 | 437 | |
438 | - /** |
|
439 | - * Compile the code |
|
440 | - * |
|
441 | - * @access protected |
|
442 | - * @param string $parsed_code |
|
443 | - * @throws RainTpl_SyntaxException |
|
444 | - * @return string |
|
445 | - */ |
|
446 | - protected function compileCode( $parsed_code ){ |
|
438 | + /** |
|
439 | + * Compile the code |
|
440 | + * |
|
441 | + * @access protected |
|
442 | + * @param string $parsed_code |
|
443 | + * @throws RainTpl_SyntaxException |
|
444 | + * @return string |
|
445 | + */ |
|
446 | + protected function compileCode( $parsed_code ){ |
|
447 | 447 | |
448 | - //variables initialization |
|
449 | - $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null; |
|
450 | - $loop_level = 0; |
|
448 | + //variables initialization |
|
449 | + $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null; |
|
450 | + $loop_level = 0; |
|
451 | 451 | |
452 | - //read all parsed code |
|
453 | - while( $html = array_shift( $parsed_code ) ){ |
|
452 | + //read all parsed code |
|
453 | + while( $html = array_shift( $parsed_code ) ){ |
|
454 | 454 | |
455 | - //close ignore tag |
|
456 | - if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) ) |
|
457 | - $ignore_is_open = false; |
|
455 | + //close ignore tag |
|
456 | + if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) ) |
|
457 | + $ignore_is_open = false; |
|
458 | 458 | |
459 | - //code between tag ignore id deleted |
|
460 | - elseif( $ignore_is_open ){ |
|
461 | - //ignore the code |
|
462 | - } |
|
459 | + //code between tag ignore id deleted |
|
460 | + elseif( $ignore_is_open ){ |
|
461 | + //ignore the code |
|
462 | + } |
|
463 | 463 | |
464 | - //close no parse tag |
|
465 | - elseif( strpos( $html, '{/noparse}' ) !== FALSE ) |
|
466 | - $comment_is_open = false; |
|
464 | + //close no parse tag |
|
465 | + elseif( strpos( $html, '{/noparse}' ) !== FALSE ) |
|
466 | + $comment_is_open = false; |
|
467 | 467 | |
468 | - //code between tag noparse is not compiled |
|
469 | - elseif( $comment_is_open ) |
|
470 | - $compiled_code .= $html; |
|
468 | + //code between tag noparse is not compiled |
|
469 | + elseif( $comment_is_open ) |
|
470 | + $compiled_code .= $html; |
|
471 | 471 | |
472 | - //ignore |
|
473 | - elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE ) |
|
474 | - $ignore_is_open = true; |
|
472 | + //ignore |
|
473 | + elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE ) |
|
474 | + $ignore_is_open = true; |
|
475 | 475 | |
476 | - //noparse |
|
477 | - elseif( strpos( $html, '{noparse}' ) !== FALSE ) |
|
478 | - $comment_is_open = true; |
|
476 | + //noparse |
|
477 | + elseif( strpos( $html, '{noparse}' ) !== FALSE ) |
|
478 | + $comment_is_open = true; |
|
479 | 479 | |
480 | - //include tag |
|
481 | - elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){ |
|
480 | + //include tag |
|
481 | + elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){ |
|
482 | 482 | |
483 | - //variables substitution |
|
484 | - $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level ); |
|
483 | + //variables substitution |
|
484 | + $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level ); |
|
485 | 485 | |
486 | - // if the cache is active |
|
487 | - if( isset($code[ 2 ]) ){ |
|
486 | + // if the cache is active |
|
487 | + if( isset($code[ 2 ]) ){ |
|
488 | 488 | |
489 | - //dynamic include |
|
490 | - $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
491 | - 'if( $cache = $tpl->cache( $template = basename("'.$include_var.'") ) )' . |
|
492 | - ' echo $cache;' . |
|
493 | - 'else{' . |
|
494 | - ' $tpl_dir_temp = self::$tpl_dir;' . |
|
495 | - ' $tpl->assign( $this->var );' . |
|
496 | - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
497 | - ' $tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
498 | - '} ?>'; |
|
499 | - } |
|
500 | - else{ |
|
489 | + //dynamic include |
|
490 | + $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
491 | + 'if( $cache = $tpl->cache( $template = basename("'.$include_var.'") ) )' . |
|
492 | + ' echo $cache;' . |
|
493 | + 'else{' . |
|
494 | + ' $tpl_dir_temp = self::$tpl_dir;' . |
|
495 | + ' $tpl->assign( $this->var );' . |
|
496 | + ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
497 | + ' $tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
498 | + '} ?>'; |
|
499 | + } |
|
500 | + else{ |
|
501 | 501 | |
502 | - //dynamic include |
|
503 | - $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
504 | - '$tpl_dir_temp = self::$tpl_dir;' . |
|
505 | - '$tpl->assign( $this->var );' . |
|
506 | - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
507 | - '$tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
508 | - '?>'; |
|
502 | + //dynamic include |
|
503 | + $compiled_code .= '<?php $tpl = new '.get_class($this).';' . |
|
504 | + '$tpl_dir_temp = self::$tpl_dir;' . |
|
505 | + '$tpl->assign( $this->var );' . |
|
506 | + ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). |
|
507 | + '$tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. |
|
508 | + '?>'; |
|
509 | 509 | |
510 | 510 | |
511 | - } |
|
511 | + } |
|
512 | 512 | |
513 | - } |
|
513 | + } |
|
514 | 514 | |
515 | - //loop |
|
516 | - elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){ |
|
515 | + //loop |
|
516 | + elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){ |
|
517 | 517 | |
518 | - //increase the loop counter |
|
519 | - $loop_level++; |
|
518 | + //increase the loop counter |
|
519 | + $loop_level++; |
|
520 | 520 | |
521 | - //replace the variable in the loop |
|
522 | - $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 | + //replace the variable in the loop |
|
522 | + $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 ); |
|
523 | 523 | |
524 | - //loop variables |
|
525 | - $counter = "\$counter$loop_level"; // count iteration |
|
526 | - $key = "\$key$loop_level"; // key |
|
527 | - $value = "\$value$loop_level"; // value |
|
524 | + //loop variables |
|
525 | + $counter = "\$counter$loop_level"; // count iteration |
|
526 | + $key = "\$key$loop_level"; // key |
|
527 | + $value = "\$value$loop_level"; // value |
|
528 | 528 | |
529 | - //loop code |
|
530 | - $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>"; |
|
529 | + //loop code |
|
530 | + $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>"; |
|
531 | 531 | |
532 | - } |
|
532 | + } |
|
533 | 533 | |
534 | - //close loop tag |
|
535 | - elseif( strpos( $html, '{/loop}' ) !== FALSE ) { |
|
534 | + //close loop tag |
|
535 | + elseif( strpos( $html, '{/loop}' ) !== FALSE ) { |
|
536 | 536 | |
537 | - //iterator |
|
538 | - $counter = "\$counter$loop_level"; |
|
537 | + //iterator |
|
538 | + $counter = "\$counter$loop_level"; |
|
539 | 539 | |
540 | - //decrease the loop counter |
|
541 | - $loop_level--; |
|
540 | + //decrease the loop counter |
|
541 | + $loop_level--; |
|
542 | 542 | |
543 | - //close loop code |
|
544 | - $compiled_code .= "<?php } ?>"; |
|
543 | + //close loop code |
|
544 | + $compiled_code .= "<?php } ?>"; |
|
545 | 545 | |
546 | - } |
|
546 | + } |
|
547 | 547 | |
548 | - //if |
|
549 | - elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
548 | + //if |
|
549 | + elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
550 | 550 | |
551 | - //increase open if counter (for intendation) |
|
552 | - $open_if++; |
|
551 | + //increase open if counter (for intendation) |
|
552 | + $open_if++; |
|
553 | 553 | |
554 | - //tag |
|
555 | - $tag = $code[ 0 ]; |
|
554 | + //tag |
|
555 | + $tag = $code[ 0 ]; |
|
556 | 556 | |
557 | - //condition attribute |
|
558 | - $condition = $code[ 1 ]; |
|
557 | + //condition attribute |
|
558 | + $condition = $code[ 1 ]; |
|
559 | 559 | |
560 | - // check if there's any function disabled by black_list |
|
561 | - $this->function_check( $tag ); |
|
560 | + // check if there's any function disabled by black_list |
|
561 | + $this->function_check( $tag ); |
|
562 | 562 | |
563 | - //variable substitution into condition (no delimiter into the condition) |
|
564 | - $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 | + //variable substitution into condition (no delimiter into the condition) |
|
564 | + $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
565 | 565 | |
566 | - //if code |
|
567 | - $compiled_code .= "<?php if( $parsed_condition ){ ?>"; |
|
566 | + //if code |
|
567 | + $compiled_code .= "<?php if( $parsed_condition ){ ?>"; |
|
568 | 568 | |
569 | - } |
|
569 | + } |
|
570 | 570 | |
571 | - //elseif |
|
572 | - elseif( preg_match( '/\{elseif(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
571 | + //elseif |
|
572 | + elseif( preg_match( '/\{elseif(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){ |
|
573 | 573 | |
574 | - //tag |
|
575 | - $tag = $code[ 0 ]; |
|
574 | + //tag |
|
575 | + $tag = $code[ 0 ]; |
|
576 | 576 | |
577 | - //condition attribute |
|
578 | - $condition = $code[ 1 ]; |
|
577 | + //condition attribute |
|
578 | + $condition = $code[ 1 ]; |
|
579 | 579 | |
580 | - //variable substitution into condition (no delimiter into the condition) |
|
581 | - $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 | + //variable substitution into condition (no delimiter into the condition) |
|
581 | + $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level ); |
|
582 | 582 | |
583 | - //elseif code |
|
584 | - $compiled_code .= "<?php }elseif( $parsed_condition ){ ?>"; |
|
585 | - } |
|
583 | + //elseif code |
|
584 | + $compiled_code .= "<?php }elseif( $parsed_condition ){ ?>"; |
|
585 | + } |
|
586 | 586 | |
587 | - //else |
|
588 | - elseif( strpos( $html, '{else}' ) !== FALSE ) { |
|
587 | + //else |
|
588 | + elseif( strpos( $html, '{else}' ) !== FALSE ) { |
|
589 | 589 | |
590 | - //else code |
|
591 | - $compiled_code .= '<?php }else{ ?>'; |
|
590 | + //else code |
|
591 | + $compiled_code .= '<?php }else{ ?>'; |
|
592 | 592 | |
593 | - } |
|
593 | + } |
|
594 | 594 | |
595 | - //close if tag |
|
596 | - elseif( strpos( $html, '{/if}' ) !== FALSE ) { |
|
595 | + //close if tag |
|
596 | + elseif( strpos( $html, '{/if}' ) !== FALSE ) { |
|
597 | 597 | |
598 | - //decrease if counter |
|
599 | - $open_if--; |
|
598 | + //decrease if counter |
|
599 | + $open_if--; |
|
600 | 600 | |
601 | - // close if code |
|
602 | - $compiled_code .= '<?php } ?>'; |
|
601 | + // close if code |
|
602 | + $compiled_code .= '<?php } ?>'; |
|
603 | 603 | |
604 | - } |
|
604 | + } |
|
605 | 605 | |
606 | - //function |
|
607 | - elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){ |
|
606 | + //function |
|
607 | + elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){ |
|
608 | 608 | |
609 | - //tag |
|
610 | - $tag = $code[ 0 ]; |
|
609 | + //tag |
|
610 | + $tag = $code[ 0 ]; |
|
611 | 611 | |
612 | - //function |
|
613 | - $function = $code[ 1 ]; |
|
612 | + //function |
|
613 | + $function = $code[ 1 ]; |
|
614 | 614 | |
615 | - // check if there's any function disabled by black_list |
|
616 | - $this->function_check( $tag ); |
|
615 | + // check if there's any function disabled by black_list |
|
616 | + $this->function_check( $tag ); |
|
617 | 617 | |
618 | - if( empty( $code[ 2 ] ) ) |
|
619 | - $parsed_function = $function . "()"; |
|
620 | - else |
|
621 | - // parse the function |
|
622 | - $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 ); |
|
618 | + if( empty( $code[ 2 ] ) ) |
|
619 | + $parsed_function = $function . "()"; |
|
620 | + else |
|
621 | + // parse the function |
|
622 | + $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 ); |
|
623 | 623 | |
624 | - //if code |
|
625 | - $compiled_code .= "<?php echo $parsed_function; ?>"; |
|
626 | - } |
|
624 | + //if code |
|
625 | + $compiled_code .= "<?php echo $parsed_function; ?>"; |
|
626 | + } |
|
627 | 627 | |
628 | - // show all vars |
|
629 | - elseif ( strpos( $html, '{$template_info}' ) !== FALSE ) { |
|
628 | + // show all vars |
|
629 | + elseif ( strpos( $html, '{$template_info}' ) !== FALSE ) { |
|
630 | 630 | |
631 | - //tag |
|
632 | - $tag = '{$template_info}'; |
|
631 | + //tag |
|
632 | + $tag = '{$template_info}'; |
|
633 | 633 | |
634 | - //if code |
|
635 | - $compiled_code .= '<?php echo "<pre>"; print_r( $this->var ); echo "</pre>"; ?>'; |
|
636 | - } |
|
634 | + //if code |
|
635 | + $compiled_code .= '<?php echo "<pre>"; print_r( $this->var ); echo "</pre>"; ?>'; |
|
636 | + } |
|
637 | 637 | |
638 | 638 | |
639 | - //all html code |
|
640 | - else{ |
|
639 | + //all html code |
|
640 | + else{ |
|
641 | 641 | |
642 | - //variables substitution (es. {$title}) |
|
643 | - $html = $this->var_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
644 | - //const substitution (es. {#CONST#}) |
|
645 | - $html = $this->const_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
646 | - //functions substitution (es. {"string"|functions}) |
|
647 | - $compiled_code .= $this->func_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
648 | - } |
|
649 | - } |
|
642 | + //variables substitution (es. {$title}) |
|
643 | + $html = $this->var_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
644 | + //const substitution (es. {#CONST#}) |
|
645 | + $html = $this->const_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
646 | + //functions substitution (es. {"string"|functions}) |
|
647 | + $compiled_code .= $this->func_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true ); |
|
648 | + } |
|
649 | + } |
|
650 | 650 | |
651 | - if( $open_if > 0 ) { |
|
652 | - $e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $this->tpl['tpl_filename'] . ' template'); |
|
653 | - throw $e->setTemplateFile($this->tpl['tpl_filename']); |
|
654 | - } |
|
655 | - return $compiled_code; |
|
656 | - } |
|
651 | + if( $open_if > 0 ) { |
|
652 | + $e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $this->tpl['tpl_filename'] . ' template'); |
|
653 | + throw $e->setTemplateFile($this->tpl['tpl_filename']); |
|
654 | + } |
|
655 | + return $compiled_code; |
|
656 | + } |
|
657 | 657 | |
658 | 658 | |
659 | 659 | |
660 | - /** |
|
661 | - * Reduce a path |
|
662 | - * |
|
663 | - * eg. www/library/../filepath//file => www/filepath/file |
|
664 | - * |
|
665 | - * @param string $path |
|
666 | - * @return string |
|
667 | - */ |
|
668 | - protected function reduce_path( $path ){ |
|
669 | - $path = str_replace( "://", "@not_replace@", $path ); |
|
670 | - $path = str_replace( "//", "/", $path ); |
|
671 | - $path = str_replace( "@not_replace@", "://", $path ); |
|
672 | - return preg_replace('/\w+\/\.\.\//', '', $path ); |
|
673 | - } |
|
660 | + /** |
|
661 | + * Reduce a path |
|
662 | + * |
|
663 | + * eg. www/library/../filepath//file => www/filepath/file |
|
664 | + * |
|
665 | + * @param string $path |
|
666 | + * @return string |
|
667 | + */ |
|
668 | + protected function reduce_path( $path ){ |
|
669 | + $path = str_replace( "://", "@not_replace@", $path ); |
|
670 | + $path = str_replace( "//", "/", $path ); |
|
671 | + $path = str_replace( "@not_replace@", "://", $path ); |
|
672 | + return preg_replace('/\w+\/\.\.\//', '', $path ); |
|
673 | + } |
|
674 | 674 | |
675 | 675 | |
676 | 676 | |
677 | - /** |
|
678 | - * replace the path of image src, link href and a href |
|
679 | - * |
|
680 | - * url => template_dir/url |
|
681 | - * url# => url |
|
682 | - * http://url => http://url |
|
683 | - * |
|
684 | - * @access protected |
|
685 | - * @param string $html |
|
686 | - * @param string $tpl_basedir |
|
687 | - * @return string html substitution |
|
688 | - */ |
|
689 | - protected function path_replace( $html, $tpl_basedir ){ |
|
677 | + /** |
|
678 | + * replace the path of image src, link href and a href |
|
679 | + * |
|
680 | + * url => template_dir/url |
|
681 | + * url# => url |
|
682 | + * http://url => http://url |
|
683 | + * |
|
684 | + * @access protected |
|
685 | + * @param string $html |
|
686 | + * @param string $tpl_basedir |
|
687 | + * @return string html substitution |
|
688 | + */ |
|
689 | + protected function path_replace( $html, $tpl_basedir ){ |
|
690 | 690 | |
691 | - if( self::$path_replace ){ |
|
691 | + if( self::$path_replace ){ |
|
692 | 692 | |
693 | - $tpl_dir = self::$base_url . PATH . self::$tpl_dir . $tpl_basedir; |
|
693 | + $tpl_dir = self::$base_url . PATH . self::$tpl_dir . $tpl_basedir; |
|
694 | 694 | |
695 | - // reduce the path |
|
696 | - $path = $this->reduce_path($tpl_dir); |
|
695 | + // reduce the path |
|
696 | + $path = $this->reduce_path($tpl_dir); |
|
697 | 697 | |
698 | - $exp = $sub = array(); |
|
698 | + $exp = $sub = array(); |
|
699 | 699 | |
700 | - if( in_array( "img", self::$path_replace_list ) ){ |
|
701 | - $exp = array( '/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ); |
|
702 | - $sub = array( '<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"' ); |
|
703 | - } |
|
700 | + if( in_array( "img", self::$path_replace_list ) ){ |
|
701 | + $exp = array( '/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ); |
|
702 | + $sub = array( '<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"' ); |
|
703 | + } |
|
704 | 704 | |
705 | - if( in_array( "script", self::$path_replace_list ) ){ |
|
706 | - $exp = array_merge( $exp , array( '/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
707 | - $sub = array_merge( $sub , array( '<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"' ) ); |
|
708 | - } |
|
705 | + if( in_array( "script", self::$path_replace_list ) ){ |
|
706 | + $exp = array_merge( $exp , array( '/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
707 | + $sub = array_merge( $sub , array( '<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"' ) ); |
|
708 | + } |
|
709 | 709 | |
710 | - if( in_array( "link", self::$path_replace_list ) ){ |
|
711 | - $exp = array_merge( $exp , array( '/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
712 | - $sub = array_merge( $sub , array( '<link$1href=@$2://$3@', '<link$1href=@$2@' , '<link$1href="' . $path . '$2"', '<link$1href="$2"' ) ); |
|
713 | - } |
|
710 | + if( in_array( "link", self::$path_replace_list ) ){ |
|
711 | + $exp = array_merge( $exp , array( '/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
712 | + $sub = array_merge( $sub , array( '<link$1href=@$2://$3@', '<link$1href=@$2@' , '<link$1href="' . $path . '$2"', '<link$1href="$2"' ) ); |
|
713 | + } |
|
714 | 714 | |
715 | - if( in_array( "a", self::$path_replace_list ) ){ |
|
716 | - $exp = array_merge( $exp , array( '/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
717 | - $sub = array_merge( $sub , array( '<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"' ) ); |
|
718 | - } |
|
715 | + if( in_array( "a", self::$path_replace_list ) ){ |
|
716 | + $exp = array_merge( $exp , array( '/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
717 | + $sub = array_merge( $sub , array( '<a$1href=@$2$3@', '<a$1href="' . self::$base_url . '$2"', '<a$1href="$2"' ) ); |
|
718 | + } |
|
719 | 719 | |
720 | - if( in_array( "input", self::$path_replace_list ) ){ |
|
721 | - $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
722 | - $sub = array_merge( $sub , array( '<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"' ) ); |
|
723 | - } |
|
720 | + if( in_array( "input", self::$path_replace_list ) ){ |
|
721 | + $exp = array_merge( $exp , array( '/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i' ) ); |
|
722 | + $sub = array_merge( $sub , array( '<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"' ) ); |
|
723 | + } |
|
724 | 724 | |
725 | - return preg_replace( $exp, $sub, $html ); |
|
725 | + return preg_replace( $exp, $sub, $html ); |
|
726 | 726 | |
727 | - } |
|
728 | - else |
|
729 | - return $html; |
|
727 | + } |
|
728 | + else |
|
729 | + return $html; |
|
730 | 730 | |
731 | - } |
|
731 | + } |
|
732 | 732 | |
733 | 733 | |
734 | 734 | |
735 | - /** |
|
736 | - * replace constants |
|
737 | - * |
|
738 | - * @access public |
|
739 | - * @param string $html |
|
740 | - * @param string $tag_left_delimiter |
|
741 | - * @param string $tag_right_delimiter |
|
742 | - * @param string $php_left_delimiter (optional) |
|
743 | - * @param string $php_right_delimiter (optional) |
|
744 | - * @param string $loop_level (optional) |
|
745 | - * @param string $echo (optional) |
|
746 | - * @return string |
|
747 | - */ |
|
748 | - public function const_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
749 | - // const |
|
750 | - return preg_replace( '/\{\#(\w+)\#{0,1}\}/', $php_left_delimiter . ( $echo ? " echo " : null ) . '\\1' . $php_right_delimiter, $html ); |
|
751 | - } |
|
735 | + /** |
|
736 | + * replace constants |
|
737 | + * |
|
738 | + * @access public |
|
739 | + * @param string $html |
|
740 | + * @param string $tag_left_delimiter |
|
741 | + * @param string $tag_right_delimiter |
|
742 | + * @param string $php_left_delimiter (optional) |
|
743 | + * @param string $php_right_delimiter (optional) |
|
744 | + * @param string $loop_level (optional) |
|
745 | + * @param string $echo (optional) |
|
746 | + * @return string |
|
747 | + */ |
|
748 | + public function const_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
749 | + // const |
|
750 | + return preg_replace( '/\{\#(\w+)\#{0,1}\}/', $php_left_delimiter . ( $echo ? " echo " : null ) . '\\1' . $php_right_delimiter, $html ); |
|
751 | + } |
|
752 | 752 | |
753 | 753 | |
754 | 754 | |
755 | - /** |
|
756 | - * replace functions/modifiers on constants and strings |
|
757 | - * |
|
758 | - * @access public |
|
759 | - * @param string $html |
|
760 | - * @param string $tag_left_delimiter |
|
761 | - * @param string $tag_right_delimiter |
|
762 | - * @param string $php_left_delimiter (optional) |
|
763 | - * @param string $php_right_delimiter (optional) |
|
764 | - * @param string $loop_level (optional) |
|
765 | - * @param string $echo (optional) |
|
766 | - * @return string |
|
767 | - */ |
|
768 | - public function func_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
755 | + /** |
|
756 | + * replace functions/modifiers on constants and strings |
|
757 | + * |
|
758 | + * @access public |
|
759 | + * @param string $html |
|
760 | + * @param string $tag_left_delimiter |
|
761 | + * @param string $tag_right_delimiter |
|
762 | + * @param string $php_left_delimiter (optional) |
|
763 | + * @param string $php_right_delimiter (optional) |
|
764 | + * @param string $loop_level (optional) |
|
765 | + * @param string $echo (optional) |
|
766 | + * @return string |
|
767 | + */ |
|
768 | + public function func_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
769 | 769 | |
770 | - preg_match_all( '/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $html, $matches ); |
|
770 | + preg_match_all( '/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $html, $matches ); |
|
771 | 771 | |
772 | - for( $i=0, $n=count($matches[0]); $i<$n; $i++ ){ |
|
772 | + for( $i=0, $n=count($matches[0]); $i<$n; $i++ ){ |
|
773 | 773 | |
774 | - //complete tag ex: {$news.title|substr:0,100} |
|
775 | - $tag = $matches[ 0 ][ $i ]; |
|
774 | + //complete tag ex: {$news.title|substr:0,100} |
|
775 | + $tag = $matches[ 0 ][ $i ]; |
|
776 | 776 | |
777 | - //variable name ex: news.title |
|
778 | - $var = $matches[ 1 ][ $i ]; |
|
777 | + //variable name ex: news.title |
|
778 | + $var = $matches[ 1 ][ $i ]; |
|
779 | 779 | |
780 | - //function and parameters associate to the variable ex: substr:0,100 |
|
781 | - $extra_var = $matches[ 2 ][ $i ]; |
|
780 | + //function and parameters associate to the variable ex: substr:0,100 |
|
781 | + $extra_var = $matches[ 2 ][ $i ]; |
|
782 | 782 | |
783 | - // check if there's any function disabled by black_list |
|
784 | - $this->function_check( $tag ); |
|
783 | + // check if there's any function disabled by black_list |
|
784 | + $this->function_check( $tag ); |
|
785 | 785 | |
786 | - $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
786 | + $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
787 | 787 | |
788 | 788 | |
789 | - // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
790 | - $is_init_variable = preg_match( "/^(\s*?)\=[^=](.*?)$/", $extra_var ); |
|
789 | + // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
790 | + $is_init_variable = preg_match( "/^(\s*?)\=[^=](.*?)$/", $extra_var ); |
|
791 | 791 | |
792 | - //function associate to variable |
|
793 | - $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
792 | + //function associate to variable |
|
793 | + $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
794 | 794 | |
795 | - //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
796 | - $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
795 | + //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
796 | + $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
797 | 797 | |
798 | - //variable name |
|
799 | - $var_name = $temp[ 0 ]; |
|
798 | + //variable name |
|
799 | + $var_name = $temp[ 0 ]; |
|
800 | 800 | |
801 | - //variable path |
|
802 | - $variable_path = substr( $var, strlen( $var_name ) ); |
|
801 | + //variable path |
|
802 | + $variable_path = substr( $var, strlen( $var_name ) ); |
|
803 | 803 | |
804 | - //parentesis transform [ e ] in [" e in "] |
|
805 | - $variable_path = str_replace( '[', '["', $variable_path ); |
|
806 | - $variable_path = str_replace( ']', '"]', $variable_path ); |
|
804 | + //parentesis transform [ e ] in [" e in "] |
|
805 | + $variable_path = str_replace( '[', '["', $variable_path ); |
|
806 | + $variable_path = str_replace( ']', '"]', $variable_path ); |
|
807 | 807 | |
808 | - //transform .$variable in ["$variable"] |
|
809 | - $variable_path = preg_replace('/\.\$(\w+)/', '["$\\1"]', $variable_path ); |
|
808 | + //transform .$variable in ["$variable"] |
|
809 | + $variable_path = preg_replace('/\.\$(\w+)/', '["$\\1"]', $variable_path ); |
|
810 | 810 | |
811 | - //transform [variable] in ["variable"] |
|
812 | - $variable_path = preg_replace('/\.(\w+)/', '["\\1"]', $variable_path ); |
|
811 | + //transform [variable] in ["variable"] |
|
812 | + $variable_path = preg_replace('/\.(\w+)/', '["\\1"]', $variable_path ); |
|
813 | 813 | |
814 | - //if there's a function |
|
815 | - if( $function_var ){ |
|
814 | + //if there's a function |
|
815 | + if( $function_var ){ |
|
816 | 816 | |
817 | - // check if there's a function or a static method and separate, function by parameters |
|
818 | - $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
817 | + // check if there's a function or a static method and separate, function by parameters |
|
818 | + $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
819 | 819 | |
820 | - // get the position of the first : |
|
821 | - if( $dot_position = strpos( $function_var, ":" ) ){ |
|
820 | + // get the position of the first : |
|
821 | + if( $dot_position = strpos( $function_var, ":" ) ){ |
|
822 | 822 | |
823 | - // get the function and the parameters |
|
824 | - $function = substr( $function_var, 0, $dot_position ); |
|
825 | - $params = substr( $function_var, $dot_position+1 ); |
|
823 | + // get the function and the parameters |
|
824 | + $function = substr( $function_var, 0, $dot_position ); |
|
825 | + $params = substr( $function_var, $dot_position+1 ); |
|
826 | 826 | |
827 | - } |
|
828 | - else{ |
|
827 | + } |
|
828 | + else{ |
|
829 | 829 | |
830 | - //get the function |
|
831 | - $function = str_replace( "@double_dot@", "::", $function_var ); |
|
832 | - $params = null; |
|
830 | + //get the function |
|
831 | + $function = str_replace( "@double_dot@", "::", $function_var ); |
|
832 | + $params = null; |
|
833 | 833 | |
834 | - } |
|
834 | + } |
|
835 | 835 | |
836 | - // replace back the @double_dot@ with :: |
|
837 | - $function = str_replace( "@double_dot@", "::", $function ); |
|
838 | - $params = str_replace( "@double_dot@", "::", $params ); |
|
836 | + // replace back the @double_dot@ with :: |
|
837 | + $function = str_replace( "@double_dot@", "::", $function ); |
|
838 | + $params = str_replace( "@double_dot@", "::", $params ); |
|
839 | 839 | |
840 | 840 | |
841 | - } |
|
842 | - else |
|
843 | - $function = $params = null; |
|
841 | + } |
|
842 | + else |
|
843 | + $function = $params = null; |
|
844 | 844 | |
845 | - $php_var = $var_name . $variable_path; |
|
845 | + $php_var = $var_name . $variable_path; |
|
846 | 846 | |
847 | - // compile the variable for php |
|
848 | - if( isset( $function ) ){ |
|
849 | - if( $php_var ) |
|
850 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
851 | - else |
|
852 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $params ) )" : "$function()" ) . $php_right_delimiter; |
|
853 | - } |
|
854 | - else |
|
855 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
847 | + // compile the variable for php |
|
848 | + if( isset( $function ) ){ |
|
849 | + if( $php_var ) |
|
850 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
851 | + else |
|
852 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $params ) )" : "$function()" ) . $php_right_delimiter; |
|
853 | + } |
|
854 | + else |
|
855 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
856 | 856 | |
857 | - $html = str_replace( $tag, $php_var, $html ); |
|
857 | + $html = str_replace( $tag, $php_var, $html ); |
|
858 | 858 | |
859 | - } |
|
859 | + } |
|
860 | 860 | |
861 | - return $html; |
|
861 | + return $html; |
|
862 | 862 | |
863 | - } |
|
863 | + } |
|
864 | 864 | |
865 | 865 | |
866 | 866 | |
867 | - /** |
|
868 | - * replace variables |
|
869 | - * |
|
870 | - * @access public |
|
871 | - * @param string $html |
|
872 | - * @param string $tag_left_delimiter |
|
873 | - * @param string $tag_right_delimiter |
|
874 | - * @param string $php_left_delimiter (optional) |
|
875 | - * @param string $php_right_delimiter (optional) |
|
876 | - * @param string $loop_level (optional) |
|
877 | - * @param string $echo (optional) |
|
878 | - * @return string |
|
879 | - */ |
|
880 | - public function var_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
867 | + /** |
|
868 | + * replace variables |
|
869 | + * |
|
870 | + * @access public |
|
871 | + * @param string $html |
|
872 | + * @param string $tag_left_delimiter |
|
873 | + * @param string $tag_right_delimiter |
|
874 | + * @param string $php_left_delimiter (optional) |
|
875 | + * @param string $php_right_delimiter (optional) |
|
876 | + * @param string $loop_level (optional) |
|
877 | + * @param string $echo (optional) |
|
878 | + * @return string |
|
879 | + */ |
|
880 | + public function var_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){ |
|
881 | 881 | |
882 | - //all variables |
|
883 | - 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 | + //all variables |
|
883 | + 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 ) ){ |
|
884 | 884 | |
885 | - for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++ ) |
|
886 | - $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]); |
|
885 | + for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++ ) |
|
886 | + $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]); |
|
887 | 887 | |
888 | - foreach( $parsed as $tag => $array ){ |
|
888 | + foreach( $parsed as $tag => $array ){ |
|
889 | 889 | |
890 | - //variable name ex: news.title |
|
891 | - $var = $array['var']; |
|
890 | + //variable name ex: news.title |
|
891 | + $var = $array['var']; |
|
892 | 892 | |
893 | - //function and parameters associate to the variable ex: substr:0,100 |
|
894 | - $extra_var = $array['extra_var']; |
|
893 | + //function and parameters associate to the variable ex: substr:0,100 |
|
894 | + $extra_var = $array['extra_var']; |
|
895 | 895 | |
896 | - // check if there's any function disabled by black_list |
|
897 | - $this->function_check( $tag ); |
|
896 | + // check if there's any function disabled by black_list |
|
897 | + $this->function_check( $tag ); |
|
898 | 898 | |
899 | - $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
899 | + $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level ); |
|
900 | 900 | |
901 | - // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
902 | - $is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var ); |
|
901 | + // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value |
|
902 | + $is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var ); |
|
903 | 903 | |
904 | - //function associate to variable |
|
905 | - $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
904 | + //function associate to variable |
|
905 | + $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null; |
|
906 | 906 | |
907 | - //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
908 | - $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
907 | + //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title) |
|
908 | + $temp = preg_split( "/\.|\[|\-\>/", $var ); |
|
909 | 909 | |
910 | - //variable name |
|
911 | - $var_name = $temp[ 0 ]; |
|
910 | + //variable name |
|
911 | + $var_name = $temp[ 0 ]; |
|
912 | 912 | |
913 | - //variable path |
|
914 | - $variable_path = substr( $var, strlen( $var_name ) ); |
|
913 | + //variable path |
|
914 | + $variable_path = substr( $var, strlen( $var_name ) ); |
|
915 | 915 | |
916 | - //parentesis transform [ e ] in [" e in "] |
|
917 | - $variable_path = str_replace( '[', '["', $variable_path ); |
|
918 | - $variable_path = str_replace( ']', '"]', $variable_path ); |
|
916 | + //parentesis transform [ e ] in [" e in "] |
|
917 | + $variable_path = str_replace( '[', '["', $variable_path ); |
|
918 | + $variable_path = str_replace( ']', '"]', $variable_path ); |
|
919 | 919 | |
920 | - //transform .$variable in ["$variable"] and .variable in ["variable"] |
|
921 | - $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path ); |
|
920 | + //transform .$variable in ["$variable"] and .variable in ["variable"] |
|
921 | + $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path ); |
|
922 | 922 | |
923 | - // if is an assignment also assign the variable to $this->var['value'] |
|
924 | - if( $is_init_variable ) |
|
925 | - $extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var; |
|
923 | + // if is an assignment also assign the variable to $this->var['value'] |
|
924 | + if( $is_init_variable ) |
|
925 | + $extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var; |
|
926 | 926 | |
927 | 927 | |
928 | 928 | |
929 | - //if there's a function |
|
930 | - if( $function_var ){ |
|
929 | + //if there's a function |
|
930 | + if( $function_var ){ |
|
931 | 931 | |
932 | - // check if there's a function or a static method and separate, function by parameters |
|
933 | - $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
932 | + // check if there's a function or a static method and separate, function by parameters |
|
933 | + $function_var = str_replace("::", "@double_dot@", $function_var ); |
|
934 | 934 | |
935 | 935 | |
936 | - // get the position of the first : |
|
937 | - if( $dot_position = strpos( $function_var, ":" ) ){ |
|
936 | + // get the position of the first : |
|
937 | + if( $dot_position = strpos( $function_var, ":" ) ){ |
|
938 | 938 | |
939 | - // get the function and the parameters |
|
940 | - $function = substr( $function_var, 0, $dot_position ); |
|
941 | - $params = substr( $function_var, $dot_position+1 ); |
|
939 | + // get the function and the parameters |
|
940 | + $function = substr( $function_var, 0, $dot_position ); |
|
941 | + $params = substr( $function_var, $dot_position+1 ); |
|
942 | 942 | |
943 | - } |
|
944 | - else{ |
|
943 | + } |
|
944 | + else{ |
|
945 | 945 | |
946 | - //get the function |
|
947 | - $function = str_replace( "@double_dot@", "::", $function_var ); |
|
948 | - $params = null; |
|
946 | + //get the function |
|
947 | + $function = str_replace( "@double_dot@", "::", $function_var ); |
|
948 | + $params = null; |
|
949 | 949 | |
950 | - } |
|
950 | + } |
|
951 | 951 | |
952 | - // replace back the @double_dot@ with :: |
|
953 | - $function = str_replace( "@double_dot@", "::", $function ); |
|
954 | - $params = str_replace( "@double_dot@", "::", $params ); |
|
955 | - } |
|
956 | - else |
|
957 | - $function = $params = null; |
|
952 | + // replace back the @double_dot@ with :: |
|
953 | + $function = str_replace( "@double_dot@", "::", $function ); |
|
954 | + $params = str_replace( "@double_dot@", "::", $params ); |
|
955 | + } |
|
956 | + else |
|
957 | + $function = $params = null; |
|
958 | 958 | |
959 | - //if it is inside a loop |
|
960 | - if( $loop_level ){ |
|
961 | - //verify the variable name |
|
962 | - if( $var_name == 'key' ) |
|
963 | - $php_var = '$key' . $loop_level; |
|
964 | - elseif( $var_name == 'value' ) |
|
965 | - $php_var = '$value' . $loop_level . $variable_path; |
|
966 | - elseif( $var_name == 'counter' ) |
|
967 | - $php_var = '$counter' . $loop_level; |
|
968 | - else |
|
969 | - $php_var = '$' . $var_name . $variable_path; |
|
970 | - }else |
|
971 | - $php_var = '$' . $var_name . $variable_path; |
|
972 | - |
|
973 | - // compile the variable for php |
|
974 | - if( isset( $function ) ) |
|
975 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
976 | - else |
|
977 | - $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
978 | - |
|
979 | - $html = str_replace( $tag, $php_var, $html ); |
|
980 | - |
|
981 | - |
|
982 | - } |
|
983 | - } |
|
984 | - |
|
985 | - return $html; |
|
986 | - } |
|
987 | - |
|
988 | - |
|
989 | - |
|
990 | - /** |
|
991 | - * Check if function is in black list (sandbox) |
|
992 | - * |
|
993 | - * @access protected |
|
994 | - * @param string $code |
|
995 | - * @throws RainTpl_SyntaxException |
|
996 | - * @return void |
|
997 | - */ |
|
998 | - protected function function_check( $code ){ |
|
999 | - |
|
1000 | - $preg = '#(\W|\s)' . implode( '(\W|\s)|(\W|\s)', self::$black_list ) . '(\W|\s)#'; |
|
1001 | - |
|
1002 | - // check if the function is in the black list (or not in white list) |
|
1003 | - if( count(self::$black_list) && preg_match( $preg, $code, $match ) ){ |
|
1004 | - |
|
1005 | - // find the line of the error |
|
1006 | - $line = 0; |
|
1007 | - $rows=explode("\n",$this->tpl['source']); |
|
1008 | - while( !strpos($rows[$line],$code) ) |
|
1009 | - $line++; |
|
1010 | - |
|
1011 | - // stop the execution of the script |
|
1012 | - $e = new RainTpl_SyntaxException('Unallowed syntax in ' . $this->tpl['tpl_filename'] . ' template'); |
|
1013 | - throw $e->setTemplateFile($this->tpl['tpl_filename']) |
|
1014 | - ->setTag($code) |
|
1015 | - ->setTemplateLine($line); |
|
1016 | - } |
|
1017 | - |
|
1018 | - } |
|
1019 | - |
|
1020 | - |
|
1021 | - |
|
1022 | - /** |
|
1023 | - * Prints debug info about exception or passes it further if debug is disabled. |
|
1024 | - * |
|
1025 | - * @access protected |
|
1026 | - * @param RainTpl_Exception $e |
|
1027 | - * @return string |
|
1028 | - */ |
|
1029 | - protected function printDebug(RainTpl_Exception $e){ |
|
1030 | - if (!self::$debug) { |
|
1031 | - throw $e; |
|
1032 | - } |
|
1033 | - $output = sprintf('<h2>Exception: %s</h2><h3>%s</h3><p>template: %s</p>', |
|
1034 | - get_class($e), |
|
1035 | - $e->getMessage(), |
|
1036 | - $e->getTemplateFile() |
|
1037 | - ); |
|
1038 | - if ($e instanceof RainTpl_SyntaxException) { |
|
1039 | - if (null != $e->getTemplateLine()) { |
|
1040 | - $output .= '<p>line: ' . $e->getTemplateLine() . '</p>'; |
|
1041 | - } |
|
1042 | - if (null != $e->getTag()) { |
|
1043 | - $output .= '<p>in tag: ' . htmlspecialchars($e->getTag()) . '</p>'; |
|
1044 | - } |
|
1045 | - if (null != $e->getTemplateLine() && null != $e->getTag()) { |
|
1046 | - $rows=explode("\n", htmlspecialchars($this->tpl['source'])); |
|
1047 | - $rows[$e->getTemplateLine()] = '<font color=red>' . $rows[$e->getTemplateLine()] . '</font>'; |
|
1048 | - $output .= '<h3>template code</h3>' . implode('<br />', $rows) . '</pre>'; |
|
1049 | - } |
|
1050 | - } |
|
1051 | - $output .= sprintf('<h3>trace</h3><p>In %s on line %d</p><pre>%s</pre>', |
|
1052 | - $e->getFile(), $e->getLine(), |
|
1053 | - nl2br(htmlspecialchars($e->getTraceAsString())) |
|
1054 | - ); |
|
1055 | - return $output; |
|
1056 | - } |
|
959 | + //if it is inside a loop |
|
960 | + if( $loop_level ){ |
|
961 | + //verify the variable name |
|
962 | + if( $var_name == 'key' ) |
|
963 | + $php_var = '$key' . $loop_level; |
|
964 | + elseif( $var_name == 'value' ) |
|
965 | + $php_var = '$value' . $loop_level . $variable_path; |
|
966 | + elseif( $var_name == 'counter' ) |
|
967 | + $php_var = '$counter' . $loop_level; |
|
968 | + else |
|
969 | + $php_var = '$' . $var_name . $variable_path; |
|
970 | + }else |
|
971 | + $php_var = '$' . $var_name . $variable_path; |
|
972 | + |
|
973 | + // compile the variable for php |
|
974 | + if( isset( $function ) ) |
|
975 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter; |
|
976 | + else |
|
977 | + $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter; |
|
978 | + |
|
979 | + $html = str_replace( $tag, $php_var, $html ); |
|
980 | + |
|
981 | + |
|
982 | + } |
|
983 | + } |
|
984 | + |
|
985 | + return $html; |
|
986 | + } |
|
987 | + |
|
988 | + |
|
989 | + |
|
990 | + /** |
|
991 | + * Check if function is in black list (sandbox) |
|
992 | + * |
|
993 | + * @access protected |
|
994 | + * @param string $code |
|
995 | + * @throws RainTpl_SyntaxException |
|
996 | + * @return void |
|
997 | + */ |
|
998 | + protected function function_check( $code ){ |
|
999 | + |
|
1000 | + $preg = '#(\W|\s)' . implode( '(\W|\s)|(\W|\s)', self::$black_list ) . '(\W|\s)#'; |
|
1001 | + |
|
1002 | + // check if the function is in the black list (or not in white list) |
|
1003 | + if( count(self::$black_list) && preg_match( $preg, $code, $match ) ){ |
|
1004 | + |
|
1005 | + // find the line of the error |
|
1006 | + $line = 0; |
|
1007 | + $rows=explode("\n",$this->tpl['source']); |
|
1008 | + while( !strpos($rows[$line],$code) ) |
|
1009 | + $line++; |
|
1010 | + |
|
1011 | + // stop the execution of the script |
|
1012 | + $e = new RainTpl_SyntaxException('Unallowed syntax in ' . $this->tpl['tpl_filename'] . ' template'); |
|
1013 | + throw $e->setTemplateFile($this->tpl['tpl_filename']) |
|
1014 | + ->setTag($code) |
|
1015 | + ->setTemplateLine($line); |
|
1016 | + } |
|
1017 | + |
|
1018 | + } |
|
1019 | + |
|
1020 | + |
|
1021 | + |
|
1022 | + /** |
|
1023 | + * Prints debug info about exception or passes it further if debug is disabled. |
|
1024 | + * |
|
1025 | + * @access protected |
|
1026 | + * @param RainTpl_Exception $e |
|
1027 | + * @return string |
|
1028 | + */ |
|
1029 | + protected function printDebug(RainTpl_Exception $e){ |
|
1030 | + if (!self::$debug) { |
|
1031 | + throw $e; |
|
1032 | + } |
|
1033 | + $output = sprintf('<h2>Exception: %s</h2><h3>%s</h3><p>template: %s</p>', |
|
1034 | + get_class($e), |
|
1035 | + $e->getMessage(), |
|
1036 | + $e->getTemplateFile() |
|
1037 | + ); |
|
1038 | + if ($e instanceof RainTpl_SyntaxException) { |
|
1039 | + if (null != $e->getTemplateLine()) { |
|
1040 | + $output .= '<p>line: ' . $e->getTemplateLine() . '</p>'; |
|
1041 | + } |
|
1042 | + if (null != $e->getTag()) { |
|
1043 | + $output .= '<p>in tag: ' . htmlspecialchars($e->getTag()) . '</p>'; |
|
1044 | + } |
|
1045 | + if (null != $e->getTemplateLine() && null != $e->getTag()) { |
|
1046 | + $rows=explode("\n", htmlspecialchars($this->tpl['source'])); |
|
1047 | + $rows[$e->getTemplateLine()] = '<font color=red>' . $rows[$e->getTemplateLine()] . '</font>'; |
|
1048 | + $output .= '<h3>template code</h3>' . implode('<br />', $rows) . '</pre>'; |
|
1049 | + } |
|
1050 | + } |
|
1051 | + $output .= sprintf('<h3>trace</h3><p>In %s on line %d</p><pre>%s</pre>', |
|
1052 | + $e->getFile(), $e->getLine(), |
|
1053 | + nl2br(htmlspecialchars($e->getTraceAsString())) |
|
1054 | + ); |
|
1055 | + return $output; |
|
1056 | + } |
|
1057 | 1057 | } |
1058 | 1058 | |
1059 | 1059 | |
@@ -1062,32 +1062,32 @@ discard block |
||
1062 | 1062 | * Basic Rain tpl exception. |
1063 | 1063 | */ |
1064 | 1064 | class RainTpl_Exception extends Exception{ |
1065 | - /** |
|
1066 | - * Path of template file with error. |
|
1067 | - */ |
|
1068 | - protected $templateFile = ''; |
|
1069 | - |
|
1070 | - /** |
|
1071 | - * Returns path of template file with error. |
|
1072 | - * |
|
1073 | - * @return string |
|
1074 | - */ |
|
1075 | - public function getTemplateFile() |
|
1076 | - { |
|
1077 | - return $this->templateFile; |
|
1078 | - } |
|
1079 | - |
|
1080 | - /** |
|
1081 | - * Sets path of template file with error. |
|
1082 | - * |
|
1083 | - * @param string $templateFile |
|
1084 | - * @return RainTpl_Exception |
|
1085 | - */ |
|
1086 | - public function setTemplateFile($templateFile) |
|
1087 | - { |
|
1088 | - $this->templateFile = (string) $templateFile; |
|
1089 | - return $this; |
|
1090 | - } |
|
1065 | + /** |
|
1066 | + * Path of template file with error. |
|
1067 | + */ |
|
1068 | + protected $templateFile = ''; |
|
1069 | + |
|
1070 | + /** |
|
1071 | + * Returns path of template file with error. |
|
1072 | + * |
|
1073 | + * @return string |
|
1074 | + */ |
|
1075 | + public function getTemplateFile() |
|
1076 | + { |
|
1077 | + return $this->templateFile; |
|
1078 | + } |
|
1079 | + |
|
1080 | + /** |
|
1081 | + * Sets path of template file with error. |
|
1082 | + * |
|
1083 | + * @param string $templateFile |
|
1084 | + * @return RainTpl_Exception |
|
1085 | + */ |
|
1086 | + public function setTemplateFile($templateFile) |
|
1087 | + { |
|
1088 | + $this->templateFile = (string) $templateFile; |
|
1089 | + return $this; |
|
1090 | + } |
|
1091 | 1091 | } |
1092 | 1092 | |
1093 | 1093 | |
@@ -1102,64 +1102,64 @@ discard block |
||
1102 | 1102 | * Exception thrown when syntax error occurs. |
1103 | 1103 | */ |
1104 | 1104 | class RainTpl_SyntaxException extends RainTpl_Exception{ |
1105 | - /** |
|
1106 | - * Line in template file where error has occured. |
|
1107 | - * |
|
1108 | - * @var int | null |
|
1109 | - */ |
|
1110 | - protected $templateLine = null; |
|
1111 | - |
|
1112 | - /** |
|
1113 | - * Tag which caused an error. |
|
1114 | - * |
|
1115 | - * @var string | null |
|
1116 | - */ |
|
1117 | - protected $tag = null; |
|
1118 | - |
|
1119 | - /** |
|
1120 | - * Returns line in template file where error has occured |
|
1121 | - * or null if line is not defined. |
|
1122 | - * |
|
1123 | - * @return int | null |
|
1124 | - */ |
|
1125 | - public function getTemplateLine() |
|
1126 | - { |
|
1127 | - return $this->templateLine; |
|
1128 | - } |
|
1129 | - |
|
1130 | - /** |
|
1131 | - * Sets line in template file where error has occured. |
|
1132 | - * |
|
1133 | - * @param int $templateLine |
|
1134 | - * @return RainTpl_SyntaxException |
|
1135 | - */ |
|
1136 | - public function setTemplateLine($templateLine) |
|
1137 | - { |
|
1138 | - $this->templateLine = (int) $templateLine; |
|
1139 | - return $this; |
|
1140 | - } |
|
1141 | - |
|
1142 | - /** |
|
1143 | - * Returns tag which caused an error. |
|
1144 | - * |
|
1145 | - * @return string |
|
1146 | - */ |
|
1147 | - public function getTag() |
|
1148 | - { |
|
1149 | - return $this->tag; |
|
1150 | - } |
|
1151 | - |
|
1152 | - /** |
|
1153 | - * Sets tag which caused an error. |
|
1154 | - * |
|
1155 | - * @param string $tag |
|
1156 | - * @return RainTpl_SyntaxException |
|
1157 | - */ |
|
1158 | - public function setTag($tag) |
|
1159 | - { |
|
1160 | - $this->tag = (string) $tag; |
|
1161 | - return $this; |
|
1162 | - } |
|
1105 | + /** |
|
1106 | + * Line in template file where error has occured. |
|
1107 | + * |
|
1108 | + * @var int | null |
|
1109 | + */ |
|
1110 | + protected $templateLine = null; |
|
1111 | + |
|
1112 | + /** |
|
1113 | + * Tag which caused an error. |
|
1114 | + * |
|
1115 | + * @var string | null |
|
1116 | + */ |
|
1117 | + protected $tag = null; |
|
1118 | + |
|
1119 | + /** |
|
1120 | + * Returns line in template file where error has occured |
|
1121 | + * or null if line is not defined. |
|
1122 | + * |
|
1123 | + * @return int | null |
|
1124 | + */ |
|
1125 | + public function getTemplateLine() |
|
1126 | + { |
|
1127 | + return $this->templateLine; |
|
1128 | + } |
|
1129 | + |
|
1130 | + /** |
|
1131 | + * Sets line in template file where error has occured. |
|
1132 | + * |
|
1133 | + * @param int $templateLine |
|
1134 | + * @return RainTpl_SyntaxException |
|
1135 | + */ |
|
1136 | + public function setTemplateLine($templateLine) |
|
1137 | + { |
|
1138 | + $this->templateLine = (int) $templateLine; |
|
1139 | + return $this; |
|
1140 | + } |
|
1141 | + |
|
1142 | + /** |
|
1143 | + * Returns tag which caused an error. |
|
1144 | + * |
|
1145 | + * @return string |
|
1146 | + */ |
|
1147 | + public function getTag() |
|
1148 | + { |
|
1149 | + return $this->tag; |
|
1150 | + } |
|
1151 | + |
|
1152 | + /** |
|
1153 | + * Sets tag which caused an error. |
|
1154 | + * |
|
1155 | + * @param string $tag |
|
1156 | + * @return RainTpl_SyntaxException |
|
1157 | + */ |
|
1158 | + public function setTag($tag) |
|
1159 | + { |
|
1160 | + $this->tag = (string) $tag; |
|
1161 | + return $this; |
|
1162 | + } |
|
1163 | 1163 | } |
1164 | 1164 | |
1165 | 1165 | /** |
@@ -1173,7 +1173,7 @@ discard block |
||
1173 | 1173 | * @return string |
1174 | 1174 | */ |
1175 | 1175 | function t() { |
1176 | - return call_user_func_array(array('i18n', 'translate'), func_get_args()); |
|
1176 | + return call_user_func_array(array('i18n', 'translate'), func_get_args()); |
|
1177 | 1177 | } |
1178 | 1178 | |
1179 | 1179 | // -- end |
@@ -93,7 +93,7 @@ |
||
93 | 93 | */ |
94 | 94 | public static function setPath($path) |
95 | 95 | { |
96 | - self::$_salt = ''; |
|
96 | + self::$_salt = ''; |
|
97 | 97 | parent::setPath($path); |
98 | 98 | } |
99 | 99 | } |
@@ -98,5 +98,5 @@ |
||
98 | 98 | $diff |= ord($a[$i]) ^ ord($b[$i]); |
99 | 99 | } |
100 | 100 | return $diff === 0; |
101 | - } |
|
101 | + } |
|
102 | 102 | } |