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