Source for file TPLN.php
Documentation is available at TPLN.php
* TPLN template engine - main class
* @package Template Engine
* @website http://tpln.sourceforge.net
// Inclusion of the configuration file and the Db
define('TPLN_PATH', dirname(__FILE__
));
include_once('TPLN_Cfg.php');
if(TPLN_DBUG_CLASS)include_once('dBug.php');
include_once('lang/error_'.
TPLN_LANG.
'.inc.php'); // language file
include_once('plugin/form/lang.inc.php'); // form language file
/******************* Structure des plugins ***************************************
*********************************************************************************** @author H2LSOFT */
include_once('plugin/image/image.class.php');
include_once('plugin/rss/rss.class.php');
include_once('plugin/mail/mail.class.php');
include_once('plugin/form/form.class.php');
include_once('plugin/db/db.class.php');
/******************* Structure d'un fichier ***************************************
f - f_no - name - string // nom de fichier
- cache_name - string // nom du fichier en cache
- buffer - string // contenu du fichier
- items - array // contient tous les items
- constant_items - array // contient les constantes
- php_items - array // contient les items avec $
- cmd_items - array // contient tous les items includes
- create_cached_file - bool // cr�e le fichier en cache ?
- time_started - long int // d�but du chrono
- cache_expire - bool // expiration du cache ?
- execution_time - long int // temps d'execution
- chrono_started - long int // chrono started
Attention par reference pour les v�rifications et performances
- shortcut_blocs - array //
|_ all - array // contient le nom de tous les blocs
|_ used - array // contient les blocs appel�s par l'utilsateur
|_ name - none // contient le nom du bloc
|_ items - array // contient les items
- def_blocs - array // contiens tous les blocs structur�s
// definis par l'utisateur lors d'un appel'
// voir plus bas leurs structures
/******************* Structure d'un bloc ***************************************
def_blocs - name - string // nom du bloc
|_ structure - string // contenu du bloc
|_ parsed - array // contiens les sessions de blocs
|_ is_looped - boolean // loop� ?
|_ children - array // contiens les bloc enfants
************************************************************************************* @author H2LSOFT */
protected $def_tpl =
array(); // templates list defined with a name
* @var int index of template
public $f_no = -
1; // index of file
* @var int index of virtual template
public $vf_no = -
1; // index of virtual file
* @var array template properties
public $f =
array(); // array which contains all the properties
protected $HTMLCompress =
0; // activate the HTML compression of the exit ?
protected $blocs_double_exceptions =
array('start', 'end', 'pager', 'in', 'out', 'previous', 'next'); // allows blocs in double
protected $struct_tab =
array(); // contains array entirety
// looks at the existence of the parameter by default
if(TPLN_DEFAULT_IND ==
1)
if(!is_dir(TPLN_DEFAULT_PATH))
if(!@mkdir(TPLN_DEFAULT_PATH, 0755))
* this method allows to use plugin for TPLN.
$name = strtolower($name);
$tmp = TPLN_PATH."/plugin/$name/$name.class.php";
// PHP's version // création dynmique d'objet
$_php_ver = (float) PHP_VERSION;
die('PHP version invalid no support for PHP 5');
// Création dynamique d'objet sans la fonction aggregate
* This method allows to compress the template.
$this->htmlCompress =
$bool;
* This method allows to display the countain of a php variable to facilite debugging
public function dump($label =
'Debug', $data, $return =
0)
($format_html) ?
$v =
'<pre><div style="text-align:left;margin:5px;border:1px solid #CCCCCC;background-color:#E5E5E5;">' :
$v =
'<pre>';
if($format_html)$v .=
'<div style="text-align:left;padding:5px;background-color:#CCCCCC;">';
$v .=
"<b>$label => </b> ";
if($format_html)$v .=
'</div>';
($format_html) ?
$v .=
'</div></pre>' :
$v .=
'</pre>';
* This method returns ip used.
if(isset
($_SERVER['HTTP_X_FORWARDED_FOR']))
$ip =
$_SERVER['HTTP_X_FORWARDED_FOR'];
elseif(isset
($_SERVER['HTTP_CLIENT_IP']))
$ip =
$_SERVER['HTTP_CLIENT_IP'];
$ip =
$_SERVER['REMOTE_ADDR'];
* this method activates template structure
* this method gets opened template
* this method returns file structure
$this->struct_tab[] =
"<b>File:</b> {$this->f[$this->f_no]['name']}<br>\n
";
* this method returns item structure
$p_var =
array('_UrlBng',
$tab =
'<b>Variable(s) found:</b> '.
count($this->f[$this->f_no]['items']).
"<br>\n";
$cur_item =
array(); // initialisation
$tpln_var =
array(); // initialisation
foreach($item_tmp as $name)
$tab .=
$this->arr2List('Constants variable(s)', $this->f[$this->f_no]['php_items'], '{CONST::', '}');
$tab .=
$this->arr2List('Php variable(s)', $this->f[$this->f_no]['php_items'], '{$', '}');
$tab .=
$this->arr2List('User variable(s)', $cur_item, '{', '}');
$tab .=
$this->arr2List('Private variable(s)', $tpln_var, '{', '}');
$tab .=
$this->arr2List('Tpln Include Command(s)', $this->f[$this->f_no]['cmd_items'], '{#include(', ')}');
* this method returns block structure
* this method changes array to list
protected function arr2List($text =
'', $arr, $bng =
'', $end =
'')
$txt .=
"<b>$text:</b> ".
count($arr).
'<br>';
$txt .=
'<li>'.
$bng.
$name.
$end.
"</li>\n";
if(count($this->f[$this->f_no]['shortcut_blocs'][$name]['items']) >
0)
foreach($this->f[$this->f_no]['shortcut_blocs'][$name]['items'] as $item)
$txt .=
"<li>\{$item}</li>";
* this method changes array to html table
echo
'<table width="100%" border="1" cellspacing="0" cellpadding="3">';
* this method triggers TPLN error
protected function error($err_no, $file =
'', $bloc =
'', $item =
'')
$err_msg =
$_err["$err_no"];
array('[:FILE:]', '[:BLOC:]', '[:ITEM:]'),
array($file, $bloc, $item),
$this->error_msg =
"<B>TPLN error $err_no:</B> $err_msg";
if(in_array($err_no, array(0, 9, 10, 11, 12, 13)))
elseif(in_array($err_no, array( 8, 13)))
* this method sends mail admin alert
$err_alert =
TPLN_ERROR_ALERT;
$mail_admin =
TPLN_MAIL_ADMIN;
if(($err_alert ==
1 &&
!empty($mail_admin)) &&
(!isset
($_GET['tpln_w']) ||
$_GET['tpln_w'] !=
'adm'))
$request_url_simple =
str_replace('?'.
$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
$url =
'http://'.
$_SERVER['HTTP_HOST'].
$request_url_simple;
if(empty($_SERVER['QUERY_STRING']))
$url .=
'?'.
$_SERVER['QUERY_STRING'].
'&tpln_w=adm';
$subject =
'[TPLN] Alert Error';
$body =
date('[Y-m-d H:i] ').
" TPLN has detected an error\n\n";
$body .=
$err_msg.
' in '.
$_SERVER['SCRIPT_FILENAME'].
"\n\n\n";
$body .=
"Url <a href=\"$url\">$url</a>\n";
$body .=
"===========================================\n";
$body .=
'TPLN version '.
TPLN_VERSION.
"\n";
$body .=
'IP: '.
$this->GetIP().
"\n";
$headers =
"MIME-Version: 1.0\n";
$headers .=
"Content-type: text/html; charset=iso-8859-1\n";
$headers .=
"From: ".
TPLN_MAIL_EXPEDITOR.
"\n";
if(@mail(TPLN_MAIL_ADMIN, $subject, $body, $headers))
$msg =
"<br><br><hr>An email has been sent to the webmaster $mail_admin";
elseif(TPLN_LANG ==
'fr')
$msg =
"<br><br><hr>Mail a alerté le webmaster $mail_admin";
* this method displays error message
//if($exit == 1 && TPLN_ERROR_URI == '')
if('TPLN_OUTPUT_CHARSET' !=
'utf-8')
// $err_msg = strip_tags($err_msg);
// check if the file exists else we create it
if(!($fp =
@fopen(TPLN_ERROR_LOGS_FILE , 'a+')))
trigger_error('Impossible to open/create '.
TPLN_ERROR_LOGS_FILE, E_USER_NOTICE);
* This method allows to redirect to a new web page
* @param int $timer default 0
public function redirect($uri, $timer=
0)
die('<meta http-equiv="refresh" content="'.
$timer.
';'.
$uri.
'" />');
die('<script>document.location.href="'.
$uri.
'"</script>');
* this method initializes template variables
// initialisation of variables types for EALL **************************
$this->f[$this->f_no]['name'] =
null;
$this->f[$this->f_no]['cache_name'] =
null;
$this->f[$this->f_no]['buffer'] =
null;
$this->f[$this->f_no]['items'] =
array();
$this->f[$this->f_no]['constant_items'] =
array();
$this->f[$this->f_no]['php_items'] =
array();
$this->f[$this->f_no]['cmd_items'] =
array();
$this->f[$this->f_no]['create_cached_file'] =
0;
$this->f[$this->f_no]['time_started'] =
0;
$this->f[$this->f_no]['cache_expire'] =
0;
$this->f[$this->f_no]['execution_time'] =
0;
$this->f[$this->f_no]['chrono_started'] =
0;
$this->f[$this->f_no]['shortcut_blocs'] =
array();
$this->f[$this->f_no]['shortcut_blocs']['all'] =
array();
$this->f[$this->f_no]['shortcut_blocs']['used'] =
array();
$this->f[$this->f_no]['shortcut_blocs']['name'] =
null;
$this->f[$this->f_no]['shortcut_blocs']['name']['items'] =
array();
$this->f[$this->f_no]['def_blocs'] =
array();
* this method allows to open template file which can to support any extensions of file (.htm, .html, .tpl, .php ... ).
* if none name of file has been defined, it will open the file which have the same name
* as the script with extension per defect.
* @param int $cached_time
public function open($file =
'', $cached =
'', $cached_time =
'')
$this->f_no++
; // incremente
// check the existence of extension and the directory per defect
$file .=
'.'.
TPLN_DEFAULT_EXT;
}// puts the extension per defect if it's not exists
$file =
TPLN_DEFAULT_PATH.
'/'.
$file; // puts the directory per defect
$this->f[$this->f_no]['name'] =
$file;
$this->f[$this->f_no]['time_started'] =
time();
$this->f[$this->f_no]['chrono_started'] =
$time[1] +
$time[0];
if(!defined('TPLN_CHRONO_STARTED'))
$chrono =
$time[1] +
$time[0];
define('TPLN_CHRONO_STARTED', $chrono);
// if there is caching file
// the ? is replaced by -- in the caching file
$cached_time =
TPLN_CACHE_TIME;
$this->f[$this->f_no]['cache_expire'] =
$this->f[$this->f_no]['time_started'] +
$cached_time; // caching expiry
// check if the file is still caching
// file exists && its date of creation <= time of caching
$this->f[$this->f_no]['create_cached_file'] =
1;
// remplaces the constants
// remplaces the variables with $
if(TPLN_PARSE_GLOBALS)$this->ParseGlobals();
// remplaces the files to include
// parsing and evaluation if necessary
$this->f[$this->f_no]['cmd_items'] =
array(); // delete
// replaces the $ if contained in includind files
// captures of the items after checking of blocs
if($this->f[$this->f_no]['create_cached_file'] ==
1)return false;
* this method replaces all php constants in template
if(count($this->f[$this->f_no]['constant_items']) ==
0)return;
foreach($this->f[$this->f_no]['constant_items'] as $item)
$this->f[$this->f_no]['buffer'] =
$this->replaceItem('CONST::'.
$item, $defined_constants[$item], $this->f[$this->f_no]['buffer']);
* this method allows to delete the blocks.
* @see EraseBloc(), EraseItem().
for($i =
0; $i <
count($this->f[$this->f_no]['shortcut_blocs']['all']); $i++
)
array("<bloc::{$this->f[$this->f_no]['shortcut_blocs']['all'][$i]}>
",
"</bloc::{$this->f[$this->f_no]['shortcut_blocs']['all'][$i]}>
"
) , '', $this->f[$this->f_no]['buffer']); // bloc before/after
* this method allows to create a virtual template.
* @param string $countain
$this->vf_no++
; // increments
$this->f_no++
; // increments
//$this->f_no = count($this->def_tpl) + 1; // increments
// initialisation of variables types for EALL **************************
$this->f[$this->f_no]['name'] =
'Virtual'.
$this->vf_no;
$this->f[$this->f_no]['buffer'] =
$countain;
$this->f[$this->f_no]['items'] =
array();
$this->f[$this->f_no]['constant_items'] =
array();
$this->f[$this->f_no]['php_items'] =
array();
$this->f[$this->f_no]['cmd_items'] =
array();
$this->f[$this->f_no]['create_cached_file'] =
0;
$this->f[$this->f_no]['time_started'] =
0;
$this->f[$this->f_no]['cache_expire'] =
0;
$this->f[$this->f_no]['execution_time'] =
0;
$this->f[$this->f_no]['chrono_started'] =
0;
$this->f[$this->f_no]['shortcut_blocs'] =
array();
$this->f[$this->f_no]['shortcut_blocs']['all'] =
array();
$this->f[$this->f_no]['shortcut_blocs']['used'] =
array();
$this->f[$this->f_no]['shortcut_blocs']['name'] =
null;
$this->f[$this->f_no]['shortcut_blocs']['name']['items'] =
array();
$this->f[$this->f_no]['def_blocs'] =
array();
$this->f[$this->f_no]['time_started'] =
time();
$this->f[$this->f_no]['chrono_started'] =
$time[1] +
$time[0];
if(!defined('TPLN_CHRONO_STARTED'))
$chrono =
$time[1] +
$time[0];
define('TPLN_CHRONO_STARTED', $chrono);
// replaces the variables with $
if(TPLN_PARSE_GLOBALS)$this->ParseGlobals();
// replaces the files to include
// parsing and evaluation if necessary
$this->f[$this->f_no]['cmd_items'] =
array(); // delete
// replaces the $ if there are contained in including files
if(TPLN_PARSE_GLOBALS) $this->ParseGlobals();
// Captures the items after cheking the blocs
if($this->f[$this->f_no]['create_cached_file'] ==
1)
* this method is a macro which allows to open and to write a template.
$c_id =
$this->f_no; // saves the actual number
$this->f_no =
$c_id; // puts the number
* this method is a macro wich allows to open and to return a template.
$c_id =
$this->f_no; // saves the actual number
$output =
$this->outPut();
$this->f_no =
$c_id; // puts the number
* this method is a macro which allows to open and to save a template.
$c_id =
$this->f_no; // saves the actual number
$this->f_no =
$c_id; // puts the number
* this method allows to open and to name various templates.
foreach($arr as $key =>
$val)
* this method allows to select a template which has been opened by the Open()
* or DefineTemplate() methods is by a name or a number.
* this method allows you to select a template opened by Open()
* or DefineTemplate() methods either by name or by number.
* This method returns curent template index
* @return int template index
* this method allows to select a template opened by Open()
* or DefineTemplate() methods either by name or by number.
if($key <
0 ||
$key >=
count($this->f))
* this method allows to parse a variable Php by a variable contained in the template.
* @see Parse(), FastParse().
if(count($this->f[$this->f_no]['php_items']) ==
0)
@extract($GLOBALS, EXTR_SKIP ||
EXTR_REFS);
foreach($this->f[$this->f_no]['php_items'] as $item)
//if(!eregi("\(", $replace)) // protection security of methods and functions !
if(strpos($replace, "(") ===
false) // protection security of methods and functions !
@eval
("\$tmp = $replace;");
* this method allows to substitute a variable defined inside the template file.
* TPLN use a style javascript pseudo object language to access to the variable by the separator ".".
* @param string $functions
public function parse($path, $replace, $functions =
'')
// formatting function of data
$item =
$this->getItem($path); // taks the item
$fathers_arr =
$this->getFathers($path, 'ARRAY'); // les p�res ds un array
$bloc =
$this->getFather($path); // on prend le p�re
$this->error(2, $this->f[$this->f_no]['name'], $bloc);
if(!$this->isDefined($path)) // the path is saved ?
$this->defineBloc($path); // define the blocs one by one
* @param array $fathers_arr
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
for($i =
0;$i <
count($fathers_arr);$i++
)
$b_ref =
& $b_ref[$fathers_arr[$i]];
if($i <
count($fathers_arr)-
1)
$b_ref =
& $b_ref['children'];
// check if there was a loop>0
$loop_nb =
count($b_ref['parsed']);
$b_ref['parsed'][$loop_nb-
1] =
$this->replaceItem($item, $replace, $b_ref['parsed'][$loop_nb-
1]);
* this method is like Parse() method but it avoids to rewrite definied variables.
* @param string $functions
public function fastParse($path, $functions =
'')
// if it's a block then replace
$item =
$this->getItem($path); // taks the last item
$this->parse($path, $GLOBALS[$item], $functions);
* this method allows to substitute the items by the pairs keys of an array in a whole block.
* The 3rd parameters allows to replace entire block if no data foud in array.
foreach($arr as $current_arr)
$zpath =
$ttmp[count($ttmp)-
1];
//echo "parse: $path.$key".'<br />';
$this->parse("$path.$key", $current_arr[$key]);
$this->loadArrayInBloc($path.
'.'.
$sub_bloc, $current_arr[$key], $msg, true);
//echo "loop: $path".'<hr />';
* this method is applied to a block to report is looping.
public function loop($path)
// check the existence of the path
$this->error(4.1, $this->f[$this->f_no]['name'], $path);
$fathers_arr =
$this->getFathers($path, 'ARRAY', 0);
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
for($i =
0;$i <
count($fathers_arr);$i++
)
$b_ref =
& $b_ref[$fathers_arr[$i]];
if($i <
count($fathers_arr)-
1)
$b_ref =
& $b_ref['children'];
// all the following génération is replaced
if(count($b_ref['children']) >
0)
// $level = count($fathers_arr)-1;
// encapsulate the blocks
// increment the value of loop
$b_ref['parsed'][] =
$b_ref['structure'];
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
for($i =
0;$i <
count($bloc_arr);$i++
)
$b_ref =
& $b_ref[$bloc_arr[$i]];
if($i <
count($bloc_arr)-
1)
$b_ref =
& $b_ref['children'];
* @param array $child_blocs
// encapsulate each child CAD
// contracting the block if it has chidren
// parsing at father of the son parsed
// puts zero in the son block
foreach($child_blocs as $children)
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
for($i =
0;$i <
count($bloc_arr);$i++
)
$b_ref =
& $b_ref[$bloc_arr[$i]];
if($i ==
count($bloc_arr)-
1)
$father_parsed =
& $b_ref['parsed'][count($b_ref['parsed'])-
1]; // c'est le dernier
$b_ref =
& $b_ref['children'];
$children_all_parsed =
& $b_ref[$children]['parsed'];
$children_structure =
& $b_ref[$children]['structure'];
if(count($children_all_parsed) ==
1)
$children_parsed =
$children_all_parsed['0'];
if(count($children_all_parsed) >
1)
for($l =
0; $l <
count($children_all_parsed)-
1; $l++
)
$children_parsed =
$children_parsed.
$children_all_parsed[$l];
$father_parsed =
$this->replaceBloc($children, $children_parsed, $father_parsed);
// puts zero in the child block
$children_all_parsed =
array($children_structure);
* this method allows to delete a variable.
* this method allows you to delete a block.
* But if the bolck is one, the substitution will be done in the template.
* this method allows you to return the contents of file.
* If the block name optional parameter is specified the method will return
* the contents of the block defined in the file.
* @param string $filename
* @param string $blocname
public function getFile($filename, $blocname =
'')
if(!$fp =
@fopen($filename, 'r')) // opening the file on reding
$this->error(0, $filename);
if(!empty($blocname)) // capture the block
$filebuffer =
$this->captureBloc($blocname, $filebuffer);
* this method allows to substitute a whole bloc and markers also.
$this->pathVerify($path); // check the existence of blocks of the path
// definition of the path
if(!$this->isdefined($path, 'NOITEM')) $this->defineBloc($path.
'.none');
// go to the bloc for éliminating !
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
if(count($bloc_arr) ==
1)
$b_ref =
& $b_ref[$bloc_arr[0]];
// delete the current block in the last father
for($i =
0;$i <
count($bloc_arr);$i++
)
$b_ref =
& $b_ref[$bloc_arr[$i]];
if($i ==
count($bloc_arr)-
2)
$last_father_parsed =
& $b_ref['parsed'][count($b_ref['parsed'])-
1];
if($i <
count($bloc_arr)-
1)
$b_ref =
& $b_ref['children'];
$last_father_parsed =
$this->replaceBloc($bloc_arr[count($bloc_arr)-
1], $replace, $last_father_parsed);
$b_ref['parsed'] =
array(); // delete the session of the last block !
* this method allows to reload variables of a block contained in the template.
$last_bloc =
$this->getItem($path);
$this->f[$this->f_no]['shortcut_blocs'][$last_bloc]['items'] =
$this->captureItems($last_bloc);
* this method allows to substitute a variable from the template file by the contents of a file.
// checking for one block
$this->parse($item, "{#include(\"$file\");}"); // replace by the include command
$this->f[$this->f_no]['cmd_items'][] =
$file; // add to the list of f_cmd
* this method allows to return a whole block from the template file.
$bloc_name =
$all_bloc[count($all_bloc)-
1];
* this method allows to print file the template parsed.
echo
$this->f[$this->f_no]['buffer'];
* this method allows to return the contents of the template file parsed.
$buffer =
$this->f[$this->f_no]['buffer'];
* this method allows to save the contents of the template file parsed.
// add function for creating folders
for($i =
0;$i <
count($all_dir)-
1;$i++
)
if($i >
0)$act_dir .=
'/';
$act_dir .=
$all_dir[$i];
if(!@mkdir($act_dir, 0755))
$this->error(7.1, $act_dir);
$output =
$this->Output();
$fp =
@fopen($path, 'w'); // open file
* this method applays special function in item
* @param string $functions
if(empty($functions))return $replace;
// there is a special function ?
$functions =
explode('|', $functions);
foreach($functions as $function_name)
if($function_name !=
'B' &&
$function_name !=
'I' &&
$function_name !=
'U' &&
$function_name !=
'S' &&
!empty($function_name))
$replace =
$function_name($replace);
// Bold, Italic, Underline
if(in_array('B', $functions))$replace =
"<strong>$replace</strong>";
if(in_array('I', $functions))$replace =
"<i>$replace</i>";
if(in_array('U', $functions))$replace =
"<u>$replace</u>";
if(in_array('S', $functions))$replace =
"<s>$replace</s>";
// replace the son in the father
// preserve conserve the father (adress) and his son (nom, structure)
// delete the last member of the block
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
for($i =
0;$i <
count($bloc);$i++
)
$b_ref =
& $b_ref[$bloc[$i]];
if($i ==
count($bloc) -
2) // capture of the last father
$father_parsed =
& $b_ref['parsed'][count($b_ref['parsed'])-
1];
if($i ==
count($bloc) -
1) // capture of the last son
$children_name =
$bloc[$i];
$children_all_parsed =
$b_ref['parsed'];
if(count($children_all_parsed) >
1)
for($j =
0;$j <
count($children_all_parsed)-
1;$j++
)
$children_parsed .=
$children_all_parsed[$j];
if(count($children_all_parsed) ==
1)
$children_parsed =
$children_all_parsed[0];
$b_ref =
& $b_ref['children'];
// replace the son in the father
$father_parsed =
$this->replaceBloc($children_name, $children_parsed, $father_parsed);
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
foreach($bloc_arr as $cur_bloc)
$b_ref =
& $b_ref[$cur_bloc]['children'];
$b_names =
array_keys($b_ref); // contains all names of the blocks
foreach($b_names as $bloc)
if(count($b_ref[$bloc]['children']) >
0)
// takes the parsed father and replace it in the buffer
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
// takes the names or parent BIG
$b_parsed =
''; // initialisation
foreach($b_names as $bloc)
$b_parsed =
$b_ref[$bloc]['parsed'][0];
if(count($b_ref[$bloc]['parsed']) >
1)
for($i =
0;$i <
count($b_ref[$bloc]['parsed'])-
1;$i++
)
$b_parsed .=
$b_ref[$bloc]['parsed'][$i];
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
// obtains the names of big blocks
foreach($big_blocs as $bloc)
if(count($b_ref[$bloc]['children']) >
0)
$this->parseBigFathers(); // compress the big blocks and parse in the buffer
if(TPLN_AUTO_CLEAN_BLOCS) $this->cleanBLocs();
* this method captures items
$subject =
$this->f[$this->f_no]['buffer']; // in the file
$blocs =
$this->f[$this->f_no]['shortcut_blocs']['all'];
// capture the blocks of subjet
// exclusion of spaces etc
$motif =
"/\{\\$([^ ;\*\$\\\,\\n\\t]+)?\}/msU";
elseif($type ==
'CONSTANT')
$motif =
"/\{CONST::([^ ;\.\*\$\\\,\\n\\t]+)?\}/msU";
$motif =
"/\{([^ ;\.\*\$\\\,\\n\\t]+)?\}/msU";
* this method captures block
$motif =
"<bloc::$name>(.*)?<\\/bloc::$name>";
$match =
@preg_match("/$motif/msU", $subject, $bloc);
$this->error(2, $this->f[$this->f_no]['name'], $name);
* this method captures all blocks
$subject =
& $this->f[$this->f_no]['buffer'];
$motif =
"<bloc::([^ ;\.\*\$\\\,\\n\\t]+)?>";
* this method captures items in each block
if(count($this->f[$this->f_no]['shortcut_blocs']['all']) ==
0)
foreach($this->f[$this->f_no]['shortcut_blocs']['all'] as $bloc)
$this->f[$this->f_no]['shortcut_blocs'][$bloc]['items'] =
$this->captureItems($bloc);
* this method gets all blocks
$motif =
"<bloc::([^ ;\.\*\$\\\,\\n\\t]+)?>";
* this method captures include commands in the template
$motif =
"\{#include\(([^ ;\*,\\n\\t]+)?\);\}";
$this->f[$this->f_no]['cmd_items'] =
$one;
* this method allows to know if a variable exists in the template file.
* @param string $item_name
public function itemExists($item_name, $bloc =
'')
if(@in_array($item_name, $this->f[$this->f_no]['shortcut_blocs'][$bloc]['items']))
* this method verifies item
$this->error(1, $this->f[$this->f_no]['name'], '', $item);
$this->error(1.1, $this->f[$this->f_no]['name'], $bloc, $item);
* this method verifies path
// reach the block and add the items to this block
for($i =
0; $i <
count($tab); $i++
)
$this->error(2, $this->f[$this->f_no]['name'], $tab[$i]);
* method allows to know if a block exists in the template file.
* @param string $bloc_name
if(@in_array($bloc_name, $this->f[$this->f_no]['shortcut_blocs']['all']))
* this method verifies if end block is not in double
foreach($this->f[$this->f_no]['shortcut_blocs']['all'] as $bloc_name)
$motif =
"/<\\/bloc::$bloc_name>/U";
$this->error(8, $this->f[$this->f_no]['name'], $bloc_name);
* this method verifies if block is not in double
if(count($this->f[$this->f_no]['shortcut_blocs']['all']) ==
0)
foreach($this->f[$this->f_no]['shortcut_blocs']['all'] as $bloc)
$this->error(2.1, $this->f[$this->f_no]['name'], $bloc);
* This method allows to rise an exception for blocks defined twice or more times in the template.
* this method replaces item
protected function replaceItem($name, $replace, $subject)
* this method replaces block
protected function replaceBloc($name, $replace, $subject)
$motif =
"<bloc::$name>(.*)?<\\/bloc::$name>";
* this method replaces include command
// parsing the file's name
$filebuffer =
$this->getFile($file);
$filebuffer =
$this->evalHtml($filebuffer);
$motif =
"/\{\#include\(\"$file\"\)\;\}|\{\#include\(\'$file\'\)\;\}/";
* this method replaces all PHP commands
$motif =
"#{\#(.*|^include)}#";
if(count($matches) ==
2 &&
$matches[1] >
0)
// replace by the globals variables
foreach($matches[1] as $m)
if(!in_array($tm, array('$_GET', '$_POST', '$_COOKIE', '$_SERVER', '$_SESSION', '$_REQUEST')))
* this method replaces all include commands
if(count($this->f[$this->f_no]['cmd_items']) ==
0)
foreach($this->f[$this->f_no]['cmd_items'] as $filename)
* this method evals template file
// $string = implode('', $string);
* this method returns if template is a PHP file
* @param string $filename
$php_file_extension =
'php|phtml|php4|php3'; // extensions of the php file
* this method returns if it is a block
* this method verifies if all the block are defined
protected function isDefined($path, $type =
'')
$fathers_path =
$this->getFathers($path); // r�cup�re les p�res
$fathers_path =
$path; // recover the fathers
if(@in_array($fathers_path, $this->f[$this->f_no]['shortcut_blocs']['used'], true))
// recover the text of the bloc
// recover the item of path
* this method returns the last item
// if(!preg_match("/[.]/",$path)) {return;}
return $path_arr[count($path_arr)-
1];
// recover the father, the next to last block
* this method returns the last father
// if(!preg_match("/[.]/",$path)) {return;}
return $path_arr[count($path_arr)-
2];
* this method returns all fathers in array
protected function getFathers($path, $type =
'', $with_item =
1)
// if(!preg_match("/[.]/",$path)) {return;}
$fathers_arr =
array_slice($path_arr, 0, count($path_arr)-
1); // all the names except the last
$fathers_arr =
$path_arr;
$fathers_path =
join('.', $fathers_arr);
$fathers_path =
$this->getFathers($path); // no item
$fathers_arr =
$this->getFathers($path, 'ARRAY'); // array
foreach($fathers_arr as $bloc)
$cur_path =
join('.', $cur_arr);
// definition of all the fathers
$b_ref =
& $this->f[$this->f_no]['def_blocs'];
foreach($cur_arr as $cur_bloc)
$b_ref =
& $b_ref[$cur_bloc];
if($i <
(count($cur_arr)-
1))
$b_ref =
& $b_ref['children'];
* this method inisializes path in TPLN memory manager
$cur['structure'] =
$this->captureBloc($bloc, $this->f[$this->f_no]['buffer']); // structure
$cur['parsed'][] =
$cur['structure']; // parsed
$cur['children'] =
array();
* this method registers path in TPLN memory manager
* @param string $fathers_path
protected function savePath($fathers_path)
$this->f[$this->f_no]['shortcut_blocs']['used'][] =
$fathers_path; // save in defined blocks
* This method allows to protect data against XSS attack,
* this method is used before insetring records in a database.
foreach($string as $key =>
$val)
$string[$key] =
$this->XSSProtect($val);
$string =
str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;",), $string);
$string =
preg_replace('#(&\#*\w+)[\s\r\n]+;#U', "$1;", $string);
// remove any attribute starting with "on" or xmlns
$string =
preg_replace('#(<[^>]+[\s\r\n\"\'])(on|xmlns)[^>]*>#iU', "$1>", $string);
// remove javascript: and vbscript: protocol
$string =
preg_replace('#([a-z]*)[\s\r\n]*=[\s\n\r]*([\`\'\"]*)[\\s\n\r]*j[\s\n\r]*a[\s\n\r]*v[\s\n\r]*a[\s\n\r]*s[\s\n\r]*c[\s\n\r]*r[\s\n\r]*i[\s\n\r]*p[\s\n\r]*t[\s\n\r]*:#iU', '$1=$2nojavascript...', $string);
$string =
preg_replace('#([a-z]*)[\s\r\n]*=([\'\"]*)[\s\n\r]*v[\s\n\r]*b[\s\n\r]*s[\s\n\r]*c[\s\n\r]*r[\s\n\r]*i[\s\n\r]*p[\s\n\r]*t[\s\n\r]*:#iU', '$1=$2novbscript...', $string);
// <span style="width: expression(alert('Ping!'));"></span>
$string =
preg_replace('#(<[^>]+)style[\s\r\n]*=[\s\r\n]*([\`\'\"]*).*expression[\s\r\n]*\([^>]*>#iU', "$1>", $string);
$string =
preg_replace('#(<[^>]+)style[\s\r\n]*=[\s\r\n]*([\`\'\"]*).*s[\s\n\r]*c[\s\n\r]*r[\s\n\r]*i[\s\n\r]*p[\s\n\r]*t[\s\n\r]*:*[^>]*>#iU', "$1>", $string);
// remove namespaced elements (we do not need them...)
// remove really unwanted tags
$string =
preg_replace('#</*(style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $string);
while ($oldstring !=
$string);
* this method replaces { _Version} by TPLN current version
if($this->itemExists('_Version')) // place the logo
$this->parse('_Version', TPLN_VERSION);
* this method replaces { _QueryCount} by the numbre of queries executed.
if($this->itemExists('_QueryCount')) // place the logo
$this->parse('_QueryCount', $this->query_count);
* this method replaces { _Logo} by TPLN Logo
$this->parse('_Logo', '<a href="http://tpln.sourceforge.net" title="Powered by TPLN template !"><img src="http://tpln.sourceforge.net/logo.gif" alt="made with TPLN Template!" border="0" target="_blank" /></a>');
* this method allows to know template total time execution.
* The variable { _Chrono} must be imperatively replaced inside your template file.
* @see write(), OutPut().
* this method replaces { _Chrono} by the time clock of the template
if($this->itemExists('_Chrono')) // place the chrono
$this->parse('_Chrono', $this->f[$this->f_no]['execution_time']);
$fin =
$time[1] +
$time[0];
// we want the perf of this cette session or all
$this->f[$this->f_no]['execution_time'] =
intval(10000 *
((double)
$fin - (double)
TPLN_CHRONO_STARTED)) /
10000;
$this->f[$this->f_no]['execution_time'] =
intval(10000 *
((double)
$fin - (double)
$this->f[$this->f_no]['chrono_started'])) /
10000;
// retourne la valeur de la premi�re ligne
* this method verifies if template is expired
$expire =
$this->getTime(TPLN_CACHE_DIR.
'/'.
$this->f[$this->f_no]['cache_name']);
if($expire >=
$this->f[$this->f_no]['time_started'])
@unlink(TPLN_CACHE_DIR.
'/'.
$this->f[$this->f_no]['cache_name']); // remove the file
// prend le temps au dessus du fichier
* this method gets current time
$fp =
@fopen(TPLN_CACHE_DIR.
'/'.
$this->f[$this->f_no]['cache_name'], 'r'); // open the file
// $data = fread($fp, filesize($file));
* this method creates file cache
if(!$this->f[$this->f_no]['create_cached_file'])
// add a function for creating folders
$all_dir =
explode('/', $this->f[$this->f_no]['cache_name']);
$count =
count($all_dir)-
1; // we want the last
$act_dir =
TPLN_CACHE_DIR;
for($i =
0;$i <
$count;$i++
)
$act_dir .=
'/'.
$all_dir[$i];
if(!@mkdir($act_dir, 0755))
$cache_file_content =
$this->f[$this->f_no]['cache_expire'].
"\r".
$this->f[$this->f_no]['buffer'];
$fp =
@fopen(TPLN_CACHE_DIR.
'/'.
$this->f[$this->f_no]['cache_name'], 'w'); // open the file
@fwrite($fp, $cache_file_content);
* this method returns cached file
$this->f[$this->f_no]['create_cached_file'] =
0;
$fp =
@fopen(TPLN_CACHE_DIR.
'/'.
$this->f[$this->f_no]['cache_name'], 'r');
$tmp_expire =
@fgets($fp, 12); // for the pointer's position after the time
* this method verifies if cach directory exists
// the directory exists ?
if(!@mkdir(TPLN_CACHE_DIR, 0755))
// $this->_AddTraceMsg();
* this method extracts string
* @param bool $inc_markup
public function extractStr($str, $start, $end, $inc_markup =
0)
$pos_start =
strpos($str, $start);
$pos_end =
strpos($str, $end, ($pos_start));
$pos_end =
strpos($str, $end, ($pos_start +
strlen($start)));
if(($pos_start !==
false) &&
($pos_end !==
false))
$pos2 =
($pos_end +
strlen($end)) -
$pos1;
$pos1 =
$pos_start +
strlen($start);
$pos2 =
$pos_end -
$pos1;
return substr($str, $pos1, $pos2);
* PHP str replace with count parameter
$subject_original =
$subject;
for ($i =
1;$i <=
$times;$i++
)
$pos =
strpos($subject, $search, $pos);
if($pos ===
false) break;
$subject =
substr($subject_original, 0, $pos);
$subject .=
substr($subject_original, $pos +
$len);
$subject_original =
$subject;
* This method allows to convert a php array to a javascript array, it is useful for Ajax.
//Find out if the given array is a numerical array
$max_length =
count($arr)-
1;
if(($keys[0] ==
0) and ($keys[$max_length] ==
$max_length))
//See if the first key is 0 and last key is length - 1
for($i=
0; $i<
count($keys); $i++
)
//See if each key correspondes to its position
//A key fails at position check.
$is_list =
false; //It is an associative array.
foreach($arr as $key=>
$value)
//Custom handling for arrays
if($is_list) $parts[] =
array2json($value); /* :RECURSION: */
else $parts[] =
'"' .
$key .
'":' .
array2json($value); /* :RECURSION: */
if(!$is_list) $str =
'"' .
$key .
'":';
//Custom handling for multiple data types
elseif($value ===
false) $str .=
'false'; //The booleans
elseif($value ===
true) $str .=
'true';
else $str .=
'"' .
addslashes($value) .
'"'; //All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
if($is_list) return '[' .
$json .
']';//Return numerical JSON
return '{' .
$json .
'}';//Return associative JSON
* This method allows to recover the contents of the text of the block,
* useful to recover the text after an exit of template
* @param string $bloc_name
if(empty($out)) $out =
$this->output();
$pattern_s =
"<!-- ajax::$bloc_name -->";
$pattern_e =
"<!-- /ajax::$bloc_name -->";
$pos_start =
strpos($out, $pattern_s);
if($pos_start ===
false)return '';
$pos_start +=
strlen($pattern_s);
$pos_end =
strpos($out, $pattern_e, $pos_start);
if($pos_end ===
false)return '';
$bloc_content =
substr($out, $pos_start, $pos_end-
$pos_start);
* This method allows to convert http, www, ftp, mailto into clickable element
/*$str = eregi_replace("([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)",
"<a href=\"mailto:\\1\">\\1</a>", $str);
$str = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
"<a href=\"\\1://\\2\\3\" target=\"_blank\">\\1://\\2\\3</a>", $str);
// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
// xxxx can only be alpha characters.
// yyyy is anything up to the first space, newline, comma, double quote or <
$ret =
preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
// matches an email@domain type address at the start of a line, or after a space.
// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
$ret =
preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
// matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
// zzzz is optional.. will contain everything up to the first space, newline,
// comma, double quote or <.
//$ret = preg_replace("#(^|[\n ]|)((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
// $ret = substr($ret, 1);
Documentation generated on Sat, 06 Mar 2010 21:34:08 +0100 by phpDocumentor 1.4.3