), stylesheet (), script () and link ()
* Set true to enable the path replace.
*
* @var unknown_type
*/
static $path_replace = true;
/**
* You can set what the path_replace method will replace.
* Avaible options: a, img, link, script, input
*
* @var array
*/
static $path_replace_list = array( 'a', 'img', 'link', 'script', 'input' );
/**
* You can define in the black list what string are disabled into the template tags
*
* @var unknown_type
*/
static $black_list = array( '\$this', 'raintpl::', 'self::', '_SESSION', '_SERVER', '_ENV', 'eval', 'exec', 'unlink', 'rmdir' );
/**
* Check template.
* true: checks template update time, if changed it compile them
* false: loads the compiled template. Set false if server doesn't have write permission for cache_directory.
*
*/
static $check_template_update = true;
/**
* PHP tags ?>
* True: php tags are enabled into the template
* False: php tags are disabled into the template and rendered as html
*
* @var bool
*/
static $php_enabled = false;
/**
* Debug mode flag.
* True: debug mode is used, syntax errors are displayed directly in template. Execution of script is not terminated.
* False: exception is thrown on found error.
*
* @var bool
*/
static $debug = false;
// -------------------------
// -------------------------
// RAINTPL VARIABLES
// -------------------------
/**
* Is the array where RainTPL keep the variables assigned
*
* @var array
*/
public $var = array();
protected $tpl = array(), // variables to keep the template directories and info
$cache = false, // static cache enabled / disabled
$cache_id = null; // identify only one cache
protected static $config_name_sum = array(); // takes all the config to create the md5 of the file
// -------------------------
const CACHE_EXPIRE_TIME = 3600; // default cache expire time = hour
/**
* Assign variable
* eg. $t->assign('name','mickey');
*
* @param mixed $variable_name Name of template variable or associative array name/value
* @param mixed $value value assigned to this variable. Not set if variable_name is an associative array
*/
function assign( $variable, $value = null ){
if( is_array( $variable ) )
$this->var += $variable;
else
$this->var[ $variable ] = $value;
}
/**
* Draw the template
* eg. $html = $tpl->draw( 'demo', TRUE ); // return template in string
* or $tpl->draw( $tpl_name ); // echo the template
*
* @param string $tpl_name template to load
* @param boolean $return_string true=return a string, false=echo the template
* @return string
*/
function draw( $tpl_name, $return_string = false ){
try {
// compile the template if necessary and set the template filepath
$this->check_template( $tpl_name );
} catch (RainTpl_Exception $e) {
$output = $this->printDebug($e);
die($output);
}
// Cache is off and, return_string is false
// Rain just echo the template
if( !$this->cache && !$return_string ){
extract( $this->var );
include $this->tpl['compiled_filename'];
unset( $this->tpl );
}
// cache or return_string are enabled
// rain get the output buffer to save the output in the cache or to return it as string
else{
//----------------------
// get the output buffer
//----------------------
ob_start();
extract( $this->var );
include $this->tpl['compiled_filename'];
$raintpl_contents = ob_get_clean();
//----------------------
// save the output in the cache
if( $this->cache )
file_put_contents( $this->tpl['cache_filename'], "" . $raintpl_contents );
// free memory
unset( $this->tpl );
// return or print the template
if( $return_string ) return $raintpl_contents; else echo $raintpl_contents;
}
}
/**
* If exists a valid cache for this template it returns the cache
*
* @param string $tpl_name Name of template (set the same of draw)
* @param int $expiration_time Set after how many seconds the cache expire and must be regenerated
* @return string it return the HTML or null if the cache must be recreated
*/
function cache( $tpl_name, $expire_time = self::CACHE_EXPIRE_TIME, $cache_id = null ){
// 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 ) )
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']))
unlink($this->tpl['cache_filename'] );
$this->cache = true;
}
}
/**
* Configure the settings of RainTPL
*
*/
static function configure( $setting, $value = null ){
if( is_array( $setting ) )
foreach( $setting as $key => $value )
self::configure( $key, $value );
else if( property_exists( __CLASS__, $setting ) ){
self::$$setting = $value;
self::$config_name_sum[ $setting ] = $value; // take trace of all config
}
}
// check if has to compile the template
// return true if the template has changed
protected function check_template( $tpl_name ){
if( !isset($this->tpl['checked']) ){
$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['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'] ) ){
$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'] ) ) ){
$this->compileFile( $tpl_basename, $tpl_basedir, $this->tpl['tpl_filename'], self::$cache_dir, $this->tpl['compiled_filename'] );
return true;
}
$this->tpl['checked'] = true;
}
}
/**
* execute stripslaches() on the xml block. Invoqued by preg_replace_callback function below
* @access protected
*/
protected function xml_reSubstitution($capture) {
return "'; ?>";
}
/**
* Compile and write the compiled template file
* @access protected
*/
protected function compileFile( $tpl_basename, $tpl_basedir, $tpl_filename, $cache_dir, $compiled_filename ){
//read template file
$this->tpl['source'] = $template_code = file_get_contents( $tpl_filename );
//xml substitution
$template_code = preg_replace( "/<\?xml(.*?)\?>/s", "##XML\\1XML##", $template_code );
//disable php tag
if( !self::$php_enabled )
$template_code = str_replace( array("","?>"), array("<?","?>"), $template_code );
//xml re-substitution
$template_code = preg_replace_callback ( "/##XML(.*?)XML##/s", array($this, 'xml_reSubstitution'), $template_code );
//compile template
$template_compiled = "" . $this->compileTemplate( $template_code, $tpl_basedir );
// fix the php-eating-newline-after-closing-tag-problem
$template_compiled = str_replace( "?>\n", "?>\n\n", $template_compiled );
// create directories
if( !is_dir( $cache_dir ) )
mkdir( $cache_dir, 0755, true );
if( !is_writable( $cache_dir ) )
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/');
//write compiled file
file_put_contents( $compiled_filename, $template_compiled );
}
/**
* Compile template
* @access protected
*/
protected function compileTemplate( $template_code, $tpl_basedir ){
//tag list
$tag_regexp = array( 'loop' => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})',
'loop_close' => '(\{\/loop\})',
'if' => '(\{if(?: condition){0,1}="[^"]*"\})',
'elseif' => '(\{elseif(?: condition){0,1}="[^"]*"\})',
'else' => '(\{else\})',
'if_close' => '(\{\/if\})',
'function' => '(\{function="[^"]*"\})',
'noparse' => '(\{noparse\})',
'noparse_close'=> '(\{\/noparse\})',
'ignore' => '(\{ignore\}|\{\*)',
'ignore_close' => '(\{\/ignore\}|\*\})',
'include' => '(\{include="[^"]*"(?: cache="[^"]*")?\})',
'template_info'=> '(\{\$template_info\})',
'function' => '(\{function="(\w*?)(?:.*?)"\})'
);
$tag_regexp = "/" . join( "|", $tag_regexp ) . "/";
//split the code with the tags regexp
$template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
//path replace (src of img, background and href of link)
$template_code = $this->path_replace( $template_code, $tpl_basedir );
//compile the code
$compiled_code = $this->compileCode( $template_code );
//return the compiled code
return $compiled_code;
}
/**
* Compile the code
* @access protected
*/
protected function compileCode( $parsed_code ){
//variables initialization
$compiled_code = $open_if = $comment_is_open = $ignore_is_open = null;
$loop_level = 0;
//read all parsed code
while( $html = array_shift( $parsed_code ) ){
//close ignore tag
if( !$comment_is_open && ( strpos( $html, '{/ignore}' ) !== FALSE || strpos( $html, '*}' ) !== FALSE ) )
$ignore_is_open = false;
//code between tag ignore id deleted
elseif( $ignore_is_open ){
//ignore the code
}
//close no parse tag
elseif( strpos( $html, '{/noparse}' ) !== FALSE )
$comment_is_open = false;
//code between tag noparse is not compiled
elseif( $comment_is_open )
$compiled_code .= $html;
//ignore
elseif( strpos( $html, '{ignore}' ) !== FALSE || strpos( $html, '{*' ) !== FALSE )
$ignore_is_open = true;
//noparse
elseif( strpos( $html, '{noparse}' ) !== FALSE )
$comment_is_open = true;
//include tag
elseif( preg_match( '/\{include="([^"]*)"(?: cache="([^"]*)"){0,1}\}/', $html, $code ) ){
//variables substitution
$include_var = $this->var_replace( $code[ 1 ], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".' , $php_right_delimiter = '."', $loop_level );
// 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.'") );'.
'} ?>';
}
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
elseif( preg_match( '/\{loop(?: name){0,1}="\${0,1}([^"]*)"\}/', $html, $code ) ){
//increase the loop counter
$loop_level++;
//replace the variable in the loop
$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 );
//loop variables
$counter = "\$counter$loop_level"; // count iteration
$key = "\$key$loop_level"; // key
$value = "\$value$loop_level"; // value
//loop code
$compiled_code .= " $value ){ $counter++; ?>";
}
//close loop tag
elseif( strpos( $html, '{/loop}' ) !== FALSE ) {
//iterator
$counter = "\$counter$loop_level";
//decrease the loop counter
$loop_level--;
//close loop code
$compiled_code .= "";
}
//if
elseif( preg_match( '/\{if(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){
//increase open if counter (for intendation)
$open_if++;
//tag
$tag = $code[ 0 ];
//condition attribute
$condition = $code[ 1 ];
// check if there's any function disabled by black_list
$this->function_check( $tag );
//variable substitution into condition (no delimiter into the condition)
$parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level );
//if code
$compiled_code .= "";
}
//elseif
elseif( preg_match( '/\{elseif(?: condition){0,1}="([^"]*)"\}/', $html, $code ) ){
//tag
$tag = $code[ 0 ];
//condition attribute
$condition = $code[ 1 ];
//variable substitution into condition (no delimiter into the condition)
$parsed_condition = $this->var_replace( $condition, $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level );
//elseif code
$compiled_code .= "";
}
//else
elseif( strpos( $html, '{else}' ) !== FALSE ) {
//else code
$compiled_code .= '';
}
//close if tag
elseif( strpos( $html, '{/if}' ) !== FALSE ) {
//decrease if counter
$open_if--;
// close if code
$compiled_code .= '';
}
//function
elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){
//tag
$tag = $code[ 0 ];
//function
$function = $code[ 1 ];
// check if there's any function disabled by black_list
$this->function_check( $tag );
if( empty( $code[ 2 ] ) )
$parsed_function = $function . "()";
else
// parse the function
$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 );
//if code
$compiled_code .= "";
}
// show all vars
elseif ( strpos( $html, '{$template_info}' ) !== FALSE ) {
//tag
$tag = '{$template_info}';
//if code
$compiled_code .= '"; print_r( $this->var ); echo ""; ?>';
}
//all html code
else{
//variables substitution (es. {$title})
$html = $this->var_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '', $loop_level, $echo = true );
//const substitution (es. {#CONST#})
$html = $this->const_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '', $loop_level, $echo = true );
//functions substitution (es. {"string"|functions})
$compiled_code .= $this->func_replace( $html, $left_delimiter = '\{', $right_delimiter = '\}', $php_left_delimiter = '', $loop_level, $echo = true );
}
}
if( $open_if > 0 ) {
$e = new RainTpl_SyntaxException('Error! You need to close an {if} tag in ' . $this->tpl['tpl_filename'] . ' template');
throw $e->setTemplateFile($this->tpl['tpl_filename']);
}
return $compiled_code;
}
/**
* Reduce a path, eg. www/library/../filepath//file => www/filepath/file
* @param type $path
* @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 );
}
/**
* replace the path of image src, link href and a href.
* url => template_dir/url
* url# => url
* http://url => http://url
*
* @param string $html
* @return string html sostituito
*/
protected function path_replace( $html, $tpl_basedir ){
if( self::$path_replace ){
$tpl_dir = self::$base_url . self::$tpl_dir . $tpl_basedir;
// reduce the path
$path = $this->reduce_path($tpl_dir);
$exp = $sub = array();
if( in_array( "img", self::$path_replace_list ) ){
$exp = array( '/![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()