From c990823223a502b1154e7dc4a5e6625e5275483b Mon Sep 17 00:00:00 2001 From: Phyks Date: Thu, 5 Sep 2013 19:43:10 +0200 Subject: [PATCH] Problem with raintpl solved, raintpl class updated in inc folder and pull request submitted to raintpl --- inc/rain.tpl.class.php | 128 +++++++++++++++++++++++++---------------- index.php | 4 -- tpl/css/style.css | 2 +- 3 files changed, 79 insertions(+), 55 deletions(-) diff --git a/inc/rain.tpl.class.php b/inc/rain.tpl.class.php index ea83b2c..ef5e5fa 100644 --- a/inc/rain.tpl.class.php +++ b/inc/rain.tpl.class.php @@ -140,7 +140,7 @@ class RainTPL{ function assign( $variable, $value = null ){ if( is_array( $variable ) ) - $this->var += $variable; + $this->var = $variable + $this->var; else $this->var[ $variable ] = $value; } @@ -221,8 +221,10 @@ class RainTPL{ // set the cache_id $this->cache_id = $cache_id; - if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl['cache_filename'] ) && ( time() - filemtime( $this->tpl['cache_filename'] ) < $expire_time ) ) + if( !$this->check_template( $tpl_name ) && file_exists( $this->tpl['cache_filename'] ) && ( time() - filemtime( $this->tpl['cache_filename'] ) < $expire_time ) ){ + // return the cached file as HTML. It remove the first 43 character, which are a PHP code to secure the file return substr( file_get_contents( $this->tpl['cache_filename'] ), 43 ); + } else{ //delete the cache of the selected template if (file_exists($this->tpl['cache_filename'])) @@ -257,24 +259,30 @@ class RainTPL{ $tpl_basename = basename( $tpl_name ); // template basename $tpl_basedir = strpos($tpl_name,"/") ? dirname($tpl_name) . '/' : null; // template basedirectory - $tpl_dir = self::$tpl_dir . $tpl_basedir; // template directory - $this->tpl['tpl_filename'] = $tpl_dir . $tpl_basename . '.' . self::$tpl_ext; // template filename - $temp_compiled_filename = self::$cache_dir . $tpl_basename . "." . md5( $tpl_dir . serialize(self::$config_name_sum)); + $this->tpl['template_directory'] = self::$tpl_dir . $tpl_basedir; // template directory + $this->tpl['tpl_filename'] = $this->tpl['template_directory'] . $tpl_basename . '.' . self::$tpl_ext; // template filename + $temp_compiled_filename = self::$cache_dir . $tpl_basename . "." . md5( $this->tpl['template_directory'] . serialize(self::$config_name_sum)); $this->tpl['compiled_filename'] = $temp_compiled_filename . '.rtpl.php'; // cache filename $this->tpl['cache_filename'] = $temp_compiled_filename . '.s_' . $this->cache_id . '.rtpl.php'; // static cache filename - - // if the template doesn't exsist throw an error - if( self::$check_template_update && !file_exists( $this->tpl['tpl_filename'] ) ){ + $this->tpl['checked'] = true; + + // if the template doesn't exist and is not an external source throw an error + if( self::$check_template_update && !file_exists( $this->tpl['tpl_filename'] ) && !preg_match('/http/', $tpl_name) ){ $e = new RainTpl_NotFoundException( 'Template '. $tpl_basename .' not found!' ); throw $e->setTemplateFile($this->tpl['tpl_filename']); } - // file doesn't exsist, or the template was updated, Rain will compile the template - if( !file_exists( $this->tpl['compiled_filename'] ) || ( self::$check_template_update && filemtime($this->tpl['compiled_filename']) < filemtime( $this->tpl['tpl_filename'] ) ) ){ + // We check if the template is not an external source + if(preg_match('/http/', $tpl_name)){ + $this->compileFile('', '', $tpl_name, self::$cache_dir, $this->tpl['compiled_filename'] ); + return true; + } + // file doesn't exist, or the template was updated, Rain will compile the template + elseif( !file_exists( $this->tpl['compiled_filename'] ) || ( self::$check_template_update && filemtime($this->tpl['compiled_filename']) < filemtime( $this->tpl['tpl_filename'] ) ) ){ $this->compileFile( $tpl_basename, $tpl_basedir, $this->tpl['tpl_filename'], self::$cache_dir, $this->tpl['compiled_filename'] ); return true; } - $this->tpl['checked'] = true; + } } @@ -372,13 +380,18 @@ class RainTPL{ * @access protected */ protected function compileCode( $parsed_code ){ + + // if parsed code is empty return null string + if( !$parsed_code ) + return ""; //variables initialization $compiled_code = $open_if = $comment_is_open = $ignore_is_open = null; - $loop_level = 0; + $loop_level = 0; + //read all parsed code - while( $html = array_shift( $parsed_code ) ){ + foreach( $parsed_code as $html ){ //close ignore tag if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) ) @@ -407,37 +420,47 @@ class RainTPL{ //include tag elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){ + if (preg_match("/http/", $code[1])) { + $content = file_get_contents($code[1]); + $compiled_code .= $content; + } else { + //variables substitution + $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level ); - //variables substitution - $include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level ); + //get the folder of the actual template + $actual_folder = substr( $this->tpl['template_directory'], strlen(self::$tpl_dir) ); - // if the cache is active - if( isset($code[ 2 ]) ){ - - //dynamic include - $compiled_code .= 'cache( $template = basename("'.$include_var.'") ) )' . - ' echo $cache;' . - 'else{' . - ' $tpl_dir_temp = self::$tpl_dir;' . - ' $tpl->assign( $this->var );' . - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). - ' $tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. - '} ?>'; + //get the included template + $include_template = $actual_folder . $include_var; + + // reduce the path + $include_template = $this->reduce_path( $include_template ); + + // if the cache is active + if( isset($code[ 2 ]) ){ + + //include + $compiled_code .= 'cache( "'.$include_template.'" ) )' . + ' echo $cache;' . + 'else{' . + '$tpl->assign( $this->var );' . + ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). + '$tpl->draw( "'.$include_template.'" );'. + '}' . + '?>'; + + } + else{ + //include + $compiled_code .= 'assign( $this->var );' . + ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). + '$tpl->draw( "'.$include_template.'" );'. + '?>'; + + } } - else{ - - //dynamic include - $compiled_code .= 'assign( $this->var );' . - ( !$loop_level ? null : '$tpl->assign( "key", $key'.$loop_level.' ); $tpl->assign( "value", $value'.$loop_level.' );' ). - '$tpl->draw( dirname("'.$include_var.'") . ( substr("'.$include_var.'",-1,1) != "/" ? "/" : "" ) . basename("'.$include_var.'") );'. - '?>'; - - - } - } //loop @@ -590,10 +613,15 @@ class RainTPL{ * @return type */ protected function reduce_path( $path ){ - $path = str_replace( "://", "@not_replace@", $path ); - $path = str_replace( "//", "/", $path ); - $path = str_replace( "@not_replace@", "://", $path ); - return preg_replace('/\w+\/\.\.\//', '', $path ); + $path = str_replace( "://", "@not_replace@", $path ); + $path = preg_replace( "#(/+)#", "/", $path ); + $path = preg_replace( "#(/\./+)#", "/", $path ); + $path = str_replace( "@not_replace@", "://", $path ); + + while( preg_match( '#\.\./#', $path ) ){ + $path = preg_replace('#\w+/\.\./#', '', $path ); + } + return $path; } @@ -619,27 +647,27 @@ class RainTPL{ $exp = $sub = array(); if( in_array( "img", self::$path_replace_list ) ){ - $exp = array( '/assign('currency', htmlspecialchars(CURRENCY)); $tpl->assign('email_webmaster', htmlspecialchars(EMAIL_WEBMASTER)); - // TODO : Avoid a bug in rainTPL with img near input - $tpl->configure('path_replace_list', array('a', 'img', 'link', 'script')); - // Set sessions parameters ini_set('session.use_cookies', 1); ini_set('session.use_only_cookies', 1); diff --git a/tpl/css/style.css b/tpl/css/style.css index 33ade18..c8e6db5 100644 --- a/tpl/css/style.css +++ b/tpl/css/style.css @@ -76,7 +76,7 @@ input[type=submit] { border-radius: 10px; } -#title, #title a:visited, #install h1 { +#title, #title a, #title a:visited, #install h1 { background-color: #333; border-bottom: 0.3em solid green; color: white;