-
Notifications
You must be signed in to change notification settings - Fork 42
/
blitz.c
5810 lines (4985 loc) · 210 KB
/
blitz.c
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
/*
+----------------------------------------------------------------------+
| Blitz Templates |
+----------------------------------------------------------------------+
| Downloads and online documentation: |
| http://alexeyrybak.com/blitz/ |
| http://github.com/alexeyrybak/blitz/ |
| Author: |
| Alexey Rybak [fisher] <alexey.rybak at gmail dot com> |
| Bugfixes and patches: |
| Antony Dovgal [tony2001] <tony at daylessday dot org> |
| Konstantin Baryshnikov [fixxxer] <konstantin at symbi dot info> |
| and many others, please see full list on github |
| Mailing list: |
| blitz-php at googlegroups dot com |
+----------------------------------------------------------------------+
*/
#define BLITZ_DEBUG 0
#define BLITZ_VERSION_STRING "0.9.1"
#ifndef PHP_WIN32
#include <sys/mman.h>
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#if PHP_API_VERSION < 20100412
#include "safe_mode.h"
#endif
#include "php_globals.h"
#include "php_ini.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/info.h"
#include "ext/standard/html.h"
#include "Zend/zend_exceptions.h"
#include <fcntl.h>
#ifdef BLITZ_DEBUG
#include "ext/standard/php_smart_str.h"
#endif
#ifdef PHP_WIN32
#include "win32/time.h"
#else
#include <sys/time.h>
#endif
#if PHP_API_VERSION >= 20041225
#include "ext/date/php_date.h"
#endif
#if PHP_API_VERSION >= 20030307
#define OnUpdateLongLegacy OnUpdateLong
#else
#define OnUpdateLongLegacy OnUpdateInt
#endif
#include "SAPI.h"
#include "php_blitz.h"
ZEND_DECLARE_MODULE_GLOBALS(blitz)
/* some declarations */
static void blitz_error (blitz_tpl *tpl TSRMLS_DC, unsigned int level, char *format, ...);
static int blitz_exec_template(blitz_tpl *tpl, zval *id, char **result, unsigned long *result_len TSRMLS_DC);
static int blitz_exec_nodes(blitz_tpl *tpl, blitz_node *first, zval *id,
char **result, unsigned long *result_len, unsigned long *result_alloc_len,
unsigned long parent_begin, unsigned long parent_end, zval *parent_ctx_data TSRMLS_DC);
static int blitz_exec_nodes_ex(blitz_tpl *tpl, blitz_node *first, zval *id,
char **result, unsigned long *result_len, unsigned long *result_alloc_len,
unsigned long parent_begin, unsigned long parent_end, zval *parent_ctx_data, int push_stack TSRMLS_DC);
static inline int blitz_analize (blitz_tpl *tpl TSRMLS_DC);
static inline void blitz_remove_spaces_around_context_tags(blitz_tpl *tpl TSRMLS_DC);
static inline unsigned int blitz_extract_var(blitz_tpl *tpl, char *name, unsigned long len, unsigned char is_path, zval *params, long *l, zval ***z TSRMLS_DC);
static inline void blitz_check_arg(blitz_tpl *tpl, blitz_node *node, zval *parent_params, int *not_empty TSRMLS_DC);
static inline void blitz_check_expr(blitz_tpl *tpl, blitz_node *node, zval *id, unsigned int node_count, zval *parent_params, int *is_true TSRMLS_DC);
static int le_blitz;
/* internal classes: Blitz */
static zend_class_entry blitz_class_entry;
#ifdef COMPILE_DL_BLITZ
ZEND_GET_MODULE(blitz)
#endif
/* {{{ macros */
#define REALLOC_POS_IF_EXCEEDS \
if (p >= alloc_size) { \
alloc_size = alloc_size << 1; \
*pos = erealloc(*pos, alloc_size * sizeof(tag_pos)); \
}
#define INIT_CALL_ARGS(n) \
(n)->args = ecalloc(BLITZ_CALL_ALLOC_ARG_INIT, sizeof(call_arg)); \
(n)->n_args = 0; \
(n)->n_if_args = 0; \
(n)->n_arg_alloc = BLITZ_CALL_ALLOC_ARG_INIT;
#define REALLOC_ARG_IF_EXCEEDS(n) \
if ((n)->n_args >= (n)->n_arg_alloc) { \
(n)->n_arg_alloc = (n)->n_arg_alloc << 1; \
(n)->args = erealloc((n)->args,(n)->n_arg_alloc*sizeof(call_arg)); \
}
#define ADD_CALL_ARGS(n, buf, i_len, i_type) \
REALLOC_ARG_IF_EXCEEDS(n); \
i_arg = (n)->args + (n)->n_args; \
if (i_len) { \
i_arg->name = estrndup((char *)(buf),(i_len)); \
i_arg->len = (i_len); \
} else { \
i_arg->name = ""; \
i_arg->len = 0; \
} \
i_arg->type = (i_type); \
++(n)->n_args;
#define GET_CALL_ARGS_SIZE(n) \
((n)->n_args)
/* }}} */
static ZEND_INI_MH(OnUpdateVarPrefixHandler) /* {{{ */
{
char *p;
#ifndef ZTS
char *base = (char *) mh_arg2;
#else
char *base;
base = (char *) ts_resource(*((int *) mh_arg2));
#endif
p = (char *) (base+(size_t) mh_arg1);
if (new_value && new_value_length == 0) {
*p = '\x0';
} else {
if (!new_value || new_value_length != 1) {
blitz_error(NULL TSRMLS_CC, E_WARNING, "failed to set blitz.var_prefix (only one character is allowed, like $ or %%)");
return FAILURE;
}
*p = new_value[0];
}
return SUCCESS;
}
/* }}} */
/* {{{ ini options */
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("blitz.throw_exceptions", "0", PHP_INI_ALL,
OnUpdateBool, throw_exceptions, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.var_prefix", BLITZ_TAG_VAR_PREFIX_S, PHP_INI_ALL,
OnUpdateVarPrefixHandler, var_prefix, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.tag_open", BLITZ_TAG_OPEN, PHP_INI_ALL,
OnUpdateString, tag_open, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.tag_close", BLITZ_TAG_CLOSE, PHP_INI_ALL,
OnUpdateString, tag_close, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.tag_open_alt", BLITZ_TAG_OPEN_ALT, PHP_INI_ALL,
OnUpdateString, tag_open_alt, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.tag_close_alt", BLITZ_TAG_CLOSE_ALT, PHP_INI_ALL,
OnUpdateString, tag_close_alt, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.comment_open", BLITZ_TAG_COMMENT_OPEN, PHP_INI_ALL,
OnUpdateString, tag_comment_open, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.comment_close", BLITZ_TAG_COMMENT_CLOSE, PHP_INI_ALL,
OnUpdateString, tag_comment_close, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.enable_alternative_tags", "1", PHP_INI_ALL,
OnUpdateBool, enable_alternative_tags, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.enable_comments", "0", PHP_INI_ALL,
OnUpdateBool, enable_comments, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.path", "", PHP_INI_ALL,
OnUpdateString, path, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.remove_spaces_around_context_tags", "1", PHP_INI_ALL,
OnUpdateBool, remove_spaces_around_context_tags, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.warn_context_duplicates", "0", PHP_INI_ALL,
OnUpdateBool, warn_context_duplicates, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.check_recursion", "1", PHP_INI_ALL,
OnUpdateBool, check_recursion, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.scope_lookup_limit", "0", PHP_INI_ALL,
OnUpdateLongLegacy, scope_lookup_limit, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.lower_case_method_names", "1", PHP_INI_ALL,
OnUpdateBool, lower_case_method_names, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.enable_include", "1", PHP_INI_ALL,
OnUpdateBool, enable_include, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.enable_callbacks", "1", PHP_INI_ALL,
OnUpdateBool, enable_callbacks, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.enable_php_callbacks", "1", PHP_INI_ALL,
OnUpdateBool, enable_php_callbacks, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.php_callbacks_first", "1", PHP_INI_ALL,
OnUpdateBool, php_callbacks_first, zend_blitz_globals, blitz_globals)
STD_PHP_INI_ENTRY("blitz.auto_escape", "0", PHP_INI_ALL,
OnUpdateBool, auto_escape, zend_blitz_globals, blitz_globals)
PHP_INI_END()
/* }}} */
static void dump_call_args(blitz_node *node) {
int i;
php_printf("[D] call args (%u):\n", node->n_args);
call_arg *n_args = node->args;
for (i = 0; i < node->n_args; i++) {
n_args = node->args + i;
php_printf("- %d: name:%s len:%lu type:%s (%d)\n", i, n_args->name,
n_args->len, BLITZ_ARG_TO_STRING(n_args->type), n_args->type);
}
}
static void dump_if_stack(call_arg *stack, int stack_level) {
int i;
char *str;
php_printf("[D] if stack (%u):\n", stack_level + 1);
for (i = 0; i <= stack_level; i++) {
str = estrndup(stack[i].name, stack[i].len);
php_printf("- %d: type:%s (%d)\n", i, str, stack[i].type);
efree(str);
}
}
static void blitz_error (blitz_tpl *tpl TSRMLS_DC, unsigned int level, char *format, ...) { /* {{{ */
char *msg = NULL;
unsigned char free_msg = 0;
va_list arg;
va_start(arg, format);
if (tpl) {
if (tpl->error) {
efree(tpl->error);
}
vspprintf(&tpl->error, BLITZ_ERROR_MAX_LEN, format, arg);
msg = tpl->error;
} else {
vspprintf(&msg, BLITZ_ERROR_MAX_LEN, format, arg);
free_msg = 1;
}
va_end(arg);
php_error_docref(NULL TSRMLS_CC, level, "%s", msg);
if (BLITZ_G(throw_exceptions) && level == E_WARNING) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "%s", msg);
}
if (free_msg) {
efree(msg);
}
}
/* }}} */
static inline unsigned int blitz_realloc_list (blitz_list *list, unsigned long new_size) { /* {{{ */
void *p = NULL;
unsigned long allocated = 0;
if (new_size < list->allocated)
return 1;
allocated = list->allocated << 1;
p = (void*)erealloc(list->first, list->item_size * allocated);
if (!p)
return 0;
list->first = p;
list->allocated = allocated;
list->end = list->first + list->item_size * list->size;
return 1;
}
/* }}} */
static inline unsigned int blitz_realloc_list_to_add (blitz_list *list) { /* {{{ */
if (list->size >= BLITZ_MAX_LIST_SIZE)
return 0;
return blitz_realloc_list(list, list->size + 1);
}
/* }}} */
static inline int blitz_read_with_stream(blitz_tpl *tpl TSRMLS_DC) /* {{{ */
{
char *filename = tpl->static_data.name;
php_stream *stream;
if (php_check_open_basedir(filename TSRMLS_CC)) {
return 0;
}
stream = php_stream_open_wrapper(filename, "rb", IGNORE_PATH|IGNORE_URL|IGNORE_URL_WIN|ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL);
if (!stream) {
return 0;
}
tpl->static_data.body_len = php_stream_copy_to_mem(stream, &tpl->static_data.body, PHP_STREAM_COPY_ALL, 0);
php_stream_close(stream);
return 1;
}
/* }}} */
static inline int blitz_read_with_fread(blitz_tpl *tpl TSRMLS_DC) /* {{{ */
{
FILE *stream;
unsigned int get_len;
char *filename = tpl->static_data.name;
if (php_check_open_basedir(filename TSRMLS_CC)) {
return 0;
}
/* VCWD_FOPEN() fixes problems with relative paths in multithreaded environments */
if (!(stream = VCWD_FOPEN(filename, "rb"))) {
blitz_error(tpl TSRMLS_CC, E_WARNING, "unable to open file \"%s\"", filename);
return 0;
}
tpl->static_data.body = (char*)emalloc(BLITZ_INPUT_BUF_SIZE);
tpl->static_data.body_len = 0;
while ((get_len = fread(tpl->static_data.body + tpl->static_data.body_len, 1, BLITZ_INPUT_BUF_SIZE, stream)) > 0) {
tpl->static_data.body_len += get_len;
tpl->static_data.body = (char*)erealloc(tpl->static_data.body, tpl->static_data.body_len + BLITZ_INPUT_BUF_SIZE);
}
fclose(stream);
return 1;
}
/* }}} */
static blitz_tpl *blitz_init_tpl_base(HashTable *globals, zval *iterations, blitz_tpl *tpl_parent TSRMLS_DC) /* {{{ */
{
blitz_static_data *static_data = NULL;
blitz_tpl *tpl = ecalloc(1, sizeof(blitz_tpl));
static_data = & tpl->static_data;
static_data->body = NULL;
tpl->flags = 0;
static_data->n_nodes = 0;
static_data->nodes = NULL;
static_data->tag_open_len = strlen(BLITZ_G(tag_open));
static_data->tag_close_len = strlen(BLITZ_G(tag_close));
static_data->tag_open_alt_len = strlen(BLITZ_G(tag_open_alt));
static_data->tag_close_alt_len = strlen(BLITZ_G(tag_close_alt));
static_data->tag_comment_open_len = strlen(BLITZ_G(tag_comment_open));
static_data->tag_comment_close_len = strlen(BLITZ_G(tag_comment_close));
tpl->loop_stack_level = 0;
if (iterations) {
/*
"Inherit" iterations from opener, used for incudes. Just make a copy mark template
with special flag BLITZ_FLAG_ITERATION_IS_OTHER not to free this object.
*/
tpl->iterations = iterations;
tpl->flags |= BLITZ_FLAG_ITERATION_IS_OTHER;
} else {
MAKE_STD_ZVAL(tpl->iterations);
array_init(tpl->iterations);
}
if (tpl_parent) {
tpl->tpl_parent = tpl_parent;
} else {
tpl->tpl_parent = NULL;
}
tpl->error = NULL;
tpl->current_iteration = NULL;
tpl->caller_iteration = NULL;
tpl->last_iteration = NULL;
tpl->current_iteration_parent = & tpl->iterations;
tpl->current_path = estrndup("/", sizeof("/") - 1);
tpl->tmp_buf = emalloc(BLITZ_TMP_BUF_MAX_LEN);
static_data->fetch_index = NULL;
/* allocate "personal" hash-table */
if (globals == NULL) {
ALLOC_HASHTABLE(tpl->hash_globals);
zend_hash_init(tpl->hash_globals, 8, NULL, ZVAL_PTR_DTOR, 0);
} else {
/*
"Inherit" globals from opener, used for includes. Just make a copy mark template
with special flag BLITZ_FLAG_GLOBALS_IS_OTHER not to free this object.
*/
tpl->hash_globals = globals;
tpl->flags |= BLITZ_FLAG_GLOBALS_IS_OTHER;
}
ALLOC_HASHTABLE(tpl->ht_tpl_name);
zend_hash_init(tpl->ht_tpl_name, 8, NULL, ZVAL_PTR_DTOR, 0);
tpl->itpl_list = ecalloc(BLITZ_ITPL_ALLOC_INIT, sizeof(blitz_tpl*));
tpl->itpl_list_len = 0;
tpl->itpl_list_alloc = BLITZ_ITPL_ALLOC_INIT;
tpl->scope_stack_pos = 0;
return tpl;
}
/* }}} */
static inline void blitz_free_node(blitz_node *node) /* {{{ */
{
int i = 0;
call_arg *a = NULL;
a = node->args;
for (i = 0; i < node->n_args; i++, a++) {
if (a->len) {
efree(a->name);
}
}
node->n_args = 0;
if (node->args) {
efree(node->args);
node->args = NULL;
}
node->namespace_code = 0;
}
/* }}} */
static void blitz_free_tpl(blitz_tpl *tpl) /* {{{ */
{
int n_nodes = 0, i = 0;
if (!tpl) {
return;
}
n_nodes = tpl->static_data.n_nodes;
for(i = 0 ; i < n_nodes; i++) {
blitz_free_node(&tpl->static_data.nodes[i]);
}
if (tpl->static_data.nodes) {
efree(tpl->static_data.nodes);
}
if (tpl->static_data.body) {
efree(tpl->static_data.body);
}
if (tpl->hash_globals && !(tpl->flags & BLITZ_FLAG_GLOBALS_IS_OTHER)) {
zend_hash_destroy(tpl->hash_globals);
FREE_HASHTABLE(tpl->hash_globals);
}
if (tpl->ht_tpl_name) {
zend_hash_destroy(tpl->ht_tpl_name);
FREE_HASHTABLE(tpl->ht_tpl_name);
}
if (tpl->static_data.fetch_index) {
zend_hash_destroy(tpl->static_data.fetch_index);
FREE_HASHTABLE(tpl->static_data.fetch_index);
}
if (tpl->tmp_buf) {
efree(tpl->tmp_buf);
}
if (tpl->iterations && !(tpl->flags & BLITZ_FLAG_ITERATION_IS_OTHER)) {
zval_dtor(tpl->iterations);
efree(tpl->iterations);
tpl->iterations = NULL;
}
if (tpl->itpl_list) {
for(i=0; i<tpl->itpl_list_len; i++) {
if (tpl->itpl_list[i]) {
blitz_free_tpl(tpl->itpl_list[i]);
}
}
efree(tpl->itpl_list);
}
if (tpl->current_path) {
efree(tpl->current_path);
tpl->current_path = NULL;
}
if (tpl->error) {
efree(tpl->error);
}
efree(tpl);
}
/* }}} */
static blitz_tpl *blitz_init_tpl(const char *filename, int filename_len,
HashTable *globals, zval *iterations, blitz_tpl *tpl_parent TSRMLS_DC) /* {{{ */
{
int global_path_len = 0;
unsigned int filename_normalized_len = 0;
unsigned int add_buffer_len = 0;
int result = 0;
blitz_tpl *tpl = NULL;
blitz_tpl *i_tpl = NULL;
char *s = NULL;
int i = 0, check_loop_max = BLITZ_LOOP_STACK_MAX;
blitz_static_data *static_data;
tpl = blitz_init_tpl_base(globals, iterations, tpl_parent TSRMLS_CC);
if (!tpl) {
return NULL;
}
if (!filename || !filename_len) {
return tpl; /* OK, will be loaded after */
}
filename_normalized_len = filename_len;
#ifndef PHP_WIN32
/* "/dir" starts from root - don't add global path */
if ('/' != filename[0] && BLITZ_G(path)[0] != 0) {
#else
/* "C:\dir" starts from "root" - don't add global path */
if (!(filename_len>2 && filename[1] == ':' && BLITZ_IS_ALPHA_STRICT(filename[0]) && BLITZ_G(path)[0] != 0)) {
#endif
global_path_len = strlen(BLITZ_G(path));
if ((global_path_len + filename_len) > MAXPATHLEN) {
blitz_error(NULL TSRMLS_CC, E_WARNING, "INTERNAL ERROR: file path is too long (limited by MAXPATHLEN)");
blitz_free_tpl(tpl);
return NULL;
}
memcpy(tpl->static_data.name, BLITZ_G(path), global_path_len);
memcpy(tpl->static_data.name + global_path_len, filename, filename_len);
filename_normalized_len = filename_len + global_path_len;
tpl->static_data.name[filename_normalized_len] = '\0';
} else {
// we need realpath to check looped includes
VCWD_REALPATH(filename, tpl->static_data.name);
filename_normalized_len = strlen(tpl->static_data.name);
if (filename_normalized_len == 0) {
blitz_error(NULL TSRMLS_CC, E_WARNING, "unable to open file \"%s\" (realpath failed)", filename);
return NULL;
}
filename_normalized_len = strlen(tpl->static_data.name);
}
/* check loops */
if (BLITZ_G(check_recursion)) {
i_tpl = tpl;
while (i++ < check_loop_max) {
i_tpl = i_tpl->tpl_parent;
if (!i_tpl) break;
s = i_tpl->static_data.name;
if (0 == strncmp(s, tpl->static_data.name, filename_normalized_len)) {
blitz_error(NULL TSRMLS_CC, E_WARNING,
"ERROR: include recursion detected for \"%s\". You can disable this check setting blitz.check_recursion to 0 (please, don't do this if you don't know what you are doing)",
tpl->static_data.name
);
blitz_free_tpl(tpl);
return NULL;
}
}
}
#ifdef BLITZ_USE_STREAMS
result = blitz_read_with_stream(tpl TSRMLS_CC);
#else
result = blitz_read_with_fread(tpl TSRMLS_CC);
#endif
if (!result) {
return tpl;
}
/* search algorithm requires lager buffer: body_len + add_buffer */
static_data = & tpl->static_data;
add_buffer_len = MAX(
MAX(static_data->tag_open_len, static_data->tag_close_len),
MAX(
MAX(static_data->tag_open_alt_len, static_data->tag_close_alt_len),
MAX(static_data->tag_comment_open_len, static_data->tag_comment_close_len)
)
);
tpl->static_data.body = erealloc(tpl->static_data.body, tpl->static_data.body_len + add_buffer_len);
memset(tpl->static_data.body + tpl->static_data.body_len, '\0', add_buffer_len);
return tpl;
}
/* }}} */
static int blitz_load_body(blitz_tpl *tpl, const char *body, int body_len TSRMLS_DC) /* {{{ */
{
unsigned int add_buffer_len = 0;
char *name = "noname_loaded_from_zval";
int name_len = sizeof("noname_loaded_from_zval") - 1;
if (!tpl || !body || !body_len) {
return 0;
}
tpl->static_data.body_len = body_len;
if (tpl->static_data.body_len) {
add_buffer_len = MAX(
MAX(tpl->static_data.tag_open_len, tpl->static_data.tag_close_len),
MAX(
MAX(tpl->static_data.tag_open_alt_len, tpl->static_data.tag_close_alt_len),
MAX(tpl->static_data.tag_comment_open_len, tpl->static_data.tag_comment_close_len)
)
);
tpl->static_data.body = emalloc(tpl->static_data.body_len + add_buffer_len);
memcpy(tpl->static_data.body, body, body_len);
memset(tpl->static_data.body + tpl->static_data.body_len, '\0', add_buffer_len);
}
memcpy(tpl->static_data.name, name, name_len);
return 1;
}
/* }}} */
static int blitz_include_tpl_cached(blitz_tpl *tpl, const char *filename, unsigned int filename_len,
zval *iteration_params, blitz_tpl **itpl TSRMLS_DC) /* {{{ */
{
zval **desc = NULL;
unsigned long itpl_idx = 0;
zval *temp = NULL;
unsigned char has_protected_iteration = 0;
/* try to find already parsed tpl index */
if (SUCCESS == zend_hash_find(tpl->ht_tpl_name, (char *)filename, filename_len, (void **)&desc)) {
*itpl = tpl->itpl_list[Z_LVAL_PP(desc)];
if (iteration_params) {
(*itpl)->iterations = iteration_params;
(*itpl)->flags |= BLITZ_FLAG_ITERATION_IS_OTHER;
} else {
has_protected_iteration = (*itpl)->flags & BLITZ_FLAG_ITERATION_IS_OTHER;
if ((*itpl)->iterations && !has_protected_iteration) {
zend_hash_clean(HASH_OF((*itpl)->iterations));
} else if (!(*itpl)->iterations) {
MAKE_STD_ZVAL((*itpl)->iterations);
array_init((*itpl)->iterations);
(*itpl)->flags ^= BLITZ_FLAG_ITERATION_IS_OTHER;
}
}
return 1;
}
if (filename_len >= MAXPATHLEN) {
blitz_error(NULL TSRMLS_CC, E_WARNING, "Filename exceeds the maximum allowed length of %d characters", MAXPATHLEN);
return 0;
}
/* initialize template */
if (!(*itpl = blitz_init_tpl(filename, filename_len, tpl->hash_globals, iteration_params, tpl TSRMLS_CC))) {
return 0;
}
/* analize template */
if (!blitz_analize(*itpl TSRMLS_CC)) {
blitz_free_tpl(*itpl);
return 0;
}
/* realloc list if needed */
if (tpl->itpl_list_len >= tpl->itpl_list_alloc-1) {
tpl->itpl_list = erealloc(tpl->itpl_list,(tpl->itpl_list_alloc<<1) * sizeof(blitz_tpl*));
tpl->itpl_list_alloc <<= 1;
}
/* save template index values */
itpl_idx = tpl->itpl_list_len;
tpl->itpl_list[itpl_idx] = *itpl;
MAKE_STD_ZVAL(temp);
ZVAL_LONG(temp, itpl_idx);
zend_hash_update(tpl->ht_tpl_name, (char *)filename, filename_len, &temp, sizeof(zval *), NULL);
tpl->itpl_list_len++;
return 1;
}
/* }}} */
static void blitz_resource_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
{
blitz_tpl *tpl = NULL;
tpl = (blitz_tpl*)rsrc->ptr;
blitz_free_tpl(tpl);
}
/* }}} */
static void php_blitz_init_globals(zend_blitz_globals *blitz_globals) /* {{{ */
{
memset(blitz_globals, 0, sizeof(zend_blitz_globals));
blitz_globals->var_prefix = BLITZ_TAG_VAR_PREFIX;
blitz_globals->tag_open = BLITZ_TAG_OPEN;
blitz_globals->tag_close = BLITZ_TAG_CLOSE;
blitz_globals->tag_open_alt = BLITZ_TAG_OPEN_ALT;
blitz_globals->tag_close_alt = BLITZ_TAG_CLOSE_ALT;
blitz_globals->enable_alternative_tags = 1;
blitz_globals->enable_comments = 0;
blitz_globals->enable_include = 1;
blitz_globals->enable_callbacks = 1;
blitz_globals->enable_php_callbacks = 1;
blitz_globals->php_callbacks_first = 1;
blitz_globals->path = "";
blitz_globals->remove_spaces_around_context_tags = 1;
blitz_globals->warn_context_duplicates = 0;
blitz_globals->check_recursion = 1;
blitz_globals->tag_comment_open = BLITZ_TAG_COMMENT_OPEN;
blitz_globals->tag_comment_close = BLITZ_TAG_COMMENT_CLOSE;
blitz_globals->scope_lookup_limit = 0;
blitz_globals->auto_escape = 0;
blitz_globals->throw_exceptions = 0;
}
/* }}} */
static void blitz_get_node_paths(zval *list, blitz_node *node, const char *parent_path, unsigned int skip_vars, unsigned int with_type) /* {{{ */
{
char suffix[2] = "\x0";
char path[BLITZ_CONTEXT_PATH_MAX_LEN] = "\x0";
unsigned int may_have_children = 0;
blitz_node *i_node = NULL;
if (!node)
return;
/* hidden, error or non-finalized node (end was not found) */
if (node->hidden || node->type == BLITZ_NODE_TYPE_BEGIN)
return;
if (node->type == BLITZ_NODE_TYPE_CONTEXT ||
node->type == BLITZ_NODE_TYPE_IF_CONTEXT ||
node->type == BLITZ_NODE_TYPE_UNLESS_CONTEXT ||
node->type == BLITZ_NODE_TYPE_ELSEIF_CONTEXT ||
node->type == BLITZ_NODE_TYPE_ELSE_CONTEXT)
{
may_have_children = 1;
suffix[0] = '/';
if (node->type == BLITZ_NODE_TYPE_CONTEXT) { /* contexts: use context name from args instead of useless "BEGIN" */
sprintf(path, "%s%s%s", parent_path, node->args[0].name, suffix);
} else {
sprintf(path, "%s%s%s", parent_path, node->lexem, suffix);
}
add_next_index_string(list, path, 1);
} else if (!skip_vars) {
sprintf(path, "%s%s%s", parent_path, node->lexem, suffix);
add_next_index_string(list, path, 1);
}
if (*path != '\0' && with_type) {
if (node->type == BLITZ_NODE_TYPE_CONTEXT) {
add_next_index_long(list, BLITZ_NODE_PATH_UNIQUE);
} else {
add_next_index_long(list, BLITZ_NODE_PATH_NON_UNIQUE);
}
}
if (node->first_child) {
i_node = node->first_child;
while (i_node) {
blitz_get_node_paths(list, i_node, path, skip_vars, with_type);
i_node = i_node->next;
}
}
}
/* }}} */
static void blitz_get_path_list(blitz_tpl *tpl, zval *list, unsigned int skip_vars, unsigned int with_type) /* {{{ */
{
unsigned long i = 0;
unsigned int last_close = 0;
char path[BLITZ_CONTEXT_PATH_MAX_LEN] = "/";
for (i = 0; i < tpl->static_data.n_nodes; ++i) {
if (tpl->static_data.nodes[i].pos_begin >= last_close) {
blitz_get_node_paths(list, tpl->static_data.nodes + i, path, skip_vars, with_type);
last_close = tpl->static_data.nodes[i].pos_end;
}
}
return;
}
/* }}} */
static void php_blitz_dump_node(blitz_node *node, unsigned int *p_level) /* {{{ */
{
unsigned long j = 0;
unsigned int level = 0;
char shift_str[] = "==========================================================";
blitz_node *child = NULL;
if (!node) {
return;
}
level = p_level ? *p_level : 0;
if (level >= 10) {
level = 10;
}
memset(shift_str,' ',2 * level + 1);
shift_str[2*level + 1] = '^';
shift_str[2*level + 3] = '\x0';
php_printf("\n%s%s[%u] (%lu(%lu), %lu(%lu)); ",
shift_str,
node->lexem,
(unsigned int)node->type,
node->pos_begin, node->pos_begin_shift,
node->pos_end, node->pos_end_shift
);
if (BLITZ_IS_METHOD(node->type)) {
php_printf("ARGS(%d): ",node->n_args);
for (j = 0; j < node->n_args; ++j) {
if (j) php_printf(",");
php_printf("%s(%d)",node->args[j].name, node->args[j].type);
}
if (node->first_child) {
php_printf("; CHILDREN:");
child = node->first_child;
while (child) {
(*p_level)++;
php_blitz_dump_node(child, p_level);
(*p_level)--;
child = child->next;
}
}
}
}
/* }}} */
static void php_blitz_dump_struct(blitz_tpl *tpl) /* {{{ */
{
unsigned int level = 0;
blitz_node *node = NULL;
php_printf("== TREE STRUCT (%ld nodes):",(unsigned long)tpl->static_data.n_nodes);
node = tpl->static_data.nodes;
while (node) {
php_blitz_dump_node(node, &level);
node = node->next;
}
php_printf("\n");
return;
}
/* }}} */
static void php_blitz_get_node_tokens(blitz_node *node, zval *list) /* {{{ */
{
unsigned long j = 0;
blitz_node *child = NULL;
zval *node_token;
zval *args_list;
zval *child_list;
zval *arg;
if (!node) {
return;
}
ALLOC_INIT_ZVAL(node_token);
array_init(node_token);
add_assoc_string(node_token, "lexem", node->lexem, 1);
add_assoc_long(node_token, "type", (unsigned int)node->type);
add_assoc_long(node_token, "begin", node->pos_begin);
add_assoc_long(node_token, "begin_shift", node->pos_begin_shift);
add_assoc_long(node_token, "end", node->pos_end);
add_assoc_long(node_token, "end_shift", node->pos_end_shift);
if (BLITZ_IS_METHOD(node->type)) {
if( node->n_args > 0 ) {
ALLOC_INIT_ZVAL(args_list);
array_init(args_list);
for (j = 0; j < node->n_args; ++j) {
ALLOC_INIT_ZVAL(arg);
array_init(arg);
add_assoc_stringl(arg, "name", node->args[j].name, node->args[j].len, 1);
add_assoc_long(arg, "type", node->args[j].type);
add_next_index_zval(args_list, arg);
}
add_assoc_zval(node_token, "args", args_list);
}
if (node->first_child) {
child = node->first_child;
ALLOC_INIT_ZVAL(child_list);
array_init(child_list);
while (child) {
php_blitz_get_node_tokens(child, child_list);
child = child->next;
}
add_assoc_zval(node_token, "children", child_list);
}
}
add_next_index_zval(list, node_token);
}
/* }}} */
static void php_blitz_get_tokens(blitz_tpl *tpl, zval *list) /* {{{ */
{
blitz_node *node = NULL;
node = tpl->static_data.nodes;
while (node) {
php_blitz_get_node_tokens(node, list);
node = node->next;
}
}
/* }}} */
void blitz_warn_context_duplicates(blitz_tpl *tpl TSRMLS_DC) /* {{{ */
{
zval *path_list = NULL;
HashTable uk_path;
zval **path = NULL, **path_type = NULL;
int z = 1;
MAKE_STD_ZVAL(path_list);
array_init(path_list);
zend_hash_init(&uk_path, 10, NULL, NULL, 0);
blitz_get_path_list(tpl, path_list, 1, 1);
zend_hash_internal_pointer_reset(HASH_OF(path_list));
while (zend_hash_get_current_data_ex(HASH_OF(path_list), (void**) &path, NULL) == SUCCESS) {
zend_hash_move_forward(HASH_OF(path_list));
zend_hash_get_current_data_ex(HASH_OF(path_list), (void**) &path_type, NULL);
if (Z_LVAL_PP(path_type) == BLITZ_NODE_PATH_NON_UNIQUE) {
zend_hash_move_forward(HASH_OF(path_list));
continue;
}
if (zend_hash_exists(&uk_path, Z_STRVAL_PP(path), Z_STRLEN_PP(path))) {
blitz_error(tpl TSRMLS_CC, E_WARNING,
"WARNING: context name \"%s\" duplicate in %s",
Z_STRVAL_PP(path), tpl->static_data.name
);
} else {
zend_hash_add(&uk_path, Z_STRVAL_PP(path), Z_STRLEN_PP(path), &z, sizeof(int), NULL);
}
zend_hash_move_forward(HASH_OF(path_list));
}
zval_ptr_dtor(&path_list);
zend_hash_destroy(&uk_path);
return;
}
/* }}} */
static int blitz_find_tag_positions(blitz_string *body, blitz_list *list_pos TSRMLS_DC) { /* {{{ */
unsigned char current_tag_pos[BLITZ_TAG_LIST_LEN];
unsigned char idx_tag[BLITZ_TAG_LIST_LEN];
blitz_string list_tag[BLITZ_TAG_LIST_LEN];
unsigned char tag_list_len = BLITZ_TAG_LIST_LEN;
unsigned long pos = 0, pos_check_max = 0;
unsigned char c = 0, p = 0, tag_id = 0, idx_tag_id = 0, found = 0, skip_steps = 0;
unsigned char *pc = NULL;
unsigned int tag_min_len = 0;
unsigned char check_map[256];
blitz_string *t = NULL;
tag_pos *tp = NULL;
list_tag[BLITZ_TAG_ID_OPEN].s = BLITZ_G(tag_open);
list_tag[BLITZ_TAG_ID_CLOSE].s = BLITZ_G(tag_close);
idx_tag[0] = BLITZ_TAG_ID_OPEN;