c_SavantSmall.php
13.1 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
/**
*
* Error constants.
*
*/
define('SAVANT2_ERROR_ASSIGN', -1);
define('SAVANT2_ERROR_ASSIGNREF', -2);
define('SAVANT2_ERROR_COMPILER', -3);
define('SAVANT2_ERROR_NOFILTER', -4);
define('SAVANT2_ERROR_NOPLUGIN', -5);
define('SAVANT2_ERROR_NOSCRIPT', -6);
define('SAVANT2_ERROR_NOTEMPLATE', -7);
define('SAVANT2_ERROR_COMPILE_FAIL', -8);
/**
*
* Error messages.
*
*/
if (! isset($GLOBALS['_SAVANT2']['error'])) {
$GLOBALS['_SAVANT2']['error'] = array(
SAVANT2_ERROR_ASSIGN => 'assign() parameters not correct',
SAVANT2_ERROR_ASSIGNREF => 'assignRef() parameters not correct',
SAVANT2_ERROR_COMPILER => 'compiler not an object or has no compile() method',
SAVANT2_ERROR_NOFILTER => 'filter file not found',
SAVANT2_ERROR_NOPLUGIN => 'plugin file not found',
SAVANT2_ERROR_NOSCRIPT => 'compiled template script file not found',
SAVANT2_ERROR_NOTEMPLATE => 'template source file not found',
SAVANT2_ERROR_COMPILE_FAIL => 'template source failed to compile'
);
}
class Savant2_Error {
var $code = null;
var $info = array();
var $text = null;
var $backtrace = null;
function Savant2_Error($conf = array())
{
// set public properties
foreach ($conf as $key => $val) {
$this->$key = $val;
}
// generate a backtrace
if (function_exists('debug_backtrace')) {
$this->backtrace = debug_backtrace();
}
// extended behaviors
$this->_genError();
}
function _genError()
{
}
}
/**
*
* This is totally based on Savent by Paul M. Jones <pmjones@ciaweb.net>.
* But is made much smaller...
*/
class SavantSmall {
var $_error = null;
var $_escape = array('htmlspecialchars');
var $_output = null;
var $_path = array(
'resource' => array(),
'template' => array()
);
var $_restrict = false;
var $_template = null;
// -----------------------------------------------------------------
//
// Constructor and general property setters
//
// -----------------------------------------------------------------
function __construct($conf = array())
{
// set the default template search dirs
if (isset($conf['template_path'])) {
// user-defined dirs
$this->setPath('template', $conf['template_path']);
} else {
// default directory only
$this->setPath('template', null);
}
// set the error class
if (isset($conf['error'])) {
$this->setError($conf['error']);
}
// set the restrict flag
if (isset($conf['restrict'])) {
$this->setRestrict($conf['restrict']);
}
// set the default template
if (isset($conf['template'])) {
$this->setTemplate($conf['template']);
}
// set the output escaping callbacks
if (isset($config['escape'])) {
call_user_func_array(
array($this, 'setEscape'),
(array) $config['escape']
);
}
}
function setError($error)
{
if (! $error) {
$this->_error = null;
} else {
$this->_error = $error;
}
}
function setRestrict($flag = false)
{
if ($flag) {
$this->_restrict = true;
} else {
$this->_restrict = false;
}
}
function setTemplate($template)
{
$this->_template = $template;
}
// -----------------------------------------------------------------
//
// Output escaping and management.
//
// -----------------------------------------------------------------
function setEscape()
{
$this->_escape = func_get_args();
}
function addEscape()
{
$args = func_get_args();
$this->_escape = array_merge($this->_escape, $args);
}
function getEscape()
{
return $this->_escape;
}
function escape($value)
{
// if value is not set return
if (! isset ($value))
return $value;
// were custom callbacks passed?
if (func_num_args() == 1) {
// no, only a value was passed.
// loop through the predefined callbacks.
foreach ($this->_escape as $func) {
$value = call_user_func($func, $value);
}
} else {
// yes, use the custom callbacks instead.
$callbacks = func_get_args();
// drop $value
array_shift($callbacks);
// loop through custom callbacks.
foreach ($callbacks as $func) {
$value = call_user_func($func, $value);
}
}
return $value;
}
function eprint($value)
{
$args = func_get_args();
echo call_user_func_array(
array($this, 'escape'),
$args
);
}
function _($value)
{
$args = func_get_args();
return call_user_func_array(
array($this, 'eprint'),
$args
);
}
// -----------------------------------------------------------------
//
// Path management and file finding
//
// -----------------------------------------------------------------
function setPath($type, $new)
{
// clear out the prior search dirs
$this->_path[$type] = array();
// convert from string to path
if (is_string($new) && ! strpos('://', $new)) {
// the search config is a string, and it's not a stream
// identifier (the "://" piece), add it as a path
// string.
$new = explode(PATH_SEPARATOR, $new);
} else {
// force to array
settype($new, 'array');
}
// always add the fallback directories as last resort
switch (strtolower($type)) {
case 'template':
$this->addPath($type, '.');
break;
case 'resource':
$this->addPath($type, dirname(__FILE__) . '/Savant2/');
break;
}
// actually add the user-specified directories
foreach ($new as $dir) {
$this->addPath($type, $dir);
}
}
function addPath($type, $dir)
{
// no surrounding spaces allowed!
$dir = trim($dir);
// add trailing separators as needed
if (strpos($dir, '://') && substr($dir, -1) != '/') {
// stream
$dir .= '/';
} elseif (substr($dir, -1) != DIRECTORY_SEPARATOR) {
// directory
$dir .= DIRECTORY_SEPARATOR;
}
// add to the top of the search dirs
array_unshift($this->_path[$type], $dir);
}
function getPath($type = null)
{
if (! $type) {
return $this->_path;
} else {
return $this->_path[$type];
}
}
function findFile($type, $file)
{
// get the set of paths
$set = $this->getPath($type);
// start looping through them
foreach ($set as $path) {
// get the path to the file
$fullname = $path . $file;
// are we doing path checks?
if (! $this->_restrict) {
// no. this is faster but less secure.
if (file_exists($fullname) && is_readable($fullname)) {
return $fullname;
}
} else {
// yes. this is slower, but attempts to restrict
// access only to defined paths.
// is the path based on a stream?
if (strpos('://', $path) === false) {
// not a stream, so do a realpath() to avoid
// directory traversal attempts on the local file
// system. Suggested by Ian Eure, initially
// rejected, but then adopted when the secure
// compiler was added.
$path = realpath($path); // needed for substr() later
$fullname = realpath($fullname);
}
// the substr() check added by Ian Eure to make sure
// that the realpath() results in a directory registered
// with Savant so that non-registered directores are not
// accessible via directory traversal attempts.
if (file_exists($fullname) && is_readable($fullname) &&
substr($fullname, 0, strlen($path)) == $path) {
return $fullname;
}
}
}
// could not find the file in the set of paths
return false;
}
// -----------------------------------------------------------------
//
// Variable and reference assignment
//
// -----------------------------------------------------------------
function assign()
{
// this method is overloaded.
$arg = func_get_args();
// must have at least one argument. no error, just do nothing.
if (! isset($arg[0])) {
return;
}
// assign by object
if (is_object($arg[0])) {
// assign public properties
foreach (get_object_vars($arg[0]) as $key => $val) {
if (substr($key, 0, 1) != '_') {
$this->$key = $val;
}
}
return;
}
// assign by associative array
if (is_array($arg[0])) {
foreach ($arg[0] as $key => $val) {
if (substr($key, 0, 1) != '_') {
$this->$key = $val;
}
}
return;
}
// assign by string name and mixed value.
//
// we use array_key_exists() instead of isset() becuase isset()
// fails if the value is set to null.
if (is_string($arg[0]) &&
substr($arg[0], 0, 1) != '_' &&
array_key_exists(1, $arg)) {
$this->$arg[0] = $arg[1];
} else {
return $this->_genError(SAVANT2_ERROR_ASSIGN, $arg);
}
}
function assignRef($key, &$val)
{
if (is_string($key) && substr($key, 0, 1) != '_') {
$this->$key =& $val;
} else {
return $this->_genError(
SAVANT2_ERROR_ASSIGNREF,
array('key' => $key, 'val' => $val)
);
}
}
function clear($var = null)
{
if (is_null($var)) {
// clear all variables
$var = array_keys(get_object_vars($this));
} else {
// clear specific variables
settype($var, 'array');
}
// clear out the selected variables
foreach ($var as $name) {
if (substr($name, 0, 1) != '_' && isset($this->$name)) {
unset($this->$name);
}
}
}
function getVars($key = null)
{
if (is_null($key)) {
$key = array_keys(get_object_vars($this));
}
if (is_array($key)) {
// return a series of vars
$tmp = array();
foreach ($key as $var) {
if (substr($var, 0, 1) != '_' && isset($this->$var)) {
$tmp[$var] = $this->$var;
}
}
return $tmp;
} else {
// return a single var
if (substr($key, 0, 1) != '_' && isset($this->$key)) {
return $this->$key;
}
}
}
// -----------------------------------------------------------------
//
// Template processing
//
// -----------------------------------------------------------------
function loadTemplate($tpl = null, $setScript = false)
{
// set to default template if none specified.
if (is_null($tpl)) {
$tpl = $this->_template;
}
// find the template source.
$file = $this->findFile('template', $tpl);
if (! $file) {
return $this->_genError(
SAVANT2_ERROR_NOTEMPLATE,
array('template' => $tpl)
);
}
// no compiling requested, return the source path
$this->_script = $file;
return $file;
}
function findTemplate($tpl = null)
{
return $this->loadTemplate($tpl, false);
}
function fetch($_tpl = null)
{
// clear prior output
$this->_output = null;
// load the template script
$_result = $this->loadTemplate($_tpl, true);
// is there a template script to be processed?
if ($this->isError($_result)) {
return $_result;
}
// unset so as not to introduce into template scope
unset($_tpl);
unset($_result);
// never allow a 'this' property
if (isset($this->this)) {
unset($this->this);
}
// start capturing output into a buffer
ob_start();
// include the requested template filename in the local scope
// (this will execute the view logic).
include $this->_script;
// done with the requested template; get the buffer and
// clear it.
$this->_output = ob_get_contents();
ob_end_clean();
// done!
return $this->_output;
}
function display($tpl = null)
{
$result = $this->fetch($tpl);
if ($this->isError($result)) {
return $result;
} else {
echo $result;
}
}
// -----------------------------------------------------------------
//
// Error handling
//
// -----------------------------------------------------------------
function &_genError($code, $info = array())
{
// the error config array
$conf = array(
'code' => $code,
'text' => 'Savant2: ',
'info' => (array) $info
);
// set an error message from the globals
if (isset($GLOBALS['_SAVANT2']['error'][$code])) {
$conf['text'] .= $GLOBALS['_SAVANT2']['error'][$code];
} else {
$conf['text'] .= '???';
}
// set up the error class name
if ($this->_error) {
$class = 'Savant2_Error_' . $this->_error;
} else {
$class = 'Savant2_Error';
}
// set up the error class file name
$file = $class . '.php';
// is it loaded?
if (! class_exists($class)) {
// find the error class
$result = $this->findFile('resource', $file);
if (! $result) {
// could not find the custom error class, revert to
// Savant_Error base class.
$class = 'Savant2_Error';
$result = dirname(__FILE__) . '/Savant2/Error.php';
}
// include the error class
include_once $result;
}
// instantiate and return the error class
$err = new $class($conf);
return $err;
}
function isError(&$obj)
{
if (is_object($obj)) {
if ($obj instanceof Savant2_Error ||
is_subclass_of($obj, 'Savant2_Error')) {
return true;
}
}
return false;
}
}
?>