]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
added test blueprints, fixed? encoding issues
[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                         list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
120                         $this->_push_stack();
121                         $r = $this->r;
122                         if (!is_null($newctx)) {
123                                 list($a,$b) = array_map('trim', explode("=",$newctx));
124                                 $r[$a] = $this->_get_var($b); 
125                         }
126                         $this->nodes = Array();
127                         $tpl = get_markup_template($tplfile);
128                         $ret = $this->replace($tpl, $r);
129                         $this->_pop_stack();
130                         return $ret;
131                         
132                 }
133
134                 private function _replcb_node($m) {
135                         $node = $this->nodes[$m[1]];
136                         if (method_exists($this, "_replcb_".$node[1])){
137                                 $s = call_user_func(array($this, "_replcb_".$node[1]),  $node);
138                         } else {
139                                 $s = "";
140                         }
141                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
142                         return $s;
143                 }
144                                                 
145                 private function _replcb($m){
146                         //var_dump(array_map('htmlspecialchars', $m));
147                         $this->done = false;    
148                         $this->nodes[] = (array) $m;
149                         return "||". (count($this->nodes)-1) ."||";
150                 }
151                 
152                 private function _build_nodes($s){
153                         $this->done = false;
154                         while (!$this->done){
155                                 $this->done=true;
156                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
157                                 if ($s==Null) $this->_preg_error();
158                         }
159                         //({{ *else *}}[^{]*)?
160                         krsort($this->nodes);
161                         return $s;
162                 }
163                 
164         /*
165                 private function _str_replace($str){
166                         #$this->search,$this->replace,
167                         $searchs = $this->search;
168                         foreach($searchs as $search){
169                                 $search = "|".preg_quote($search)."(\|[a-zA-Z0-9_]*)*|";
170                                 $m = array();
171                                 if (preg_match_all($search, $str,$m)){
172                                         foreach ($m[0] as $match){
173                                                 $toks = explode("|",$match);
174                                                 $val = $this->_get_var($toks[0]);
175                                                 for($k=1; $k<count($toks); $k++){
176                                                         $func = $toks[$k];
177                                                         if (function_exists($func)) $val = $func($val);
178                                                 }
179                                                 if (count($toks)>1){
180                                                         $str = str_replace( $match, $val, $str);
181                                                 } 
182                                         }
183                                 }
184                                 
185                         }
186                         return str_replace($this->search,$this->replace, $str);
187                 }*/
188
189         
190                 public function replace($s, $r) {
191                         $this->r = $r;
192                         $this->search = array();
193                         $this->replace = array();
194
195                         $this->_build_replace($r, "");
196                         
197                         #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
198                         $s = $this->_build_nodes($s);
199
200                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
201                         if ($s==Null) $this->_preg_error();
202                         
203                         // remove comments block
204                         $s = preg_replace('/{#[^#]*#}/', "" , $s);
205                         // replace strings recursively (limit to 10 loops)
206                         $os = ""; $count=0;
207                         while($os!=$s && $count<10){
208                                 $os=$s; $count++;
209                                 //$s = $this->_str_replace($s);
210                                 $s = str_replace($this->search, $this->replace, $s);
211                         }
212                         return template_unescape($s);
213                 }
214         }
215         
216         $t = new Template;
217
218
219
220
221 function template_escape($s) {
222
223         return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
224
225
226 }
227
228 function template_unescape($s) {
229
230         return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);
231
232
233
234 }