]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
Merge branch 'master' into newui
[friendica.git] / include / template_processor.php
1 <?php
2
3         class Template {
4                 var $r;
5                 var $search;
6                 var $replace;
7                 var $stack = array();
8                 var $nodes = array();
9                 var $done = false;
10                 var $d = false;
11                 
12                 private function _preg_error(){
13                         switch(preg_last_error()){
14                             case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR'); break;
15                             case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR'); break;
16                             case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR'); break;
17                             case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR'); break;
18                             case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
19                             default:
20                                         //die("Unknown preg error.");
21                                         return;
22                         }
23                         echo "<hr><pre>";
24                         debug_print_backtrace();
25                         die();
26                 }
27                 
28                 private function _build_replace($r, $prefix){
29         
30                         if(is_array($r) && count($r)) {
31                                 foreach ($r as $k => $v ) {
32                                         if (is_array($v))
33                                                 $this->_build_replace($v, "$prefix$k.");
34                                         
35                                         $this->search[] =  $prefix . $k;
36                                         $this->replace[] = $v;
37                                 }
38                         }
39                 } 
40                 
41                 private function _push_stack(){
42                         $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
43                 }
44                 private function _pop_stack(){
45                         list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
46                         
47                 }
48                 
49                 private function _get_var($name){
50                         $keys = array_map('trim',explode(".",$name));           
51                         $val = $this->r;
52                         foreach($keys as $k) {
53                                 $val = $val[$k];
54                         }
55                         return $val;
56                 }
57                 
58                 /**
59                  * IF node
60                  * 
61                  * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
62                  * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
63                  * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
64                  */
65                 private function _replcb_if($args){
66                         if (strpos($args[2],"==")>0){
67                                 list($a,$b) = array_map("trim",explode("==",$args[2]));
68                                 $a = $this->_get_var($a);
69                                 if ($b[0]=="$") $b =  $this->_get_var($b);
70                                 $val = ($a == $b);
71                         } else if (strpos($args[2],"!=")>0){
72                                 list($a,$b) = explode("!=",$args[2]);
73                                 $a = $this->_get_var($a);
74                                 if ($b[0]=="$") $b =  $this->_get_var($b);
75                                 $val = ($a != $b);
76                         } else {
77                                 $val = $this->_get_var($args[2]);
78                         }
79                         list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
80                         return ($val?$strue:$sfalse);
81                 }
82                 
83                 /**
84                  * FOR node
85                  * 
86                  * {{ for <$var> as $name }}...{{ endfor }}
87                  * {{ for <$var> as $key=>$name }}...{{ endfor }}
88                  */
89                 private function _replcb_for($args){
90                         $m = array_map('trim', explode(" as ", $args[2]));
91                         list($keyname, $varname) = explode("=>",$m[1]);
92                         if (is_null($varname)) { $varname=$keyname; $keyname=""; }
93                         if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
94                         //$vals = $this->r[$m[0]];
95                         $vals = $this->_get_var($m[0]);
96                         $ret="";
97                         if (!is_array($vals)) return $ret; 
98                         foreach ($vals as $k=>$v){
99                                 $this->_push_stack();
100                                 $r = $this->r;
101                                 $r[$varname] = $v;
102                                 if ($keyname!='') $r[$keyname] = $k;
103                                 $ret .=  $this->replace($args[3], $r);
104                                 $this->_pop_stack();
105                         }
106                         return $ret;
107                 }
108
109                 /**
110                  * INC node
111                  * 
112                  * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
113                  */
114                 private function _replcb_inc($args){
115                         list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
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                 public function replace($s, $r) {
161                         $this->r = $r;
162                         $this->search = array();
163                         $this->replace = array();
164         
165                         $this->_build_replace($r, "");
166                         
167                         #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
168                         $s = $this->_build_nodes($s);
169                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
170                         if ($s==Null) $this->_preg_error();
171                         
172                         // remove comments block
173                         $s = preg_replace('/{#[^#]*#}/', "" , $s);
174                         
175                         // replace strings recursively (limit to 10 loops)
176                         $os = ""; $count=0;
177                         while($os!=$s && $count<10){
178                                 $os=$s; $count++;
179                                 $s = str_replace($this->search,$this->replace, $s);
180                         }
181                         return $s;
182                 }
183         }
184         
185         $t = new Template;