From c462ed9fc50e2c2a60e8cb01314a7d5fa498b412 Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Thu, 1 Mar 2012 11:32:49 +0100 Subject: template proc: avoid a notice and allow template name to include to be passed with a variable value --- include/template_processor.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'include/template_processor.php') diff --git a/include/template_processor.php b/include/template_processor.php index 28c3f07dd..b4d444e1c 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -116,7 +116,15 @@ * {{ inc [with $var1=$var2] }}{{ endinc }} */ private function _replcb_inc($args){ - list($tplfile, $newctx) = array_map('trim', explode("with",$args[2])); + if (strpos($args[2],"with")) { + list($tplfile, $newctx) = array_map('trim', explode("with",$args[2])); + } else { + $tplfile = trim($args[2]); + $newctx = null; + } + + if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile); + $this->_push_stack(); $r = $this->r; if (!is_null($newctx)) { -- cgit v1.2.3 From 60e5dda1b5e1be36a15c45ded2635340087a0b0f Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Fri, 2 Mar 2012 12:24:35 +0100 Subject: template proc: first optimization --- include/template_processor.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'include/template_processor.php') diff --git a/include/template_processor.php b/include/template_processor.php index b4d444e1c..a80a3997a 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -1,5 +1,5 @@ stack[] = array($this->r, $this->search, $this->replace, $this->nodes); + $this->stack[] = array($this->r, $this->nodes); } private function _pop_stack(){ - list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack); + list($this->r, $this->nodes) = array_pop($this->stack); } - private function _get_var($name){ - $keys = array_map('trim',explode(".",$name)); + private function _get_var($name, $retNoKey=false){ + $keys = array_map('trim',explode(".",$name)); + if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS; $val = $this->r; foreach($keys as $k) { $val = (isset($val[$k]) ? $val[$k] : null); @@ -194,13 +195,24 @@ return str_replace($this->search,$this->replace, $str); }*/ + private function var_replace($s){ + $m = array(); + if (preg_match_all('/\$([a-zA-Z0-9-_]+\.*)+/', $s,$m)){ + foreach($m[0] as $var){ + $val = $this->_get_var($var, true); + if ($val!=KEY_NOT_EXISTS) + $s = str_replace($var, $val, $s); + } + } + return $s; + } public function replace($s, $r) { $this->r = $r; - $this->search = array(); - $this->replace = array(); + /*$this->search = array(); + $this->replace = array();*/ - $this->_build_replace($r, ""); + //$this->_build_replace($r, ""); #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s); $s = $this->_build_nodes($s); @@ -215,7 +227,8 @@ while($os!=$s && $count<10){ $os=$s; $count++; //$s = $this->_str_replace($s); - $s = str_replace($this->search, $this->replace, $s); + $s = $this->var_replace($s); + //$s = str_replace($this->search, $this->replace, $s); } return template_unescape($s); } -- cgit v1.2.3 From 0bf087c8957ba28b76b7bc96345908735418a71f Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Fri, 2 Mar 2012 12:36:22 +0100 Subject: template proc: remove unused function and commented out code --- include/template_processor.php | 45 +----------------------------------------- 1 file changed, 1 insertion(+), 44 deletions(-) (limited to 'include/template_processor.php') diff --git a/include/template_processor.php b/include/template_processor.php index a80a3997a..9ac0a1313 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -28,19 +28,6 @@ die(); } - private function _build_replace($r, $prefix){ - - if(is_array($r) && count($r)) { - foreach ($r as $k => $v ) { - if (is_array($v)) { - $this->_build_replace($v, "$prefix$k."); - } else { - $this->search[] = $prefix . $k; - $this->replace[] = $v; - } - } - } - } private function _push_stack(){ $this->stack[] = array($this->r, $this->nodes); @@ -170,30 +157,6 @@ return $s; } - /* - private function _str_replace($str){ - #$this->search,$this->replace, - $searchs = $this->search; - foreach($searchs as $search){ - $search = "|".preg_quote($search)."(\|[a-zA-Z0-9_]*)*|"; - $m = array(); - if (preg_match_all($search, $str,$m)){ - foreach ($m[0] as $match){ - $toks = explode("|",$match); - $val = $this->_get_var($toks[0]); - for($k=1; $k1){ - $str = str_replace( $match, $val, $str); - } - } - } - - } - return str_replace($this->search,$this->replace, $str); - }*/ private function var_replace($s){ $m = array(); @@ -209,12 +172,7 @@ public function replace($s, $r) { $this->r = $r; - /*$this->search = array(); - $this->replace = array();*/ - - //$this->_build_replace($r, ""); - #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s); $s = $this->_build_nodes($s); $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s); @@ -222,13 +180,12 @@ // remove comments block $s = preg_replace('/{#[^#]*#}/', "" , $s); + // replace strings recursively (limit to 10 loops) $os = ""; $count=0; while($os!=$s && $count<10){ $os=$s; $count++; - //$s = $this->_str_replace($s); $s = $this->var_replace($s); - //$s = str_replace($this->search, $this->replace, $s); } return template_unescape($s); } -- cgit v1.2.3 From aef737f37637270b4f79ea390ee23a61f7fcb4ba Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Fri, 2 Mar 2012 15:53:48 +0100 Subject: template proc: allow isolation of var name between [ and ] $var.name -> $[var.name] --- include/template_processor.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'include/template_processor.php') diff --git a/include/template_processor.php b/include/template_processor.php index 9ac0a1313..111fc5849 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -160,13 +160,15 @@ private function var_replace($s){ $m = array(); - if (preg_match_all('/\$([a-zA-Z0-9-_]+\.*)+/', $s,$m)){ + if (preg_match_all('/\$\[{0,1}([a-zA-Z0-9-_]+\.*)+\]{0,1}/', $s,$m)){ foreach($m[0] as $var){ - $val = $this->_get_var($var, true); + $varn = str_replace(array("[","]"), array("",""), $var); + $val = $this->_get_var($varn, true); if ($val!=KEY_NOT_EXISTS) $s = str_replace($var, $val, $s); } } + return $s; } -- cgit v1.2.3 From 685686b3e81e53264b4946f54904e8f5323da2ea Mon Sep 17 00:00:00 2001 From: Fabio Comuni Date: Sat, 3 Mar 2012 00:00:38 +0100 Subject: template proc: fix variables regexp --- include/template_processor.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'include/template_processor.php') diff --git a/include/template_processor.php b/include/template_processor.php index 111fc5849..8671587fc 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -160,7 +160,16 @@ private function var_replace($s){ $m = array(); - if (preg_match_all('/\$\[{0,1}([a-zA-Z0-9-_]+\.*)+\]{0,1}/', $s,$m)){ + /** regexp: + * \$ literal $ + * (\[)? optional open square bracket + * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional + * dot, repeated at least 1 time + * (?(1)\]) if there was opened square bracket + * (subgrup 1), match close bracket + */ + if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){ + foreach($m[0] as $var){ $varn = str_replace(array("[","]"), array("",""), $var); $val = $this->_get_var($varn, true); -- cgit v1.2.3