]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
Merge pull request #317 from CatoTH/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                         
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) = 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                 private function _replcb_node($m) {
138                         $node = $this->nodes[$m[1]];
139                         if (method_exists($this, "_replcb_".$node[1])){
140                                 $s = call_user_func(array($this, "_replcb_".$node[1]),  $node);
141                         } else {
142                                 $s = "";
143                         }
144                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
145                         return $s;
146                 }
147                                                 
148                 private function _replcb($m){
149                         //var_dump(array_map('htmlspecialchars', $m));
150                         $this->done = false;    
151                         $this->nodes[] = (array) $m;
152                         return "||". (count($this->nodes)-1) ."||";
153                 }
154                 
155                 private function _build_nodes($s){
156                         $this->done = false;
157                         while (!$this->done){
158                                 $this->done=true;
159                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
160                                 if ($s==Null) $this->_preg_error();
161                         }
162                         //({{ *else *}}[^{]*)?
163                         krsort($this->nodes);
164                         return $s;
165                 }
166                 
167
168                 private function var_replace($s){
169                         $m = array();
170                         /** regexp:
171                          * \$                                           literal $
172                          * (\[)?                                        optional open square bracket
173                          * ([a-zA-Z0-9-_]+\.?)+         var name, followed by optional
174                          *                                                      dot, repeated at least 1 time
175                          * (?(1)\])                                     if there was opened square bracket
176                          *                                                      (subgrup 1), match close bracket
177                          */
178                         if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){
179                                 
180                                 foreach($m[0] as $var){
181                                         $varn = str_replace(array("[","]"), array("",""), $var);
182                                         $val = $this->_get_var($varn, true);
183                                         if ($val!=KEY_NOT_EXISTS)
184                                                 $s = str_replace($var, $val, $s);
185                                 }
186                         }
187                         
188                         return $s;
189                 }
190         
191                 public function replace($s, $r) {
192                         $this->r = $r;
193                         
194                         $s = $this->_build_nodes($s);
195
196                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
197                         if ($s==Null) $this->_preg_error();
198                         
199                         // remove comments block
200                         $s = preg_replace('/{#[^#]*#}/', "" , $s);
201                         
202                         // replace strings recursively (limit to 10 loops)
203                         $os = ""; $count=0;
204                         while($os!=$s && $count<10){
205                                 $os=$s; $count++;
206                                 $s = $this->var_replace($s);
207                         }
208                         return $s;
209                 }
210         }
211         
212         $t = new Template;
213
214
215
216
217 function template_escape($s) {
218
219         return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
220
221
222 }
223
224 function template_unescape($s) {
225
226         return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);
227
228
229
230 }