This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
/
template.php
188 lines (152 loc) · 4.72 KB
/
template.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Gate - Wiki engine and web-interface for WebTester Server
*
* Templates manipulation stuff
*
* Copyright (c) 2008-2009 Sergey I. Sharybin <g.ulairi@gmail.com>
*
* This program can be distributed under the terms of the GNU GPL.
* See the file COPYING.
*/
global $IFACE;
if ($IFACE != "SPAWNING NEW IFACE" || $_GET['IFACE'] != '') {
print ('HACKERS?');
die;
}
if ($_template_included_ != '#template_Included#') {
$_template_included_ = '#template_Included#';
$tpl_args = array ();
$tpl_namespace_depth = -1;
$tpl_stack = array ('count' => 0, 'data' => array ());
function tpl_set_args ($args) {
global $tpl_args, $tpl_namespace_depth;
$tpl_args[$tpl_namespace_depth] = $args;
}
function tpl_arg ($name) {
global $tpl_args, $tpl_namespace_depth;
return $tpl_args[$tpl_namespace_depth][$name];
}
function tpl_args () {
global $tpl_args, $tpl_namespace_depth;
return $tpl_args[$tpl_namespace_depth];
}
function tpl_full_args () { global $tpl_args; return $tpl_args; }
function targ ($name) { return tpl_arg ($name); }
function targs () { return tpl_args (); }
function tpl_eval ($src) {
return eval_code ('<? global $pIFACE, $CORE; ?>'.$src);
}
function tpl_parse ($src, $args) {
$i = 0;
$n = strlen ($src);
$tpl = '';
while ($i < $n) {
$c = $src[$i];
if ($c == '\\') {
// Backslahed character
$i++;
$c = $src[$i];
if ($c == 'n') $tpl .= "\n"; else
if ($c == 'r') $tpl .= "\r"; else
if ($c == 't') $tpl .= "\t"; else $tpl .= $c;
} else
if ($c == '<' && $src[$i+1] == '?') {
// Some script code
$scriptCode = '';
while ($i < $n) {
$ch = $src[$i];
if ($ch == '>' && $src[$i-1] == '?') {
break;
}
$scriptCode .= $ch;
$i++;
}
if ($ch == '>') {
$scriptCode .= $ch;
}
$tpl .= $scriptCode;
} else
if ($c == '$' && $src[$i + 1] == '{') {
// Variable
$token = '';
while ($i < $n) {
$ch = $src[$i];
if ($ch == '}') {
break;
}
$token .= $ch;
$i++;
}
if ($ch == '}') {
$token .= $ch;
$varName = ereg_replace ('\$\{(.+)\}', '\\1', $token);
$tpl .= $args[$varName];
} else {
$tpl .= $token;
}
} else {
$tpl .= $c;
}
$i++;
}
return $tpl;
}
function tpl_src ($src, $vars = array (), $parse = true, $eval = true) {
global $tpl_namespace_depth, $tpl_args;
$tpl_namespace_depth++;
tpl_set_args ($vars);
if ($parse) {
$src = tpl_parse ($src, $vars);
}
if ($eval) {
$r = tpl_eval ($src);
} else {
$r = $src;
}
$tpl_namespace_depth--;
return $r;
}
function tpl_src_unparsed ($src, $vars = array ()) {
return tpl_src ($src, $vars, false, false);
}
function tpl ($name, $vars = array (), $parse = true, $eval = true) {
global $DOCUMENT_ROOT, $tpl_stack;
if ($name[0] == '.') {
$dir = $tpl_stack[$tpl_stack['count']-1];
$name = preg_replace ('/^\./', '', $name);
$name = $dir.$name;
}
$tpl_stack[$tpl_stack['count']] = dirname ($name);
$tpl_stack['count']++;
$src = get_file ($DOCUMENT_ROOT.'/inc/templates/'.$name);
$res = tpl_src ($src, $vars, $parse, $eval);
$tpl_stack['count']--;
return $res;
}
function tpl_unparsed ($src, $vars = array ()) {
return tpl ($src, $vars, false, false);
}
function tpl_dir_relative () { return '/inc/templates'; }
function tpl_dir () {
global $DOCUMENT_ROOT;
return $DOCUMENT_ROOT.tpl_dir_relative ();
}
function tplp ($name, $vars = array ()) { print (tpl ($name, $vars)); }
function tpl_srcp ($src, $vars = array ()) { print (tpl_src ($src, $vars)); }
function tpl_linkage ($dir, $vars = array ()) {
global $DOCUMENT_ROOT, $tpl_stack;
if ($dir[0] == '.') {
$_dir = $tpl_stack[$tpl_stack['count']-1];
$dir = preg_replace ('/^\./', '', $dir);
$dir = $_dir.$dir;
}
$relative = $dir;
$dir = '/inc/templates/'.$dir;
$arr = dir_listing ($dir);
for ($i = 0; $i < count ($arr); $i++) {
tplp ($relative.'/'.$arr[$i], $vars);
}
}
}
?>