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