Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

174 lines
4.7 KiB

  1. <?php
  2. class Template {
  3. var $r;
  4. var $search;
  5. var $replace;
  6. var $stack = array();
  7. var $nodes = array();
  8. var $done = false;
  9. private function _preg_error(){
  10. switch(preg_last_error()){
  11. case PREG_INTERNAL_ERROR: die('PREG_INTERNAL_ERROR'); break;
  12. case PREG_BACKTRACK_LIMIT_ERROR: die('PREG_BACKTRACK_LIMIT_ERROR'); break;
  13. case PREG_RECURSION_LIMIT_ERROR: die('PREG_RECURSION_LIMIT_ERROR'); break;
  14. case PREG_BAD_UTF8_ERROR: die('PREG_BAD_UTF8_ERROR'); break;
  15. case PREG_BAD_UTF8_OFFSET_ERROR: die('PREG_BAD_UTF8_OFFSET_ERROR'); break;
  16. default:
  17. die("Unknown preg error.");
  18. }
  19. }
  20. private function _build_replace($r, $prefix){
  21. if(is_array($r) && count($r)) {
  22. foreach ($r as $k => $v ) {
  23. if (is_array($v))
  24. $this->_build_replace($v, "$prefix$k.");
  25. $this->search[] = $prefix . $k;
  26. $this->replace[] = $v;
  27. }
  28. }
  29. }
  30. private function _push_stack(){
  31. $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
  32. }
  33. private function _pop_stack(){
  34. list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
  35. }
  36. private function _get_var($name){
  37. $keys = array_map('trim',explode(".",$name));
  38. $val = $this->r;
  39. foreach($keys as $k) {
  40. $val = $val[$k];
  41. }
  42. return $val;
  43. }
  44. /**
  45. * IF node
  46. *
  47. * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
  48. * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
  49. * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
  50. */
  51. private function _replcb_if($args){
  52. if (strpos($args[2],"==")>0){
  53. list($a,$b) = array_map("trim",explode("==",$args[2]));
  54. $a = $this->_get_var($a);
  55. if ($b[0]=="$") $b = $this->_get_var($b);
  56. $val = ($a == $b);
  57. } else if (strpos($args[2],"!=")>0){
  58. list($a,$b) = explode("!=",$args[2]);
  59. $a = $this->_get_var($a);
  60. if ($b[0]=="$") $b = $this->_get_var($b);
  61. $val = ($a != $b);
  62. } else {
  63. $val = $this->_get_var($args[2]);
  64. }
  65. list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
  66. return ($val?$strue:$sfalse);
  67. }
  68. /**
  69. * FOR node
  70. *
  71. * {{ for <$var> as $name }}...{{ endfor }}
  72. * {{ for <$var> as $key=>$name }}...{{ endfor }}
  73. */
  74. private function _replcb_for($args){
  75. $m = array_map('trim', explode(" as ", $args[2]));
  76. list($keyname, $varname) = explode("=>",$m[1]);
  77. if (is_null($varname)) { $varname=$keyname; $keyname=""; }
  78. if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
  79. //$vals = $this->r[$m[0]];
  80. $vals = $this->_get_var($m[0]);
  81. $ret="";
  82. if (!is_array($vals)) return $ret;
  83. foreach ($vals as $k=>$v){
  84. $this->_push_stack();
  85. $r = $this->r;
  86. $r[$varname] = $v;
  87. if ($keyname!='') $r[$keyname] = $k;
  88. $ret .= $this->replace($args[3], $r);
  89. $this->_pop_stack();
  90. }
  91. return $ret;
  92. }
  93. /**
  94. * INC node
  95. *
  96. * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
  97. */
  98. private function _replcb_inc($args){
  99. list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
  100. $this->_push_stack();
  101. $r = $this->r;
  102. if (!is_null($newctx)) {
  103. list($a,$b) = array_map('trim', explode("=",$newctx));
  104. $r[$a] = $this->_get_var($b);
  105. }
  106. $this->nodes = Array();
  107. $tpl = get_markup_template($tplfile);
  108. $ret = $this->replace($tpl, $r);
  109. $this->_pop_stack();
  110. return $ret;
  111. }
  112. private function _replcb_node($m) {
  113. $node = $this->nodes[$m[1]];
  114. if (method_exists($this, "_replcb_".$node[1])){
  115. $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
  116. } else {
  117. $s = "";
  118. }
  119. $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
  120. return $s;
  121. }
  122. private function _replcb($m){
  123. //var_dump(array_map('htmlspecialchars', $m));
  124. $this->done = false;
  125. $this->nodes[] = (array) $m;
  126. return "||". (count($this->nodes)-1) ."||";
  127. }
  128. private function _build_nodes($s){
  129. $this->done = false;
  130. while (!$this->done){
  131. $this->done=true;
  132. $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
  133. if ($s==Null) $this->_preg_error();
  134. }
  135. //({{ *else *}}[^{]*)?
  136. krsort($this->nodes);
  137. return $s;
  138. }
  139. public function replace($s, $r) {
  140. $this->r = $r;
  141. $this->search = array();
  142. $this->replace = array();
  143. $this->_build_replace($r, "");
  144. #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
  145. $s = $this->_build_nodes($s);
  146. $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
  147. if ($s==Null) $this->_preg_error();
  148. $s = str_replace($this->search,$this->replace, $s);
  149. return $s;
  150. }
  151. }
  152. $t = new Template;