]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
Merge pull request #118 from fabrixxm/admin
[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                 
11                 private function _preg_error(){
12                         switch(preg_last_error()){
13                             case PREG_INTERNAL_ERROR: die('PREG_INTERNAL_ERROR'); break;
14                             case PREG_BACKTRACK_LIMIT_ERROR: die('PREG_BACKTRACK_LIMIT_ERROR'); break;
15                             case PREG_RECURSION_LIMIT_ERROR: die('PREG_RECURSION_LIMIT_ERROR'); break;
16                             case PREG_BAD_UTF8_ERROR: die('PREG_BAD_UTF8_ERROR'); break;
17                             case PREG_BAD_UTF8_OFFSET_ERROR: die('PREG_BAD_UTF8_OFFSET_ERROR'); break;
18                             default:
19                                         //die("Unknown preg error.");
20                                         return;
21                         }
22                 }
23                 
24                 private function _build_replace($r, $prefix){
25         
26                         if(is_array($r) && count($r)) {
27                                 foreach ($r as $k => $v ) {
28                                         if (is_array($v))
29                                                 $this->_build_replace($v, "$prefix$k.");
30                                         
31                                         $this->search[] =  $prefix . $k;
32                                         $this->replace[] = $v;
33                                 }
34                         }
35                 } 
36                 
37                 private function _push_stack(){
38                         $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
39                 }
40                 private function _pop_stack(){
41                         list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
42                 }
43                 
44                 private function _get_var($name){
45                         $keys = array_map('trim',explode(".",$name));                   
46                         $val = $this->r;
47                         foreach($keys as $k) {
48                                 $val = $val[$k];
49                         }
50                         return $val;
51                 }
52                 
53                 /**
54                  * IF node
55                  * 
56                  * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
57                  * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
58                  * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
59                  */
60                 private function _replcb_if($args){
61                         
62                         if (strpos($args[2],"==")>0){
63                                 list($a,$b) = array_map("trim",explode("==",$args[2]));
64                                 $a = $this->_get_var($a);
65                                 if ($b[0]=="$") $b =  $this->_get_var($b);
66                                 $val = ($a == $b);
67                         } else if (strpos($args[2],"!=")>0){
68                                 list($a,$b) = explode("!=",$args[2]);
69                                 $a = $this->_get_var($a);
70                                 if ($b[0]=="$") $b =  $this->_get_var($b);
71                                 $val = ($a != $b);
72                         } else {
73                                 $val = $this->_get_var($args[2]);
74                         }
75                         list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
76                         
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                         $s = str_replace($this->search,$this->replace, $s);
169                         
170                         return $s;
171                 }
172         }
173         
174         $t = new Template;