-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsluz.class.php
1054 lines (846 loc) · 28.4 KB
/
sluz.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
////////////////////////////////////////////////////////
define('SLUZ_INLINE', 'INLINE_TEMPLATE'); // Just a specific string
class sluz {
public $version = '0.8.3';
public $tpl_file = null; // The path to the TPL file
public $inc_tpl_file = null; // The path to the {include} file
public $debug = 0; // Enable debug mode
public $in_unit_test = false; // Boolean if we are in unit testing mode
public $use_mo = true; // Use micro- optimiziations
public $tpl_vars = []; // Array of variables assigned to the TPL
public $parent_tpl = null; // Path to parent TPL
private $var_prefix = "sluz_pfx"; // Variable prefix for extract()
private $php_file = null; // Path to the calling PHP file
private $simple_mode = false; // Boolean are we in simple mode
private $fetch_called = false; // Boolean used in simple if fetch has been called
private $char_pos = -1; // Character offset in the TPL
public function __construct() { }
public function __destruct() {
// In simple mode we auto print the output
if ($this->simple_mode && !$this->fetch_called) {
print $this->fetch();
}
}
// Assign variables to pass to the TPL
public function assign($key, $val = null) {
// Single item call (assign array at once)
if (is_null($val) && is_array($key)) {
$this->tpl_vars = array_merge($this->tpl_vars, $key);
} else {
$this->tpl_vars[$key] = $val;
}
}
// Convert template blocks in to output strings
public function process_block(string $str, int $char_pos = -1) {
$ret = '';
$this->char_pos = $char_pos;
// Micro-optimization for "" input!
if (strlen($str) === 0) {
return '';
}
// If it doesn't start with a '{' it's plain text so we just return it
if ($str[0] !== '{') {
$ret = $str;
// Simple variable replacement {$foo} or {$foo|default:"123"}
} elseif (str_starts_with($str, '{$') && preg_match('/^\{\$(\w[\w\|\.\'":,]*)\s*\}$/', $str, $m)) {
$ret = $this->variable_block($m[1]);
// If statement {if $foo}{/if}
} elseif (str_starts_with($str, '{if ') && str_ends_with($str, '{/if}')) {
$ret = $this->if_block($str);
// Foreach {foreach $foo as $x}{/foreach}
} elseif (str_starts_with($str, '{foreach ') && preg_match('/^\{foreach (\$\w[\w.]*) as \$(\w+)( => \$(\w+))?\}(.+)\{\/foreach\}$/s', $str, $m)) {
$ret = $this->foreach_block($m);
// Include {include file='my.stpl' number='99'}
} elseif (str_starts_with($str, '{include ')) {
$ret = $this->include_block($str);
// Liternal {literal}Stuff here{/literal}
} elseif (str_starts_with($str, '{literal}') && preg_match('/^\{literal\}(.+)\{\/literal\}$/s', $str, $m)) {
$ret = $m[1];
// This is for complicated variables with default values that don't match the above rule
} elseif (str_contains($str, "|") && preg_match('/^\{\$(\w.+)\}/', $str, $m)) {
$ret = $this->variable_block($m[1]);
// Catch all for other { $num + 3 } type of blocks
} elseif (preg_match('/^\{(.+)}$/s', $str, $m)) {
$ret = $this->expression_block($str, $m);
// If it starts with '{' (from above) but does NOT contain a closing tag
} elseif (!str_ends_with($str, '}')) {
list($line, $col, $file) = $this->get_char_location($this->char_pos, $this->tpl_file);
return $this->error_out("Unclosed tag <code>$str</code> in <code>$file</code> on line #$line", 45821);
// Something went WAY wrong
} else {
$ret = $str;
}
return $ret;
}
// Break the text up in to tokens/blocks to process by process_block()
public function get_blocks($str) {
$start = 0;
$blocks = [];
$slen = strlen($str);
for ($i = 0; $i < $slen; $i++) {
$char = $str[$i];
$is_open = $char === "{";
$is_closed = $char === "}";
$has_len = $start != $i;
$is_comment = false;
// Check to see if it's a real {} block
if ($is_open) {
$prev_c = $str[$i - 1];
$next_c = $str[$i + 1];
$chunk = $prev_c . $char . $next_c;
// If the { is surrounded by whitespace it's not a block
if (preg_match("/\s[\{\}]\s/", $chunk)) {
$is_open = false;
}
if ($next_c === "*") {
$is_comment = true;
}
}
// if it's a "{" then the block is every from the last $start to here
if ($is_open && $has_len) {
$len = $i - $start;
$block = substr($str, $start, $len);
$blocks[] = [$block, $i];
$start = $i;
// If it's a "}" it's a closing block that starts at $start
} elseif ($is_closed) {
$len = $i - $start + 1;
$block = substr($str, $start, $len);
$is_function = preg_match("/^\{(if|foreach|literal)\b/", $block, $m);
// If we're in a function, loop until we find the closing tag
if ($is_function) {
// Go character by character until we find a '}' and see if we find the closing tag
for ($j = $i + 1; $j < strlen($str); $j++) {
$closed = ($str[$j] === "}");
// If we find a close tag we check to see if it's the final closed tag
if ($closed) {
$len = $j - $start + 1;
$tmp = substr($str, $start, $len);
// Open tag is whatever word is after the '{'
$open_tag = $m[1];
// Build the closing tag so we can look for it later
$close_tag = "{/$open_tag}";
$open_count = substr_count($tmp, '{' . $open_tag);
$close_count = substr_count($tmp, $close_tag);
//k([$open_tag, $close_tag, $open_count, $close_count, $tmp], KRUMO_EXPAND_ALL);
// If this closing bracket is the closing tag we found the pair
if ($open_count === $close_count && (str_ends_with($tmp, $close_tag))) {
$block = $tmp;
break;
}
}
}
}
$blocks[] = [$block, $i];
$start += strlen($block);
$i = $start;
}
// If it's a comment we slurp all the chars until the first '*}' and make that the block
if ($is_comment) {
$end = $this->find_ending_tag(substr($str, $start), '{*', '*}');
if ($end === false) {
list($line, $col, $file) = $this->get_char_location($i, $this->tpl_file);
return $this->error_out("Missing closing <code>*}</code> for comment in <code>$file</code> on line #$line", 48724);
}
$end += 2; // '*}' is 2 long so we add that
$end_rel = $end - $start;
$start += $end;
$i = $start;
}
}
// If we're not at the end of the string, add the last block
if ($start < $slen) {
$blocks[] = [substr($str, $start), $i];
}
return $blocks;
}
// This is just a wrapper function because early versions of Sluz used parse() instead of fetch()
public function parse($tpl_file = "") {
$ret = $this->fetch($tpl_file);
return $ret;
}
// Wrapper function to make us more compatible with Smarty
public function display($tpl_file = "") {
print $this->fetch($tpl_file);
}
// Specify a path to the .stpl file, or pass nothing to let sluz 'guess'
// Guess is 'tpls/[scriptname_minus_dot_php].stpl
public function fetch($tpl_file = "", $parent = null) {
if (!$this->php_file) {
$this->php_file = $this->get_php_file();
}
// We use ABSOLUTE paths here because this may be called in the destructor which has a cwd() of '/'
if (!$tpl_file) {
$tpl_file = dirname($this->php_file) . '/' . $this->guess_tpl_file($this->php_file);
}
$parent_tpl = $parent ?? $this->parent_tpl;
if (!empty($parent_tpl)) {
$this->assign("__CHILD_TPL", $tpl_file);
$tpl_file = $parent_tpl;
}
$str = $this->get_tpl_content($tpl_file);
$blocks = $this->get_blocks($str);
$html = $this->process_blocks($blocks);
$this->fetch_called = true;
return $html;
}
// Guess the TPL filename based on the PHP file
public function guess_tpl_file($php_file) {
$ret = "tpls/" . preg_replace('/\.php$/', '.stpl', basename($php_file));
return $ret;
}
// Find the calling parent PHP file (from the perspective of the sluz class)
public function get_php_file() {
$x = debug_backtrace();
$last = count($x) - 1;
$ret = basename($x[$last]['file'] ?? "");
return $ret;
}
// Turn an array of blocks into output HTML
private function process_blocks(array $blocks) {
$html = '';
foreach ($blocks as $x) {
$block = $x[0];
$char_pos = $x[1];
$html .= $this->process_block($block, $char_pos);
}
return $html;
}
// Load the template file into a string
private function get_tpl_content($tpl_file) {
$tf = $this->tpl_file = $tpl_file;
// If we're in simple mode and we have a __halt_compiler() we can assume inline mode
$inline_simple = $this->simple_mode && $this->get_inline_content($this->php_file);
$is_inline = ($tpl_file === SLUZ_INLINE) || $inline_simple;
if ($is_inline) {
$str = $this->get_inline_content($this->php_file);
} elseif ($tf && !is_readable($tf)) {
return $this->error_out("Unable to load template file <code>$tf</code>",42280);
} elseif ($tf) {
$str = file_get_contents($tf);
}
if (empty($str)) { $str = ""; }
return $str;
}
// Get the text after __halt_compiler()
private function get_inline_content($file) {
$str = file_get_contents($file);
$offset = stripos($str, '__halt_compiler();');
if ($offset === false) {
return null;
}
$ret = substr($str, $offset + 18);
return $ret;
}
// Find the include TPL in the {include} string
function extract_include_file($str) {
// {include file='foo.stpl'}
if (preg_match("/\s(file=)(['\"].+?['\"])/", $str, $m)) {
$xstr = $this->convert_variables_in_string($m[2]);
$ret = $this->peval($xstr);
// {include 'foo.stpl'} - unofficial
} elseif (preg_match("/\s(['\"].+?['\"])/", $str, $m)) {
$xstr = $this->convert_variables_in_string($m[1]);
$ret = $this->peval($xstr);
} else {
list($line, $col, $file) = $this->get_char_location($this->char_pos, $this->tpl_file);
return $this->error_out("Unable to find a file in include block <code>$str</code> in <code>$file</code> on line #$line", 68493);
}
$this->inc_tpl_file = $ret;
return $ret;
}
// Extract data from an array in the form of $foo.key.baz
public function array_dive(string $needle, array $haystack) {
// Do a simple hash lookup first before we dive deep (we may get lucky)
if ($this->use_mo) {
$x = $haystack[$needle] ?? null;
if ($x) {
return $x;
}
}
// Split at the periods
$parts = explode(".", $needle);
// Loop through each level of the hash looking for elem
$arr = $haystack;
foreach ($parts as $elem) {
$arr = $arr[$elem] ?? null;
// If we don't find anything stop looking
if ($arr === null) {
break;
}
}
// If we find a scalar it's the end of the line, anything else is just
// another branch, so it doesn't cound as finding something
if (is_scalar($arr) || is_array($arr)) {
$ret = $arr;
} else {
$ret = null;
}
return $ret;
}
// Convert $cust.name.first -> $cust['name']['first'] and $num.0.3 -> $num[0][3]
private function convert_variables_in_string($str) {
// If there are no dollars signs it's not a variable string, nothing to do
if (!str_contains($str, '$')) {
return $str;
}
// Process flat arrays in the test like $cust.name or $array[3]
$callback = array($this, 'dot_to_bracket_callback');
$str = preg_replace_callback('/(\$\w[\w\.]*)/', $callback, $str);
return $str;
}
// Build an evalable string from a variable string
private function dot_to_bracket_callback($m) {
$str = $m[1];
$parts = explode(".", $str);
$ret = array_shift($parts);
$ret = "$" . $this->var_prefix . '_' . substr($ret,1);
foreach ($parts as $x) {
if (is_numeric($x)) {
$ret .= "[" . $x . "]";
} else {
$ret .= "['" . $x . "']";
}
}
return $ret;
}
// Spit out an error message
public function error_out($msg, int $err_num) {
$style = "
.s_error {
font-family : sans;
color : #842029;
padding : 0.8em;
border-radius: 4px;
margin-bottom: 8px;
background : #f8d7da;
border : 1px solid #f5c2c7;
max-width : 70%;
margin : auto;
min-width : 370px;
}
.s_error_head {
margin-top : 0;
color : white;
text-shadow: 0px 0px 7px gray;
}
.s_error_num { margin-top: 1em; }
.s_error_file {
margin-top : 2em;
padding-top: 0.5em;
font-size : .8em;
border-top : 1px solid gray;
}
.s_error code {
padding : .2rem .4rem;
font-size : 1.1em;
border-radius : .2rem;
background-color: #dad5d5;
color : #1a1a1a;
border : 1px solid #c2c2c2;
}
";
if ($this->in_unit_test) {
return "ERROR-$err_num";
}
$d = debug_backtrace();
$file = $d[0]['file'] ?? "";
$line = $d[0]['line'] ?? 0;
$title = "Sluz error #$err_num";
$body = "<div class=\"s_error\">\n";
$body .= "<h1 class=\"s_error_head\">Sluz Fatal Error #$err_num</h1>";
$body .= "<div class=\"s_error_desc\"><b>Description:</b> $msg</div>";
if ($file && $line) {
$body .= "<div class=\"s_error_file\">Source: <code>$file</code> #$line</div>";
}
$body .= "</div>\n";
$out = "<!doctype html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
<title>$title</title>
<style>$style</style>
</head>
<body>
$body
</body>
</html>";
print $out;
exit;
}
// A bunch of little optimizations and shortcuts
private function micro_optimize($input) {
// Optimize raw integers
if (is_numeric($input)) {
return $input;
}
////////////////////////////////////////////
// Optimize simple $vars
$first_char = $last_char = null;
if (is_string($input)) {
$first_char = $input[0];
$last_char = $input[-1];
// It's not a number or a string?
} else {
return $input;
}
// If it starts with a '$' we might be able to cheat
if ($first_char === '$') {
// Remove the prefix so we can look it up raw
$new = str_replace('$' . $this->var_prefix . '_', '', $input);
$ret = $this->tpl_vars[$new] ?? null;
return $ret;
}
// If it starts with a '!$' we might be able to cheat and invert
if (str_starts_with($input, '!$')) {
// Remove the prefix so we can look it up raw
$new = str_replace('!$' . $this->var_prefix . '_', '', $input);
$ret = $this->tpl_vars[$new] ?? null;
if ($ret !== null) {
return !$ret;
}
}
////////////////////////////////////////////
// Optimize a simple 'string'
if ($first_char === "'" && $last_char === "'") {
$tmp = substr($input,1,strlen($input) - 2);
$is_complex = str_contains($tmp, "'");
if (!$is_complex) {
return $tmp;
}
}
////////////////////////////////////////////
// Optimize a simple "string"
if ($first_char === '"' && $last_char === '"') {
$tmp = substr($input,1,strlen($input) - 2);
$is_complex = str_contains($tmp, '$') || str_contains($tmp, '"');
if (!$is_complex) {
return $tmp;
}
}
return null;
}
// A smart wrapper around eval()
private function peval($str) {
if ($this->use_mo) {
$x = $this->micro_optimize($str);
if ($x) {
return $x;
}
}
extract($this->tpl_vars, EXTR_PREFIX_ALL, $this->var_prefix);
$ret = '';
$cmd = '$ret = (' . $str. ");";
try {
@eval($cmd);
} catch (ParseError $e) {
// Ooops
}
return $ret;
}
// Turn on simple mode
public function enable_simple_mode($php_file) {
$this->php_file = $php_file;
$this->simple_mode = true;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// parse a simple variable
private function variable_block($str) {
// If it has a '|' it's either a function call or 'default'
if (preg_match("/(.+?)\|(.*)/", $str, $m)) {
$key = $m[1];
$mod = $m[2];
$tmp = $this->array_dive($key, $this->tpl_vars) ?? "";
$is_nothing = ($tmp === null || $tmp === "");
$is_default = str_contains($mod, "default:");
// Empty with a default value
if ($is_nothing && $is_default) {
$p = explode("default:", $str, 2);
$dval = $p[1] ?? "";
$ret = $this->peval($dval);
// Non-empty, but has a default value
} elseif (!$is_nothing && $is_default) {
$ret = $this->array_dive($key, $this->tpl_vars) ?? "";
// User function
} else {
$pre = $this->array_dive($key, $this->tpl_vars) ?? "";
// Each modifier is separated by a | but we only split on
// pipes that are NOT in a quoted string. Pattern provided
// by ChatGPT.
$pattern = '/\|(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)/';
$parts = preg_split($pattern, $mod);
// Loop through each modifier (chaining)
foreach ($parts as $mod) {
$x = preg_split("/:/", $mod);
$func = $x[0] ?? "";
$param_str = $x[1] ?? "";
$params = [$pre];
if ($param_str) {
$new = preg_split("/,/", $param_str);
$new = array_map([$this, 'peval'], $new);
$params = array_merge($params, $new);
}
//k([$str, $parts, $func, $new, $params]);
//printf("Calling: %s([%s])<br />\n", $func, join(", ", $params[0]));
if (!is_callable($func)) {
list($line, $col, $file) = $this->get_char_location($this->char_pos, $this->tpl_file);
return $this->error_out("Unknown function call <code>$func</code> in <code>$file</code> on line #$line", 47204);
}
try {
$pre = call_user_func_array($func, $params);
} catch (Exception $e) {
$msg = "Exception: " . $e->getMessage();
$this->error_out($msg, 79134);
} catch (TypeError $e) {
$msg = "TypeError: " . $e->getMessage();
$this->error_out($msg, 58200);
}
}
$ret = $pre;
}
} else {
$ret = $this->array_dive($str, $this->tpl_vars) ?? "";
}
// Array used as a scalar should silently convert to a string
if (is_array($ret)) {
return 'Array';
}
return $ret;
}
// Find the line/column based on char offset in a file
private function get_char_location($pos, $tpl_file) {
// If we're in an {include} the tpl is that temporarily
if ($this->inc_tpl_file) {
$tpl_file = $this->inc_tpl_file;
}
$str = $this->get_tpl_content($tpl_file);
// Error catching...
if ($pos < 0) {
return [-1, -1, $tpl_file];
}
$line = 1;
$col = 0;
for ($i = 0; $i < strlen($str); $i++) {
$col++;
$char = $str[$i];
if ($char === "\n") {
$line++;
$col = 0;
}
if ($pos === $i) {
$ret = [$line, $col, $tpl_file];
return $ret;
}
}
if ($pos === strlen($str)) {
return [$line, $col, $tpl_file];
}
return [-1, -1, $tpl_file];
}
// Parse an if statement
private function if_block($str) {
// If it's a simple {if $name}Output{/if} we can save a lot of
// time parsing detailed rules
if ($this->use_mo) {
// If there is no {else} or {elseif}
$is_simple = (strpos($str, "{else", 7) === false);
} else {
$is_simple = false;
}
if ($is_simple) {
//k($str);
preg_match("/{if (.+?)}(.+){\/if}/s", $str, $m);
$cond = $m[1] ?? "";
$payload = $m[2] ?? "";
$rules[0] = [$cond, $payload];
} else {
$toks = $this->get_tokens($str);
$rules = $this->get_if_rules_from_tokens($toks);
}
// Put the tpl_vars in the current scope so if works against them
extract($this->tpl_vars, EXTR_PREFIX_ALL, $this->var_prefix);
$ret = "";
foreach ($rules as $x) {
$test = $x[0];
$payload = $x[1];
$testp = $this->convert_variables_in_string($test);
if ($this->peval($testp)) {
$blocks = $this->get_blocks($payload);
$ret .= $this->process_blocks($blocks);
// One of the tests was true so we stop processing
break;
}
}
return $ret;
}
// Parse an include block
private function include_block($str) {
// Include blocks may modify tpl vars, so we save them here
$save = $this->tpl_vars;
$inc_tpl = $this->extract_include_file($str);
// Extra variables to include sub templates
if (preg_match_all("/(\w+)=(['\"](.+?)['\"])/", $str, $m)) {
for ($i = 0; $i < count($m[0]); $i++) {
$key = $m[1][$i] ?? "";
$val = $m[2][$i] ?? "";
// We skip the file='header.stpl' option
if ($key === 'file') { continue; }
$val = $this->convert_variables_in_string($val);
$val = $this->peval($val);
$this->assign($key, $val);
}
}
if (!is_file($inc_tpl) || !is_readable($inc_tpl)) {
$this->inc_tpl_file = null; // Clear temp override so this error displays correctly
list($line, $col, $file) = $this->get_char_location($this->char_pos, $this->tpl_file);
return $this->error_out("Unable to load include template <code>$inc_tpl</code> in <code>$file</code> on line #$line", 18485);
}
$str = file_get_contents($inc_tpl);
$blocks = $this->get_blocks($str);
$ret = $this->process_blocks($blocks);
// Restore the TPL vars to pre 'include' state
$this->tpl_vars = $save;
$this->inc_tpl_file = null; // Clear temp override
return $ret;
}
// Parse a foreach block
private function foreach_block($m) {
$src = $this->convert_variables_in_string($m[1]); // src array
$okey = $m[2]; // orig key
$oval = $m[4]; // orig val
$payload = $m[5]; // code block to parse on iteration
$blocks = $this->get_blocks($payload);
$src = $this->peval($src);
// If $src isn't an array we convert it to one so foreach doesn't barf
if (isset($src) && !is_array($src)) {
$src = [$src];
// This prevents an E_WARNING on null (but doesn't output anything)
} elseif (is_null($src)) {
$src = [];
}
// Save the current values so we can restore them later
$save = $this->tpl_vars;
$ret = '';
$idx = 0;
$last = count($src) - 1;
// Temp set a key/val so when we process this section it's correct
foreach ($src as $key => $val) {
// Set if we're on the FIRST iteration
if ($idx === 0) {
$this->tpl_vars['__FOREACH_FIRST'] = true;
} else {
$this->tpl_vars['__FOREACH_FIRST'] = false;
}
// Set if we're on the LAST iteration
if ($idx === $last) {
$this->tpl_vars['__FOREACH_LAST'] = true;
} else {
$this->tpl_vars['__FOREACH_LAST'] = false;
}
$this->tpl_vars['__FOREACH_INDEX'] = $idx;
// This is a key/val pair: foreach $key => $val
if ($oval) {
$this->tpl_vars[$okey] = $key;
$this->tpl_vars[$oval] = $val;
// This is: foreach $array as $item
} else {
$this->tpl_vars[$okey] = $val;
}
$ret .= $this->process_blocks($blocks);
$idx++;
}
// Restore the TPL vars to the version before the {foreach} started
$this->tpl_vars = $save;
return $ret;
}
// Parse a simple expression block
private function expression_block($str, $m) {
$ret = "";
// Make sure the block has something parseble... at least a $ or "
if (!preg_match("/[\"\d$]/", $str)) {
list($line, $col, $file) = $this->get_char_location($this->char_pos, $this->tpl_file);
return $this->error_out("Unknown block type <code>$str</code> in <code>$file</code> on line #$line", 73467);
}
$blk = $m[1] ?? "";
$after = $this->convert_variables_in_string($blk);
$ret = $this->peval($after);
if (!$ret) {
list($line, $col, $file) = $this->get_char_location($this->char_pos, $this->tpl_file);
return $this->error_out("Unknown tag <code>$str</code> in <code>$file</code> on line #$line", 18933);
}
return $ret;
}
// Find the position of the closing tag in a string. This *IS* nesting aware
function find_ending_tag($haystack, $open_tag, $close_tag) {
// Do a quick check up to the FIRST closing tag to see if we find it
$pos = strpos($haystack, $close_tag);
$substr = substr($haystack,0, $pos);
$open_count = substr_count($substr, $open_tag);
if ($open_count === 1) {
return $pos;
}
// This is the FULL search, where we keep adding chunks of the string looking for
// the close tag, and then checking if the open tags match the close tags
// We skip ahead past the first match above because we know there isn't a match in
// the first X characters
$close_len = strlen($close_tag);
$offset = $pos + $close_len;
// We only go five deep... this prevents endless loops
// No one should need more than five levels of nested comments
for ($h = 0; $h < 5; $h++) {
$pos = strpos($haystack, $close_tag, $offset);
if ($pos === false) {
return false;
}
$substr = substr($haystack, 0, $pos + 2);
// If we find the end delimiter and the open/closed tag count is the same
$open_count = substr_count($substr, $open_tag);
$close_count = substr_count($substr, $close_tag);
if ($open_count === $close_count) {
return $pos;
}
$offset = $pos + $close_len;
}
return false;
}
// Break up a string into tokens: pieces of {} and the text between them
function get_tokens($str) {
$x = preg_split('/({[^}]+})/', $str, 0, PREG_SPLIT_DELIM_CAPTURE);
$x = array_filter($x);
$x = array_values($x);
return $x;
}
// Is the string part of an if token
function is_if_token($str) {
if ($str === '{else}') {
return true;
}
if ($str === '{/if}') {
return true;
}
// Return the conditional for this
if (preg_match("/({if|{elseif) (.+?)}/", $str, $m)) {
$ret = trim($m[2] ?? "");
return $ret;
};
return false;
}
// Take an array of tokens and build a list of if/else rules
private function get_if_rules_from_tokens($toks) {
$num = count($toks);
$nested = 0;
// This builds an array of which tokens are pieces of the if
$tmp = [];
for ($i = 0; $i < $num; $i++) {
$item = $toks[$i];
if (str_starts_with($item, '{if')) { $nested++; }
if ($item === '{/if}') { $nested--; }
// If we're in the middle of a nest, it's automatically NOT an if piece
if ($nested !== 1) {
$yes = false;
} else {
$yes = boolval($this->is_if_token($item));
}
// The last {/if} of a nested doesn't count
if ($nested === 1 && $item === '{/if}') {
$yes = false;
}
$tmp[$i] = $yes;
}
$tmp[$num - 1] = true;
////////////////////////////////////////////////////////////////////////
// Now that we know what pieces are the ifs we can pull those out
// because they are the test conditions
$conds = [];
for ($i = 0; $i < $num; $i++) {
$item = $tmp[$i];
if ($item) {
$test = $this->is_if_token($toks[$i]);
$is_last = ($i === ($num - 1));
if (!$is_last) {
$conds[] = $test;
}
}
}
// Last one is the final {/if} and it's always true
$tmp[$num] = true;
////////////////////////////////////////////////////////////////////////
// Everything AFTER an {if} piece is the payload to that test condition
$str = '';
$payloads = [];
$first = true;
for ($i = 0; $i < $num; $i++) {
$item = $tmp[$i];
if (!$item) {
$str .= $toks[$i];
} else {
if (!$first) {
$payloads[] = $str;
}
$first = false;
$str = '';
}
}
$cond_count = count($conds);
$payl_count = count($payloads);
if ($cond_count !== $payl_count) {
$this->error_out("Error parsing {if} conditions in '$str'", 95320);
}
$ret = [];
for ($i = 0; $i < count($conds); $i++) {
$ret[] = [$conds[$i], $payloads[$i]];
}
return $ret;
}
// Get/Set parent tpl
function parent_tpl($tpl) {
if (isset($tpl)) {
$this->parent_tpl = $tpl;