-
Notifications
You must be signed in to change notification settings - Fork 19
/
template.class.php
2338 lines (2108 loc) · 88.8 KB
/
template.class.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
// +--------------------------------------------------------------------------+
// | Geeklog 2.0 |
// +--------------------------------------------------------------------------+
// | template.class.php |
// | |
// | Geeklog template library. |
// +--------------------------------------------------------------------------+
// | Copyright (C) 2007-2013 by the following authors: |
// | |
// | Authors: Tony Bibbs - tony AT tonybibbs DOT com |
// | Dirk Haun - dirk AT haun-online DOT de |
// | Oliver Spiesshofer - oliver AT spiesshofer DOT com |
// | Blaine Lang - blaine AT portalparts DOT com |
// | Kenji ITO - geeklog AT mystral-kk DOT net |
// | dengen - taharaxp AT gmail DOT com |
// | Tom Homer - websitemaster AT cogeco DOT net |
// | Joe Mucchiello - joe AT throwingdice DOT com |
// | Mark R. Evans - mark AT glfusion DOT org |
// | |
// | Based on phpLib Template Library |
// | (C) Copyright 1999-2000 NetUSE GmbH |
// | Kristian Koehntopp |
// | Bug fixes to version 7.2c compiled by |
// | Richard Archer <rha@juggernaut.com.au>: |
// | (credits given to first person to post a diff to phplib mailing list) |
// +--------------------------------------------------------------------------+
// | |
// | This program is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License |
// | as published by the Free Software Foundation; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software Foundation, |
// | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +--------------------------------------------------------------------------+
/**
* The template class allows you to keep your HTML code in some external files
* which are completely free of PHP code, but contain replacement fields.
* The class provides you with functions which can fill in the replacement fields
* with arbitrary strings. These strings can become very large, e.g. entire tables.
*
* Note: If you think that this is like FastTemplates, read carefully. It isn't.
*/
/* This should be the only Geeklog-isms in the file. Didn't want to "infect" the class but it was necessary.
* These options are global to all templates.
*/
class Template
{
/**
* Serialization helper, the name of this class.
*
* @var string
*/
public $classname = 'Template';
/**
* Determines how much debugging output Template will produce.
* This is a bitwise mask of available debug levels:
* 0 = no debugging
* 1 = debug variable assignments
* 2 = debug calls to get variable
* 4 = debug internals (outputs all function calls with parameters).
* 8 = debug caching (incomplete)
*
* Note: setting $this->debug = true will enable debugging of variable
* assignments only which is the same behaviour as versions up to release 7.2d.
*
* @var int
*/
public $debug = 0;
/**
* The base directory array from which template files are loaded. When
* attempting to open a file, the paths in this array are searched one at
* a time. As soon as a file exists, the array search stops.
*
* @var array|string
* @see set_root
*/
private $root = array();
/**
* A hash of strings forming a translation table which translates variable names
* into names of files containing the variable content.
* $file[$varName] = "filename";
*
* @var array/string
* @see set_file
*/
private $file = array();
/**
* A hash of strings forming a translation table which translates variable names
* into names of files containing the variable content.
* $location[$varName] = "full path to template";
*
* @var array
* @see set_file
*/
private $location = array();
/**
* Tells template class if dealing with a template file or a view passed instead.
* If view no file access needed and no caching allowed. Default assumed always is no view.
* $view[$varName] = true;
*
* @var array/boolean
* @see set_view
*/
private $view = array();
/**
* The in memory template
*
* @var array
* @see set_file
*/
private $templateCode = array();
/**
* A hash of strings forming a translation table which translates variable names
* into names of files containing the variable content.
* $file[$varName] = "filename";
*
* @var array
* @see cache_blocks,set_block
*/
private $blocks = array();
/**
* A hash of strings forming a translation table which translates variable names
* into the parent name of the variable.
*
* @var array
* @see cache_blocks,set_block, block_echo
*/
private $block_replace = array();
/**
* A hash of strings forming a translation table which translates variable names
* into regular expressions for themselves.
* $varKeys[$varName] = "/varName/"
*
* @var array
* @see set_var
*/
private $varKeys = array();
/**
* A hash of strings forming a translation table which translates variable names
* into values for their respective varKeys.
* $varVals[$varName] = "value"
*
* @var array
* @access private
* @see set_var
*/
private $varVals = array();
/**
* A hash of vars that are not to be translated when create_instance() is called.
* $nocache[varName] = true
*
* @var array
* @see create_instance, val_echo, mod_echo
*/
private $nocache = array();
/**
* Determines how to output variable tags with no assigned value in templates.
*
* @var string
* @see set_unknowns
*/
private $unknowns = 'remove';
/**
* Determines how Template handles error conditions.
* "yes" = the error is reported, then execution is halted
* "report" = the error is reported, then execution continues by returning "false"
* "no" = errors are silently ignored, and execution resumes reporting "false"
* "log" = writes errors to Error log and returns false.
*
* @var string
* @see halt
*/
public $halt_on_error = 'yes';
/**
* The last error message is retained in this variable.
*
* @var string
* @see halt
*/
public $last_error = '';
/**
* The name of a function is retained in this variable and is used to do any pre processing work.
*
* @var string
* @see _preprocess
*/
public $preprocess_fn = '';
/**
* The name of a function is retained in this variable and is used to do any post processing work.
*
* @var string
* @see _postprocess
*/
public $postprocess_fn = '';
/**
* Pre Process
*
* Perform any post processing work by calling the function held in $preprocess_fn
*
* @param string $str
* @return string
*/
private function _preprocess($str)
{
$function = $this->preprocess_fn;
if (function_exists($function)) {
$str = $function($str);
}
return $str;
}
/**
* Post Process
*
* Perform any post processing work by calling the function held in $postprocess_fn
*
* @param string $str
* @return string
*/
private function _postprocess($str)
{
$function = $this->postprocess_fn;
if (function_exists($function)) {
$str = $function($str);
}
return $str;
}
/******************************************************************************
* Class constructor. May be called with two optional parameters.
* The first parameter sets the template directory the second parameter
* sets the policy regarding handling of unknown variables.
*
* usage: Template([string $root = array()], [string $unknowns = "remove"])
*
* @param array|string $root path to template directory
* @param string $unknowns what to do with undefined variables
* @see set_root
* @see set_unknowns
*/
public function __construct($root = array('.'), $unknowns = 'remove')
{
global $_CONF, $TEMPLATE_OPTIONS;
// Set $TEMPLATE_OPTIONS if Template class is called during tests
if (empty($TEMPLATE_OPTIONS) || !is_array($TEMPLATE_OPTIONS)) {
$TEMPLATE_OPTIONS = array(
'path_cache' => $_CONF['path_data'] . 'layout_cache/', // location of template cache
'path_prefixes' => array( // used to strip directories off file names. Order is important here.
$_CONF['path_themes'], // this is not path_layout. When stripping directories, you want files in different themes to end up in different directories.
$_CONF['path'],
'/' // this entry must always exist and must always be last
),
'incl_phpself_header' => true, // set this to true if your template cache exists within your web server's docroot.
'cache_by_language' => true, // create cache directories for each language. Takes extra space but moves all $LANG variable text directly into the cached file
'cache_for_mobile' => $_CONF['cache_mobile'], // create cache directories for mobile devices. Non mobile devices uses regular directory. If disabled mobile uses regular cache files. Takes extra space
'default_vars' => array( // list of vars found in all templates.
'xhtml' => (defined('XHTML') ? XHTML : ''),
'site_url' => $_CONF['site_url'],
'site_admin_url' => $_CONF['site_admin_url'],
'layout_url' => $_CONF['layout_url'], // Can be set by lib-common on theme change
'anonymous_user' => true,
'device_mobile' => false,
'front_page' => false
),
'hook' => array(),
);
}
if (array_key_exists('default_vars', $TEMPLATE_OPTIONS) && is_array($TEMPLATE_OPTIONS['default_vars'])) {
foreach ($TEMPLATE_OPTIONS['default_vars'] as $k => $v) {
$this->set_var($k, $v);
}
}
$this->set_root($root);
$this->set_unknowns($unknowns);
if (isset($_CONF['cache_templates']) && ($_CONF['cache_templates'] == true)) {
clearstatcache();
}
// Since GL-2.2.0
if (COM_isEnableDeveloperModeLog('template')) {
$this->halt_on_error = 'log';
}
}
/******************************************************************************
* Checks that $root is a valid directory and if so sets this directory as the
* base directory from which templates are loaded by storing the value in
* $this->root. Relative file names are prepended with the path in $this->root.
*
* Returns true on success, false on error.
*
* usage: set_root(string $root)
*
* @param string|array $root string|array containing new template directory
* @see root
* @return boolean
*/
public function set_root($root)
{
global $TEMPLATE_OPTIONS;
if ($this->debug & 4) {
echo '<p><b>set_root:</b> root = array(' . (count($root) > 0 ? '"' . implode('","', $root) . '"' : '') . ")</p>\n";
}
if (isset($TEMPLATE_OPTIONS['hook']['set_root'])) {
$function = $TEMPLATE_OPTIONS['hook']['set_root'];
if (is_callable($function)) {
$root = call_user_func($function, $root);
}
}
// Make root now array if not already (hook above runs CTL_setTemplateRoot for plugins that do not use COM_newTemplate and CTL_core_templatePath which will be required as of Geeklog 3.0.0
// CTL_setTemplateRoot needs to figure out things based on if the root passed is an array or not
// As of Geeklog 3.0.0 this arracy check should be moved to right after setting global variables in this function
// For more info see COM_newTemplate, CTL_setTemplateRoot, CTL_core_templatePath, CTL_plugin_templatePath
if (!is_array($root)) {
$root = array($root);
}
if ($this->debug & 4) {
echo '<p><b>set_root:</b> root = array(' . (count($root) > 0 ? '"' . implode('","', $root) . '"' : '') . ")</p>\n";
}
$this->root = array();
$missing = array();
foreach ($root as $r) {
if (substr($r, -1) == '/') {
$r = substr($r, 0, -1);
}
if (!@is_dir($r)) {
$missing[] = $r;
continue;
}
$this->root[] = $r;
}
if ($this->debug & 4) {
echo '<p><b>set_root:</b> root = array(' . (count($root) > 0 ? '"' . implode('","', $root) . '"' : '') . ")</p>\n";
}
if (count($this->root) > 0) {
return true;
}
if (count($missing) > 0) {
$this->halt("set_root: none of these directories exist: " . implode(', ', $missing));
} else {
$this->halt("set_root: at least one existing directory must be set as root.");
}
return false;
}
/**
* Return the root directory of the templates
*
* @return array|string
*/
public function getRoot()
{
return $this->root;
}
/**
* Return unknowns
*
* @return string
*/
public function getUnknowns()
{
return $this->unknowns;
}
/******************************************************************************
* Sets the policy for dealing with unresolved variable names.
*
* unknowns defines what to do with undefined template variables
* "remove" = remove undefined variables
* "comment" = replace undefined variables with comments
* "keep" = keep undefined variables
*
* Note: "comment" can cause unexpected results when the variable tag is embedded
* inside an HTML tag, for example a tag which is expected to be replaced with a URL.
*
* usage: set_unknowns(string $unknowns)
*
* @param string $unknowns new value for unknowns
* @see unknowns
* @return void
*/
public function set_unknowns($unknowns = '')
{
global $TEMPLATE_OPTIONS;
if (isset($TEMPLATE_OPTIONS['force_unknowns'])) {
$unknowns = $TEMPLATE_OPTIONS['force_unknowns'];
} elseif (empty($unknowns)) {
if (isset($TEMPLATE_OPTIONS['unknowns'])) {
$unknowns = $TEMPLATE_OPTIONS['unknowns'];
} else {
$unknowns = 'remove';
}
}
if ($this->debug & 4) {
echo "<p><b>unknowns:</b> unknowns = $unknowns</p>\n";
}
$this->unknowns = $unknowns;
}
/******************************************************************************
* Defines a view for the initial value of a variable. A view is the template
* contents (similarly what a template file contains).
*
* It may be passed either a var name and a view as two strings or
* a hash of strings with the key being the var name and the value
* being the file name.
*
* Since the content is already loaded and passed to the class (from whatever
* source like database or other file, the class does not care) there is no
* mappings. The contents of the view are then complied right away. Views
* cannot be cached since in most cases whatever is requesting the view to be
* complied is probably going to be cached.
*
* Returns true on success, false on error.
*
* usage: set_view(array $views = (string $varName => string $view))
* or
* usage: set_view(string $varName, string $view)
*
* @param string|array $varName either a string containing a var name
* or a hash of var name/view pairs.
* @param string $view if var name is a string this is the view
* otherwise view is not required
* @return bool
*/
public function set_view($varName, $view = '')
{
if (!is_array($varName)) {
if ($this->debug & 4) {
echo "<p><b>set_view:</b> (with scalar) varName = $varName, view = $view</p>\n";
}
if ($view == '') {
$this->halt("set_view: For varName $varName view is empty.");
return false;
}
$templateCode = $this->compile_template($varName, $view, false);
$this->templateCode[$varName] = $templateCode;
$this->location[$varName] = ''; // No location since view
$this->view[$varName] = true;
} else {
foreach ($varName as $var => $v) {
if ($this->debug & 4) {
echo "<p><b>set_view:</b> (with array) varName = $var, view = $v</p>\n";
}
if ($v == '') {
$this->halt("set_view: For varName $var view is empty.");
return false;
}
$v = $this->compile_template($var, $v, false);
$this->templateCode[$var] = $v;
$this->location[$var] = ''; // no location since view
$this->view[$var] = true;
}
}
return true;
}
/******************************************************************************
* Defines a filename for the initial value of a variable.
*
* It may be passed either a var name and a file name as two strings or
* a hash of strings with the key being the var name and the value
* being the file name.
*
* The new mappings are stored in the array $this->file.
* The files are not loaded yet, but only when needed.
*
* Returns true on success, false on error.
*
* usage: set_file(array $fileList = (string $varName => string $filename))
* or
* usage: set_file(string $varName, string $filename)
*
* @param string|array $varName either a string containing a var name
* or a hash of var name/file name pairs.
* @param string $filename if var name is a string this is the filename otherwise filename is not
* required
* @return boolean
*/
public function set_file($varName, $filename = '')
{
global $_CONF;
if (!is_array($varName)) {
if ($this->debug & 4) {
echo "<p><b>set_file:</b> (with scalar) varName = $varName, filename = $filename</p>\n";
}
if ($filename == "") {
$this->halt("set_file: For varName $varName filename is empty.");
return false;
}
$tFilename = $this->filename($filename);
if (isset($_CONF['cache_templates']) && ($_CONF['cache_templates'] == true)) {
$filename = $this->check_cache($varName, $tFilename);
$this->file[$varName] = $filename;
} else {
$templateCode = $this->compile_template($varName, $tFilename);
$this->templateCode[$varName] = $templateCode;
}
$this->location[$varName] = $tFilename;
$this->view[$varName] = false;
} else {
foreach ($varName as $v => $f) {
if ($this->debug & 4) {
echo "<p><b>set_file:</b> (with array) varName = $v, filename = $f</p>\n";
}
if ($f == "") {
$this->halt("set_file: For varName $v filename is empty.");
return false;
}
$tFilename = $this->filename($f);
if (isset($_CONF['cache_templates']) && ($_CONF['cache_templates'] == true)) {
$f = $this->check_cache($v, $tFilename);
$this->file[$v] = $f;
} else {
$f = $this->compile_template($v, $tFilename);
$this->templateCode[$v] = $f;
}
$this->location[$v] = $tFilename;
$this->view[$v] = false;
}
}
return true;
}
/******************************************************************************
* A variable $parent may contain a variable block defined by:
* <!-- BEGIN $varName --> content <!-- END $varName -->. This function removes
* that block from $parent and replaces it with a variable reference named $name.
* The block is inserted into the varKeys and varVals hashes. If $name is
* omitted, it is assumed to be the same as $varName.
*
* Blocks may be nested but care must be taken to extract the blocks in order
* from the innermost block to the outermost block.
*
* Returns true on success, false on error.
*
* usage: set_block(string $parent, string $varName, [string $name = ""])
*
* @param string $parent a string containing the name of the parent variable
* @param string $varName a string containing the name of the block to be extracted
* @param string $name the name of the variable in which to store the block
* @return boolean
*/
public function set_block($parent, $varName, $name = '')
{
global $_CONF;
$this->block_replace[$varName] = !empty($name) ? $name : $parent;
// if (isset($_CONF['cache_templates']) && ($_CONF['cache_templates'] == true)) {
// if (isset($_CONF['cache_templates']) && ($_CONF['cache_templates'] == true) && (!isset($this->view[$varName]) || ($this->view[$varName] == false))) {
// Should use parent here when checking view since assumed parent is the view that was set or not.
// NOTE: This means while blocks can be set in views they CANNOT be nested
// VIEWS with blocks need to be TESTED better as maybe there is a workaround to this
if (isset($_CONF['cache_templates']) && ($_CONF['cache_templates'] == true) && (!isset($this->view[$parent]) || ($this->view[$parent] == false))) {
$filename = $this->file[$parent];
$p = pathinfo($filename);
$this->blocks[$varName] = $p['dirname'] . '/' . substr($p['basename'], 0, -(strlen($p['extension']) + 1)) . '__' . $varName . '.' . $p['extension'];
$this->file[$varName] = $p['dirname'] . '/' . substr($p['basename'], 0, -(strlen($p['extension']) + 1)) . '.' . $p['extension'];
}
return true;
}
/**
* Modifies template location to prevent non-Root users from seeing it
*
* @param string $location
* @return string If the current user is in the Root group, $location is
* unchanged. Otherwise, $location is changed into a path
* relative to $_CONF['path_layout'].
*/
protected function _modifyTemplateLocation($location)
{
global $_CONF;
static $switch = null;
if ($switch === null) {
$switch = ($this->debug > 0) && SEC_inGroup('Root');
}
if (!$switch) {
$location = str_ireplace($_CONF['path_layout'], '', $location);
}
return $location;
}
/******************************************************************************
* This functions sets the value of a variable.
*
* It may be called with either a varName and a value as two strings or an
* an associative array with the key being the varName and the value being
* the new variable value.
*
* The function inserts the new value of the variable into the $varKeys and
* $varVals hashes. It is not necessary for a variable to exist in these hashes
* before calling this function.
*
* An optional third parameter allows the value for each varName to be appended
* to the existing variable instead of replacing it. The default is to replace.
* This feature was introduced after the 7.2d release.
*
* usage: set_var(string $varName, [string $value = ""], [boolean $append = false])
* or
* usage: set_var(array $varName = (string $varName => string $value), [mixed $dummy_var], [boolean $append =
* false])
*
* @param string|array $varName either a string containing a varName or a hash of varName/value pairs.
* @param string $value if $varName is a string this contains the new value
* for the variable otherwise this parameter is ignored
* @param boolean $append if true, the value is appended to the variable's existing value
* @param boolean $nocache if true, the variable is added to the list of variable that are not
* instance cached.
* @return void
*/
public function set_var($varName, $value = '', $append = false, $nocache = false)
{
if (!is_array($varName)) {
if (!empty($varName) || ($varName == 0)) { // Allow varName to be numbers including 0
if ($this->debug & 1) {
printf("<b>set_var:</b> (with scalar) <b>%s</b> = '%s'<br>\n", $varName, htmlentities($value));
}
if ($varName === 'templatelocation') {
$value = $this->_modifyTemplateLocation($value);
}
if ($append && isset($this->varVals[$varName])) {
$this->varVals[$varName] .= $value;
} else {
$this->varVals[$varName] = $value;
}
if ($nocache) {
$this->nocache[$varName] = true;
}
}
} else {
foreach ($varName as $k => $v) {
if (!empty($k) || ($k == 0)) { // Allow varName to be numbers including 0
if ($this->debug & 1) {
printf("<b>set_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $k, htmlentities($v));
}
if ($k === 'templatelocation') {
$v = $this->_modifyTemplateLocation($v);
}
if ($append && isset($this->varVals[$k])) {
$this->varVals[$k] .= $v;
} else {
$this->varVals[$k] = $v;
if (is_array($value)) {
foreach ($value as $k2 => $v2) {
$this->set_var($varName . '[' . $k2 . ']', $v2, $append);
}
}
}
if ($nocache) {
$this->nocache[$k] = true;
}
}
}
}
}
/******************************************************************************
* This functions clears the value of a variable.
*
* It may be called with either a varName as a string or an array with the
* values being the varNames to be cleared.
*
* The function sets the value of the variable in the $varKeys and $varVals
* hashes to "". It is not necessary for a variable to exist in these hashes
* before calling this function.
*
*
* usage: clear_var(string $varName)
* or
* usage: clear_var(array $varName = (string $varName))
*
* @param string|array $varName either a string containing a varName or an array of varNames.
* @return void
*/
public function clear_var($varName)
{
if (!is_array($varName)) {
if (!empty($varName) || ($varName == 0)) { // Allow number variable names including 0
if ($this->debug & 1) {
printf("<b>clear_var:</b> (with scalar) <b>%s</b><br>\n", $varName);
}
$this->set_var($varName, "");
}
} else {
foreach ($varName as $k => $v) {
if (!empty($v) || ($v == 0)) { // Allow number variable names including 0
if ($this->debug & 1) {
printf("<b>clear_var:</b> (with array) <b>%s</b><br>\n", $v);
}
$this->set_var($v, '');
}
}
}
}
/******************************************************************************
* This functions unsets a variable completely.
*
* It may be called with either a varName as a string or an array with the
* values being the varNames to be cleared.
*
* The function removes the variable from the $varKeys and $varVals hashes.
* It is not necessary for a variable to exist in these hashes before calling
* this function.
*
* usage: unset_var(string $varName)
* or
* usage: unset_var(array $varName = (string $varName))
*
* @param string|array $varName either a string containing a varName or an array of varNames.
* @return void
*/
public function unset_var($varName)
{
if (!is_array($varName)) {
if (!empty($varName) || ($varName == 0)) { // Allow number variable names including 0
if ($this->debug & 1) {
printf("<b>unset_var:</b> (with scalar) <b>%s</b><br>\n", $varName);
}
unset($this->varKeys[$varName]);
unset($this->varVals[$varName]);
}
} else {
foreach ($varName as $k => $v) {
if (!empty($v) || ($v == 0)) { // Allow number variable names including 0
if ($this->debug & 1) {
printf("<b>unset_var:</b> (with array) <b>%s</b><br>\n", $v);
}
unset($this->varKeys[$v]);
unset($this->varVals[$v]);
}
}
}
}
/******************************************************************************
* This function fills in all the variables contained within the variable named
* $varName. The resulting value is returned as the function result and the
* original value of the variable varName is not changed. The resulting string
* is not "finished", that is, the unresolved variable name policy has not been
* applied yet.
*
* Returns: the value of the variable $varName with all variables substituted.
*
* usage: subst(string $varName)
*
* @param string $varName the name of the variable within which variables are to be substituted
* @return string
*/
public function subst($varName)
{
global $_CONF;
// If view always bypass cache
if (isset($_CONF['cache_templates']) && ($_CONF['cache_templates'] == true) &&
(!isset($this->view[$varName]) || ($this->view[$varName] == false))) {
if (isset($this->blocks[$varName])) {
$filename = $this->blocks[$varName];
} elseif (isset($this->file[$varName])) {
$filename = $this->file[$varName];
} elseif (isset($this->varVals[$varName]) || empty($varName)) {
return $this->slow_subst($varName);
} else {
// $varName does not reference a file so return
if ($this->debug & 4) {
echo "<p><b>subst:</b> varName $varName does not reference a file</p>\n";
}
return '';
}
if (!is_readable($filename)) {
// file missing
if ($this->debug & 4) {
echo "<p><b>subst:</b> file $filename Does Not Exist or is not readable</p>\n";
}
return '';
}
$p = pathinfo($filename);
if ($p['extension'] === 'php') {
ob_start();
/** @noinspection PhpIncludeInspection */
include $filename;
$str = ob_get_clean();
} else {
$str = $this->slow_subst($varName);
}
return $str;
} else {
if (isset($this->blocks[$varName])) {
$templateCode = $this->blocks[$varName];
} elseif (isset($this->templateCode[$varName])) {
$templateCode = $this->templateCode[$varName];
} elseif (isset($this->varVals[$varName]) || empty($varName)) {
return $this->slow_subst($varName);
} else {
// $varName does not reference a file so return
if ($this->debug & 4) {
echo "<p><b>subst:</b> varName $varName does not reference a file</p>\n";
}
return '';
}
ob_start();
eval('?>' . $templateCode . '<?php ');
$str = ob_get_clean();
return $str;
}
}
/******************************************************************************
* This function fills in all the variables contained within the variable named
* $varName. The resulting value is returned as the function result and the
* original value of the variable varName is not changed. The resulting string
* is not "finished", that is, the unresolved variable name policy has not been
* applied yet.
*
* This is the old version of subst.
*
* Returns: the value of the variable $varName with all variables substituted.
*
* usage: subst(string $varName)
*
* @param string $varName the name of the variable within which variables are to be substituted
* @return string
*/
public function slow_subst($varName)
{
$varVals_quoted = array();
if ($this->debug & 4) {
echo "<p><b>subst:</b> varName = $varName</p>\n";
}
if (count($this->varKeys) < count($this->varVals)) {
foreach ($this->varVals as $k => $v) {
$this->varKeys[$k] = '{' . $k . '}';
}
}
// quote the replacement strings to prevent bogus stripping of special chars
foreach ($this->varVals as $k => $v) {
$varVals_quoted[$k] = str_replace(array('\\\\', '$'), array('\\\\\\\\', '\\\\$'), $v);
}
$str = $this->get_var($varName);
$str = str_replace($this->varKeys, $varVals_quoted, $str);
return $str;
}
/******************************************************************************
* This is shorthand for print $this->subst($varName). See subst for further
* details.
*
* usage: psubst(string $varName)
*
* @param string $varName the name of the variable within which variables are to be substituted
* @return false (always)
* @see subst
*/
public function psubst($varName)
{
if ($this->debug & 4) {
echo "<p><b>psubst:</b> varName = $varName</p>\n";
}
echo $this->subst($varName);
return false;
}
/******************************************************************************
* The function substitutes the values of all defined variables in the variable
* named $varName and stores or appends the result in the variable named $target.
*
* It may be called with either a target and a varName as two strings or a
* target as a string and an array of variable names in varName.
*
* The function inserts the new value of the variable into the $varKeys and
* $varVals hashes. It is not necessary for a variable to exist in these hashes
* before calling this function.
*
* An optional third parameter allows the value for each varName to be appended
* to the existing target variable instead of replacing it. The default is to
* replace.
*
* If $target and $varName are both strings, the substituted value of the
* variable $varName is inserted into or appended to $target.
*
* If $handle is an array of variable names the variables named by $handle are
* sequentially substituted and the result of each substitution step is
* inserted into or appended to in $target. The resulting substitution is
* available in the variable named by $target, as is each intermediate step
* for the next $varName in sequence. Note that while it is possible, it
* is only rarely desirable to call this function with an array of varNames
* and with $append = true. This append feature was introduced after the 7.2d
* release.
*
* usage: parse(string $target, string $varName, [boolean $append])
* or
* usage: parse(string $target, array $varName = (string $varName), [boolean $append])
*
* @param string $target a string containing the name of the variable into
* which substituted $varnames are to be stored
* @param string $varName if a string, the name the name of the variable to
* substitute or if an array a list of variables to
* be substituted
* @param boolean $append if true, the substituted variables are appended to
* $target otherwise the existing value of $target is replaced
* @return string the last value assigned to $target
* @see subst
*/
public function parse($target, $varName, $append = false)
{
$str = '';
if (!is_array($varName)) {
if ($this->debug & 4) {
echo "<p><b>parse:</b> (with scalar) target = $target, varName = $varName, append = $append</p>\n";
}
if (isset($this->location[$varName])) {
$this->set_var('templatelocation', $this->location[$varName]);
}
$str = $this->subst($varName);
if ($append) {
$this->set_var($target, $this->get_var($target) . $str);
} else {