]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
Merge branch 'master' of git://github.com/friendica/friendica
[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                         $x = explode("=>",$m[1]);
84                         if (count($x) == 1) {
85                                 $varname = $x[0];
86                                 $keyname = "";
87                         } else {
88                                 list($keyname, $varname) = $x;
89                         }
90                         if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
91                         //$vals = $this->r[$m[0]];
92                         $vals = $this->_get_var($m[0]);
93                         $ret="";
94                         if (!is_array($vals)) return $ret; 
95                         foreach ($vals as $k=>$v){
96                                 $this->_push_stack();
97                                 $r = $this->r;
98                                 $r[$varname] = $v;
99                                 if ($keyname!='') $r[$keyname] = $k;
100                                 $ret .=  $this->replace($args[3], $r);
101                                 $this->_pop_stack();
102                         }
103                         return $ret;
104                 }
105
106                 /**
107                  * INC node
108                  * 
109                  * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
110                  */
111                 private function _replcb_inc($args){
112                         if (strpos($args[2],"with")) {
113                                 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
114                         } else {
115                                 $tplfile = trim($args[2]);
116                                 $newctx = null;
117                         }
118                         
119                         if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile);
120                         
121                         $this->_push_stack();
122                         $r = $this->r;
123                         if (!is_null($newctx)) {
124                                 list($a,$b) = array_map('trim', explode("=",$newctx));
125                                 $r[$a] = $this->_get_var($b); 
126                         }
127                         $this->nodes = Array();
128                         $tpl = get_markup_template($tplfile);
129                         $ret = $this->replace($tpl, $r);
130                         $this->_pop_stack();
131                         return $ret;
132                         
133                 }
134
135                 private function _replcb_node($m) {
136                         $node = $this->nodes[$m[1]];
137                         if (method_exists($this, "_replcb_".$node[1])){
138                                 $s = call_user_func(array($this, "_replcb_".$node[1]),  $node);
139                         } else {
140                                 $s = "";
141                         }
142                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
143                         return $s;
144                 }
145                                                 
146                 private function _replcb($m){
147                         //var_dump(array_map('htmlspecialchars', $m));
148                         $this->done = false;    
149                         $this->nodes[] = (array) $m;
150                         return "||". (count($this->nodes)-1) ."||";
151                 }
152                 
153                 private function _build_nodes($s){
154                         $this->done = false;
155                         while (!$this->done){
156                                 $this->done=true;
157                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
158                                 if ($s==Null) $this->_preg_error();
159                         }
160                         //({{ *else *}}[^{]*)?
161                         krsort($this->nodes);
162                         return $s;
163                 }
164                 
165
166                 private function var_replace($s){
167                         $m = array();
168                         /** regexp:
169                          * \$                                           literal $
170                          * (\[)?                                        optional open square bracket
171                          * ([a-zA-Z0-9-_]+\.?)+         var name, followed by optional
172                          *                                                      dot, repeated at least 1 time
173                          * (?(1)\])                                     if there was opened square bracket
174                          *                                                      (subgrup 1), match close bracket
175                          */
176                         if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){
177                                 
178                                 foreach($m[0] as $var){
179                                         $varn = str_replace(array("[","]"), array("",""), $var);
180                                         $val = $this->_get_var($varn, true);
181                                         if ($val!=KEY_NOT_EXISTS)
182                                                 $s = str_replace($var, $val, $s);
183                                 }
184                         }
185                         
186                         return $s;
187                 }
188         
189                 public function replace($s, $r) {
190                         $this->r = $r;
191                         
192                         $s = $this->_build_nodes($s);
193
194                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
195                         if ($s==Null) $this->_preg_error();
196                         
197                         // remove comments block
198                         $s = preg_replace('/{#[^#]*#}/', "" , $s);
199                         
200                         // replace strings recursively (limit to 10 loops)
201                         $os = ""; $count=0;
202                         while($os!=$s && $count<10){
203                                 $os=$s; $count++;
204                                 $s = $this->var_replace($s);
205                         }
206                         return template_unescape($s);
207                 }
208         }
209         
210         $t = new Template;
211
212
213
214
215 function template_escape($s) {
216
217         return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
218
219
220 }
221
222 function template_unescape($s) {
223
224         return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);
225
226
227
228 }