-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.template.inc
308 lines (267 loc) · 14.9 KB
/
class.template.inc
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/***************************************
** Title........: Template class
** Filename.....: class.template.inc
** Author.......: Richard Heyes
** Version......: 1.4
** Notes........:
** Last changed.: 14 July 2001
** Last change..: Added if conditional syntax
***************************************/
class template{
var $var_names = array();
var $files = array();
var $start = '{';
var $end = '}';
/***************************************
** Function to load a template into
** the class.
***************************************/
function load_file($file_id, $filename){
$this->files[$file_id] = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);
}
/***************************************
** Function to load a template into
** the class.
***************************************/
function set_identifiers($start, $end){
$this->start = $start;
$this->end = $end;
}
/***************************************
** This function is used only by the
** register() method, for going through
** arays and extracting the values.
***************************************/
function traverse_array($file_id, $array){
while(list(,$value) = each($array)){
if(is_array($value)) $this->traverse_array($file_id, $value);
else $this->var_names[$file_id][] = $value;
}
}
/***************************************
** Function to register a variable(s).
***************************************/
function register($file_id, $var_name){
if(is_array($var_name)){
$this->traverse_array($file_id, $var_name);
}elseif($var_name != ''){
if(is_long(strpos($var_name, ',')) == TRUE){
$var_name = explode(',', $var_name);
for(reset($var_name); $current = current($var_name); next($var_name)) $this->var_names[$file_id][] = trim($current);
}else{
$this->var_names[$file_id][] = $var_name;
}
}
}
/***************************************
** Function to include another file.
** eg. A header/footer.
***************************************/
function include_file($file_id, $filename){
if(file_exists($filename)){
$include = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);
}else $include = '[ERROR: "'.$filename.'" does not exist.]';
$tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<include filename="'.$filename.'">'), strlen('<include filename="'.$filename.'">'));
$this->files[$file_id] = str_replace($tag, $include, $this->files[$file_id]);
}
/***************************************
** Function for reading and parsing the
** html file for normal variables. Also
** now checks for include tags and if
** necessary calls include_file()
***************************************/
function parse($file_id){
$file_ids = explode(',', $file_id);
for(reset($file_ids); $file_id = trim(current($file_ids)); next($file_ids)){
while(is_long($pos = strpos(strtolower($this->files[$file_id]), '<include filename="'))){
$pos += 19;
$endpos = strpos($this->files[$file_id], '">', $pos);
$filename = substr($this->files[$file_id], $pos, $endpos-$pos);
$this->include_file($file_id, $filename);
}
if(isset($this->var_names[$file_id]) AND count($this->var_names[$file_id]) > 0){
for($i=0; $i<count($this->var_names[$file_id]); $i++){
$temp_var = $this->var_names[$file_id][$i];
if(is_long(strpos($this->files[$file_id], $this->start.$temp_var.$this->end))){
global $$temp_var;
$this->files[$file_id] = str_replace($this->start.$temp_var.$this->end, $$temp_var, $this->files[$file_id]);
}elseif(is_long(strpos($this->files[$file_id], $this->start.$temp_var.'()'.$this->end))){
global $$temp_var;
$arguments = array();
for($i=0; $i<count($$temp_var); $i++) $arguments[] = ${$temp_var}[$i];
if(count($arguments) > 0) $arguments = '"'.implode('", "', $arguments).'"'; else $arguments = '';
eval('$output = '.$temp_var.'('.$arguments.');');
$this->files[$file_id] = str_replace($this->start.$temp_var.'()'.$this->end, $output, $this->files[$file_id]);
}
}
}
}
}
/***************************************
** Function for parsing an array.
***************************************/
function parse_loop($file_id, $array_name){
global $$array_name;
$loop_code = '';
$start_pos = strpos(strtolower($this->files[$file_id]), '<loop name="'.$array_name.'">') + strlen('<loop name="'.$array_name.'">');
$end_pos = strpos(strtolower($this->files[$file_id]), '</loop name="'.$array_name.'">');
$loop_code = substr($this->files[$file_id], $start_pos, $end_pos-$start_pos);
$start_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.$array_name.'">'),strlen('<loop name="'.$array_name.'">'));
$end_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.$array_name.'">'),strlen('</loop name="'.$array_name.'">'));
if($loop_code != ''){
$new_code = '';
for($i=0; $i<count($$array_name); $i++){
$temp_code = $loop_code;
while(list($key,) = each(${$array_name}[$i])){
$temp_code = str_replace($this->start.$key.$this->end,${$array_name}[$i][$key], $temp_code);
}
$new_code .= $temp_code;
}
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag, $new_code, $this->files[$file_id]);
}
}
/***************************************
** Function for parsing a Mysql result
** set.
***************************************/
function parse_sql($file_id, $result_name){
global $$result_name;
$loop_code = '';
$start_pos = strpos(strtolower($this->files[$file_id]), '<loop name="'.$result_name.'">') + strlen('<loop name="'.$result_name.'">');
$end_pos = strpos(strtolower($this->files[$file_id]), '</loop name="'.$result_name.'">');
$loop_code = substr($this->files[$file_id], $start_pos, $end_pos-$start_pos);
$start_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.$result_name.'">'),strlen('<loop name="'.$result_name.'">'));
$end_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.$result_name.'">'),strlen('</loop name="'.$result_name.'">'));
if($loop_code != ''){
$new_code = '';
$field_names = array();
for($i=0; $i<mysql_num_fields($$result_name); $i++) $field_names[] = mysql_field_name($$result_name,$i);
while($row_data = mysql_fetch_array($$result_name, MYSQL_ASSOC)){
$temp_code = $loop_code;
for($i=0; $i<count($field_names); $i++){
$temp_code = str_replace($this->start.$field_names[$i].$this->end, $row_data[$field_names[$i]], $temp_code);
}
$new_code.= $temp_code;
}
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag, $new_code, $this->files[$file_id]);
}
}
/***************************************
** Function for parsing a Postgres result
** set.
***************************************/
function parse_pgsql($file_id, $result_name){
global $$result_name;
$loop_code = '';
$start_pos = strpos(strtolower($this->files[$file_id]), '<loop name="'.$result_name.'">') + strlen('<loop name="'.$result_name.'">');
$end_pos = strpos(strtolower($this->files[$file_id]), '</loop name="'.$result_name.'">');
$loop_code = substr($this->files[$file_id], $start_pos, $end_pos-$start_pos);
$start_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.$result_name.'">'),strlen('<loop name="'.$result_name.'">'));
$end_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.$result_name.'">'),strlen('</loop name="'.$result_name.'">'));
if($loop_code != ''){
$new_code = '';
$field_names = array();
for($i=0; $i<pg_numfields($$result_name); $i++) $field_names[] = pg_fieldname($$result_name,$i);
for($i=0; $i<pg_numrows($$result_name) AND $row_data = pg_fetch_array($$result_name, $i); $i++){
$temp_code = $loop_code;
for($j=0; $j<count($field_names); $j++){
$temp_code = str_replace($this->start.$field_names[$j].$this->end, $row_data[$field_names[$j]], $temp_code);
}
$new_code.= $temp_code;
}
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag, $new_code, $this->files[$file_id]);
}
}
/***************************************
** Function looking for if blocks
** added by Stephan Lüderitz
***************************************/
function parse_if($file_id, $array_name){
$var_names = explode(',', $array_name);
for($i=0; $i<count($var_names); $i++){
$if_code = '';
$start_pos = strpos(strtolower($this->files[$file_id]), '<if name="'.strtolower($var_names[$i]).'">') + strlen('<if name="'.strtolower($var_names[$i]).'">');
$end_pos = strpos(strtolower($this->files[$file_id]), '</if name="'.strtolower($var_names[$i]).'">');
$if_code = substr($this->files[$file_id], $start_pos, $end_pos-$start_pos);
$start_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<if name="'.strtolower($var_names[$i]).'">'),strlen('<if name="'.strtolower($var_names[$i]).'">'));
$end_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</if name="'.strtolower($var_names[$i]).'">'),strlen('</if name="'.strtolower($var_names[$i]).'">'));
$new_code = '';
if($if_code != ''){
global ${$var_names[$i]};
if(@${$var_names[$i]})
$new_code = $if_code;
$this->files[$file_id] = str_replace($start_tag.$if_code.$end_tag, $new_code, $this->files[$file_id]);
}
}
}
/***************************************
** Function for printing the resulting
** file(s).
***************************************/
function print_file($file_id){
if(is_long(strpos($file_id, ',')) == TRUE){
$file_id = explode(',', $file_id);
for(reset($file_id); $current = current($file_id); next($file_id)) echo $this->files[trim($current)];
}else{
echo $this->files[$file_id];
}
}
/***************************************
** Function for returning the resulting
** file(s).
***************************************/
function return_file($file_id){
$ret = '';
if(is_long(strpos($file_id, ',')) == TRUE){
$file_id = explode(',', $file_id);
for(reset($file_id); $current = current($file_id); next($file_id)) $ret .= $this->files[trim($current)];
}else{
$ret .= $this->files[$file_id];
}
return $ret;
}
/***************************************
** Parses and then immediately prints
** the file. This function added by
** Bruce Christensen.
***************************************/
function pprint($file_id, $replacements = ''){
$this->register($file_id, $replacements);
$this->parse($file_id);
$this->print_file($file_id);
}
/***************************************
** Parses and then immediately returns
** the file's contents. Function added
** by Bruce Christensen.
***************************************/
function pget($file_id, $replacements = ''){
$this->register($file_id, $replacements);
$this->parse($file_id);
return $this->return_file($file_id);
}
/***************************************
** Loads a file, parses it, and prints it.
** This function added by Bruce Christensen.
***************************************/
function pprint_file($filename, $replacements = ''){
for($file_id=1; isset($this->files[$file_id]); $file_id++);
$this->load_file($file_id, $filename);
$this->pprint($file_id, $replacements);
unset($this->files[$file_id]);
}
/***************************************
** Loads, parses and then immediately
** returns the file's contents.
** Function added by Bruce Christensen.
***************************************/
function pget_file($filename, $replacements = ''){
for($file_id=1; isset($this->files[$file_id]); $file_id++);
$this->load_file($file_id, $filename);
return $this->pget($file_id, $replacements);
}
} // End of class
?>