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