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.
 
 
 
 
 
 

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