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.
 
 
 
 
 
 

229 lines
5.9 KiB

  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. $x = explode("=>",$m[1]);
  76. if (count($x) == 1) {
  77. $varname = $x[0];
  78. $keyname = "";
  79. } else {
  80. list($keyname, $varname) = $x;
  81. }
  82. if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
  83. //$vals = $this->r[$m[0]];
  84. $vals = $this->_get_var($m[0]);
  85. $ret="";
  86. if (!is_array($vals)) return $ret;
  87. foreach ($vals as $k=>$v){
  88. $this->_push_stack();
  89. $r = $this->r;
  90. $r[$varname] = $v;
  91. if ($keyname!='') $r[$keyname] = (($k === 0) ? '0' : $k);
  92. $ret .= $this->replace($args[3], $r);
  93. $this->_pop_stack();
  94. }
  95. return $ret;
  96. }
  97. /**
  98. * INC node
  99. *
  100. * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
  101. */
  102. private function _replcb_inc($args){
  103. if (strpos($args[2],"with")) {
  104. list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
  105. } else {
  106. $tplfile = trim($args[2]);
  107. $newctx = null;
  108. }
  109. if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile);
  110. $this->_push_stack();
  111. $r = $this->r;
  112. if (!is_null($newctx)) {
  113. list($a,$b) = array_map('trim', explode("=",$newctx));
  114. $r[$a] = $this->_get_var($b);
  115. }
  116. $this->nodes = Array();
  117. $tpl = get_markup_template($tplfile);
  118. $ret = $this->replace($tpl, $r);
  119. $this->_pop_stack();
  120. return $ret;
  121. }
  122. private function _replcb_node($m) {
  123. $node = $this->nodes[$m[1]];
  124. if (method_exists($this, "_replcb_".$node[1])){
  125. $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
  126. } else {
  127. $s = "";
  128. }
  129. $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
  130. return $s;
  131. }
  132. private function _replcb($m){
  133. //var_dump(array_map('htmlspecialchars', $m));
  134. $this->done = false;
  135. $this->nodes[] = (array) $m;
  136. return "||". (count($this->nodes)-1) ."||";
  137. }
  138. private function _build_nodes($s){
  139. $this->done = false;
  140. while (!$this->done){
  141. $this->done=true;
  142. $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
  143. if ($s==Null) $this->_preg_error();
  144. }
  145. //({{ *else *}}[^{]*)?
  146. krsort($this->nodes);
  147. return $s;
  148. }
  149. private function var_replace($s){
  150. $m = array();
  151. /** regexp:
  152. * \$ literal $
  153. * (\[)? optional open square bracket
  154. * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
  155. * dot, repeated at least 1 time
  156. * (?(1)\]) if there was opened square bracket
  157. * (subgrup 1), match close bracket
  158. */
  159. if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){
  160. foreach($m[0] as $var){
  161. $varn = str_replace(array("[","]"), array("",""), $var);
  162. $val = $this->_get_var($varn, true);
  163. if ($val!=KEY_NOT_EXISTS)
  164. $s = str_replace($var, $val, $s);
  165. }
  166. }
  167. return $s;
  168. }
  169. public function replace($s, $r) {
  170. $this->r = $r;
  171. $s = $this->_build_nodes($s);
  172. $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
  173. if ($s==Null) $this->_preg_error();
  174. // remove comments block
  175. $s = preg_replace('/{#[^#]*#}/', "" , $s);
  176. // replace strings recursively (limit to 10 loops)
  177. $os = ""; $count=0;
  178. while($os!=$s && $count<10){
  179. $os=$s; $count++;
  180. $s = $this->var_replace($s);
  181. }
  182. return template_unescape($s);
  183. }
  184. }
  185. $t = new Template;
  186. function template_escape($s) {
  187. return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
  188. }
  189. function template_unescape($s) {
  190. return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);
  191. }