]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
Initial work on new UI
[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: die('PREG_INTERNAL_ERROR'); break;
15                             case PREG_BACKTRACK_LIMIT_ERROR: die('PREG_BACKTRACK_LIMIT_ERROR'); break;
16                             case PREG_RECURSION_LIMIT_ERROR: die('PREG_RECURSION_LIMIT_ERROR'); break;
17                             case PREG_BAD_UTF8_ERROR: die('PREG_BAD_UTF8_ERROR'); break;
18                             case PREG_BAD_UTF8_OFFSET_ERROR: die('PREG_BAD_UTF8_OFFSET_ERROR'); break;
19                             default:
20                                         //die("Unknown preg error.");
21                                         return;
22                         }
23                 }
24                 
25                 private function _build_replace($r, $prefix){
26         
27                         if(is_array($r) && count($r)) {
28                                 foreach ($r as $k => $v ) {
29                                         if (is_array($v))
30                                                 $this->_build_replace($v, "$prefix$k.");
31                                         
32                                         $this->search[] =  $prefix . $k;
33                                         $this->replace[] = $v;
34                                 }
35                         }
36                 } 
37                 
38                 private function _push_stack(){
39                         $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
40                 }
41                 private function _pop_stack(){
42                         list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
43                         
44                 }
45                 
46                 private function _get_var($name){
47                         $keys = array_map('trim',explode(".",$name));           
48                         $val = $this->r;
49                         foreach($keys as $k) {
50                                 $val = $val[$k];
51                         }
52                         return $val;
53                 }
54                 
55                 /**
56                  * IF node
57                  * 
58                  * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
59                  * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
60                  * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
61                  */
62                 private function _replcb_if($args){
63                         if (strpos($args[2],"==")>0){
64                                 list($a,$b) = array_map("trim",explode("==",$args[2]));
65                                 $a = $this->_get_var($a);
66                                 if ($b[0]=="$") $b =  $this->_get_var($b);
67                                 $val = ($a == $b);
68                         } else if (strpos($args[2],"!=")>0){
69                                 list($a,$b) = explode("!=",$args[2]);
70                                 $a = $this->_get_var($a);
71                                 if ($b[0]=="$") $b =  $this->_get_var($b);
72                                 $val = ($a != $b);
73                         } else {
74                                 $val = $this->_get_var($args[2]);
75                         }
76                         list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
77                         return ($val?$strue:$sfalse);
78                 }
79                 
80                 /**
81                  * FOR node
82                  * 
83                  * {{ for <$var> as $name }}...{{ endfor }}
84                  * {{ for <$var> as $key=>$name }}...{{ endfor }}
85                  */
86                 private function _replcb_for($args){
87                         $m = array_map('trim', explode(" as ", $args[2]));
88                         list($keyname, $varname) = explode("=>",$m[1]);
89                         if (is_null($varname)) { $varname=$keyname; $keyname=""; }
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                         list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
113                         $this->_push_stack();
114                         $r = $this->r;
115                         if (!is_null($newctx)) {
116                                 list($a,$b) = array_map('trim', explode("=",$newctx));
117                                 $r[$a] = $this->_get_var($b); 
118                         }
119                         $this->nodes = Array();
120                         $tpl = get_markup_template($tplfile);
121                         $ret = $this->replace($tpl, $r);
122                         $this->_pop_stack();
123                         return $ret;
124                         
125                 }
126
127                 private function _replcb_node($m) {
128                         $node = $this->nodes[$m[1]];
129                         if (method_exists($this, "_replcb_".$node[1])){
130                                 $s = call_user_func(array($this, "_replcb_".$node[1]),  $node);
131                         } else {
132                                 $s = "";
133                         }
134                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
135                         return $s;
136                 }
137                                                 
138                 private function _replcb($m){
139                         //var_dump(array_map('htmlspecialchars', $m));
140                         $this->done = false;    
141                         $this->nodes[] = (array) $m;
142                         return "||". (count($this->nodes)-1) ."||";
143                 }
144                 
145                 private function _build_nodes($s){
146                         $this->done = false;
147                         while (!$this->done){
148                                 $this->done=true;
149                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
150                                 if ($s==Null) $this->_preg_error();
151                         }
152                         //({{ *else *}}[^{]*)?
153                         krsort($this->nodes);
154                         return $s;
155                 }
156                 
157                 public function replace($s, $r) {
158                         $this->r = $r;
159                         $this->search = array();
160                         $this->replace = array();
161         
162                         $this->_build_replace($r, "");
163                         
164                         #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
165                         $s = $this->_build_nodes($s);
166                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
167                         if ($s==Null) $this->_preg_error();
168                         
169                         // remove comments block
170                         $s = preg_replace('/{#[^#]*#}/', "" , $s);
171                         
172                         // replace strings recursively (limit to 10 loops)
173                         $os = ""; $count=0;
174                         while($os!=$s && $count<10){
175                                 $os=$s; $count++;
176                                 $s = str_replace($this->search,$this->replace, $s);
177                         }
178                         return $s;
179                 }
180         }
181         
182         $t = new Template;