/
Template.php
303 lines (255 loc) · 8.94 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
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
<?php //-*-php-*-
// rcs_id('$Id: Template.php 7592 2010-07-02 08:24:28Z vargenau $');
require_once("lib/ErrorManager.php");
/** An HTML template.
*/
class Template
{
/**
* name optionally of form "theme/template" to include parent templates in children
*/
function Template ($name, &$request, $args = false) {
global $WikiTheme;
$this->_request =& $request;
$this->_basepage = $request->getArg('pagename');
if (strstr($name, "/")) {
$oldname = $WikiTheme->_name;
$oldtheme = $WikiTheme->_theme;
list($themename, $name) = explode("/", $name);
$WikiTheme->_theme = "themes/$themename";
$WikiTheme->_name = $name;
}
$this->_name = $name;
$file = $WikiTheme->findTemplate($name);
if (!$file) {
trigger_error("no template for $name found.", E_USER_WARNING);
return;
}
if (isset($oldname)) {
$WikiTheme->_name = $oldname;
$WikiTheme->_theme = $oldtheme;
}
$fp = fopen($file, "rb");
if (!$fp) {
trigger_error("$file not found", E_USER_WARNING);
return;
}
$request->_TemplatesProcessed[$name] = 1;
$this->_tmpl = fread($fp, filesize($file));
if ($fp) fclose($fp);
//$userid = $request->_user->_userid;
if (is_array($args))
$this->_locals = $args;
elseif ($args)
$this->_locals = array('CONTENT' => $args);
else
$this->_locals = array();
}
function _munge_input($template) {
// Convert < ?plugin expr ? > to < ?php $this->_printPluginPI("expr"); ? >
$orig[] = '/<\?plugin.*?\?>/se';
$repl[] = "\$this->_mungePlugin('\\0')";
// Convert < ?= expr ? > to < ?php $this->_print(expr); ? >
$orig[] = '/<\?=(.*?)\?>/s';
$repl[] = '<?php $this->_print(\1);?>';
// Convert < ?php echo expr ? > to < ?php $this->_print(expr); ? >
$orig[] = '/<\?php echo (.*?)\?>/s';
$repl[] = '<?php $this->_print(\1);?>';
return preg_replace($orig, $repl, $template);
}
function _mungePlugin($pi) {
// HACK ALERT: PHP's preg_replace, with the /e option seems to
// escape both single and double quotes with backslashes.
// So we need to unescape the double quotes here...
$pi = preg_replace('/(?!<\\\\)\\\\"/x', '"', $pi);
return sprintf('<?php $this->_printPlugin(%s); ?>',
"'" . str_replace("'", "\'", $pi) . "'");
}
function _printPlugin ($pi) {
include_once("lib/WikiPlugin.php");
static $loader;
if (empty($loader))
$loader = new WikiPluginLoader;
$this->_print($loader->expandPI($pi, $this->_request, $this, $this->_basepage));
}
function _print ($val) {
if (isa($val, 'Template')) {
$this->_expandSubtemplate($val);
} else {
PrintXML($val);
}
}
function _expandSubtemplate (&$template) {
// FIXME: big hack!
//if (!$template->_request)
// $template->_request = &$this->_request;
if (DEBUG) {
echo "<!-- Begin $template->_name -->\n";
}
// Expand sub-template with defaults from this template.
$template->printExpansion($this->_vars);
if (DEBUG) {
echo "<!-- End $template->_name -->\n";
}
}
/**
* Substitute HTML replacement text for tokens in template.
*
* Constructs a new WikiTemplate based upon the named template.
*
* @access public
*
* @param $token string Name of token to substitute for.
*
* @param $replacement string Replacement HTML text.
*/
function replace($varname, $value) {
$this->_locals[$varname] = $value;
}
function printExpansion ($defaults = false) {
if (!is_array($defaults)) // HTML object or template object
$defaults = array('CONTENT' => $defaults);
$this->_vars = array_merge($defaults, $this->_locals);
extract($this->_vars);
global $request;
if (!isset($user))
$user = $request->getUser();
if (!isset($page))
$page = $request->getPage();
// Speedup. I checked all templates
if (!isset($revision))
$revision = false;
global $WikiTheme, $charset;
//$this->_dump_template();
$SEP = $WikiTheme->getButtonSeparator();
global $ErrorManager;
$ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_errorHandler'));
eval('?>' . $this->_munge_input($this->_tmpl));
$ErrorManager->popErrorHandler();
}
// FIXME (1.3.12)
// Find a way to do template expansion less memory intensive and faster.
// 1.3.4 needed no memory at all for dumphtml, now it needs +15MB.
// Smarty? As before?
function getExpansion ($defaults = false) {
ob_start();
$this->printExpansion($defaults);
$xml = ob_get_contents();
ob_end_clean(); // PHP problem: Doesn't release its memory?
return $xml;
}
function printXML () {
$this->printExpansion();
}
function asXML () {
return $this->getExpansion();
}
// Debugging:
function _dump_template () {
$lines = explode("\n", $this->_munge_input($this->_tmpl));
$pre = HTML::pre();
$n = 1;
foreach ($lines as $line)
$pre->pushContent(fmt("%4d %s\n", $n++, $line));
$pre->printXML();
}
function _errorHandler($error) {
//if (!preg_match('/: eval\(\)\'d code$/', $error->errfile))
// return false;
if (preg_match('/: eval\(\)\'d code$/', $error->errfile)) {
$error->errfile = "In template '$this->_name'";
// Hack alert: Ignore 'undefined variable' messages for variables
// whose names are ALL_CAPS.
if (preg_match('/Undefined variable:\s*[_A-Z]+\s*$/', $error->errstr))
return true;
}
// ignore recursively nested htmldump loop: browse -> body -> htmldump -> browse -> body ...
// FIXME for other possible loops also
elseif (strstr($error->errfile, "In template 'htmldump'")) {
; //return $error;
}
elseif (strstr($error->errfile, "In template '")) { // merge
$error->errfile = preg_replace("/'(\w+)'\)$/", "'\\1' < '$this->_name')",
$error->errfile);
}
else {
$error->errfile .= " (In template '$this->_name')";
}
if (!empty($this->_tmpl)) {
$lines = explode("\n", $this->_tmpl);
if (isset($lines[$error->errline - 1]))
$error->errstr .= ":\n\t" . $lines[$error->errline - 1];
}
return $error;
}
};
/**
* Get a templates
*
* This is a convenience function and is equivalent to:
* <pre>
* new Template(...)
* </pre>
*/
function Template($name, $args = false) {
global $request;
return new Template($name, $request, $args);
}
function alreadyTemplateProcessed($name) {
global $request;
return !empty($request->_TemplatesProcessed[$name]) ? true : false;
}
/**
* Make and expand the top-level template.
*
*
* @param $content mixed html content to put into the page
* @param $title string page title
* @param $page_revision object A WikiDB_PageRevision object or false
* @param $args hash Extract args for top-level template
*
* @return string HTML expansion of template.
*/
function GeneratePage($content, $title, $page_revision = false, $args = false) {
global $request;
if (!is_array($args))
$args = array();
$args['CONTENT'] = $content;
$args['TITLE'] = $title;
$args['revision'] = $page_revision;
if (!isset($args['HEADER']))
$args['HEADER'] = $title;
printXML(new Template('html', $request, $args));
}
/**
* For dumping pages as html to a file.
* Used for action=dumphtml,action=ziphtml,format=pdf,format=xml
*/
function GeneratePageasXML($content, $title, $page_revision = false, $args = false) {
global $request;
if (!is_array($args))
$args = array();
$content->_basepage = $title;
$args['CONTENT'] = $content;
$args['TITLE'] = SplitPagename($title);
$args['revision'] = $page_revision;
if (!isset($args['HEADER']))
$args['HEADER'] = SplitPagename($title);
global $HIDE_TOOLBARS, $NO_BASEHREF, $WikiTheme;
$HIDE_TOOLBARS = true;
if (!$WikiTheme->DUMP_MODE)
$WikiTheme->DUMP_MODE = 'HTML';
// FIXME: unfatal errors and login requirements
$html = asXML(new Template('htmldump', $request, $args));
$HIDE_TOOLBARS = false;
//$WikiTheme->DUMP_MODE = false;
return $html;
}
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>