template.class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: template.class.php 1167 2014-11-03 03:06:21Z hypowang $
  6. */
  7. class template {
  8. var $tpldir;
  9. var $objdir;
  10. var $tplfile;
  11. var $objfile;
  12. var $langfile;
  13. var $vars;
  14. var $force = 0;
  15. var $var_regexp = "\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*";
  16. var $vtag_regexp = "\<\?=(\@?\\\$[a-zA-Z_]\w*(?:\[[\w\.\"\'\[\]\$]+\])*)\?\>";
  17. var $const_regexp = "\{([\w]+)\}";
  18. var $languages = array();
  19. var $sid;
  20. function __construct() {
  21. $this->template();
  22. }
  23. function template() {
  24. ob_start();
  25. $this->defaulttpldir = UC_ROOT.'./view/default';
  26. $this->tpldir = UC_ROOT.'./view/default';
  27. $this->objdir = UC_DATADIR.'./view';
  28. $this->langfile = UC_ROOT.'./view/default/templates.lang.php';
  29. if (version_compare(PHP_VERSION, '5') == -1) {
  30. register_shutdown_function(array(&$this, '__destruct'));
  31. }
  32. }
  33. function assign($k, $v) {
  34. $this->vars[$k] = $v;
  35. }
  36. function display($file) {
  37. extract($this->vars, EXTR_SKIP);
  38. include $this->gettpl($file);
  39. }
  40. function gettpl($file) {
  41. isset($_REQUEST['inajax']) && ($file == 'header' || $file == 'footer') && $file = $file.'_ajax';
  42. isset($_REQUEST['inajax']) && ($file == 'admin_header' || $file == 'admin_footer') && $file = substr($file, 6).'_ajax';
  43. $this->tplfile = $this->tpldir.'/'.$file.'.htm';
  44. $this->objfile = $this->objdir.'/'.$file.'.php';
  45. $tplfilemtime = @filemtime($this->tplfile);
  46. if($tplfilemtime === FALSE) {
  47. $this->tplfile = $this->defaulttpldir.'/'.$file.'.htm';
  48. }
  49. if($this->force || !file_exists($this->objfile) || @filemtime($this->objfile) < filemtime($this->tplfile)) {
  50. if(empty($this->language)) {
  51. @include $this->langfile;
  52. if(is_array($languages)) {
  53. $this->languages += $languages;
  54. }
  55. }
  56. $this->complie();
  57. }
  58. return $this->objfile;
  59. }
  60. function complie() {
  61. $template = file_get_contents($this->tplfile);
  62. $template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
  63. $template = preg_replace_callback("/\{lang\s+(\w+?)\}/is", array($this, 'complie_callback_lang_1'), $template);
  64. $template = preg_replace("/\{($this->var_regexp)\}/", "<?=\\1?>", $template);
  65. $template = preg_replace("/\{($this->const_regexp)\}/", "<?=\\1?>", $template);
  66. $template = preg_replace("/(?<!\<\?\=|\\\\)$this->var_regexp/", "<?=\\0?>", $template);
  67. $template = preg_replace_callback("/\<\?=(\@?\\\$[a-zA-Z_]\w*)((\[[\\$\[\]\w]+\])+)\?\>/is", array($this, 'complie_callback_arrayindex_12'), $template);
  68. $template = preg_replace_callback("/\{\{eval (.*?)\}\}/is", array($this, 'complie_callback_stripvtag_1'), $template);
  69. $template = preg_replace_callback("/\{eval (.*?)\}/is", array($this, 'complie_callback_stripvtag_1'), $template);
  70. $template = preg_replace_callback("/\{for (.*?)\}/is", array($this, 'complie_callback_stripvtag_for1'), $template);
  71. $template = preg_replace_callback("/\{elseif\s+(.+?)\}/is", array($this, 'complie_callback_stripvtag_elseif1'), $template);
  72. for($i=0; $i<2; $i++) {
  73. $template = preg_replace_callback("/\{loop\s+$this->vtag_regexp\s+$this->vtag_regexp\s+$this->vtag_regexp\}(.+?)\{\/loop\}/is", array($this, 'complie_callback_loopsection_1234'), $template);
  74. $template = preg_replace_callback("/\{loop\s+$this->vtag_regexp\s+$this->vtag_regexp\}(.+?)\{\/loop\}/is", array($this, 'complie_callback_loopsection_123'), $template);
  75. }
  76. $template = preg_replace_callback("/\{if\s+(.+?)\}/is", array($this, 'complie_callback_stripvtag_if1'), $template);
  77. $template = preg_replace("/\{template\s+(\w+?)\}/is", "<? include \$this->gettpl('\\1');?>", $template);
  78. $template = preg_replace_callback("/\{template\s+(.+?)\}/is", array($this, 'complie_callback_stripvtag_template1'), $template);
  79. $template = preg_replace("/\{else\}/is", "<? } else { ?>", $template);
  80. $template = preg_replace("/\{\/if\}/is", "<? } ?>", $template);
  81. $template = preg_replace("/\{\/for\}/is", "<? } ?>", $template);
  82. $template = preg_replace("/$this->const_regexp/", "<?=\\1?>", $template);
  83. $template = "<? if(!defined('UC_ROOT')) exit('Access Denied');?>\r\n$template";
  84. $template = preg_replace("/(\\\$[a-zA-Z_]\w+\[)([a-zA-Z_]\w+)\]/i", "\\1'\\2']", $template);
  85. $template = preg_replace("/\<\?(\s{1})/is", "<?php\\1", $template);
  86. $template = preg_replace("/\<\?\=(.+?)\?\>/is", "<?php echo \\1;?>", $template);
  87. $fp = fopen($this->objfile, 'w');
  88. fwrite($fp, $template);
  89. fclose($fp);
  90. }
  91. function complie_callback_lang_1($matches) {
  92. return $this->lang($matches[1]);
  93. }
  94. function complie_callback_arrayindex_12($matches) {
  95. return $this->arrayindex($matches[1], $matches[2]);
  96. }
  97. function complie_callback_stripvtag_1($matches) {
  98. return $this->stripvtag('<? '.$matches[1].'?>');
  99. }
  100. function complie_callback_stripvtag_for1($matches) {
  101. return $this->stripvtag('<? for('.$matches[1].') {?>');
  102. }
  103. function complie_callback_stripvtag_elseif1($matches) {
  104. return $this->stripvtag('<? } elseif('.$matches[1].') { ?>');
  105. }
  106. function complie_callback_loopsection_1234($matches) {
  107. return $this->loopsection($matches[1], $matches[2], $matches[3], $matches[4]);
  108. }
  109. function complie_callback_loopsection_123($matches) {
  110. return $this->loopsection($matches[1], '', $matches[2], $matches[3]);
  111. }
  112. function complie_callback_stripvtag_if1($matches) {
  113. return $this->stripvtag('<? if('.$matches[1].') { ?>');
  114. }
  115. function complie_callback_stripvtag_template1($matches) {
  116. return $this->stripvtag('<? include $this->gettpl('.$matches[1].'); ?>');
  117. }
  118. function arrayindex($name, $items) {
  119. $items = preg_replace("/\[([a-zA-Z_]\w*)\]/is", "['\\1']", $items);
  120. return "<?=$name$items?>";
  121. }
  122. function stripvtag($s) {
  123. return preg_replace("/$this->vtag_regexp/is", "\\1", str_replace("\\\"", '"', $s));
  124. }
  125. function loopsection($arr, $k, $v, $statement) {
  126. $arr = $this->stripvtag($arr);
  127. $k = $this->stripvtag($k);
  128. $v = $this->stripvtag($v);
  129. $statement = str_replace("\\\"", '"', $statement);
  130. return $k ? "<? foreach((array)$arr as $k => $v) {?>$statement<? }?>" : "<? foreach((array)$arr as $v) {?>$statement<? } ?>";
  131. }
  132. function lang($k) {
  133. return !empty($this->languages[$k]) ? $this->languages[$k] : "{ $k }";
  134. }
  135. function _transsid($url, $tag = '', $wml = 0) {
  136. $sid = $this->sid;
  137. $tag = stripslashes($tag);
  138. if(!$tag || (!preg_match("/^(http:\/\/|mailto:|#|javascript)/i", $url) && !strpos($url, 'sid='))) {
  139. if($pos = strpos($url, '#')) {
  140. $urlret = substr($url, $pos);
  141. $url = substr($url, 0, $pos);
  142. } else {
  143. $urlret = '';
  144. }
  145. $url .= (strpos($url, '?') ? ($wml ? '&amp;' : '&') : '?').'sid='.$sid.$urlret;
  146. }
  147. return $tag.$url;
  148. }
  149. function __destruct() {
  150. if($_COOKIE['sid']) {
  151. }
  152. $sid = rawurlencode($this->sid);
  153. $content = preg_replace_callback("/\<a(\s*[^\>]+\s*)href\=([\"|\']?)([^\"\'\s]+)/is", array($this, 'destruct_callback_transsid_312'), ob_get_contents());
  154. $content = preg_replace("/(\<form.+?\>)/is", "\\1\n<input type=\"hidden\" name=\"sid\" value=\"".rawurldecode(rawurldecode(rawurldecode($sid)))."\" />", $content);
  155. ob_end_clean();
  156. echo $content;
  157. }
  158. function destruct_callback_transsid_312($matches) {
  159. return $this->_transsid($matches[3],'<a'.$matches[1].'href='.$matches[2]);
  160. }
  161. }