]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
Merge pull request #117 from tobiasd/master
[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                         }
21                 }
22                 
23                 private function _build_replace($r, $prefix){
24         
25                         if(is_array($r) && count($r)) {
26                                 foreach ($r as $k => $v ) {
27                                         if (is_array($v))
28                                                 $this->_build_replace($v, "$prefix$k.");
29                                         
30                                         $this->search[] =  $prefix . $k;
31                                         $this->replace[] = $v;
32                                 }
33                         }
34                 } 
35                 
36                 private function _push_stack(){
37                         $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
38                 }
39                 private function _pop_stack(){
40                         list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
41                 }
42                 
43                 private function _get_var($name){
44                         $keys = array_map('trim',explode(".",$name));                   
45                         $val = $this->r;
46                         foreach($keys as $k) {
47                                 $val = $val[$k];
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                         
61                         if (strpos($args[2],"==")>0){
62                                 list($a,$b) = array_map("trim",explode("==",$args[2]));
63                                 $a = $this->_get_var($a);
64                                 if ($b[0]=="$") $b =  $this->_get_var($b);
65                                 $val = ($a == $b);
66                         } else if (strpos($args[2],"!=")>0){
67                                 list($a,$b) = explode("!=",$args[2]);
68                                 $a = $this->_get_var($a);
69                                 if ($b[0]=="$") $b =  $this->_get_var($b);
70                                 $val = ($a != $b);
71                         } else {
72                                 $val = $this->_get_var($args[2]);
73                         }
74                         list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
75                         
76                         return ($val?$strue:$sfalse);
77                 }
78                 
79                 /**
80                  * FOR node
81                  * 
82                  * {{ for <$var> as $name }}...{{ endfor }}
83                  * {{ for <$var> as $key=>$name }}...{{ endfor }}
84                  */
85                 private function _replcb_for($args){
86                         $m = array_map('trim', explode(" as ", $args[2]));
87                         list($keyname, $varname) = explode("=>",$m[1]);
88                         if (is_null($varname)) { $varname=$keyname; $keyname=""; }
89                         if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
90                         //$vals = $this->r[$m[0]];
91                         $vals = $this->_get_var($m[0]);
92                         $ret="";
93                         if (!is_array($vals)) return $ret; 
94                         foreach ($vals as $k=>$v){
95                                 $this->_push_stack();
96                                 $r = $this->r;
97                                 $r[$varname] = $v;
98                                 if ($keyname!='') $r[$keyname] = $k;
99                                 $ret .=  $this->replace($args[3], $r);
100                                 $this->_pop_stack();
101                         }
102                         return $ret;
103                 }
104
105                 /**
106                  * INC node
107                  * 
108                  * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
109                  */
110                 private function _replcb_inc($args){
111                         list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
112                         $this->_push_stack();
113                         $r = $this->r;
114                         if (!is_null($newctx)) {
115                                 list($a,$b) = array_map('trim', explode("=",$newctx));
116                                 $r[$a] = $this->_get_var($b); 
117                         }
118                         $this->nodes = Array();
119                         $tpl = get_markup_template($tplfile);
120                         $ret = $this->replace($tpl, $r);
121                         $this->_pop_stack();
122                         return $ret;
123                         
124                 }
125
126                 private function _replcb_node($m) {
127                         $node = $this->nodes[$m[1]];
128                         if (method_exists($this, "_replcb_".$node[1])){
129                                 $s = call_user_func(array($this, "_replcb_".$node[1]),  $node);
130                         } else {
131                                 $s = "";
132                         }
133                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
134                         return $s;
135                 }
136                                                 
137                 private function _replcb($m){
138                         //var_dump(array_map('htmlspecialchars', $m));
139                         $this->done = false;    
140                         $this->nodes[] = (array) $m;
141                         return "||". (count($this->nodes)-1) ."||";
142                 }
143                 
144                 private function _build_nodes($s){
145                         $this->done = false;
146                         while (!$this->done){
147                                 $this->done=true;
148                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
149                                 if ($s==Null) $this->_preg_error();
150                         }
151                         //({{ *else *}}[^{]*)?
152                         krsort($this->nodes);
153                         return $s;
154                 }
155                 
156                 public function replace($s, $r) {
157                         $this->r = $r;
158                         $this->search = array();
159                         $this->replace = array();
160         
161                         $this->_build_replace($r, "");
162                         
163                         #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
164                         $s = $this->_build_nodes($s);
165                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
166                         if ($s==Null) $this->_preg_error();
167                         $s = str_replace($this->search,$this->replace, $s);
168                         
169                         return $s;
170                 }
171         }
172         
173         $t = new Template;