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
|
<?php
// PHP Template Preprocessor by Savetheinternet
// http://codecanyon.net/user/Savetheinternet
// savetheinternet@n0v4.com
// -----------------------------------------------------
// Standard configuration
//
// Enable global things like %gentime, etc.
$templateGlobals = true;
// If $templateGlobals is enabled.
// Do not change the keys, but the values (if you must), or it will not work. (Prefixed with %)
$templateGlobalsNames = Array(
'gentime' => 'gentime',
'template' => 'template'
);
// Allow {$phpvar}, etc, to be placed in the template file. This will use a (global) variable defined in PHP.
// Requires eval() to be enabled. Might be a security risk, so ensure your template files aren't writable before
// enabling this. (Prefixed with $)
$templateVariables = false;
// End config
// -----------------------------------------------------
//'/\{(!?[$%]?[\w\[\]]+)(([=\?:])(([^{^}]|\{.+?\})?)?\}/s'
// Don't change this if you don't know what you're doing.
// EXTREMELY CONFUSING RECURSION!
$templateRegex = '/\{(!?[$%]?[\w\[\]]+)(([=\?:])((?>[^{^}]|\{[^{^}]+\}|(?R))+?))?\}/s';
function templateParse($template, array $options, $globals = null, $templateFile = null) {
global $templateGlobals, $templateGlobalsNames, $templateVariables, $templateRegex;
//For the global variable {%gentime}
if($globals == null) {
$globals = Array();
if(isset($templateFile)) $globals['template'] = $templateFile;
$globals['gentime'] = microtime(true);
}
// What we'll end up finishing with
$templateBody = '';
$previousPosition = 0;
// Find the matches
if(preg_match_all($templateRegex, $template, $templateMatch)) {
//Iterate through matches
for($matchIndex=0;$matchIndex<count($templateMatch[0]);$matchIndex++) {
$optionName = $templateMatch[1][$matchIndex];
$optionValue = $templateMatch[0][$matchIndex];
$optionDelim = $templateMatch[3][$matchIndex];
$optionBlock = $templateMatch[4][$matchIndex];
$option = (isset($options[$optionName])?$options[$optionName]:null);
$position = strpos($template, $templateMatch[0][$matchIndex]);
// Replace the found string with "xxxx[...]". ("Bug fix"; allows duplicate tags)
$template = substr_replace($template, str_repeat('x', strlen($templateMatch[0][$matchIndex])), $position, strlen($templateMatch[0][$matchIndex]));
if($optionName[0] == '!') {
$optionReversed = true;
$optionName = substr($optionName, 1);
} else $optionReversed = false;
if($optionName[0] == '%') {
$tmpOptionName = substr($optionName, 1);
// $templateGlobals
if($tmpOptionName == $templateGlobalsNames['gentime']) {
$option = microtime(true)-$globals['gentime'].'s';
} elseif(isset($globals[$tmpOptionName])) {
$option = $globals[$tmpOptionName];
}
unset($tmpOptionName);
}
if(preg_match('/(.+?)\[/', $optionName, $optionArrayMatches)) {
$optionArrayKey = $optionArrayMatches[1];
$arrayOptionsTemp = $options[$optionArrayKey];
if(is_array($arrayOptionsTemp)) {
if(preg_match_all('/\[(.+?)\]/', $optionName, $optionArrayMatches)) {
for($optionArrayIndex=0;$optionArrayIndex<count($optionArrayMatches[0]);$optionArrayIndex++) {
if(isset($arrayOptionsTemp[$optionArrayMatches[1][$optionArrayIndex]])) {
$arrayOptionsTemp = $arrayOptionsTemp[$optionArrayMatches[1][$optionArrayIndex]];
$option = $arrayOptionsTemp;
} else break;
}
}
}
}
if($optionDelim==':') {
if(isset($option) && $option) {
if(is_array($option)) {
$optionValue = '';
for($optionIndex=0;$optionIndex<count($option);$optionIndex++) {
$tmpOption = $option[$optionIndex];
$tmpOptions = $options;
$tmpOptions[$optionName] = $tmpOption;
if($optionIndex == count($option)-1)
$globals['last'] = true;
else {
unset($globals['last']);
if($optionIndex == 0)
$globals['first'] = true;
else
unset($globals['first']);
}
$optionValue .= templateParse($optionBlock, $tmpOptions, $globals);
}
unset($tmpOption);
unset($tmpOptions);
unset($optionIndex);
unset($globals['first']);
unset($globals['last']);
} else {
$optionValue = templateParse($optionBlock, $options, $globals);
}
} else {
$optionValue = '';
}
} elseif($optionDelim=='?') {
// Conditionals
if((!$optionReversed && isset($option) && $option) || ($optionReversed && (!isset($option) || !$option))) {
/*echo print_r(Array(
$optionReversed?'reversed':'no',
isset($option)?'exists':'does not exist',
$option,
$optionName
));*/
$optionValue = templateParse($optionBlock, $options, $globals);
} else {
$optionValue = '';
}
} elseif(isset($option)) {
// If the value is specified...
if(is_array($option)) {
$optionValue = implode($option);
} else {
$optionValue = $option;
}
} elseif($optionDelim=='=') {
// If it has a default
$optionValue = templateParse($optionBlock, $options, $globals);
$options[$optionName] = $optionValue;
} elseif($templateVariables && $optionName[0] == '$') {
// Conditionals
$optionValue = eval("global ${optionName}; return ${optionName};");
}
// Append it to the body
$templateBody .= substr($template, $previousPosition, $position-$previousPosition).$optionValue;
$previousPosition = $position+strlen($templateMatch[0][$matchIndex]);
unset($position);
unset($optionValue);
}
}
// Append the rest of the template
$templateBody .= substr($template, $previousPosition);
return $templateBody;
}
function Element($templateFile, array $options) {
global $config;
// Small little hack to add the PM system
if(function_exists('create_pm_header') && (@$options['mod'] || @$options['__mod'])) {
$options['pm'] = create_pm_header();
}
// Read the template file
if($template = @file_get_contents("{$config['dir']['template']}/${templateFile}")) {
return templateParse($template, $options, null, $templateFile);
} else {
throw new Exception("Template file '${templateFile}' does not exist or is empty in '{$config['dir']['template']}'!");
}
}
?>
|