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