]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
template proc: first optimization
[friendica.git] / include / template_processor.php
1 <?php
2         define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
3
4         class Template {
5                 var $r;
6                 var $search;
7                 var $replace;
8                 var $stack = array();
9                 var $nodes = array();
10                 var $done = false;
11                 var $d = false;
12                 var $lang = null;
13                 var $debug=false;
14                 
15                 private function _preg_error(){
16                         switch(preg_last_error()){
17                             case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR'); break;
18                             case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR'); break;
19                             case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR'); break;
20                             case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR'); break;
21                             case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
22                             default:
23                                         //die("Unknown preg error.");
24                                         return;
25                         }
26                         echo "<hr><pre>";
27                         debug_print_backtrace();
28                         die();
29                 }
30                 
31                 private function _build_replace($r, $prefix){
32         
33                         if(is_array($r) && count($r)) {
34                                 foreach ($r as $k => $v ) {
35                                         if (is_array($v)) {
36                                                 $this->_build_replace($v, "$prefix$k.");
37                                         } else {
38                                                 $this->search[] =  $prefix . $k;
39                                                 $this->replace[] = $v;
40                                         }
41                                 }
42                         }
43                 } 
44                 
45                 private function _push_stack(){
46                         $this->stack[] = array($this->r, $this->nodes);
47                 }
48                 private function _pop_stack(){
49                         list($this->r, $this->nodes) = array_pop($this->stack);
50                         
51                 }
52                 
53                 private function _get_var($name, $retNoKey=false){
54                         $keys = array_map('trim',explode(".",$name));
55                         if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS;
56                         $val = $this->r;
57                         foreach($keys as $k) {
58                                 $val = (isset($val[$k]) ? $val[$k] : null);
59                         }
60                         return $val;
61                 }
62                 
63                 /**
64                  * IF node
65                  * 
66                  * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
67                  * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
68                  * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
69                  */
70                 private function _replcb_if($args){
71                         if (strpos($args[2],"==")>0){
72                                 list($a,$b) = array_map("trim",explode("==",$args[2]));
73                                 $a = $this->_get_var($a);
74                                 if ($b[0]=="$") $b =  $this->_get_var($b);
75                                 $val = ($a == $b);
76                         } else if (strpos($args[2],"!=")>0){
77                                 list($a,$b) = explode("!=",$args[2]);
78                                 $a = $this->_get_var($a);
79                                 if ($b[0]=="$") $b =  $this->_get_var($b);
80                                 $val = ($a != $b);
81                         } else {
82                                 $val = $this->_get_var($args[2]);
83                         }
84                         $x = preg_split("|{{ *else *}}|", $args[3]);
85                         return ( $val ? $x[0] : (isset($x[1]) ? $x[1] : ""));
86                 }
87                 
88                 /**
89                  * FOR node
90                  * 
91                  * {{ for <$var> as $name }}...{{ endfor }}
92                  * {{ for <$var> as $key=>$name }}...{{ endfor }}
93                  */
94                 private function _replcb_for($args){
95                         $m = array_map('trim', explode(" as ", $args[2]));
96                         list($keyname, $varname) = explode("=>",$m[1]);
97                         if (is_null($varname)) { $varname=$keyname; $keyname=""; }
98                         if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
99                         //$vals = $this->r[$m[0]];
100                         $vals = $this->_get_var($m[0]);
101                         $ret="";
102                         if (!is_array($vals)) return $ret; 
103                         foreach ($vals as $k=>$v){
104                                 $this->_push_stack();
105                                 $r = $this->r;
106                                 $r[$varname] = $v;
107                                 if ($keyname!='') $r[$keyname] = $k;
108                                 $ret .=  $this->replace($args[3], $r);
109                                 $this->_pop_stack();
110                         }
111                         return $ret;
112                 }
113
114                 /**
115                  * INC node
116                  * 
117                  * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
118                  */
119                 private function _replcb_inc($args){
120                         if (strpos($args[2],"with")) {
121                                 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
122                         } else {
123                                 $tplfile = trim($args[2]);
124                                 $newctx = null;
125                         }
126                         
127                         if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile);
128                         
129                         $this->_push_stack();
130                         $r = $this->r;
131                         if (!is_null($newctx)) {
132                                 list($a,$b) = array_map('trim', explode("=",$newctx));
133                                 $r[$a] = $this->_get_var($b); 
134                         }
135                         $this->nodes = Array();
136                         $tpl = get_markup_template($tplfile);
137                         $ret = $this->replace($tpl, $r);
138                         $this->_pop_stack();
139                         return $ret;
140                         
141                 }
142
143                 private function _replcb_node($m) {
144                         $node = $this->nodes[$m[1]];
145                         if (method_exists($this, "_replcb_".$node[1])){
146                                 $s = call_user_func(array($this, "_replcb_".$node[1]),  $node);
147                         } else {
148                                 $s = "";
149                         }
150                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
151                         return $s;
152                 }
153                                                 
154                 private function _replcb($m){
155                         //var_dump(array_map('htmlspecialchars', $m));
156                         $this->done = false;    
157                         $this->nodes[] = (array) $m;
158                         return "||". (count($this->nodes)-1) ."||";
159                 }
160                 
161                 private function _build_nodes($s){
162                         $this->done = false;
163                         while (!$this->done){
164                                 $this->done=true;
165                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
166                                 if ($s==Null) $this->_preg_error();
167                         }
168                         //({{ *else *}}[^{]*)?
169                         krsort($this->nodes);
170                         return $s;
171                 }
172                 
173         /*
174                 private function _str_replace($str){
175                         #$this->search,$this->replace,
176                         $searchs = $this->search;
177                         foreach($searchs as $search){
178                                 $search = "|".preg_quote($search)."(\|[a-zA-Z0-9_]*)*|";
179                                 $m = array();
180                                 if (preg_match_all($search, $str,$m)){
181                                         foreach ($m[0] as $match){
182                                                 $toks = explode("|",$match);
183                                                 $val = $this->_get_var($toks[0]);
184                                                 for($k=1; $k<count($toks); $k++){
185                                                         $func = $toks[$k];
186                                                         if (function_exists($func)) $val = $func($val);
187                                                 }
188                                                 if (count($toks)>1){
189                                                         $str = str_replace( $match, $val, $str);
190                                                 } 
191                                         }
192                                 }
193                                 
194                         }
195                         return str_replace($this->search,$this->replace, $str);
196                 }*/
197
198                 private function var_replace($s){
199                         $m = array();
200                         if (preg_match_all('/\$([a-zA-Z0-9-_]+\.*)+/', $s,$m)){
201                                 foreach($m[0] as $var){
202                                         $val = $this->_get_var($var, true);
203                                         if ($val!=KEY_NOT_EXISTS)
204                                                 $s = str_replace($var, $val, $s);
205                                 }
206                         }
207                         return $s;
208                 }
209         
210                 public function replace($s, $r) {
211                         $this->r = $r;
212                         /*$this->search = array();
213                         $this->replace = array();*/
214
215                         //$this->_build_replace($r, "");
216                         
217                         #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
218                         $s = $this->_build_nodes($s);
219
220                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
221                         if ($s==Null) $this->_preg_error();
222                         
223                         // remove comments block
224                         $s = preg_replace('/{#[^#]*#}/', "" , $s);
225                         // replace strings recursively (limit to 10 loops)
226                         $os = ""; $count=0;
227                         while($os!=$s && $count<10){
228                                 $os=$s; $count++;
229                                 //$s = $this->_str_replace($s);
230                                 $s = $this->var_replace($s);
231                                 //$s = str_replace($this->search, $this->replace, $s);
232                         }
233                         return template_unescape($s);
234                 }
235         }
236         
237         $t = new Template;
238
239
240
241
242 function template_escape($s) {
243
244         return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
245
246
247 }
248
249 function template_unescape($s) {
250
251         return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);
252
253
254
255 }