This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvlibTemplate.php
1570 lines (1397 loc) · 46.6 KB
/
vlibTemplate.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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// +------------------------------------------------------------------------+
// | PHP version 5.x, tested with 5.1.4, 5.1.6, 5.2.6 |
// +------------------------------------------------------------------------+
// | Copyright (c) 2002-2008 Kelvin Jones, Claus van Beek, Stefan Deussen |
// +------------------------------------------------------------------------+
// | Authors: Kelvin Jones, Claus van Beek, Stefan Deussen |
// +------------------------------------------------------------------------+
// | Artistic License 2.0 |
// +------------------------------------------------------------------------+
namespace clausvb\vlib;
\mb_internal_encoding('UTF-8');
\mb_regex_encoding('UTF-8');
// check to avoid multiple including of class
if (!defined('vlibTemplateClassLoaded'))
{
define('vlibTemplateClassLoaded', 1);
include_once (dirname(__FILE__) . '/vlibTemplate/error.php');
include_once (dirname(__FILE__) . '/vlibIni.php');
/**
* vlibTemplate is a class used to seperate PHP and HTML.
*
* @since 2002-03-07
* @package vLIB
* @access public
*/
class vlibTemplate
{
/*-----------------------------------------------------------------------------\
| ATTENTION: Do not touch the following variables. vlibTemplate |
| will not work otherwise. |
\-----------------------------------------------------------------------------*/
var $OPTIONS = array(
'MAX_INCLUDES' => 2,
'TEMPLATE_DIR' => null,
'GLOBAL_VARS' => null,
'GLOBAL_CONTEXT_VARS' => null,
'LOOP_CONTEXT_VARS' => null,
'SET_LOOP_VAR' => null,
'DEFAULT_ESCAPE' => null,
'STRICT' => null,
'CASELESS' => null,
'UNKNOWNS' => null,
'TIME_PARSE' => null,
'ENABLE_PHPINCLUDE' => FALSE,
'ENABLE_SHORTTAGS' => null,
'INCLUDE_PATHS' => array(),
'CACHE_DIRECTORY' => null,
'CACHE_LIFETIME' => null,
'CACHE_EXTENSION' => null,
'DEBUG_WITHOUT_JAVASCRIPT' => 0,
);
/** open and close tags used for escaping */
var $ESCAPE_TAGS = array(
'html' => array('open' => 'htmlspecialchars(', 'close'=> ', ENT_QUOTES)'),
'url' => array('open' => 'urlencode(', 'close'=> ')'),
'rawurl'=>array('open' => 'rawurlencode(', 'close'=> ')'),
'sq' => array('open' => 'addcslashes(', 'close'=> ", \"'\")"),
'dq' => array('open' => 'addcslashes(', 'close'=> ", '\"')"),
'1' => array('open' => 'htmlspecialchars(', 'close'=> ', ENT_QUOTES)'),
'0' => array('open' => '', 'close'=> ''),
'none' => array('open' => '', 'close'=> ''),
'hex' => array('open' => '$this->_escape_hex(', 'close'=> ', false)'),
'hexentity' => array('open' => '$this->_escape_hex(', 'close'=> ', true)')
);
/** open and close tags used for formatting */
var $FORMAT_TAGS = array(
'uc' => array('open' => 'strtoupper(', 'close'=> ')'),
'lc' => array('open' => 'strtolower(', 'close'=> ')'),
'ucfirst' => array('open' => 'ucfirst(', 'close'=> ')'),
'lcucfirst' => array('open' => 'ucfirst(strtolower(', 'close'=> '))'),
'ucwords' => array('open' => 'ucwords(', 'close'=> ')'),
'lcucwords' => array('open' => 'ucwords(strtolower(', 'close'=> '))')
);
/** operators allowed when using extended TMPL_IF syntax */
var $allowed_if_ops = array('==','!=','<>','<','>','<=','>=');
/** dbs allowed by vlibTemplate::setDbLoop(). */
var $allowed_loop_dbs = array(
'MYSQL',
'POSTGRESQL',
'INFORMIX',
'INTERBASE',
'INGRES',
'MSSQL',
'MSQL',
'OCI8',
'ORACLE',
'OVRIMOS',
'SYBASE'
);
/** root directory of vlibTemplate automagically filled in */
var $VLIBTEMPLATE_ROOT = null;
/** contains current directory used when doing recursive include */
var $_currentincludedir = array();
/** current depth of includes */
var $_includedepth = 0;
/** full path to tmpl file */
var $_tmplfilename = null;
/** file data before it's parsed */
var $_tmplfile = null;
/** parsed version of file, ready for eval()ing */
var $_tmplfilep = null;
/** eval()ed version ready for printing or whatever */
var $_tmploutput = null;
/** array for variables to be kept */
var $_vars = array();
/** array where loop variables are kept */
var $_arrvars = array();
/** array which holds the current namespace during parse */
var $_namespace = array();
/** variable is set to true once the template is parsed, to save re-parsing everything */
var $_parsed = false;
/** array holds all unknowns vars */
var $_unknowns = array();
/** microtime when template parsing began */
var $_firstparsetime = null;
/** total time taken to parse template */
var $_totalparsetime = null;
/** name of current loop being passed in */
var $_currloopname = null;
/** rows with the above loop */
var $_currloop = array();
/** define vars to avoid warnings */
var $_debug = null;
var $_cache = null;
/*-----------------------------------------------------------------------------\
| public methods |
\-----------------------------------------------------------------------------*/
/**
* METHOD: newTemplate
*
* Usually called by the class constructor.
* Stores the filename in $this->_tmplfilename.
* Raises an error if the template file is not found.
*
* @param string $tmplfile full path to template file
* @return boolean true
* @access public
*/
function newTemplate($tmplfile)
{
if (!$tfile = $this->_fileSearch($tmplfile))
{
vlibTemplateError::raiseError('VT_ERROR_NOFILE', KILL, $tmplfile);
}
// make sure that any parsing vars are cleared for the new template
$this->_tmplfile = null;
$this->_tmplfilep = null;
$this->_tmploutput = null;
$this->_parsed = false;
$this->_unknowns = array();
$this->_firstparsetime = null;
$this->_totalparsetime = null;
// reset debug module
if ($this->_debug)
{
$this->_debugReset();
}
$this->_tmplfilename = $tfile;
return TRUE;
}
/**
* METHOD: setVar
*
* Sets variables to be used by the template
* If $k is an array, then it will treat it as an associative array
* using the keys as variable names and the values as variable values.
*
* @param mixed $k key to define variable name
* @param mixed $v variable to assign to $k
* @return boolean true/false
* @access public
*/
function setVar($k, $v=null)
{
if (is_array($k)) {
foreach($k as $key => $value){
$key = ($this->OPTIONS['CASELESS']) ? strtolower(trim($key)) : trim($key);
if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $key) && $value !== null ) {
$this->_vars[$key] = $value;
}
}
}
else {
if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $k) && $v !== null) {
if ($this->OPTIONS['CASELESS']) $k = strtolower($k);
$this->_vars[trim($k)] = $v;
}
else {
return false;
}
}
return true;
}
/**
* METHOD: unsetVar
*
* Unsets a variable which has already been set
* Parse in all vars wanted for deletion in seperate parametres
*
* @param string var name to remove use: vlibTemplate::unsetVar(var[, var..])
* @return boolean true/false returns true unless called with 0 params
* @access public
*/
function unsetVar () {
$num_args = func_num_args();
if ($num_args < 1) return false;
for ($i = 0; $i < $num_args; $i++) {
$var = func_get_arg($i);
if ($this->OPTIONS['CASELESS']) $var = strtolower($var);
if (!preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $var)) continue;
unset($this->_vars[$var]);
}
return true;
}
/**
* METHOD: getVars
*
* Gets all vars currently set in global namespace.
*
* @return array
* @access public
*/
function getVars () {
if (empty($this->_vars)) return false;
return $this->_vars;
}
/**
* METHOD: getVar
*
* Gets a single var from the global namespace
*
* @return var
* @access public
*/
function getVar ($var) {
if ($this->OPTIONS['CASELESS']) $var = strtolower($var);
if (empty($var) || !isset($this->_vars[$var])) return false;
return $this->_vars[$var];
}
/**
* METHOD: setContextVars
*
* sets the GLOBAL_CONTEXT_VARS
*
* @return true
* @access public
*/
function setContextVars () {
$_phpself = @$GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'];
$_pathinfo = @$GLOBALS['HTTP_SERVER_VARS']['PATH_INFO'];
$_request_uri = @$GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'];
$_qs = @$GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING'];
// the following fixes bug of $PHP_SELF on Win32 CGI and IIS.
$_self = (!empty($_pathinfo)) ? $_pathinfo : $_phpself;
$_uri = (!empty($_request_uri)) ? $_request_uri : $_self.'?'.$_qs;
$this->setvar('__SELF__', $_self);
$this->setvar('__REQUEST_URI__', $_uri);
return true;
}
/**
* METHOD: setLoop
*
* Builds the loop construct for use with <TMPL_LOOP>.
*
* @param string $k string to define loop name
* @param array $v array to assign to $k
* @return boolean true/false
* @access public
*/
function setLoop ($k,$v) {
if (is_array($v) && preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $k)) {
$k = ($this->OPTIONS['CASELESS']) ? strtolower(trim($k)) : trim($k);
$this->_arrvars[$k] = array();
if ($this->OPTIONS['SET_LOOP_VAR'] && !empty($v)) $this->setvar($k, 1);
if (($this->_arrvars[$k] = $this->_arrayBuild($v)) == false) {
vlibTemplateError::raiseError('VT_WARNING_INVALID_ARR', WARNING,$k);
}
}
return true;
}
/**
* METHOD: setDbLoop [** EXPERIMENTAL **]
*
* Function to create a loop from a Db result resource link.
*
* @param string $loopname to commit loop. If not set, will use last loopname set using newLoop()
* @param string $result link to a Db result resource or to a Db result object
* @param string $db_type, type of db that the result resource belongs to.
* @return boolean true/false
* @access public
*/
function setDbLoop ($loopname, $result, $db_type='MYSQL') {
$db_type = strtoupper($db_type);
if (!in_array($db_type, $this->allowed_loop_dbs)) {
vlibTemplateError::raiseError('VT_WARNING_INVALID_LOOP_DB', WARNING, $db_type);
return false;
}
$loop_arr = array();
switch ($db_type) {
case 'MYSQL':
if(is_object($result)) {
if (get_class($result) != 'mysqli_result') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_CLASS', WARNING, $db_type);
return false;
}
while($r = $result->fetch_assoc()) {
$loop_arr[] = $r;
}
}
else {
if (get_resource_type($result) != 'mysql result') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while($r = mysql_fetch_assoc($result)) {
$loop_arr[] = $r;
}
}
break;
case 'POSTGRESQL':
if (get_resource_type($result) != 'pgsql result') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
$nr = (function_exists('pg_num_rows')) ? pg_num_rows($result) : pg_numrows($result);
for ($i=0; $i < $nr; $i++) {
$loop_arr[] = pg_fetch_array($result, $i, PGSQL_ASSOC);
}
break;
case 'INFORMIX':
if (!$result) {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while($r = ifx_fetch_row($result, 'NEXT')) {
$loop_arr[] = $r;
}
break;
case 'INTERBASE':
if (get_resource_type($result) != 'interbase result') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while($r = ibase_fetch_row($result)) {
$loop_arr[] = $r;
}
break;
case 'INGRES':
if (!$result) {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while($r = ingres_fetch_array(INGRES_ASSOC, $result)) {
$loop_arr[] = $r;
}
break;
case 'MSSQL':
if (get_resource_type($result) != 'mssql result') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while($r = mssql_fetch_array($result)) {
$loop_arr[] = $r;
}
break;
case 'MSQL':
if (get_resource_type($result) != 'msql result') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while($r = msql_fetch_array($result, MSQL_ASSOC)) {
$loop_arr[] = $r;
}
break;
case 'OCI8':
if (get_resource_type($result) != 'oci8 statement') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while(OCIFetchInto($result, $r, OCI_ASSOC+OCI_RETURN_LOBS)) {
$loop_arr[] = $r;
}
break;
case 'ORACLE':
if (get_resource_type($result) != 'oracle Cursor') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while(ora_fetch_into($result, $r, ORA_FETCHINTO_ASSOC)) {
$loop_arr[] = $r;
}
break;
case 'OVRIMOS':
if (!$result) {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while(ovrimos_fetch_into($result, $r, 'NEXT')) {
$loop_arr[] = $r;
}
break;
case 'SYBASE':
if (get_resource_type($result) != 'sybase-db result') {
vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
return false;
}
while($r = sybase_fetch_array($result)) {
$loop_arr[] = $r;
}
break;
}
$this->setLoop($loopname, $loop_arr);
return true;
}
/**
* METHOD: newLoop
*
* Sets the name for the curent loop in the 3 step loop process.
*
* @param string $name string to define loop name
* @return boolean true/false
* @access public
*/
function newLoop ($loopname) {
if (preg_match('/^[a-z_]+[a-z0-9_]*$/i', $loopname)) {
$this->_currloopname[$loopname] = $loopname;
$this->_currloop[$loopname] = array();
return true;
}
else {
return false;
}
}
/**
* METHOD: addRow
*
* Adds a row to the current loop in the 3 step loop process.
*
* @param array $row loop row to add to current loop
* @param string $loopname loop to which you want to add row, if not set will use last loop set using newLoop().
* @return boolean true/false
* @access public
*/
function addRow ($row, $loopname=null) {
if (!$loopname) $loopname = end($this->_currloopname);
if (!isset($this->_currloop[$loopname]) || empty($this->_currloopname)) {
vlibTemplateError::raiseError('VT_WARNING_LOOP_NOT_SET', WARNING);
return false;
}
if (is_array($row)) {
$this->_currloop[$loopname][] = $row;
return true;
}
else {
return false;
}
}
/**
* METHOD: addLoop
*
* Completes the 3 step loop process. This assigns the rows and resets
* the variables used.
*
* @param string $loopname to commit loop. If not set, will use last loopname set using newLoop()
* @return boolean true/false
* @access public
*/
function addLoop ($loopname=null) {
if ($loopname == null) { // add last loop used
if (!empty($this->_currloop)) {
foreach ($this->_currloop as $k => $v) {
$this->setLoop($k, $v);
unset($this->_currloop[$k]);
}
$this->_currloopname = array();
return true;
}
else {
return false;
}
}
elseif (!isset($this->_currloop[$loopname]) || empty($this->_currloopname)) { // newLoop not yet envoked
vlibTemplateError::raiseError('VT_WARNING_LOOP_NOT_SET', WARNING);
return false;
}
else { // add a specific loop
$this->setLoop($loopname, $this->_currloop[$loopname]);
unset($this->_currloopname[$loopname], $this->_currloop[$loopname]);
}
return true;
}
/**
* METHOD: getLoop
*
* Use this function to return the loop structure. This is useful in setting
* inner loops using the 3-step-loop process.
*
* @param string $loopname name of loop to get
* @return boolean true/false
* @access public
*/
function getLoop ($loopname=null) {
if (!$loopname) $loopname = end($this->_currloopname);
if (!isset($this->_currloop[$loopname]) || empty($this->_currloopname)) {
vlibTemplateError::raiseError('VT_WARNING_LOOP_NOT_SET', WARNING);
return false;
}
$loop = $this->_currloop[$loopname];
unset($this->_currloopname[$loopname], $this->_currloop[$loopname]);
return $loop;
}
/**
* METHOD: unsetLoop
*
* Unsets a loop which has already been set.
* Can only unset top level loops.
*
* @param string loop to remove use: vlibTemplate::unsetLoop(loop[, loop..])
* @return boolean true/false returns true unless called with 0 params
* @access public
*/
function unsetLoop () {
$num_args = func_num_args();
if ($num_args < 1) return false;
for ($i = 0; $i < $num_args; $i++) {
$var = func_get_arg($i);
if ($this->OPTIONS['CASELESS']) $var = strtolower($var);
if (!preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $var)) continue;
unset($this->_arrvars[$var]);
}
return true;
}
/**
* METHOD: reset
*
* Resets the vlibTemplate object. After using vlibTemplate::reset() you must
* use vlibTemplate::newTemplate(tmpl) to reuse, not passing in the options array.
*
* @return boolean true
* @access public
*/
function reset () {
$this->clearVars();
$this->clearLoops();
$this->_tmplfilename = null;
$this->_tmplfile = null;
$this->_tmplfilep = null;
$this->_tmploutput = null;
$this->_parsed = false;
$this->_unknowns = array();
$this->_firstparsetime = null;
$this->_totalparsetime = null;
$this->_currloopname = null;
$this->_currloop = array();
return true;
}
/**
* METHOD: clearVars
*
* Unsets all variables in the template
*
* @return boolean true
* @access public
*/
function clearVars () {
$this->_vars = array();
return true;
}
/**
* METHOD: clearLoops
*
* Unsets all loops in the template
*
* @return boolean true
* @access public
*/
function clearLoops () {
$this->_arrvars = array();
$this->_currloopname = null;
$this->_currloop = array();
return true;
}
/**
* METHOD: clearAll
*
* Unsets all variables and loops set using setVar/Loop()
*
* @return boolean true
* @access public
*/
function clearAll () {
$this->clearVars();
$this->clearLoops();
return true;
}
/**
* METHOD: unknownsExist
*
* Returns true if unknowns were found after parsing.
* Function MUST be called AFTER one of the parsing functions to have any relevance.
*
* @return boolean true/false
* @access public
*/
function unknownsExist () {
return (!empty($this->_unknowns));
}
/**
* METHOD: unknowns
*
* Alias for unknownsExist.
*
* @access public
*/
function unknowns () {
return $this->unknownsExist();
}
/**
* METHOD: getUnknowns
*
* Returns an array of all unknown vars found when parsing.
* This function is only relevant after parsing a document.
*
* @return array
* @access public
*/
function getUnknowns () {
return $this->_unknowns;
}
/**
* METHOD: setUnknowns
*
* Sets how you want to handle variables that were found in the
* template but not set in vlibTemplate using vlibTemplate::setVar().
*
* @param string $arg ignore, remove, print, leave or comment
* @return boolean
* @access public
*/
function setUnknowns ($arg) {
$arg = strtolower(trim($arg));
if (preg_match('/^ignore|remove|print|leave|comment$/', $arg)) {
$this->OPTIONS['UNKNOWNS'] = $arg;
return true;
}
return false;
}
/**
* METHOD: setPath
*
* function sets the paths to use when including files.
* Use of this function: vlibTemplate::setPath(string path [, string path, ..]);
* i.e. if $tmpl is your template object do: $tmpl->setPath('/web/htdocs/templates','/web/htdocs/www');
* with as many paths as you like.
* if this function is called without any arguments, it will just delete any previously set paths.
*
* @param string path (mulitple)
* @return bool success
* @access public
*/
function setPath () {
$num_args = func_num_args();
if ($num_args < 1) {
$this->OPTIONS['INCLUDE_PATHS'] = array();
return true;
}
for ($i = 0; $i < $num_args; $i++) {
$thispath = func_get_arg($i);
array_push($this->OPTIONS['INCLUDE_PATHS'], realpath($thispath));
}
return true;
}
/**
* METHOD: getParseTime
*
* After using one of the parse functions, this will allow you
* access the time taken to parse the template.
* see OPTION 'TIME_PARSE'.
*
* @return float time taken to parse template
* @access public
*/
function getParseTime () {
if ($this->OPTIONS['TIME_PARSE'] && $this->_parsed) {
return $this->_totalparsetime;
}
return false;
}
/**
* METHOD: fastPrint
*
* Identical to pparse() except that it uses output buffering w/ gz compression thus
* printing the output directly and compressed if poss.
* Will possibly if parsing a huge template.
*
* @access public
* @return boolean true/false
*/
function fastPrint () {
$ret = $this->_parse('ob_gzhandler');
print($this->_tmploutput);
return $ret;
}
/**
* METHOD: pparse
*
* Calls parse, and then prints out $this->_tmploutput
*
* @access public
* @return boolean true/false
*/
function pparse () {
if (!$this->_parsed) $this->_parse();
print($this->_tmploutput);
return true;
}
/**
* METHOD: pprint
*
* Alias for pparse()
*
* @access public
*/
function pprint () {
return $this->pparse();
}
/**
* METHOD: grab
*
* Returns the parsed output, ready for printing, passing to mail() ...etc.
* Invokes $this->_parse() if template has not yet been parsed.
*
* @access public
* @return boolean true/false
*/
function grab () {
if (!$this->_parsed) $this->_parse();
return $this->_tmploutput;
}
/*-----------------------------------------------------------------------------\
| private functions |
\-----------------------------------------------------------------------------*/
/**
* METHOD: PHP5 now "__construct" (PHP4: vlibTemplate)
*
* vlibTemplate constructor.
* if $tmplfile has been passed to it, it will send to $this->newTemplate()
*
* @param string $tmplfile full path to template file
* @param array $options see above
* @return boolean true/false
* @access private
*/
function __construct($tmplfile=null, $options=null)
{
if (is_array($tmplfile) && $options == null)
{
$options = $tmplfile;
unset($tmplfile);
}
$this->VLIBTEMPLATE_ROOT = dirname(realpath(__FILE__));
if (is_array(vlibIni::vlibTemplate()))
{
foreach (vlibIni::vlibTemplate() as $name => $val)
{
$this->OPTIONS[$name] = $val;
}
}
if (is_array($options))
{
foreach($options as $key => $val)
{
$key = strtoupper($key);
if ($key == 'PATH')
{
$this->setPath($val);
}
else
{
$this->_setOption($key, strtolower($val));
}
}
}
if($tmplfile)
{
$this->newTemplate($tmplfile);
}
if ($this->OPTIONS['GLOBAL_CONTEXT_VARS'])
{
$this->setContextVars();
}
return TRUE;
}
/** METHOD: _getData
*
* function returns the text from the file, or if we're using cache, the text
* from the cache file. MUST RETURN DATA.
* @param string tmplfile contains path to template file
* @param do_eval used for included files. If set then this function must do the eval()'ing.
* @access private
* @return mixed data/string or boolean
*/
function _getData ($tmplfile, $do_eval=false) {
// check the current file depth
if ($this->_includedepth > $this->OPTIONS['MAX_INCLUDES'] || $tmplfile == false) {
return;
}
else {
if ($this->_debug) array_push ($this->_debugIncludedfiles, $tmplfile);
if ($do_eval) {
array_push($this->_currentincludedir, dirname($tmplfile));
$this->_includedepth++;
}
}
if($this->_cache && $this->_checkCache($tmplfile)) { // cache exists so lets use it
$data = fread($fp = fopen($this->_cachefile, 'r'), filesize($this->_cachefile));
fclose($fp);
}
else { // no cache lets parse the file
$data = fread($fp = fopen($tmplfile, 'r'), filesize($tmplfile));
fclose($fp);
// xml check disabled see 1 below, but it's now mandatory to turn off short_open_tag in php.ini.
// "<?xml" creates "Parse error!"
// $data = str_replace('<?xml', '<div style="margin-top: 20px; margin-bottom: 20px; font-size: 2.5em;"><strong>vLIB:</strong> Use "setVar()" to use "<?xml" ...</div>', $data);
// check for PHP-Tags
$data = str_replace('<?php', '<div style="margin-top: 40px; margin-bottom: 40px; font-size: 2.5em;"><strong>vLIB:</strong> PHP is not allowed within the template ...</div>', $data);
$data = str_replace('<?=', '<div style="margin-top: 40px; margin-bottom: 40px; font-size: 2.5em;"><strong>vLIB:</strong> PHP is not allowed within the template ...</div>', $data);
// 1 modified to just allow <?x(ml ...)
$data = preg_replace('/<[?][a-wy-zA-Z].*/', '<div style="margin-top: 40px; margin-bottom: 40px; font-size: 2.5em;"><strong>vLIB:</strong> PHP is not allowed within the template ...</div>', $data);
$regex = '/(<|<\/|{|{\/|<!--|<!--\/){1}\s*';
$regex.= '(?:tmpl_)';
if ($this->OPTIONS['ENABLE_SHORTTAGS']) $regex.= '?'; // makes the TMPL_ bit optional
$regex.= '(var|if|elseif|else|endif|unless|endunless|loop|endloop|include|comment|endcomment)\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value|file)';
$regex.= '\s*=\s*';
$regex.= ')?';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value)';
$regex.= '\s*=\s*';
$regex.= ')';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:';
$regex.= '(?:';
$regex.= '(name|format|escape|op|value)';
$regex.= '\s*=\s*';
$regex.= ')';
$regex.= '(?:[\"\'])?';
$regex.= '((?<=[\"\'])';
$regex.= '[^\"\']*|[a-z0-9_\.]*)';
$regex.= '[\"\']?';
$regex.= ')?\s*';
$regex.= '(?:>|\/>|}|-->){1}';
$regex.= '/i';
$self = $this;
$replace_callback = function ($m) use ($self) {
return $self->_parseTag($m);
};
$data = preg_replace_callback($regex, $replace_callback, $data);
// replaced because it's a known to have bad side effects see http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php#reference.pcre.pattern.modifiers.eval
// $regex.= '/ie';
// $data = preg_replace($regex,"\$this->_parseTag(array('\\0','\\1','\\2','\\3','\\4','\\5','\\6','\\7','\\8'));",$data);
if ($this->_cache) { // add cache if need be
$this->_createCache($data);
}
}
// now we must parse the $data and check for any <tmpl_include>'s
if ($this->_debug) $this->doDebugWarnings(file($tmplfile), $tmplfile);
if ($do_eval) {
$success = @eval('?>'.$data.'<?php return 1;');
$this->_includedepth--;
array_pop($this->_currentincludedir);
return $success;
}
else {
return $data;
}
}
/**
* METHOD: _fileSearch