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