6 * Realized by Federico Ulfo & maintained by the Rain Team
7 * Distributed under the MIT license http://www.opensource.org/licenses/mit-license.php
15 // -------------------------
17 // -------------------------
24 static $tpl_dir = "tpl/";
28 * Cache directory. Is the directory where RainTPL will compile the template and save the cache
32 static $cache_dir = "tmp/";
36 * Template base URL. RainTPL will add this URL to the relative paths of element selected in $path_replace_list.
40 static $base_url = null;
48 static $tpl_ext = "html";
52 * 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="...">)
53 * Set true to enable the path replace.
57 static $path_replace = true;
61 * You can set what the path_replace method will replace.
62 * Avaible options: a, img, link, script, input
66 static $path_replace_list = array( 'a', 'img', 'link', 'script', 'input' );
70 * You can define in the black list what string are disabled into the template tags
74 static $black_list = array( '\$this', 'raintpl::', 'self::', '_SESSION', '_SERVER', '_ENV', 'eval', 'exec', 'unlink', 'rmdir' );
79 * true: checks template update time, if changed it compile them
80 * false: loads the compiled template. Set false if server doesn't have write permission for cache_directory.
83 static $check_template_update = true;
88 * True: php tags are enabled into the template
89 * False: php tags are disabled into the template and rendered as html
93 static $php_enabled = false;
98 * True: debug mode is used, syntax errors are displayed directly in template. Execution of script is not terminated.
99 * False: exception is thrown on found error.
103 static $debug = false;
105 // -------------------------
108 // -------------------------
110 // -------------------------
113 * Is the array where RainTPL keep the variables assigned
117 public $var = array();
119 protected $tpl = array(), // variables to keep the template directories and info
120 $cache = false, // static cache enabled / disabled
121 $cache_id = null; // identify only one cache
123 protected static $config_name_sum = array(); // takes all the config to create the md5 of the file
125 // -------------------------
129 const CACHE_EXPIRE_TIME
= 3600; // default cache expire time = hour
135 * eg. $t->assign('name','mickey');
137 * @param mixed $variable_name Name of template variable or associative array name/value
138 * @param mixed $value value assigned to this variable. Not set if variable_name is an associative array
141 function assign( $variable, $value = null ){
142 if( is_array( $variable ) )
143 $this->var = $variable +
$this->var;
145 $this->var[ $variable ] = $value;
152 * eg. $html = $tpl->draw( 'demo', TRUE ); // return template in string
153 * or $tpl->draw( $tpl_name ); // echo the template
155 * @param string $tpl_name template to load
156 * @param boolean $return_string true=return a string, false=echo the template
160 function draw( $tpl_name, $return_string = false ){
163 // compile the template if necessary and set the template filepath
164 $this->check_template( $tpl_name );
165 } catch (RainTpl_Exception
$e) {
166 $output = $this->printDebug($e);
170 // Cache is off and, return_string is false
171 // Rain just echo the template
173 if( !$this->cache
&& !$return_string ){
174 extract( $this->var );
175 include $this->tpl
['compiled_filename'];
180 // cache or return_string are enabled
181 // rain get the output buffer to save the output in the cache or to return it as string
185 //----------------------
186 // get the output buffer
187 //----------------------
189 extract( $this->var );
190 include $this->tpl
['compiled_filename'];
191 $raintpl_contents = ob_get_clean();
192 //----------------------
195 // save the output in the cache
197 file_put_contents( $this->tpl
['cache_filename'], "<?php if(!class_exists('raintpl')){exit;}?>" . $raintpl_contents );
202 // return or print the template
203 if( $return_string ) return $raintpl_contents; else echo $raintpl_contents;
212 * If exists a valid cache for this template it returns the cache
214 * @param string $tpl_name Name of template (set the same of draw)
215 * @param int $expiration_time Set after how many seconds the cache expire and must be regenerated
216 * @return string it return the HTML or null if the cache must be recreated
219 function cache( $tpl_name, $expire_time = self
::CACHE_EXPIRE_TIME
, $cache_id = null ){
222 $this->cache_id
= $cache_id;
224 if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl
['cache_filename'] ) && ( time() - filemtime( $this->tpl
['cache_filename'] ) < $expire_time ) ){
225 // return the cached file as HTML. It remove the first 43 character, which are a PHP code to secure the file <?php if(!class_exists('raintpl')){exit;}? >
226 return substr( file_get_contents( $this->tpl
['cache_filename'] ), 43 );
229 //delete the cache of the selected template
230 if (file_exists($this->tpl
['cache_filename']))
231 unlink($this->tpl
['cache_filename'] );
239 * Configure the settings of RainTPL
242 static function configure( $setting, $value = null ){
243 if( is_array( $setting ) )
244 foreach( $setting as $key => $value )
245 self
::configure( $key, $value );
246 else if( property_exists( __CLASS__
, $setting ) ){
247 self
::$
$setting = $value;
248 self
::$config_name_sum[ $setting ] = $value; // take trace of all config
254 // check if has to compile the template
255 // return true if the template has changed
256 protected function check_template( $tpl_name ){
258 if( !isset($this->tpl
['checked']) ){
260 $tpl_basename = basename( $tpl_name ); // template basename
261 $tpl_basedir = strpos($tpl_name,"/") ? dirname($tpl_name) . '/' : null; // template basedirectory
262 $this->tpl
['template_directory'] = self
::$tpl_dir . $tpl_basedir; // template directory
263 $this->tpl
['tpl_filename'] = $this->tpl
['template_directory'] . $tpl_basename . '.' . self
::$tpl_ext; // template filename
264 $temp_compiled_filename = self
::$cache_dir . $tpl_basename . "." . md5( $this->tpl
['template_directory'] . serialize(self
::$config_name_sum));
265 $this->tpl
['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename
266 $this->tpl
['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id
. '.rtpl.php'; // static cache filename
267 $this->tpl
['checked'] = true;
269 // if the template doesn't exist and is not an external source throw an error
270 if( self
::$check_template_update && !file_exists( $this->tpl
['tpl_filename'] ) && !preg_match('/http/', $tpl_name) ){
271 $e = new RainTpl_NotFoundException( 'Template '. $tpl_basename .' not found!' );
272 throw $e->setTemplateFile($this->tpl
['tpl_filename']);
275 // We check if the template is not an external source
276 if(preg_match('/http/', $tpl_name)){
277 $this->compileFile('', '', $tpl_name, self
::$cache_dir, $this->tpl
['compiled_filename'] );
280 // file doesn't exist, or the template was updated, Rain will compile the template
281 elseif( !file_exists( $this->tpl
['compiled_filename'] ) || ( self
::$check_template_update && filemtime($this->tpl
['compiled_filename']) < filemtime( $this->tpl
['tpl_filename'] ) ) ){
282 $this->compileFile( $tpl_basename, $tpl_basedir, $this->tpl
['tpl_filename'], self
::$cache_dir, $this->tpl
['compiled_filename'] );
291 * execute stripslaches() on the xml block. Invoqued by preg_replace_callback function below
294 protected function xml_reSubstitution($capture) {
295 return "<?php echo '<?xml ".stripslashes($capture[1])." ?>'; ?>";
299 * Compile and write the compiled template file
302 protected function compileFile( $tpl_basename, $tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename ){
305 $this->tpl
['source'] = $template_code = file_get_contents( $tpl_filename );
308 $template_code = preg_replace( "/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code );
311 if( !self
::$php_enabled )
312 $template_code = str_replace( array("<?","?>"), array("<?","?>"), $template_code );
314 //xml re-substitution
315 $template_code = preg_replace_callback ( "/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code );
318 $template_compiled = "<?php if(!class_exists('raintpl')){exit;}?>" . $this->compileTemplate( $template_code, $tpl_basedir );
321 // fix the php-eating-newline-after-closing-tag-problem
322 $template_compiled = str_replace( "?>\n", "?>\n\n", $template_compiled );
324 // create directories
325 if( !is_dir( $cache_dir ) )
326 mkdir( $cache_dir, 0755, true );
328 if( !is_writable( $cache_dir ) )
329 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/');
331 //write compiled file
332 file_put_contents( $compiled_filename, $template_compiled );
341 protected function compileTemplate( $template_code, $tpl_basedir ){
344 $tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})',
345 'break' => '(\{break\})',
346 'loop_close' => '(\{\/loop\})',
347 'if' => '(\{if(?: condition){0,1}="[^"]*"\})',
348 'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})',
349 'else' => '(\{else\})',
350 'if_close' => '(\{\/if\})',
351 'function' => '(\{function="[^"]*"\})',
352 'noparse' => '(\{noparse\})',
353 'noparse_close'=> '(\{\/noparse\})',
354 'ignore' => '(\{ignore\}|\{\*)',
355 'ignore_close' => '(\{\/ignore\}|\*\})',
356 'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})',
357 'template_info'=> '(\{\$template_info\})',
358 'function' => '(\{function="(\w*?)(?:.*?)"\})'
361 $tag_regexp = "/" . join( "|", $tag_regexp ) . "/";
363 //path replace (src of img, background and href of link)
364 $template_code = $this->path_replace( $template_code, $tpl_basedir );
366 //split the code with the tags regexp
367 $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE
| PREG_SPLIT_NO_EMPTY
);
370 $compiled_code = $this->compileCode( $template_code );
372 //return the compiled code
373 return $compiled_code;
383 protected function compileCode( $parsed_code ){
385 // if parsed code is empty return null string
389 //variables initialization
390 $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null;
394 //read all parsed code
395 foreach( $parsed_code as $html ){
398 if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) )
399 $ignore_is_open = false;
401 //code between tag ignore id deleted
402 elseif( $ignore_is_open ){
407 elseif( strpos( $html, '{/noparse}' ) !== FALSE )
408 $comment_is_open = false;
410 //code between tag noparse is not compiled
411 elseif( $comment_is_open )
412 $compiled_code .= $html;
415 elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE )
416 $ignore_is_open = true;
419 elseif( strpos( $html, '{noparse}' ) !== FALSE )
420 $comment_is_open = true;
423 elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){
424 if (preg_match("/http/", $code[1])) {
425 $content = file_get_contents($code[1]);
426 $compiled_code .= $content;
428 //variables substitution
429 $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level );
431 //get the folder of the actual template
432 $actual_folder = substr( $this->tpl
['template_directory'], strlen(self
::$tpl_dir) );
434 //get the included template
435 $include_template = $actual_folder . $include_var;
438 $include_template = $this->reduce_path( $include_template );
440 // if the cache is active
441 if( isset($code[ 2 ]) ){
444 $compiled_code .= '<?php $tpl = new '.get_called_class().';' .
445 'if( $cache = $tpl->cache( "'.$include_template.'" ) )' .
448 '$tpl->assign( $this->var );' .
449 ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ).
450 '$tpl->draw( "'.$include_template.'" );'.
457 $compiled_code .= '<?php $tpl = new '.get_called_class().';' .
458 '$tpl->assign( $this->var );' .
459 ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ).
460 '$tpl->draw( "'.$include_template.'" );'.
468 elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){
470 //increase the loop counter
473 //replace the variable in the loop
474 $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 );
477 $counter = "\$counter$loop_level"; // count iteration
478 $key = "\$key$loop_level"; // key
479 $value = "\$value$loop_level"; // value
482 $compiled_code .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>";
487 elseif( strpos( $html, '{
break}' ) !== FALSE ) {
490 $compiled_code .= '<?php
break; ?>';
495 elseif( strpos( $html, '{
/loop
}' ) !== FALSE ) {
498 $counter = "\$counter$loop_level";
500 //decrease the loop counter
504 $compiled_code .= "<?php } ?>";
509 elseif( preg_match( '/\{
if(?: condition
){0,1}="([^"]*)"\}/', $html, $code ) ){
511 //increase open if counter (for intendation)
517 //condition attribute
518 $condition = $code[ 1 ];
520 // check if there's any function disabled by black_list
521 $this->function_check( $tag );
523 //variable substitution into condition (no delimiter into the condition)
524 $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level );
527 $compiled_code .= "<?php
if( $parsed_condition ){ ?>";
532 elseif( preg_match( '/\{elseif(?: condition){0,1}="([^
"]*)"\
}/', $html, $code ) ){
537 //condition attribute
538 $condition = $code[ 1 ];
540 //variable substitution into condition (no delimiter into the condition)
541 $parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level );
544 $compiled_code .= "<?php }elseif( $parsed_condition ){ ?>";
548 elseif( strpos( $html, '{
else}' ) !== FALSE ) {
551 $compiled_code .= '<?php
}else{ ?>';
556 elseif( strpos( $html, '{
/if}' ) !== FALSE ) {
558 //decrease if counter
562 $compiled_code .= '<?php
} ?>';
567 elseif( preg_match( '/\{
function="(\w*)(.*?)"\
}/', $html, $code ) ){
573 $function = $code[ 1 ];
575 // check if there's any
function disabled by black_list
576 $this->function_check( $tag );
578 if( empty( $code[ 2 ] ) )
579 $parsed_function = $function . "()";
581 // parse the function
582 $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 );
585 $compiled_code .= "<?php echo $parsed_function; ?>";
589 elseif ( strpos( $html, '{$template_info}' ) !== FALSE ) {
592 $tag = '{$template_info}';
595 $compiled_code .= '<?php echo "<pre>"; print_r( $this->var ); echo "</pre>"; ?>';
602 //variables substitution (es. {$title})
603 $html = $this->var_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true );
604 //const substitution (es. {#CONST#})
605 $html = $this->const_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true );
606 //functions substitution (es. {"string"|functions})
607 $compiled_code .= $this->func_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '<?php ', $php_right_delimiter = ';?>', $loop_level, $echo = true );
612 $e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $this->tpl
['tpl_filename'] . ' template');
613 throw $e->setTemplateFile($this->tpl
['tpl_filename']);
615 return $compiled_code;
621 * Reduce a path, eg. www/library/../filepath//file => www/filepath/file
625 protected function reduce_path( $path ){
626 $path = str_replace( "://", "@not_replace@", $path );
627 $path = preg_replace( "#(/+)#", "/", $path );
628 $path = preg_replace( "#(/\./+)#", "/", $path );
629 $path = str_replace( "@not_replace@", "://", $path );
631 while( preg_match( '#\.\./#', $path ) ){
632 $path = preg_replace('#\w+/\.\./#', '', $path );
640 * Replace URL according to the following rules:
641 * http://url => http://url
643 * /url => base_dir/url
644 * url => path/url (where path generally is base_url/template_dir)
645 * (The last one is => base_dir/url for <a> href)
647 * @param string $url Url to rewrite.
648 * @param string $tag Tag in which the url has been found.
649 * @param string $path Path to prepend to relative URLs.
650 * @return string rewritten url
652 protected function rewrite_url( $url, $tag, $path ) {
653 // If we don't have to rewrite for this tag, do nothing.
654 if( !in_array( $tag, self
::$path_replace_list ) ) {
658 // Make protocol list. It is a little bit different for <a>.
659 $protocol = 'http|https|ftp|file|apt|magnet';
661 $protocol .= '|mailto|javascript';
664 // Regex for URLs that should not change (except the leading #)
665 $no_change = "/(^($protocol)\:)|(#$)/i";
666 if ( preg_match( $no_change, $url ) ) {
667 return rtrim( $url, '#' );
670 // Regex for URLs that need only base url (and not template dir)
671 $base_only = '/^\//';
672 if ( $tag == 'a' or $tag == 'form' ) {
675 if ( preg_match( $base_only, $url ) ) {
676 return rtrim( self
::$base_url, '/' ) . '/' . ltrim( $url, '/' );
686 * replace one single path corresponding to a given match in the `path_replace` regex.
687 * This function has no reason to be used anywhere but in `path_replace`.
690 * @param array $matches
691 * @return replacement string
693 protected function single_path_replace ( $matches ){
698 $new_url = $this->rewrite_url( $url, $tag, $this->path
);
700 return "<$tag$_$attr=\"$new_url\"";
706 * replace the path of image src, link href and a href.
707 * @see rewrite_url for more information about how paths are replaced.
709 * @param string $html
710 * @return string html sostituito
712 protected function path_replace( $html, $tpl_basedir ){
714 if( self
::$path_replace ){
716 $tpl_dir = self
::$base_url . self
::$tpl_dir . $tpl_basedir;
718 // Prepare reduced path not to compute it for each link
719 $this->path
= $this->reduce_path( $tpl_dir );
721 $url = '(?:(?:\\{.*?\\})?[^{}]*?)*?'; // allow " inside {} for cases in which url contains {function="foo()"}
725 $tags = array_intersect( array( "link", "a" ), self::$path_replace_list );
726 $exp[] = '/<(' . join( '|', $tags ) . ')(.*?)(href
)="(' . $url . ')"/i
';
728 $tags = array_intersect( array( "img", "script", "input" ), self::$path_replace_list );
729 $exp[] = '/<(' . join( '|', $tags ) . ')(.*?)(src
)="(' . $url . ')"/i
';
731 $tags = array_intersect( array( "form" ), self::$path_replace_list );
732 $exp[] = '/<(' . join( '|', $tags ) . ')(.*?)(action
)="(' . $url . ')"/i
';
734 return preg_replace_callback( $exp, 'self
::single_path_replace
', $html );
745 function const_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){
747 return preg_replace( '/\{\
#(\w+)\#{0,1}\}/', $php_left_delimiter . ( $echo ? " echo " : null ) . '\\1' . $php_right_delimiter, $html );
752 // replace functions/modifiers on constants and strings
753 function func_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){
755 preg_match_all( '/' . '\{\#{0,1}(\"{0,1}.*?\"{0,1})(\|\w.*?)\#{0,1}\}' . '/', $html, $matches );
757 for( $i=0, $n=count($matches[0]); $i<$n; $i++
){
759 //complete tag ex: {$news.title|substr:0,100}
760 $tag = $matches[ 0 ][ $i ];
762 //variable name ex: news.title
763 $var = $matches[ 1 ][ $i ];
765 //function and parameters associate to the variable ex: substr:0,100
766 $extra_var = $matches[ 2 ][ $i ];
768 // check if there's any function disabled by black_list
769 $this->function_check( $tag );
771 $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level );
774 // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value
775 $is_init_variable = preg_match( "/^(\s*?)\=[^=](.*?)$/", $extra_var );
777 //function associate to variable
778 $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null;
780 //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title)
781 $temp = preg_split( "/\.|\[|\-\>/", $var );
784 $var_name = $temp[ 0 ];
787 $variable_path = substr( $var, strlen( $var_name ) );
789 //parentesis transform [ e ] in [" e in "]
790 $variable_path = str_replace( '[', '["', $variable_path );
791 $variable_path = str_replace( ']', '"]', $variable_path );
793 //transform .$variable in ["$variable"]
794 $variable_path = preg_replace('/\.\$(\w+)/', '["$\\1"]', $variable_path );
796 //transform [variable] in ["variable"]
797 $variable_path = preg_replace('/\.(\w+)/', '["\\1"]', $variable_path );
799 //if there's a function
802 // check if there's a function or a static method and separate, function by parameters
803 $function_var = str_replace("::", "@double_dot@", $function_var );
805 // get the position of the first :
806 if( $dot_position = strpos( $function_var, ":" ) ){
808 // get the function and the parameters
809 $function = substr( $function_var, 0, $dot_position );
810 $params = substr( $function_var, $dot_position+
1 );
816 $function = str_replace( "@double_dot@", "::", $function_var );
821 // replace back the @double_dot@ with ::
822 $function = str_replace( "@double_dot@", "::", $function );
823 $params = str_replace( "@double_dot@", "::", $params );
828 $function = $params = null;
830 $php_var = $var_name . $variable_path;
832 // compile the variable for php
833 if( isset( $function ) ){
835 $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter;
837 $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $params ) )" : "$function()" ) . $php_right_delimiter;
840 $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter;
842 $html = str_replace( $tag, $php_var, $html );
852 function var_replace( $html, $tag_left_delimiter, $tag_right_delimiter, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level = null, $echo = null ){
855 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 ) ){
857 for( $parsed=array(), $i=0, $n=count($matches[0]); $i<$n; $i++
)
858 $parsed[$matches[0][$i]] = array('var'=>$matches[1][$i],'extra_var'=>$matches[2][$i]);
860 foreach( $parsed as $tag => $array ){
862 //variable name ex: news.title
863 $var = $array['var'];
865 //function and parameters associate to the variable ex: substr:0,100
866 $extra_var = $array['extra_var'];
868 // check if there's any function disabled by black_list
869 $this->function_check( $tag );
871 $extra_var = $this->var_replace( $extra_var, null, null, null, null, $loop_level );
873 // check if there's an operator = in the variable tags, if there's this is an initialization so it will not output any value
874 $is_init_variable = preg_match( "/^[a-z_A-Z\.\[\](\-\>)]*=[^=]*$/", $extra_var );
876 //function associate to variable
877 $function_var = ( $extra_var and $extra_var[0] == '|') ? substr( $extra_var, 1 ) : null;
879 //variable path split array (ex. $news.title o $news[title]) or object (ex. $news->title)
880 $temp = preg_split( "/\.|\[|\-\>/", $var );
883 $var_name = $temp[ 0 ];
886 $variable_path = substr( $var, strlen( $var_name ) );
888 //parentesis transform [ e ] in [" e in "]
889 $variable_path = str_replace( '[', '["', $variable_path );
890 $variable_path = str_replace( ']', '"]', $variable_path );
892 //transform .$variable in ["$variable"] and .variable in ["variable"]
893 $variable_path = preg_replace('/\.(\${0,1}\w+)/', '["\\1"]', $variable_path );
895 // if is an assignment also assign the variable to $this->var['value']
896 if( $is_init_variable )
897 $extra_var = "=\$this->var['{$var_name}']{$variable_path}" . $extra_var;
901 //if there's a function
904 // check if there's a function or a static method and separate, function by parameters
905 $function_var = str_replace("::", "@double_dot@", $function_var );
908 // get the position of the first :
909 if( $dot_position = strpos( $function_var, ":" ) ){
911 // get the function and the parameters
912 $function = substr( $function_var, 0, $dot_position );
913 $params = substr( $function_var, $dot_position+
1 );
919 $function = str_replace( "@double_dot@", "::", $function_var );
924 // replace back the @double_dot@ with ::
925 $function = str_replace( "@double_dot@", "::", $function );
926 $params = str_replace( "@double_dot@", "::", $params );
929 $function = $params = null;
931 //if it is inside a loop
933 //verify the variable name
934 if( $var_name == 'key' )
935 $php_var = '$key' . $loop_level;
936 elseif( $var_name == 'value
' )
937 $php_var = '$value' . $loop_level . $variable_path;
938 elseif( $var_name == 'counter' )
939 $php_var = '$counter' . $loop_level;
941 $php_var = '$' . $var_name . $variable_path;
943 $php_var = '$' . $var_name . $variable_path;
945 // compile the variable for php
946 if( isset( $function ) )
947 $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . ( $params ? "( $function( $php_var, $params ) )" : "$function( $php_var )" ) . $php_right_delimiter;
949 $php_var = $php_left_delimiter . ( !$is_init_variable && $echo ? 'echo ' : null ) . $php_var . $extra_var . $php_right_delimiter;
951 $html = str_replace( $tag, $php_var, $html );
963 * Check if function is in black list (sandbox)
965 * @param string $code
968 protected function function_check( $code ){
970 $preg = '#(\W|\s)' . implode( '(\W|\s)|(\W|\s)', self::$black_list ) . '(\W|\s)#';
972 // check if the function is in the black list (or not in white list)
973 if( count(self
::$black_list) && preg_match( $preg, $code, $match ) ){
975 // find the line of the error
977 $rows=explode("\n",$this->tpl
['source']);
978 while( !strpos($rows[$line],$code) )
981 // stop the execution of the script
982 $e = new RainTpl_SyntaxException('Unallowed syntax in ' . $this->tpl
['tpl_filename'] . ' template');
983 throw $e->setTemplateFile($this->tpl
['tpl_filename'])
985 ->setTemplateLine($line);
991 * Prints debug info about exception or passes it further if debug is disabled.
993 * @param RainTpl_Exception $e
996 protected function printDebug(RainTpl_Exception
$e){
1000 $output = sprintf('<h2>Exception: %s</h2><h3>%s</h3><p>template: %s</p>',
1003 $e->getTemplateFile()
1005 if ($e instanceof RainTpl_SyntaxException
) {
1006 if (null != $e->getTemplateLine()) {
1007 $output .= '<p>line: ' . $e->getTemplateLine() . '</p>';
1009 if (null != $e->getTag()) {
1010 $output .= '<p>in tag: ' . htmlspecialchars($e->getTag()) . '</p>';
1012 if (null != $e->getTemplateLine() && null != $e->getTag()) {
1013 $rows=explode("\n", htmlspecialchars($this->tpl
['source']));
1014 $rows[$e->getTemplateLine()] = '<font color=red>' . $rows[$e->getTemplateLine()] . '</font>';
1015 $output .= '<h3>template code</h3>' . implode('<br />', $rows) . '</pre>';
1018 $output .= sprintf('<h3>trace</h3><p>In %s on line %d</p><pre>%s</pre>',
1019 $e->getFile(), $e->getLine(),
1020 nl2br(htmlspecialchars($e->getTraceAsString()))
1028 * Basic Rain tpl exception.
1030 class RainTpl_Exception
extends Exception
{
1032 * Path of template file with error.
1034 protected $templateFile = '';
1037 * Returns path of template file with error.
1041 public function getTemplateFile()
1043 return $this->templateFile
;
1047 * Sets path of template file with error.
1049 * @param string $templateFile
1050 * @return RainTpl_Exception
1052 public function setTemplateFile($templateFile)
1054 $this->templateFile
= (string) $templateFile;
1060 * Exception thrown when template file does not exists.
1062 class RainTpl_NotFoundException
extends RainTpl_Exception
{
1066 * Exception thrown when syntax error occurs.
1068 class RainTpl_SyntaxException
extends RainTpl_Exception
{
1070 * Line in template file where error has occured.
1074 protected $templateLine = null;
1077 * Tag which caused an error.
1079 * @var string | null
1081 protected $tag = null;
1084 * Returns line in template file where error has occured
1085 * or null if line is not defined.
1087 * @return int | null
1089 public function getTemplateLine()
1091 return $this->templateLine
;
1095 * Sets line in template file where error has occured.
1097 * @param int $templateLine
1098 * @return RainTpl_SyntaxException
1100 public function setTemplateLine($templateLine)
1102 $this->templateLine
= (int) $templateLine;
1107 * Returns tag which caused an error.
1111 public function getTag()
1117 * Sets tag which caused an error.
1119 * @param string $tag
1120 * @return RainTpl_SyntaxException
1122 public function setTag($tag)
1124 $this->tag
= (string) $tag;