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.

223 lines
5.8 KiB

10 years ago
10 years ago
  1. <?php
  2. define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
  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. var $lang = null;
  12. var $debug=false;
  13. private function _preg_error(){
  14. switch(preg_last_error()){
  15. case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR'); break;
  16. case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR'); break;
  17. case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR'); break;
  18. case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR'); break;
  19. case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
  20. default:
  21. //die("Unknown preg error.");
  22. return;
  23. }
  24. echo "<hr><pre>";
  25. debug_print_backtrace();
  26. die();
  27. }
  28. private function _push_stack(){
  29. $this->stack[] = array($this->r, $this->nodes);
  30. }
  31. private function _pop_stack(){
  32. list($this->r, $this->nodes) = array_pop($this->stack);
  33. }
  34. private function _get_var($name, $retNoKey=false){
  35. $keys = array_map('trim',explode(".",$name));
  36. if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS;
  37. $val = $this->r;
  38. foreach($keys as $k) {
  39. $val = (isset($val[$k]) ? $val[$k] : null);
  40. }
  41. return $val;
  42. }
  43. /**
  44. * IF node
  45. *
  46. * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
  47. * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
  48. * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
  49. */
  50. private function _replcb_if($args){
  51. if (strpos($args[2],"==")>0){
  52. list($a,$b) = array_map("trim",explode("==",$args[2]));
  53. $a = $this->_get_var($a);
  54. if ($b[0]=="$") $b = $this->_get_var($b);
  55. $val = ($a == $b);
  56. } else if (strpos($args[2],"!=")>0){
  57. list($a,$b) = explode("!=",$args[2]);
  58. $a = $this->_get_var($a);
  59. if ($b[0]=="$") $b = $this->_get_var($b);
  60. $val = ($a != $b);
  61. } else {
  62. $val = $this->_get_var($args[2]);
  63. }
  64. $x = preg_split("|{{ *else *}}|", $args[3]);
  65. return ( $val ? $x[0] : (isset($x[1]) ? $x[1] : ""));
  66. }
  67. /**
  68. * FOR node
  69. *
  70. * {{ for <$var> as $name }}...{{ endfor }}
  71. * {{ for <$var> as $key=>$name }}...{{ endfor }}
  72. */
  73. private function _replcb_for($args){
  74. $m = array_map('trim', explode(" as ", $args[2]));
  75. @list($keyname, $varname) = explode("=>",$m[1]);
  76. if (is_null($varname)) { $varname=$keyname; $keyname=""; }
  77. if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
  78. //$vals = $this->r[$m[0]];
  79. $vals = $this->_get_var($m[0]);
  80. $ret="";
  81. if (!is_array($vals)) return $ret;
  82. foreach ($vals as $k=>$v){
  83. $this->_push_stack();
  84. $r = $this->r;
  85. $r[$varname] = $v;
  86. if ($keyname!='') $r[$keyname] = $k;
  87. $ret .= $this->replace($args[3], $r);
  88. $this->_pop_stack();
  89. }
  90. return $ret;
  91. }
  92. /**
  93. * INC node
  94. *
  95. * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
  96. */
  97. private function _replcb_inc($args){
  98. if (strpos($args[2],"with")) {
  99. list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
  100. } else {
  101. $tplfile = trim($args[2]);
  102. $newctx = null;
  103. }
  104. if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile);
  105. $this->_push_stack();
  106. $r = $this->r;
  107. if (!is_null($newctx)) {
  108. list($a,$b) = array_map('trim', explode("=",$newctx));
  109. $r[$a] = $this->_get_var($b);
  110. }
  111. $this->nodes = Array();
  112. $tpl = get_markup_template($tplfile);
  113. $ret = $this->replace($tpl, $r);
  114. $this->_pop_stack();
  115. return $ret;
  116. }
  117. private function _replcb_node($m) {
  118. $node = $this->nodes[$m[1]];
  119. if (method_exists($this, "_replcb_".$node[1])){
  120. $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
  121. } else {
  122. $s = "";
  123. }
  124. $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
  125. return $s;
  126. }
  127. private function _replcb($m){
  128. //var_dump(array_map('htmlspecialchars', $m));
  129. $this->done = false;
  130. $this->nodes[] = (array) $m;
  131. return "||". (count($this->nodes)-1) ."||";
  132. }
  133. private function _build_nodes($s){
  134. $this->done = false;
  135. while (!$this->done){
  136. $this->done=true;
  137. $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
  138. if ($s==Null) $this->_preg_error();
  139. }
  140. //({{ *else *}}[^{]*)?
  141. krsort($this->nodes);
  142. return $s;
  143. }
  144. private function var_replace($s){
  145. $m = array();
  146. /** regexp:
  147. * \$ literal $
  148. * (\[)? optional open square bracket
  149. * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
  150. * dot, repeated at least 1 time
  151. * (?(1)\]) if there was opened square bracket
  152. * (subgrup 1), match close bracket
  153. */
  154. if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){
  155. foreach($m[0] as $var){
  156. $varn = str_replace(array("[","]"), array("",""), $var);
  157. $val = $this->_get_var($varn, true);
  158. if ($val!=KEY_NOT_EXISTS)
  159. $s = str_replace($var, $val, $s);
  160. }
  161. }
  162. return $s;
  163. }
  164. public function replace($s, $r) {
  165. $this->r = $r;
  166. $s = $this->_build_nodes($s);
  167. $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
  168. if ($s==Null) $this->_preg_error();
  169. // remove comments block
  170. $s = preg_replace('/{#[^#]*#}/', "" , $s);
  171. // replace strings recursively (limit to 10 loops)
  172. $os = ""; $count=0;
  173. while($os!=$s && $count<10){
  174. $os=$s; $count++;
  175. $s = $this->var_replace($s);
  176. }
  177. return template_unescape($s);
  178. }
  179. }
  180. $t = new Template;
  181. function template_escape($s) {
  182. return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
  183. }
  184. function template_unescape($s) {
  185. return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);
  186. }