-
Notifications
You must be signed in to change notification settings - Fork 3
/
PtcDebug.php
2231 lines (2226 loc) · 83.8 KB
/
PtcDebug.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
/**
* DEBUGGER & LOGGER CLASS
* <br>All class properties and methods are static because it's required
* to let them work on script shutdown when FATAL error occurs.
* PHP version 5.3
* @category Library
* @version 0.9.2
* @author Irony <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link http://phptoolcase.com
*/
//declare( ticks = 1 ); // used by the watch var , function calls trace and code coverage utilities
class PtcDebug
{
/**
* Returns the buffer array
* @return the buffer array
*/
public static function getBuffer( ) { return static::$_buffer; }
/**
* Retrieves the code coverage analysis data stored in the PtcDebug::$_finalCoverageData property
*/
public static function getCoverage( ) { return static::$_finalCoverageData; }
/**
* Adds style properties to the floating panel styles array
* @param string $css some css to add
*/
public static function addCss( $css ){ static::$_panelCss = static::$_panelCss . "\n" . $css; }
/**
* Checks if the debug "url_key" and "url_pass" are set on the referer url. See @ref check_referer
* @return true if "url_key" and "url_pass" are in the referer url, otherwise false
*/
public static function checkReferer( )
{
if ( @array_key_exists( 'HTTP_REFERER' , @$_SERVER ) )
{
$query = parse_url( $_SERVER[ 'HTTP_REFERER' ] , PHP_URL_QUERY );
$params = array( );
parse_str( $query , $params );
if ( @$params[ static::$_options[ 'url_key' ] ] == static::$_options[ 'url_pass' ] )
{
$_GET[ static::$_options[ 'url_key' ] ] = $params[ static::$_options[ 'url_key' ] ];
return true;
}
}
return false;
}
/**
* Sets the error handler to be the debug class. good for production with "$dieOnFatal" set to false.
* See @ref replaceErrorHandler
* @param string $dieOnFatal die if fatal error occurs
*/
public static function setErrorHandler( $dieOnFatal = true )
{
ini_set( 'display_errors' , false );
ini_set( 'html_errors' , false );
if ( !@static::$_options[ 'error_reporting' ] )
{
@static::$_options[ 'error_reporting' ] = static::$_defaultOptions[ 'error_reporting' ];
}
ini_set( 'error_reporting' , static::$_options[ 'error_reporting' ] );
@static::$_options[ 'die_on_error' ] = $dieOnFatal;
set_error_handler( array( get_called_class( ) , 'errorHandler' ) );
}
/**
* Loads the debug interface and/or the console class if requested. See @ref dbg_getting_started
* @param array $options array of options, see PtcDebug::$ _defaultOptions
*/
public static function load( $options = null )
{
$now = microtime( true );
if ( defined( '_PTCDEBUG_NAMESPACE_' ) ) // check if the debug class is already loaded
{
$err = array( 'errno' => static::_msgType( E_USER_NOTICE ),
'errstr' => 'Debug already loaded!','errfile' => 'trace' );
static::_buildBuffer( 'log' , '{errorHandler}' , $err );
return;
}
$called_class = get_called_class( );
/* if error handler was called previously */
if ( @isset( static::$_options[ 'die_on_error' ] ) )
{
static::$_defaultOptions[ 'die_on_error' ] = static::$_options[ 'die_on_error' ];
}
if ( @isset( static::$_options[ 'error_reporting' ] ) )
{
static::$_defaultOptions[ 'error_reporting' ] = static::$_options[ 'error_reporting' ];
}
static::$_options = ( is_array( $options ) ) ?
array_merge( static::$_defaultOptions , $options ) : static::$_defaultOptions;
if ( !$has_access = static::_checkAccess( ) ){ return; } // check access with ips
$buffer = 'Debug Info:';
if ( static::$_options[ 'check_referer' ] ){ static::checkReferer( ); }// check if referer has debug vars
if ( static::$_options[ 'session_start' ] ) // start session on request
{
if ( session_id( ) === '' ) // check if session is already active
{
session_start( );
$buffer .= '<br>Initialized browser session with session_start( )';
}
else{ $buffer .= '<br>Session id is ' . session_id( ); }
}
if ( !@$_SESSION ){ $_SESSION = array( ); }
if ( !@$_SESSION[ 'ptcdebug' ] ){ $_SESSION[ 'ptcdebug' ] = array( ); }
if ( @$_GET[ static::$_options[ 'url_key' ] ] == static::$_options[ 'url_pass' ] )
{
$_SESSION[ 'ptcdebug' ][ static::$_options[ 'url_key' ] ] = true;
$_SESSION[ 'ptcdebug' ][ 'code_highlighter' ] = true;
$_SESSION[ 'ptcdebug' ][ 'search_files' ] = true;
//$buffer .= '<br>PtcDebug turned on!';
}
else if ( @$_GET[ static::$_options[ 'url_key' ] . '_off' ] == static::$_options[ 'url_pass' ] )
{
$_SESSION[ 'ptcdebug' ][ static::$_options[ 'url_key' ] ] = false;
$_SESSION[ 'ptcdebug' ][ 'code_highlighter' ] = false;
$_SESSION[ 'ptcdebug' ][ 'search_files' ] = false;
}
if ( static::_getSessionVars( static::$_options[ 'url_key' ] ) )
{
static::$_startTime = microtime( true );
if ( static::$_options[ 'set_time_limit' ] )
{
set_time_limit( static::$_options[ 'set_time_limit' ] );
}
if ( static::$_options[ 'memory_limit' ] )
{
ini_set( 'memory_limit' , static::$_options[ 'memory_limit' ] );
}
if ( static::$_options[ 'show_interface' ] || static::$_options[ 'debug_console' ] )
{
register_shutdown_function( array( $called_class , 'processBuffer' ) );
}
if ( static::$_options[ 'replace_error_handler' ] ) // replace error handler
{
static::setErrorHandler( static::$_options[ 'die_on_error' ] );
$buffer .= '<br>Error handler has been overridden!';
}
if ( static::$_options[ 'catch_exceptions' ] ) // set exception handler
{
set_exception_handler( array( $called_class , 'exceptionHandler' ) );
$buffer .= "<br>Exception Handler turned on!";
}
if ( static::$_options[ 'debug_console' ] ) // try to laod the console class
{
static::$_consoleStarted = false;
$buffer.='<br>Console debug turned on';
if ( file_exists( dirname( __FILE__ ) . '/PhpConsole/__autoload.php' ) )
{
require_once( dirname(__FILE__).'/PhpConsole/__autoload.php' );
static::$_consoleStarted = true;
\PhpConsole\Helper::register( );
$buffer .= ", phpConsole class started!";
}
else{ $buffer .= ', but could not find phpConsole class!'; }
}
if ( static::$_options[ 'enable_inspector' ] || static::$_options[ 'code_coverage' ] ||
static::$_options[ 'trace_functions' ] )
{
register_tick_function( array( $called_class , 'tickHandler' ) );
//if ( static::$_options[ 'declare_ticks' ] ) { declare( ticks = 1 ); }
$buffer .= "<br>Variables inspector enabled!";
}
if ( static::$_options[ 'code_coverage' ] === 'full' )
{
static::startCoverage( );
$buffer .= "<br>Code coverage analysis for all scripts enabled!";
}
if ( static::$_options[ 'trace_functions' ] === 'full' )
{
static::startTrace( );
$buffer .= "<br>Function calls tracing for all scripts enabled!";
}
if ( !static::_getSessionVars( 'show_messages' ) ){ static::_setSessionVars( ); }
if ( @$_GET[ 'hidepanels' ] ){ static::_disablePanels( ); }
else
{
static::$_options[ 'show_messages' ] = static::_getSessionVars( 'show_messages' );
static::$_options[ 'show_globals' ] = static::_getSessionVars( 'show_globals' );
static::$_options[ 'show_sql' ] = static::_getSessionVars( 'show_sql' );
static::$_options[ 'show_w3c' ] = static::_getSessionVars( 'show_w3c' );
}
@define( '_PTCDEBUG_NAMESPACE_' , $called_class );
static::$_tickTime = ( ( microtime( true ) - $now ) + static::$_tickTime );
static::bufferLog( '' , '<span>' . $buffer . '<span>' , 'Debug Loader' );
}
}
/**
* The ticks handler to execute all tickable functions
*/
public static function tickHandler( )
{
//$now = microtime( true );
if ( static::$_codeCoverage || static::$_functionTrace ) { $bt = debug_backtrace( ); }
if ( static::$_disableOpcode ) // try to disable opcode cache
{
static::_disableOpcodeCache( );
static::$_disableOpcode = false;
}
if ( static::$_options[ 'enable_inspector' ] && count( static::$_watchedVars ) )
{
static::_watchCallback( );
}
if ( static::$_codeCoverage ) { static::_codeCoverageAnalysis( $bt ); }
if ( static::$_functionTrace ) { static::_traceFunctionCalls( $bt ); }
//if ( static::$_options[ 'profiler' ] ) { }
unset( $bt );
// FIXME: the timer goes to minus here
//static::$_tickTime = ( ( microtime( true ) - $now ) + static::$_tickTime );
}
/**
* Starts the code coverage analysis utility to find executed lines. See @ref codeCoverage
*/
public static function startCoverage( )
{
if ( @static::$_options[ 'code_coverage' ] )
{
if ( static::$_codeCoverage && static::$_options[ 'code_coverage' ] !== 'full' )
{
static::bufferLog( 'Coverage already started, please use stopCoverage( )
before starting a new one!', '' , 'Debugger Notice' );
return false;
}
static::$_codeCoverage = true;
}
}
/**
* Stops the code coverage analysis utility. See @ref codeCoverage
*/
public static function stopCoverage( )
{
if ( static::$_options[ 'code_coverage' ] !== 'full' )
{
static::$_codeCoverage = false;
if( static::$_coverageData )
{
static::$_finalCoverageData[ ] = static::$_coverageData;
static::$_coverageData = null;
}
}
}
/**
* Starts the function calls trace utility. See @ref traceFunctions
*/
public static function startTrace( )
{
if ( @static::$_options[ 'trace_functions' ] )
{
if ( static::$_functionTrace && static::$_options[ 'trace_functions' ] !== 'full' )
{
static::bufferLog( 'Function calls tracing has been already started, please use stopTrace( )
before starting a new one!' , '' , 'Debugger Notice' );
return false;
}
static::$_functionTrace = true;
}
}
/**
* Stops the function calls trace utility. See @ref traceFunctions
*/
public static function stopTrace( )
{
if ( static::$_options[ 'trace_functions' ] !== 'full' )
{
static::$_functionTrace = false;
if ( static::$_traceData )
{
static::$_finalTraceData[ ] = static::$_traceData;
static::$_traceData = null;
}
}
}
/**
* Excludes functions from the function calls tracing engine
* @param array | string $functions the function the exclude by their name
*/
public static function excludeFromTrace( $functions )
{
$functions = ( is_array( $functions) ) ? $functions : array( $functions );
static::$_excludeFromTrace = array_merge( static::$_excludeFromTrace , $functions );
}
/**
* Watches a variable that is in a declare(ticks=n); code block, for changes. See @ref variableInspector
* @param string $variableName the name of the variable to watch
* @param string $callback a callback function that retrieves the variable
*/
public static function watch( $variableName , $callback = null )
{
if ( @static::$_options[ 'enable_inspector' ] )
{
$var = ( $callback ) ? array( 'value' => $callback( ) , 'callback' => $callback ) :
static::_findWatchVar( $variableName );
static::$_watchedVars[ $variableName ] = $var;
$value = ( $callback ) ? call_user_func( $callback ) : static::$_watchedVars[ $variableName ];
static::bufferLog( $value , 'Watching variable <span style="font-weight:bold;">$' .
$variableName . '</span> = ' , 'Inspector' );
}
else
{
$err = array( 'errno' => static::_msgType( E_USER_NOTICE ) , 'errfile' => 'trace' ,
'errstr' => 'Please set to true [\'enable_inspector\'] option to be able to watch a variable' );
static::_buildBuffer( 'log' , '{errorHandler}' , $err );
}
}
/**
* Writes data to the messages panel. See @ref logging_data
* @param mixed $string the string to pass
* @param mixed $statement some statement if required
* @param string $category a category for the messages panel
*/
public static function bufferLog( $string , $statement = null , $category = null )
{
static::_buildBuffer( 'log' , $string , $statement , $category );
}
/**
* Writes data to the sql panel. See @ref log_sql
* @param mixed $string the string to pass
* @param mixed $statement some statement if required
* @param string $category a category for the sql panel
*/
public static function bufferSql( $string , $statement = null , $category = null )
{
static::_buildBuffer( 'sql' , $string , $statement , $category );
}
/**
* Monitors the execution of php code, or sql queries based on a reference. See @ref execution_timing
* @param string $reference a reference to look for ("$statement")
* @param string|numeric $precision sec/ms
* @return true if a given reference is found, otherwise false
*/
public static function stopTimer( $reference = null , $precision = 1 )
{
$now = microtime( true );
$last = static::_findReference( $reference , 1 );
if ( !$last ){ return false; }
$time = ( $now - @$last[ 'data' ][ 'start_time' ] );
switch( $precision )
{
case 0 : // seconds
case 'sec' : // seconds
static::$_buffer[ $last[ 'key' ] ][ 'time' ] = round( $time , 3 ) . ' sec';
break;
case 1 : // millisecons
case 'ms' : // millisecons
default :
static::$_buffer[ $last[ 'key' ] ][ 'time' ] = round( $time * 1000 , 3 ) . ' ms';
break;
}
if ( static::$_options[ 'debug_console' ] )
{
static::$_buffer[ $last[ 'key' ] ][ 'console_time' ] =
static::$_buffer[ $last[ 'key' ] ][ 'time' ];
}
return true;
}
/**
* Convert memory_usage( ) into a readable format
* @param float $val The value to convert
* @param int $precision the decimal points
*/
public static function convertMemUsage( $val , $precision = 2)
{
$ram = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return $usage = ( $val ) ? @round( $val / pow( 1024 ,
($i = floor( log( $val , 1024) ) ) ) , $precision ) . $ram[ $i ] : '0 Bytes';
}
/**
* Handles php errors. See @ref replaceErrorHandler
* @param string $errno error number (php standards)
* @param string $errstr error string
* @param string $errfile error file
* @param string $errline error line
@return true to prevent php default error handler to fire
*/
public static function errorHandler( $errno , $errstr , $errfile , $errline )
{
if ( error_reporting( ) == 0 ){ return; } // if error has been supressed with an @
$err = array( 'errno' => static::_msgType( $errno ) , 'errstr' => $errstr ,
'errfile' => $errfile , 'errline' => $errline );
static::_buildBuffer( 'log' , '{errorHandler}' , $err );
// stop if fatal error occurs
if ( static::$_options[ 'die_on_error' ] && static::_msgType( $errno ) == 'Php Error') { die( ); }
return true; // don't execute php error handler
}
/**
* Exception handler, catches exceptions that are not in a try/catch block
* @param object $exception the exception object
*/
public static function exceptionHandler( $exception )
{
$err = array( 'errno' => static::_msgType( 'exception' ) , 'errstr' => $exception->getMessage( ) ,
'errfile' => $exception->getFile( ) , 'errline' => $exception->getLine( ) );
static::_buildBuffer( 'log' , '{errorHandler}' , $err );
}
/**
* Attaches a message to the end of the buffer array to add data based on a reference. See @ref add_to_log
* @param string $reference a reference to look for ("$statement")
* @param mixed $string the message to show
* @param string $statement a new statement if required
* @return true if the given reference is found, false otherwise
*/
public static function addToBuffer( $reference , $string , $statement = null )
{
$raw_buffer = static::_findReference( $reference , 2 );
if ( !$raw_buffer ){ return false; }
$last = $raw_buffer[ 'data' ];
if ( @$string )
{
$last[ 'var_type' ] = gettype( $string );
$last[ 'errstr' ] = $string;
}
if ( $statement ){ $last[ 'errmsg' ] = $statement; }
if ( static::$_options[ 'debug_console' ] )
{
$last[ 'console_string' ] = ( !@$string ) ? $last[ 'errstr' ] : $string;
$last[ 'console_statement' ] = ( !@$statement ) ? $last[ 'errmsg' ] : $statement;
}
@static::$_buffer[ $raw_buffer[ 'key' ] ] = $last;
return true;
}
/**
* Processes the buffer to show the interface and/or the console messages
*/
public static function processBuffer( )
{
@unregister_tick_function( 'tickHandler' );
static::$_countTime = false;
if ( static::$_codeCoverage )
{
$trace_o = static::$_options[ 'code_coverage' ];
static::$_options[ 'code_coverage' ] = true;
static::stopCoverage( );
static::$_options[ 'code_coverage' ] = $trace_o;
}
if ( static::$_functionTrace )
{
$trace_o = static::$_options[ 'trace_functions' ];
static::$_options[ 'trace_functions' ] = true;
static::stopTrace( );
static::$_options[ 'trace_functions' ] = $trace_o;
}
static::$_endTime = microtime( true );
if( static::$_consoleStarted ){ static::_debugConsole(); }
if( static::$_options[ 'show_interface' ] )
{
static::_lastError( ); // get last php fatal error
$interface = static::_buildInterface( ); // build the interface
print $interface;
}
}
/**
* Searches files for a string inside a given folder recursively. See @ref search_string
* @param string $query the string to lok for
* @param string $path a starting path to search recursively
* @param int $last check last result
* @return an html table with all results
*/
public static function findString( $query , $path = null , $last = 1 )
{
$path = ( $path ) ? $path : dirname(__FILE__);
$real_path = realpath( $path );
if ( !$real_path ) // need to trigger error if path is not found
{
trigger_error( 'The path "' . $path .
'" does not exists or is not accessible!' , E_USER_WARNING );
return false;
}
$fp = @opendir( $real_path );
if ( !$fp ) // need to trigger error if path is not found
{
trigger_error( 'The path "' . $path .
'" does not exists or is not accessible!' , E_USER_WARNING );
return false;
}
while ( $f = readdir( $fp ) )
{
if ( preg_match( '#^\.+$#' , $f ) ){ continue; } // ignore symbolic links
$file_full_path = $real_path . DIRECTORY_SEPARATOR . $f;
if ( is_dir( $file_full_path ) )
{
@$result .= static::findString( $query , $file_full_path , $last );
}
else
{
$file_lines = @file( $file_full_path );
if ( $file_lines )
{
$line_number = 1;
foreach ( $file_lines as $line )
{
$search_count = substr_count( $line , $query );
if ( $search_count > 0 ) // we found matches
{
$line = preg_replace( '|' . $query . '|',
'<span style="color:yellow;font-weight:bold;">' . $query .
'</span>' , htmlentities( $line ) );
@$result .= '<tr>';
$result .= '<td style="white-space: nowrap;"># ' . $last . '</td>';
$result .= '<td><div style="color:blue;">' . $file_full_path . '</div></td>';
$result .= '<td><div style="font-weight:bold;color:black;">' .
$line_number . '</div></td>';
$result .= '<td><div style="color:darkred;font-weight:bold;"">' . $line . '</div></td>';
$result .= '<td><div style="font-weight:bold;color:red;">
' . $search_count . '</div></td>';
$result .= '</tr>';
$last++;
}
$line_number++;
}}
}
}
return @$result;
}
/**
* File highlighter that opens a popup window inspect source code. See @ref file_inspector
* @param string $file the full path for the file
* @param string $line the line to be highlighted
* @return the html output of the source code
*/
public static function highlightFile( $file , $line = null )
{
$lines = implode( range( 1 , count( file ( $file ) ) ) , '<br />' );
$content = highlight_file( $file , true );
if ( $line )
{
$line = $line - 1;
$l = explode( '<br />' , $content );
$l[ $line ] = '<div id="line" style="display:inline;background-color:yellow;">' . $l[ $line ] . '</div>';
$content = implode( '<br />' , $l );
}
$html = '
<style type="text/css">
.num{float:left;color:gray;font-size:13px;
font-family:monospace;text-align:right;margin-right:6pt;
padding-right:6pt;border-right:1px solid gray;}
body{margin:0px;margin-left:5px;}
td{vertical-align:top;}code{white-space:nowrap;}
</style>
<script>
window.onload=function()
{
var SupportDiv = document.getElementById("line");
window.scroll(0,findPos(SupportDiv));
return false;
};
function findPos(obj)
{
var curtop=0;
if(obj.offsetParent)
{
do{
curtop+=(obj.offsetTop-40);
} while (obj = obj.offsetParent);
return [curtop];
}
};
</script>';
$html .= "<table><tr><td class=\"num\">\n$lines\n</td>";
$html .= "<td>\n$content\n</td></tr></table>";
return $html;
}
/**
* Default options for the debug class. See @ref dbg_class_options
*/
protected static $_defaultOptions = array
(
'url_key' => 'debug' , // the key to pass to the url to turn on debug
'url_pass' => 'true' , // the pass to turn on debug
'replace_error_handler' => true , // replace default php error handler
'error_reporting' => E_ALL , // error reporting flag
'catch_exceptions' => true , // sets exception handler to be this class method
'check_referer' => false , // check referer for key and pass ( good for ajax debugging )
'die_on_error' => true , // die if fatal error occurs ( with this class error handler )
'debug_console' => false , // only for Chrome,show messages in console ( phpConsole needed )
'allowed_ips' => null , // restrict access with ip's
'session_start' => false , // start session for persistent debugging
'show_interface' => true , // show the interface ( false to debug in console only )
'set_time_limit' => null , // set php execution time limit
'memory_limit' => null , // set php memory size
'show_messages' => true , // show messages panel
'show_globals' => true , // show global variables in vars panel
'show_sql' => true , // show sql panel
'show_w3c' => true, // show the w3c panel
'minified_html' => true , // compress html for a lighter output
'trace_depth' => 10 , // maximum depth for the backtrace
'max_dump_depth' => 6 , // maximum depth for the dump function
'panel_top' => '0px' , // panel top position
'panel_right' => '0px' , // panel right position
'default_category' => 'General' , // default category for the messages
'enable_inspector' => true , // enable variables inspector, use declare(ticks=n); in code block
'code_coverage' => true, // enable code coverage analysis, use "full" to start globally
'trace_functions' => true, // enable function calls tracing, use "full" to start globally
'exclude_categories' => array( 'Event Manager' , 'Autoloader' ) // exclude categories from the output
);
/**
* Array of methods excluded from the backtrace
*/
protected static $_excludeMethods=array( );
/**
* Code coverage analysis storage
*/
protected static $_coverageData = null;
/**
* Final data array for the code coveage
*/
protected static $_finalCoverageData = array( );
/**
* Function calls tracing storage property
*/
protected static $_traceData = null;
/**
* Final data array for the function calls trace
*/
protected static $_finalTraceData = array( );
/**
* Array with all options
*/
protected static $_options = array( );
/**
* The debug buffer
*/
protected static $_buffer = array( );
/**
* Application start time
*/
protected static $_startTime = null;
/**
* Application end time
*/
protected static $_endTime = null;
/**
* Decides if we should send the buffer to the PhpConsole class
*/
protected static $_consoleStarted = false;
/**
* Array of watched variables declared
*/
protected static $_watchedVars = array();
/**
* Tick execution time property
*/
protected static $_tickTime = 0;
/**
* Exclude PtcDebug::$_buildBuffer from execution timing property
*/
protected static $_countTime = true;
/**
* Code coverage analysis property to start coverage
*/
protected static $_codeCoverage = false;
/**
* Function calls trace property to start the analysis
*/
protected static $_functionTrace = false;
/**
* Controlls when to disable opcode cache
*/
protected static $_disableOpcode = true;
/**
* Exclude functions from the function calls trace array
*/
protected static $_excludeFromTrace = array( );
/**
* Property that holds the css for the floating panel
*/
protected static $_panelCss = '#ptcDebugPanel{font-family:Arial,sant-serif;
position:fixed;top:{PANEL_TOP};right:{PANEL_RIGHT};
background:#eee;color:#333;z-index:10000;line-height:1.3em;
text-align:left;padding:0px;margin:0px;height:25px;}
#ptcDebugPanel ul.tabs li{background-color:#ddd;border-color:#999;margin:0 -3px -1px 0;
padding:3px 6px;border-width:1px;list-style:none;display:inline-block;border-style:solid;}
#ptcDebugPanel ul.tabs li.active{background-color:#fff;border-bottom-color:transparent;
text-decoration:}#ptcDebugPanel ul.tabs li:hover{background-color:#eee;}
#ptcDebugPanel ul.tabs li.active:hover{background-color:#fff;}
#ptcDebugPanel ul.tabs.merge-up{margin-top:-24px;}
#ptcDebugPanel ul.tabs.right{padding:0 0 0 0;text-align:right;}
#ptcDebugPanel ul.tabs{border-bottom-color:#999;border-bottom-width:1px;font-size:14px;
list-style:none;margin:0;padding:0;z-index:100000;position:relative;
background-color:#EEE}#ptcDebugPanel ul.tabs a{color:purple;font-size:10px;
text-decoration:none;}#ptcDebugPanel .tabs a:hover{color:red;}
#ptcDebugPanel ul.tabs a.active{color:black;background-color:yellow;}
.msgTable{padding:0;margin:0;border:1px solid #999;font-family:Arial;
font-size:11px;text-align:left;border-collapse:separate;border-spacing:2px;}
.msgTable th{margin:0;border:0;padding:3px 5px;vertical-align:top;
background-color:#999;color:#EEE;white-space:nowrap;}
.msgTable td{margin:0;border:0;padding:3px 3px 3px 3px;vertical-align:top;}
.msgTable tr td{background-color:#ddd;color:#333}
.msgTable tr.php-notice td{background-color:lightblue;}
.msgTable tr.exception td{background-color:greenyellow;}
.msgTable tr.php-warning td{background-color:yellow;}
.msgTable tr.php-error td{background-color:orange;}
.msgTable tr.inspector td{background-color:lightgreen;}
.innerTable a.php-notice{color:lightblue;}
.innerTable a.exception{color:greenyellow;}.innerTable a.php-warning{color:yellow;}
.innerTable a.php-error{color:orange;}.innerTable a.inspector{color:lightgreen;}
.innerTable a.general{color:darkgrey;}.innerTable a.show-all{color:red;}
#ptcDebugFilterBar{background-color:black;margin-bottom:8px;padding:4px;
font-size:13px;}.innerTable{z-index:10000;position:relative;background:#eee;
height:300px;padding:30px 10px 0 10px;overflow:auto;clear:both;}
.innerTable a{color:dodgerBlue;font-size:bold;text-decoration:none}
.innerTable p{font-size:12px;color:#333;text-align:left;line-height:12px;}
.innerPanel h1{font-size:16px;font-weight:bold;margin-bottom:20px;
padding:0;border:0px;background-color:#EEE;}
#ptcDebugPanelTitle{height:25px;float:left;z-index:1000000;position:relative;}
#ptcDebugPanelTitle h1{font-size:16px;font-weight:bold;margin-bottom:20px;
margin-left:10px;padding:0 0 0 0;border:0px;background-color:#EEE;
color:#669;margin-top:5px;;height:20px;}
#analysisPanel h2{font-size:14px;font-weight:bold;margin-bottom:20px;
padding:0 0 0 0;border:0px;background-color:#EEE;
color:#669;margin-top:5px;;height:20px;}
.vars-config, .vars-config span{font-weight:bold;}
.msgTable pre span, .vars-config span{padding:2px;}
.msgTable pre, span, .vars{font-size:11px;line-height:15px;
font-family:"andale mono","monotype.com","courier new",courier,monospace;}
.msgTable pre,.msgTable span{font-weight:bold;}
#varsPanel a{text-decoration:none;font-size:14px;font-weight:bold;color:#669;
line-height:25px;}.count_vars{font-size:11px;color:purple;padding:0;margin:0;}
.fixed{width:1%;white-space:nowrap;}.fixed1{width:5%;white-space:nowrap;}
#ptcDebugStatusBar{height:2px;background-color:#999;}' ;
/**
* Sends the buffer to the PhpConsole class. See @ref ajax_env
*/
protected static function _debugConsole()
{
$handler = \PhpConsole\Handler::getInstance( );
$handler->setHandleErrors( false );
$handler->setHandleExceptions( false );
$handler->start( );
foreach ( static::$_buffer as $k => $arr )
{
if ( @$arr[ 'console_string' ] || @$arr[ 'console_statement' ] )
{
if ( !@$arr )
{
$php_trace = static::_debugTrace( 1 );
$arr=array( 'errline' => $php_trace[ 'line' ] , 'errfile' => $php_trace[ 'file' ] );
}
$statement = ( @$arr[ 'console_statement' ] ) ?
strip_tags( preg_replace( "=<br */?>=i" , "\n" ,
@$arr[ 'console_statement' ] ) ) : null;
$statement .= ( @$arr[ 'console_time' ] ) ? ' [time: ' . $arr[ 'console_time' ] . ']' : '';
$console_type = '[' . @end( @explode( '/' , $arr[ 'errfile' ][ 0 ] ) ) . ':';
$console_type .= $arr[ 'errline' ][ 0 ] . ']';
$key=(@$arr['type']=='log') ? 'messages' : 'sql';
if(static::$_options['show_'.$key])
{
if ( 'error' === $arr['console_statement'] )
{
$handler->handleError( $arr[ 'console_category' ] , $arr[ 'errstr' ] ,
$arr[ 'errfile' ][ 0 ] , $arr[ 'errline' ][ 0 ] , null , 2 );
}
else
{
\PC::debug( $console_type , $arr[ 'console_category' ] . '.file' );
if ( $statement )
{
\PC::debug( $statement , $arr[ 'console_category' ] . '.message' );
}
if ( @$arr[ 'console_string' ] )
{
\PC::debug( $arr[ 'console_string' ] , $arr[ 'console_category' ] . '.result' );
}
if ( @$arr[ 'errfile' ] )
{
unset( $arr[ 'errfile' ][ 0 ] );
if ( !empty( $arr[ 'errfile' ] ) )
{
\PC::debug( $arr[ 'errfile' ] , $arr[ 'console_category' ] . '.trace' );
}
}
//\PC::debug( $arr , $arr[ 'console_category' ] . '[full]' );
}
}
}
}
if ( !static::$_options[ 'show_interface' ] )
{
static::_buildCoverageData( );
static::_buildTraceData( );
}
$time = ( ( static::$_endTime - static::$_startTime ) - static::$_tickTime );
$console_final = 'Seconds: ' . round( $time , 3 ) . ' | Milliseconds: ' . round( $time * 1000 , 3 );
\PC::debug( array( @get_included_files( ) ) , static::$_options[ 'default_category' ] . '.includedFiles' );
\PC::debug( 'Global Execution Time ' . $console_final , static::$_options[ 'default_category' ] );
}
/**
* Checks if a given ip has access
* @param string|array $allowedIps the ip's that are allowed
*/
protected static function _checkAccess($allowedIps=null)
{
static::$_options['allowed_ips']=(!$allowedIps) ? static::$_options['allowed_ips'] : $allowedIps;
if(static::$_options['allowed_ips'])
{
static::$_options['allowed_ips']=(is_array(static::$_options['allowed_ips'])) ?
static::$_options['allowed_ips'] : array(static::$_options['allowed_ips']);
if(@in_array(@$_SERVER['REMOTE_ADDR'],static::$_options['allowed_ips'])){ return true; }
return false;
}
return true;
}
/**
* Sets session vars to control which panels will be shown
*/
protected static function _setSessionVars()
{
$_SESSION[ 'ptcdebug' ]['show_messages']=static::$_options['show_messages'];
$_SESSION[ 'ptcdebug' ]['show_globals']=static::$_options['show_globals'];
$_SESSION[ 'ptcdebug' ]['show_sql']=static::$_options['show_sql'];
$_SESSION[ 'ptcdebug' ]['show_w3c']=static::$_options['show_w3c'];
}
/**
* Controls which panels will be shown with $_GET variable "hidepanels"
*/
protected static function _disablePanels( )
{
$hide = @explode( ',' , $_GET[ 'hidepanels' ] );
if ( !@empty( $hide ) )
{
$_SESSION[ 'ptcdebug' ][ 'show_messages' ] = true;
$_SESSION[ 'ptcdebug' ][ 'show_globals' ] = true;
$_SESSION[ 'ptcdebug' ][ 'show_sql' ] = true;
$_SESSION[ 'ptcdebug' ][ 'show_w3c' ] = true;
foreach ( $hide as $k => $v )
{
if ( $v == 'msg' || $v == 'all' )
{
$_SESSION[ 'ptcdebug' ][ 'show_messages' ] = false;
}
if ( $v == 'globals' || $v == 'all' )
{
$_SESSION[ 'ptcdebug' ][ 'show_globals' ] = false;
}
if ( $v == 'sql' || $v == 'all' )
{
$_SESSION[ 'ptcdebug' ][ 'show_sql' ] = false;
}
if ( $v == 'w3c' || $v == 'all' )
{
$_SESSION[ 'ptcdebug' ][ 'show_w3c' ] = false;
}
}
}
static::$_options[ 'show_messages' ] = static::_getSessionVars( 'show_messages' );
static::$_options[ 'show_globals' ] = static::_getSessionVars( 'show_globals' );
static::$_options[ 'show_sql' ] = static::_getSessionVars( 'show_sql' );
static::$_options[ 'show_w3c' ] = static::_getSessionVars( 'show_w3c' );
}
/**
* Builds the buffer
* @param string $type log/sql
* @param mixed $string the string to pass
* @param mixed $statement some statement preceding the string
* @param string $category a category for the message
*/
protected static function _buildBuffer( $type , $string , $statement = null , $category = null )
{
if ( @in_array( $category , static::$_options[ 'exclude_categories' ] ) ){ return; }
if ( defined( '_PTCDEBUG_NAMESPACE_' ) &&
@static::_getSessionVars( static::$_options[ 'url_key' ] ) &&
( static::$_options[ 'show_interface' ] || static::$_options[ 'debug_console' ] ) )
{
$buffer = array( 'start_time' => microtime( true ) , 'type' => $type );
$php_trace = static::_debugTrace( static::$_options[ 'trace_depth' ] );
$buffer[ 'errline' ] = @$php_trace[ 'line' ];
$buffer[ 'errfile' ] = @$php_trace[ 'file' ];
$buffer[ 'function' ] = @$php_trace[ 'function' ];
$buffer[ 'class' ] = @$php_trace[ 'class' ];
if ( $string === '{errorHandler}' )
{
$buffer[ 'errno' ] = $statement[ 'errno' ];
$buffer[ 'errstr' ] = $statement[ 'errstr' ];
if ( $statement[ 'errfile' ] == 'trace' )
{
$params = @explode( ':' , @str_replace( ':\\' , '{win-patch}' ,
@$buffer[ 'errfile' ][ 0 ] ) ); // windows patch
@$buffer[ 'errfile' ][ 0 ] = @str_replace( '{win-patch}' , ':\\' , @$params[ 0 ] );
}
else // if static::errorHandler() called the function
{
if ( !@is_array( $buffer[ 'errline' ] ) ){ $buffer[ 'errline' ] = array( ); }
if ( !@is_array( $buffer[ 'errfile' ] ) ){ $buffer[ 'errfile' ] = array( ); }
if ( !@is_array( $buffer[ 'function' ] ) ){ $buffer[ 'function' ] = array( ); }
if ( !@is_array( $buffer[ 'class' ] ) ){ $buffer[ 'class' ] = array( ); }
@array_unshift( $buffer[ 'errline' ] , $statement[ 'errline' ] );
@array_unshift( $buffer[ 'errfile' ] , $statement[ 'errfile' ] );
@array_unshift( $buffer[ 'function' ] , '' );
@array_unshift( $buffer[ 'class' ] , '' );
}
if ( static::$_options[ 'debug_console' ] )
{
//var_dump($buffer );
$buffer[ 'console_string' ] = $buffer;
$buffer[ 'console_statement' ] = 'error';
$buffer[ 'console_category' ] = $statement[ 'errno' ];
}
}
else
{
$params = @explode( ':' , @str_replace( ':\\' , '{win-patch}' ,
@$buffer[ 'errfile' ][ 0 ] ) ); // windows patch
@$buffer[ 'errfile' ][ 0 ] = @str_replace( '{win-patch}' , ':\\' , @$params[ 0 ] );
$buffer[ 'var_type' ] = gettype( $string );
if ( !$category ){ $category = static::$_options[ 'default_category' ]; }
$buffer[ 'errno' ] = $category;
$buffer[ 'errstr' ] = $string;
$buffer[ 'errmsg' ] = $statement;
if ( static::$_options[ 'debug_console' ] )
{
$buffer[ 'console_string' ] = $string;
$buffer[ 'console_statement' ] = $statement;
$buffer[ 'console_category' ] = $category;
}
}
@static::$_buffer[ ] = $buffer;
if ( static::$_countTime )
{
static::$_tickTime = ( ( microtime( true ) - $buffer[ 'start_time' ] ) + static::$_tickTime );
}
}
}
/**
* Callback function that checks if a given variable has changed
*/
protected static function _watchCallback( )
{
if ( count( static::$_watchedVars ) )
{
foreach ( static::$_watchedVars as $variableName => $variableValue )
{
if ( is_array( $variableValue ) )
{
$var = $variableValue[ 'callback' ]( );
if ( @$var !== @$variableValue[ 'value' ] )
{
$info=array
(
'variable' => '$' . $variableName ,
'previous_value' => $variableValue[ 'value' ] ,
'new_value' => $var
);
static::$_watchedVars[ $variableName ] =
array( 'value' => $var , 'callback' => $variableValue[ 'callback' ] );
}
}
else
{
$var = static::_findWatchVar( $variableName );
if ( @$var !== @$variableValue )
{
$info=array
(
'variable' => '$' . $variableName ,
'previous_value' => $variableValue ,
'new_value' => $var
);
static::$_watchedVars[ $variableName ] = $var;
}
}
}
if ( @$info )
{
static::bufferLog( $info ,' Watched variable changed <span style="font-weight:bold;">$'.
$variableName . '</span> = ','Inspector');
}
}
}
/**
* Collect data for code coverage analysis
* @param array $backtrace the debug_backtrace( )
*/
protected static function _codeCoverageAnalysis( $backtrace = null)
{
$backtrace = ( !$backtrace ) ?
debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ) : $backtrace;
$backtrace = array_reverse( $backtrace );
foreach ( $backtrace as $k => $v )
{
if ( @$v[ 'file' ] == __FILE__ ||
@strpos( $v[ 'file' ] , 'runtime-created function' ) ) { continue; }
if ( @$v[ 'line' ] && !@array_key_exists( @$v[ 'line' ] ,
@static::$_coverageData[ $v[ 'file' ] ] ) )
{
if ( !@array_key_exists( $v[ 'file' ] , static::$_coverageData ) )
{
static::$_coverageData[ $v[ 'file' ] ] = array( );
}
static::$_coverageData[ $v[ 'file' ] ][ $v[ 'line' ] ] = 1;
}
}
}
/**
* Evaluates the type of variable for output
* @param mixed $var the variable to pass
* @return the html output with the variable content
*/
protected static function _formatVar($var)