forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
1384 lines (1100 loc) · 42.6 KB
/
debug.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
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
#include "debug.h"
#include "exception.h"
#include "version.h"
#include <ext/standard/php_string.h>
#include <Zend/zend_builtin_functions.h>
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/object.h"
#include "kernel/fcall.h"
#include "kernel/array.h"
#include "kernel/string.h"
#include "kernel/operators.h"
#include "kernel/hash.h"
#include "kernel/concat.h"
#include "kernel/file.h"
#include "kernel/output.h"
/**
* Phalcon\Debug
*
* Provides debug capabilities to Phalcon applications
*/
zend_class_entry *phalcon_debug_ce;
PHP_METHOD(Phalcon_Debug, setUri);
PHP_METHOD(Phalcon_Debug, setShowBackTrace);
PHP_METHOD(Phalcon_Debug, setShowFiles);
PHP_METHOD(Phalcon_Debug, setShowFileFragment);
PHP_METHOD(Phalcon_Debug, listen);
PHP_METHOD(Phalcon_Debug, listenExceptions);
PHP_METHOD(Phalcon_Debug, listenLowSeverity);
PHP_METHOD(Phalcon_Debug, debugVar);
PHP_METHOD(Phalcon_Debug, clearVars);
PHP_METHOD(Phalcon_Debug, _escapeString);
PHP_METHOD(Phalcon_Debug, _getArrayDump);
PHP_METHOD(Phalcon_Debug, _getVarDump);
PHP_METHOD(Phalcon_Debug, getMajorVersion);
PHP_METHOD(Phalcon_Debug, getVersion);
PHP_METHOD(Phalcon_Debug, getCssSources);
PHP_METHOD(Phalcon_Debug, getJsSources);
PHP_METHOD(Phalcon_Debug, showTraceItem);
PHP_METHOD(Phalcon_Debug, onUncaughtException);
PHP_METHOD(Phalcon_Debug, getCharset);
PHP_METHOD(Phalcon_Debug, setCharset);
PHP_METHOD(Phalcon_Debug, getLinesBeforeContext);
PHP_METHOD(Phalcon_Debug, setLinesBeforeContext);
PHP_METHOD(Phalcon_Debug, getLinesAfterContext);
PHP_METHOD(Phalcon_Debug, setLinesAfterContext);
PHP_METHOD(Phalcon_Debug, getFileLink);
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_seturi, 0, 0, 1)
ZEND_ARG_INFO(0, uri)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_setshowbacktrace, 0, 0, 1)
ZEND_ARG_INFO(0, showBackTrace)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_setshowfiles, 0, 0, 1)
ZEND_ARG_INFO(0, showFiles)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_setshowfilefragment, 0, 0, 1)
ZEND_ARG_INFO(0, showFileFragment)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_listen, 0, 0, 0)
ZEND_ARG_INFO(0, exceptions)
ZEND_ARG_INFO(0, lowSeverity)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_debugvar, 0, 0, 1)
ZEND_ARG_INFO(0, var)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_onuncaughtexception, 0, 0, 1)
ZEND_ARG_INFO(0, exception)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_setcharset, 0, 0, 1)
ZEND_ARG_INFO(0, charset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_setlines, 0, 0, 1)
ZEND_ARG_INFO(0, lines)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_debug_getfilelink, 0, 0, 3)
ZEND_ARG_INFO(0, file)
ZEND_ARG_INFO(0, line)
ZEND_ARG_INFO(0, format)
ZEND_END_ARG_INFO()
static const zend_function_entry phalcon_debug_method_entry[] = {
PHP_ME(Phalcon_Debug, setUri, arginfo_phalcon_debug_seturi, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, setShowBackTrace, arginfo_phalcon_debug_setshowbacktrace, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, setShowFiles, arginfo_phalcon_debug_setshowfiles, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, setShowFileFragment, arginfo_phalcon_debug_setshowfilefragment, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, listen, arginfo_phalcon_debug_listen, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, listenExceptions, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, listenLowSeverity, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, debugVar, arginfo_phalcon_debug_debugvar, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, clearVars, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, _escapeString, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Phalcon_Debug, _getArrayDump, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Phalcon_Debug, _getVarDump, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Phalcon_Debug, getMajorVersion, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, getVersion, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, getCssSources, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, getJsSources, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, showTraceItem, NULL, ZEND_ACC_PROTECTED)
PHP_ME(Phalcon_Debug, onUncaughtException, arginfo_phalcon_debug_onuncaughtexception, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, getCharset, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, setCharset, arginfo_phalcon_debug_setcharset, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, getLinesBeforeContext, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, setLinesBeforeContext, arginfo_phalcon_debug_setlines, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, getLinesAfterContext, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, setLinesAfterContext, arginfo_phalcon_debug_setlines, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Debug, getFileLink, arginfo_phalcon_debug_getfilelink, ZEND_ACC_PROTECTED)
PHP_FE_END
};
/**
* Phalcon\Debug initializer
*/
PHALCON_INIT_CLASS(Phalcon_Debug){
PHALCON_REGISTER_CLASS(Phalcon, Debug, debug, phalcon_debug_method_entry, 0);
zend_declare_property_string(phalcon_debug_ce, SL("_uri"), "//d2yyr506dy8ck0.cloudfront.net/debug/1.2.0/", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(phalcon_debug_ce, SL("_theme"), "default", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_bool(phalcon_debug_ce, SL("_hideDocumentRoot"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_bool(phalcon_debug_ce, SL("_showBackTrace"), 1, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_bool(phalcon_debug_ce, SL("_showFiles"), 1, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_bool(phalcon_debug_ce, SL("_showFileFragment"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_debug_ce, SL("_data"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_debug_ce, SL("_isActive"), ZEND_ACC_PROTECTED|ZEND_ACC_STATIC TSRMLS_CC);
zend_declare_property_string(phalcon_debug_ce, SL("_charset"), "utf-8", ZEND_ACC_PROTECTED|ZEND_ACC_STATIC TSRMLS_CC);
zend_declare_property_long(phalcon_debug_ce, SL("_beforeContext"), 7, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(phalcon_debug_ce, SL("_afterContext"), 5, ZEND_ACC_PROTECTED TSRMLS_CC);
return SUCCESS;
}
/**
* Change the base URI for static resources
*
* @param string $uri
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, setUri){
zval *uri;
phalcon_fetch_params(0, 1, 0, &uri);
phalcon_update_property_this(this_ptr, SL("_uri"), uri TSRMLS_CC);
RETURN_THISW();
}
/**
* Sets if files the exception's backtrace must be showed
*
* @param boolean $showBackTrace
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, setShowBackTrace){
zval *show_back_trace;
phalcon_fetch_params(0, 1, 0, &show_back_trace);
phalcon_update_property_this(this_ptr, SL("_showBackTrace"), show_back_trace TSRMLS_CC);
RETURN_THISW();
}
/**
* Set if files part of the backtrace must be shown in the output
*
* @param boolean $showFiles
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, setShowFiles){
zval *show_files;
phalcon_fetch_params(0, 1, 0, &show_files);
phalcon_update_property_this(this_ptr, SL("_showFiles"), show_files TSRMLS_CC);
RETURN_THISW();
}
/**
* Sets if files must be completely opened and showed in the output
* or just the fragment related to the exception
*
* @param boolean $showFileFragment
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, setShowFileFragment){
zval *show_file_fragment;
phalcon_fetch_params(0, 1, 0, &show_file_fragment);
phalcon_update_property_this(this_ptr, SL("_showFileFragment"), show_file_fragment TSRMLS_CC);
RETURN_THISW();
}
/**
* Listen for uncaught exceptions and unsilent notices or warnings
*
* @param boolean $exceptions
* @param boolean $lowSeverity
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, listen){
zval *exceptions = NULL, *low_severity = NULL;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 0, 2, &exceptions, &low_severity);
if (!exceptions || zend_is_true(exceptions)) {
PHALCON_CALL_METHOD(NULL, this_ptr, "listenexceptions");
}
if (low_severity && zend_is_true(low_severity)) {
PHALCON_CALL_METHOD(NULL, this_ptr, "listenlowseverity");
}
RETURN_THIS();
}
/**
* Listen for uncaught exceptions
*
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, listenExceptions){
zval *handler;
PHALCON_MM_GROW();
PHALCON_INIT_VAR(handler);
array_init_size(handler, 2);
phalcon_array_append(&handler, this_ptr, PH_SEPARATE);
add_next_index_stringl(handler, SL("onUncaughtException"), 1);
PHALCON_CALL_FUNCTION(NULL, "set_exception_handler", handler);
RETURN_THIS();
}
/**
* Listen for unsilent notices or warnings
*
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, listenLowSeverity){
/*
zval *handler;
PHALCON_MM_GROW();
PHALCON_INIT_VAR(handler);
array_init_size(handler, 2);
phalcon_array_append(&handler, this_ptr, PH_SEPARATE);
add_next_index_stringl(handler, SL("onUncaughtLowSeverity"), 1);
PHALCON_CALL_FUNCTION(NULL, "set_exception_handler", handler);
*/
RETURN_THISW();
}
/**
* Adds a variable to the debug output
*
* @param mixed $var
* @param string $key
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, debugVar){
zval *var, *key = NULL, *ztime, *backtrace, *data;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 1, &var, &key);
if (!key) {
key = PHALCON_GLOBAL(z_null);
}
PHALCON_INIT_VAR(ztime);
ZVAL_LONG(ztime, (long) time(NULL));
PHALCON_INIT_VAR(backtrace);
#if PHP_VERSION_ID < 50400
#ifdef DEBUG_BACKTRACE_PROVIDE_OBJECT
zend_fetch_debug_backtrace(backtrace, 0, DEBUG_BACKTRACE_PROVIDE_OBJECT TSRMLS_CC);
#else
zend_fetch_debug_backtrace(backtrace, 0, 1 TSRMLS_CC);
#endif
#else
zend_fetch_debug_backtrace(backtrace, 0, DEBUG_BACKTRACE_PROVIDE_OBJECT, 0 TSRMLS_CC);
#endif
PHALCON_INIT_VAR(data);
array_init_size(data, 3);
phalcon_array_append(&data, var, PH_SEPARATE);
phalcon_array_append(&data, backtrace, PH_SEPARATE);
phalcon_array_append(&data, ztime, PH_SEPARATE);
phalcon_update_property_array_append(this_ptr, SL("_data"), data TSRMLS_CC);
RETURN_THIS();
}
/**
* Clears are variables added previously
*
* @return Phalcon\Debug
*/
PHP_METHOD(Phalcon_Debug, clearVars){
phalcon_update_property_null(this_ptr, SL("_data") TSRMLS_CC);
RETURN_THISW();
}
/**
* Escapes a string with htmlentities
*
* @param string $value
* @return string
*/
PHP_METHOD(Phalcon_Debug, _escapeString){
zval *value, *charset, *replaced_value;
phalcon_fetch_params(0, 1, 0, &value);
if (Z_TYPE_P(value) == IS_STRING) {
zval line_break;
zval escaped_line_break;
charset = phalcon_fetch_nproperty_this(getThis(), SL("_charset"), PH_NOISY TSRMLS_CC);
INIT_ZVAL(line_break);
ZVAL_STRING(&line_break, "\n", 0);
INIT_ZVAL(escaped_line_break);
ZVAL_STRING(&escaped_line_break, "\\n", 0);
ALLOC_INIT_ZVAL(replaced_value);
phalcon_fast_str_replace(replaced_value, &line_break, &escaped_line_break, value);
phalcon_htmlentities(return_value, replaced_value, NULL, charset TSRMLS_CC);
zval_ptr_dtor(&replaced_value);
return;
}
RETURN_ZVAL(value, 1, 0);
}
/**
* Produces a recursive representation of an array
*
* @param array $argument
* @return string
*/
PHP_METHOD(Phalcon_Debug, _getArrayDump){
zval *argument, *n = NULL, *number_arguments, *dump;
zval *v = NULL, *k = NULL, *var_dump = NULL, *escaped_string = NULL, *next = NULL, *array_dump = NULL;
zval *class_name = NULL, *joined_dump;
HashTable *ah0;
HashPosition hp0;
zval **hd;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 1, &argument, &n);
if (!n) {
PHALCON_INIT_VAR(n);
ZVAL_LONG(n, 0);
}
PHALCON_INIT_VAR(number_arguments);
phalcon_fast_count(number_arguments, argument TSRMLS_CC);
if (PHALCON_LT_LONG(n, 3)) {
if (PHALCON_GT_LONG(number_arguments, 0)) {
if (PHALCON_LT_LONG(number_arguments, 10)) {
PHALCON_INIT_VAR(dump);
array_init(dump);
phalcon_is_iterable(argument, &ah0, &hp0, 0, 0);
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
PHALCON_GET_HKEY(k, ah0, hp0);
PHALCON_GET_HVALUE(v);
if (PHALCON_IS_SCALAR(v)) {
if (PHALCON_IS_STRING(v, "")) {
PHALCON_INIT_NVAR(var_dump);
PHALCON_CONCAT_SVS(var_dump, "[", k, "] => (empty string)");
} else {
PHALCON_CALL_METHOD(&escaped_string, this_ptr, "_escapestring", v);
PHALCON_INIT_NVAR(var_dump);
PHALCON_CONCAT_SVSV(var_dump, "[", k, "] => ", escaped_string);
}
phalcon_array_append(&dump, var_dump, PH_SEPARATE);
} else {
if (Z_TYPE_P(v) == IS_ARRAY) {
PHALCON_INIT_NVAR(next);
phalcon_add_function(next, n, PHALCON_GLOBAL(z_one) TSRMLS_CC);
PHALCON_CALL_METHOD(&array_dump, this_ptr, "_getarraydump", v, next);
PHALCON_INIT_NVAR(var_dump);
PHALCON_CONCAT_SVSVS(var_dump, "[", k, "] => Array(", array_dump, ")");
phalcon_array_append(&dump, var_dump, PH_SEPARATE);
zend_hash_move_forward_ex(ah0, &hp0);
continue;
}
if (Z_TYPE_P(v) == IS_OBJECT) {
zend_class_entry *ce = Z_OBJCE_P(v);
PHALCON_INIT_NVAR(class_name);
ZVAL_STRINGL(class_name, ce->name, ce->name_length, !IS_INTERNED(ce->name));
PHALCON_INIT_NVAR(var_dump);
PHALCON_CONCAT_SVSVS(var_dump, "[", k, "] => Object(", class_name, ")");
phalcon_array_append(&dump, var_dump, PH_SEPARATE);
zend_hash_move_forward_ex(ah0, &hp0);
continue;
}
if (Z_TYPE_P(v) == IS_NULL) {
PHALCON_INIT_NVAR(var_dump);
PHALCON_CONCAT_SVS(var_dump, "[", k, "] => null");
phalcon_array_append(&dump, var_dump, PH_SEPARATE);
zend_hash_move_forward_ex(ah0, &hp0);
continue;
}
PHALCON_INIT_NVAR(var_dump);
PHALCON_CONCAT_SVSV(var_dump, "[", k, "] => ", v);
phalcon_array_append(&dump, var_dump, PH_SEPARATE);
}
zend_hash_move_forward_ex(ah0, &hp0);
}
PHALCON_INIT_VAR(joined_dump);
phalcon_fast_join_str(joined_dump, SL(", "), dump TSRMLS_CC);
RETURN_CTOR(joined_dump);
}
RETURN_NCTOR(number_arguments);
}
}
RETURN_MM_NULL();
}
/**
* Produces an string representation of a variable
*
* @param mixed $variable
* @return string
*/
PHP_METHOD(Phalcon_Debug, _getVarDump){
zval *variable, *class_name, *dumped_object = NULL;
zval *array_dump = NULL, *dump = NULL;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 0, &variable);
if (PHALCON_IS_SCALAR(variable)) {
/**
* Boolean variables are represented as 'true'/'false'
*/
if (Z_TYPE_P(variable) == IS_BOOL) {
if (zend_is_true(variable)) {
RETURN_MM_STRING("true", 1);
} else {
RETURN_MM_STRING("false", 1);
}
}
/**
* String variables are escaped to avoid XSS injections
*/
if (Z_TYPE_P(variable) == IS_STRING) {
PHALCON_RETURN_CALL_METHOD(this_ptr, "_escapestring", variable);
RETURN_MM();
}
/**
* Other scalar variables are just converted to strings
*/
RETURN_CTOR(variable);
}
/**
* If the variable is an object print its class name
*/
if (Z_TYPE_P(variable) == IS_OBJECT) {
const zend_class_entry *ce = Z_OBJCE_P(variable);
PHALCON_INIT_VAR(class_name);
ZVAL_STRINGL(class_name, ce->name, ce->name_length, !IS_INTERNED(ce->name));
/**
* Try to check for a 'dump' method, this surely produces a better printable
* representation
*/
if (phalcon_method_exists_ex(variable, SS("dump") TSRMLS_CC) == SUCCESS) {
PHALCON_CALL_METHOD(&dumped_object, variable, "dump");
/**
* dump() must return an array, generate a recursive representation using
* getArrayDump
*/
PHALCON_CALL_METHOD(&array_dump, this_ptr, "_getarraydump", dumped_object);
PHALCON_INIT_VAR(dump);
PHALCON_CONCAT_SVSVS(dump, "Object(", class_name, ": ", array_dump, ")");
} else {
/**
* If dump() is not available just print the class name
*/
PHALCON_INIT_NVAR(dump);
PHALCON_CONCAT_SVS(dump, "Object(", class_name, ")</span>");
}
RETURN_CTOR(dump);
}
/**
* Recursively process the array and enclose it in Array()
*/
if (Z_TYPE_P(variable) == IS_ARRAY) {
PHALCON_CALL_METHOD(&array_dump, this_ptr, "_getarraydump", variable);
PHALCON_CONCAT_SVS(return_value, "Array(", array_dump, ")");
RETURN_MM();
}
/**
* Null variables are represented as 'null'
* Other types are represented by its type
*/
RETURN_MM_STRING(zend_zval_type_name(variable), 1);
}
/**
* Returns the major framework's version
*
* @return string
*/
PHP_METHOD(Phalcon_Debug, getMajorVersion){
zval *version = NULL, *parts, *major;
PHALCON_MM_GROW();
PHALCON_CALL_CE_STATIC(&version, phalcon_version_ce, "get");
PHALCON_INIT_VAR(parts);
phalcon_fast_explode_str(parts, SL(" "), version);
PHALCON_OBS_VAR(major);
phalcon_array_fetch_long(&major, parts, 0, PH_NOISY);
RETURN_CCTOR(major);
}
/**
* Generates a link to the current version documentation
*
* @return string
*/
PHP_METHOD(Phalcon_Debug, getVersion){
zval *version = NULL;
PHALCON_MM_GROW();
PHALCON_CALL_METHOD(&version, this_ptr, "getmajorversion");
PHALCON_CONCAT_SVSVS(return_value, "<div class=\"version\">Phalcon Framework <a target=\"_new\" href=\"http://docs.phalconphp.com/en/", version, "/\">", version, "</a></div>");
RETURN_MM();
}
/**
* Returns the css sources
*
* @return string
*/
PHP_METHOD(Phalcon_Debug, getCssSources){
zval *uri, *sources;
PHALCON_MM_GROW();
PHALCON_OBS_VAR(uri);
phalcon_read_property_this(&uri, this_ptr, SL("_uri"), PH_NOISY TSRMLS_CC);
PHALCON_INIT_VAR(sources);
PHALCON_CONCAT_SVS(sources, "<link href=\"", uri, "jquery/jquery-ui.css\" type=\"text/css\" rel=\"stylesheet\" />");
PHALCON_SCONCAT_SVS(sources, "<link href=\"", uri, "themes/default/style.css\" type=\"text/css\" rel=\"stylesheet\" />");
RETURN_CTOR(sources);
}
/**
* Returns the javascript sources
*
* @return string
*/
PHP_METHOD(Phalcon_Debug, getJsSources){
zval *uri, *sources;
PHALCON_MM_GROW();
PHALCON_OBS_VAR(uri);
phalcon_read_property_this(&uri, this_ptr, SL("_uri"), PH_NOISY TSRMLS_CC);
PHALCON_INIT_VAR(sources);
PHALCON_CONCAT_SVS(sources, "<script type=\"text/javascript\" src=\"", uri, "jquery/jquery.js\"></script>");
PHALCON_SCONCAT_SVS(sources, "<script type=\"text/javascript\" src=\"", uri, "jquery/jquery-ui.js\"></script>");
PHALCON_SCONCAT_SVS(sources, "<script type=\"text/javascript\" src=\"", uri, "jquery/jquery.scrollTo.js\"></script>");
PHALCON_SCONCAT_SVS(sources, "<script type=\"text/javascript\" src=\"", uri, "prettify/prettify.js\"></script>");
PHALCON_SCONCAT_SVS(sources, "<script type=\"text/javascript\" src=\"", uri, "pretty.js\"></script>");
RETURN_CTOR(sources);
}
PHP_METHOD(Phalcon_Debug, getFileLink) {
zval **file, **line, **format;
phalcon_fetch_params_ex(3, 0, &file, &line, &format);
PHALCON_ENSURE_IS_STRING(file);
PHALCON_ENSURE_IS_STRING(line);
if (Z_TYPE_PP(format) == IS_STRING) {
char *tmp, *link;
int tmp_len, link_len;
zval z_link = zval_used_for_init;
tmp = php_str_to_str_ex(Z_STRVAL_PP(format), Z_STRLEN_PP(format), SL("%f"), Z_STRVAL_PP(file), Z_STRLEN_PP(file), &tmp_len, 1, NULL);
link = php_str_to_str_ex(tmp, tmp_len, SL("%l"), Z_STRVAL_PP(line), Z_STRLEN_PP(line), &link_len, 1, NULL);
ZVAL_STRINGL(&z_link, link, link_len, 0);
efree(tmp);
PHALCON_CONCAT_SVSVS(return_value, "<a href=\"", &z_link, "\">", *file, "</a>");
efree(link);
}
else {
RETVAL_ZVAL(*file, 1, 0);
}
}
/**
* Shows a backtrace item
*
* @param int $n
* @param array $trace
*/
PHP_METHOD(Phalcon_Debug, showTraceItem){
zval *n, *trace, *link_format, *space, *two_spaces, *underscore;
zval *minus, *html, *class_name;
zval *namespace_separator, *prepare_uri_class;
zval *lower_class_name, *prepared_function_name;
zval *prepare_internal_class, *type, *function_name = NULL;
zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL;
zval *span_argument = NULL, *joined_arguments, *z_one;
zval *file, *line, *show_files, *lines = NULL, *number_lines;
zval *show_file_fragment, *before_context, *before_line;
zval *first_line = NULL, *after_context, *after_line, *last_line = NULL;
zval *comment_pattern, *charset, *tab;
zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL;
zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL;
zval *escaped_line = NULL, *formatted_file = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 3, 0, &n, &trace, &link_format);
PHALCON_INIT_VAR(space);
ZVAL_STRING(space, " ", 1);
PHALCON_INIT_VAR(two_spaces);
ZVAL_STRING(two_spaces, " ", 1);
PHALCON_INIT_VAR(underscore);
ZVAL_STRING(underscore, "_", 1);
PHALCON_INIT_VAR(minus);
ZVAL_STRING(minus, "-", 1);
/**
* Every trace in the backtrace have a unique number
*/
PHALCON_INIT_VAR(html);
PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>");
if (phalcon_array_isset_string(trace, SS("class"))) {
zend_class_entry *class_ce;
PHALCON_OBS_VAR(class_name);
phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY);
class_ce = zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
if (!class_ce) {
/* Unable to load the class, should never happen */
}
else if (is_phalcon_class(class_ce)) {
PHALCON_INIT_VAR(namespace_separator);
ZVAL_STRING(namespace_separator, "\\", 1);
/* Prepare the class name according to the Phalcon's conventions */
PHALCON_INIT_VAR(prepare_uri_class);
phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name);
/* Generate a link to the official docs */
PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>");
} else if (class_ce->type == ZEND_INTERNAL_CLASS) {
PHALCON_INIT_VAR(lower_class_name);
phalcon_fast_strtolower(lower_class_name, class_name);
PHALCON_INIT_VAR(prepare_internal_class);
phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name);
/* Generate a link to the official docs */
PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>");
} else {
PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>");
}
/**
* Object access operator: static/instance
*/
PHALCON_OBS_VAR(type);
phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY);
phalcon_concat_self(&html, type TSRMLS_CC);
}
/**
* Normally the backtrace contains only classes
*/
if (phalcon_array_isset_string(trace, SS("class"))) {
PHALCON_OBS_VAR(function_name);
phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
} else {
zend_function *func;
PHALCON_OBS_NVAR(function_name);
phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
convert_to_string(function_name);
/**
* Check if the function exists
*/
if (phalcon_fetch_function(&func, Z_STRVAL_P(function_name), Z_STRLEN_P(function_name) TSRMLS_CC) == SUCCESS) {
/**
* Internal functions links to the PHP documentation
*/
if (func->type == ZEND_INTERNAL_FUNCTION) {
/**
* Prepare function's name according to the conventions in the docs
*/
PHALCON_INIT_VAR(prepared_function_name);
phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name);
PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>");
} else {
PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
}
} else {
PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
}
}
/**
* Check for arguments in the function
*/
if (phalcon_array_isset_string(trace, SS("args"))) {
PHALCON_OBS_VAR(trace_args);
phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY);
if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) {
PHALCON_INIT_VAR(arguments);
array_init(arguments);
phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0);
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
PHALCON_GET_HVALUE(argument);
/**
* Every argument is generated using _getVarDump
*/
PHALCON_CALL_METHOD(&dumped_argument, this_ptr, "_getvardump", argument);
PHALCON_INIT_NVAR(span_argument);
PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>");
/**
* Append the HTML generated to the argument's list
*/
phalcon_array_append(&arguments, span_argument, PH_SEPARATE);
zend_hash_move_forward_ex(ah0, &hp0);
}
/**
* Join all the arguments
*/
PHALCON_INIT_VAR(joined_arguments);
phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC);
PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")");
} else {
phalcon_concat_self_str(&html, SL("()") TSRMLS_CC);
}
}
/**
* When 'file' is present, it usually means the function is provided by the user
*/
if (phalcon_array_isset_string(trace, SS("file"))) {
z_one = PHALCON_GLOBAL(z_one);
PHALCON_OBS_VAR(file);
phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY);
PHALCON_OBS_VAR(line);
phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY);
PHALCON_CALL_METHOD(&formatted_file, getThis(), "getfilelink", file, line, link_format);
/**
* Realpath to the file and its line using a special header
*/
PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", formatted_file, " (", line, ")</div>");
PHALCON_OBS_VAR(show_files);
phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY TSRMLS_CC);
/**
* The developer can change if the files must be opened or not
*/
if (zend_is_true(show_files)) {
/**
* Open the file to an array using 'file', this respects the openbase-dir directive
*/
PHALCON_CALL_FUNCTION(&lines, "file", file);
PHALCON_INIT_VAR(number_lines);
phalcon_fast_count(number_lines, lines TSRMLS_CC);
PHALCON_OBS_VAR(show_file_fragment);
phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY TSRMLS_CC);
/**
* File fragments just show a piece of the file where the exception is located
*/
if (zend_is_true(show_file_fragment)) {
/**
* Take lines back to the current exception's line
*/
before_context = phalcon_fetch_nproperty_this(getThis(), SL("_beforeContext"), PH_NOISY TSRMLS_CC);
PHALCON_INIT_VAR(before_line);
sub_function(before_line, line, before_context TSRMLS_CC);
/**
* Check for overflows
*/
if (PHALCON_LT_LONG(before_line, 1)) {
PHALCON_CPY_WRT_CTOR(first_line, z_one);
} else {
PHALCON_CPY_WRT(first_line, before_line);
}
/**
* Take lines after the current exception's line
*/
after_context = phalcon_fetch_nproperty_this(getThis(), SL("_afterContext"), PH_NOISY TSRMLS_CC);
PHALCON_INIT_VAR(after_line);
phalcon_add_function(after_line, line, after_context TSRMLS_CC);
/**
* Check for overflows
*/
if (PHALCON_GT(after_line, number_lines)) {
PHALCON_CPY_WRT(last_line, number_lines);
} else {
PHALCON_CPY_WRT(last_line, after_line);
}
PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>");
} else {
PHALCON_CPY_WRT_CTOR(first_line, z_one);
PHALCON_CPY_WRT(last_line, number_lines);
PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>");
}
PHALCON_INIT_VAR(comment_pattern);
ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1);
charset = phalcon_fetch_nproperty_this(getThis(), SL("_charset"), PH_NOISY TSRMLS_CC);
PHALCON_INIT_VAR(tab);
ZVAL_STRING(tab, "\t", 1);
PHALCON_INIT_VAR(comment);
ZVAL_STRING(comment, "* /", 1);
PHALCON_CPY_WRT(i, first_line);
while (PHALCON_LE(i, last_line)) {
/**
* Current line in the file
*/
PHALCON_INIT_NVAR(line_position);
sub_function(line_position, i, z_one TSRMLS_CC);
/**
* Current line content in the piece of file
*/
PHALCON_OBS_NVAR(current_line);
phalcon_array_fetch(¤t_line, lines, line_position, PH_NOISY);
/**
* File fragments are cleaned, removing tabs and comments
*/
if (zend_is_true(show_file_fragment)) {
if (PHALCON_IS_EQUAL(i, first_line)) {
PHALCON_INIT_NVAR(trimmed);
phalcon_fast_trim(trimmed, current_line, PHALCON_TRIM_RIGHT TSRMLS_CC);
PHALCON_INIT_NVAR(is_comment);
RETURN_MM_ON_FAILURE(phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC));
if (zend_is_true(is_comment)) {
PHALCON_INIT_NVAR(spaced_current_line);
phalcon_fast_str_replace(spaced_current_line, comment, space, current_line);
PHALCON_CPY_WRT(current_line, spaced_current_line);
}
}
}
/**
* Print a non break space if the current line is a line break, this allows to show
* the html zebra properly
*/
if (PHALCON_IS_STRING(current_line, "\n")) {
phalcon_concat_self_str(&html, SL(" \n") TSRMLS_CC);