Source for file debug.php
Documentation is available at debug.php
1 <? 2 /** 3 * +--------------------------------------------------------------------------+ 4 * +-- PHP_Debug : A simple and fast way to debug PHP code | 5 * +-- | 6 * +-- Support : Vernet Loic (coil@strangebuzz.com) | 7 * +--------------------------------------------------------------------------+ 8 * | | 9 * | This PHP debug libray offers you the ability to debug your PHP code | 10 * | | 11 * | - Pear integration | 12 * | - PHP Process time | 13 * | - Database and query process time | 14 * | - Dump of all type of variable in a graphical way | 15 * | - Functionnal debug | 16 * | - Debug queries | 17 * | - Show number of database queries executed | 18 * | - Allow to search in all debug infos | 19 * | - Direct links to test queries in Phpmyadmin | 20 * | - Show globals var ( $GLOBALS, $_POST, $_GET ... ) | 21 * | - Enable or disable the debug infos you want to see | 22 * | - Check performance of chunk of php code | 23 * | - Customize the general display of your debug | 24 * | - ... ( see doc for complete specification ) | 25 * +--------------------------------------------------------------------------+ 26 * 27 * @filesource 28 * @package PHP_Debug 29 * @author Loic Vernet, COil <coil@strangebuzz.com> 30 * @license http://www.php.net/license/2_02.txt The PHP License, version 2.02 31 * @example tests/debug_test_min.php Minimal example 32 * @example tests/debug_test.php Full example 33 * @todo Check TODO file or https://sourceforge.net/tracker/?group_id=95715 34 */ 35 36 /** 37 * Possible version of class Debug 38 */ 39 define ( 'DBG_VERSION_STANDALONE' , 0 ); 40 define ( 'DBG_VERSION_PEAR' , 1 ); 41 define ( 'DBG_VERSION_DEFAULT' , DBG_VERSION_STANDALONE ); 42 define ( 'DBG_VERSION' , DBG_VERSION_STANDALONE ); 43 define ( 'DBG_RELEASE' , 'BETA 1.0' ); 44 45 /** 46 * Only include Pear libraries for Pear version 47 */ 48 if ( DBG_VERSION == DBG_VERSION_PEAR ) 49 { 50 /** 51 * Include Pear Library 52 */ 53 require_once 'PEAR.php'; 54 55 /** 56 * Include Pear::Var_Dump Library 57 */ 58 require_once 'Var_Dump.php'; 59 } 60 61 62 /** 63 * Eventual external constants 64 */ 65 if ( !defined('STR_N') ) 66 define( 'STR_N' , '' ); 67 68 if ( !defined('CR') ) 69 define( 'CR' , "\r\n" ); 70 71 /** 72 * DBG_MODE Constants, define the different available debug modes. 73 * 74 * Here are the available modes : 75 * - DBG_MODE_OFF : Debug mode is OFF 76 * - DBG_MODE_USERPERF : Base debug mode, 77 * - DBG_MODE_QUERY : DBG_MODE_USERPERF + queries 78 * - DBG_MODE_QUERYTEMP : DBG_MODE_QUERY + included files 79 * - DBG_MODE_FULL : All available debug infos ( including $GLOBALS array that is quiet big ) 80 * - DBG_MODE_AUTO : Mode auto take the mode of Debug Object 81 * ( not implemented ) 82 */ 83 define ( 'DBG_MODE_OFF' , 0 ); 84 define ( 'DBG_MODE_USERPERF' , 1 ); 85 define ( 'DBG_MODE_QUERY' , 2 ); 86 define ( 'DBG_MODE_QUERYTEMP' , 3 ); 87 define ( 'DBG_MODE_FULL' , 4 ); 88 define ( 'DBG_MODE_AUTO' , 5); 89 define ( 'DBG_MODE_DEFAULT' , DBG_MODE_USERPERF); 90 91 /** 92 * This is a constant for the credits. For me :p 93 */ 94 define ( 'DBG_CREDITS' , '<b>== PHP_Debug | By COil (2003) | <a href="mailto:coil@strangebuzz.com">coil@strangebuzz.com</a></b> | <a href="http://sourceforge.net/projects/phpdebug/">PHP_Debug Project Home</a>'); 95 96 /** 97 * These are constant for DumpArr() and DumpObj() functions. 98 * 99 * - DUMP_ARR_DISP : Tell the functions to display the debug info. 100 * - DUMP_ARR_STR : Tell the fonction to return the debug info as a string 101 * - DBG_ARR_TABNAME : Default name of Array 102 * - DBG_ARR_OBJNAME : Default name of Object 103 */ 104 define ( 'DUMP_ARR_DISP' , 1 ); 105 define ( 'DUMP_ARR_STR' , 2 ); 106 define ( 'DUMP_ARR_TABNAME' , 'Array' ); 107 define ( 'DUMP_ARR_OBJNAME' , 'Object' ); 108 109 /** 110 * These are constants to define environment Super array 111 */ 112 define ( 'DBG_GLOBAL_GET' , 0 ); 113 define ( 'DBG_GLOBAL_POST' , 1 ); 114 define ( 'DBG_GLOBAL_FILES' , 2 ); 115 define ( 'DBG_GLOBAL_COOKIE' , 3 ); 116 define ( 'DBG_GLOBAL_REQUEST' , 4 ); 117 define ( 'DBG_GLOBAL_SESSION' , 5 ); 118 define ( 'DBG_GLOBAL_GLOBALS' , 6 ); 119 120 /** 121 * Debug : Class that provide a simple and fast way to debug a php application. 122 * 123 * Debug class allows you to debug all you need about your application 124 * Debug queries, process time, dump variable and much more... 125 * 126 * @package PHP_Debug 127 * @author COil, Loic Vernet <coil@strangebuzz.com> 128 * @version BETA 1.0 129 * @since 17 oct 2003 130 */ 131 class Debug 132 { 133 /** 134 * Debug Mode 135 * 136 * @var integer 137 * @access public 138 * @see DBG_MODE constants. 139 */ 140 var $DebugMode = DBG_MODE_USERPERF; 141 142 /** 143 * This is the array where debug line are. 144 * 145 * @var array $_DebugBuffer 146 * @access private 147 * @see DebugLine 148 */ 149 var $_DebugBuffer = array(); 150 151 /** 152 * Enable or disable Credits in debug infos. 153 * 154 * @var integer $DisableCredits 155 * @access public 156 * @see DebugLine 157 */ 158 var $DisableCredits = false; 159 160 /** 161 * HTML Start String 162 * 163 * Start string of HTML layout 164 * 165 * @var string $HtmlTableStart 166 * @access public 167 */ 168 var $HtmlTableStart = '<br><table cellspacing="0" cellpading="0" border="1" width="100%">'; 169 170 /** 171 * HTML end string to close HTML display for debug layout 172 * 173 * @var string $HtmlTableEnd 174 * @access public 175 */ 176 var $HtmlTableEnd = '</table>'; 177 178 /** 179 * Process perf status, 1 = a process is being running, 0 = no activity 180 * 181 * @var String $_ProcessPerfStatus 182 * @access private 183 */ 184 var $_ProcessPerfStatus = false; 185 186 /** 187 * General debug start time 188 * 189 * @var integer $_ProcessPerfStart 190 * @access private 191 */ 192 var $_ProcessPerfStartGen = 0; // Global Start Time 193 194 /** 195 * Debug Start time 196 * 197 * @var integer $_ProcessPerfStart 198 * @access private 199 */ 200 var $_ProcessPerfStart = 0; // Local Start Time 201 202 /** 203 * Debug End time 204 * 205 * @var integer $_ProcessPerfEnd 206 * @access private 207 */ 208 var $_ProcessPerfEnd = 0; 209 210 /** 211 * Global database process time 212 * 213 * @var integer $_DataPerfTotal 214 * @access private 215 */ 216 var $_DataPerfTotal = 0; 217 218 /** 219 * Number of performed queries 220 * 221 * @var integer $_DataPerfQry 222 * @access private 223 */ 224 var $_DataPerfQry = 0; 225 226 /** 227 * Enable or disable, included and required files 228 * 229 * @var boolean $ShowTemplates 230 * @access public 231 */ 232 var $ShowTemplates = true; 233 234 /** 235 * Enable or disable, pattern removing in included files 236 * 237 * @var boolean $RemoveTemplatesPattern 238 * @access public 239 */ 240 var $RemoveTemplatesPattern = false; 241 242 /** 243 * Pattern list to remove in the display of included files 244 * 245 * @var boolean $RemoveTemplatesPattern 246 * @access private 247 */ 248 var $_TemplatesPattern = array(); 249 250 /** 251 * Enable or disable $globals var in debug 252 * 253 * @var boolean $ShowGlobals 254 * @access public 255 */ 256 var $ShowGlobals = false; 257 258 /** 259 * Enable or disable search in debug 260 * 261 * @var boolean $EnableSearch 262 * @access public 263 */ 264 var $EnableSearch = true; 265 266 /** 267 * Enable or disable the use of $_REQUEST array instead of $_POST + _$GET + $_COOKIE + $_FILES 268 * 269 * @var boolean $UseRequestArr 270 * @access public 271 */ 272 var $UseRequestArr = false; 273 274 /** 275 * View Source script path 276 * 277 * @var string $ViewSourceScriptPath, default : Current directory 278 * @access public 279 */ 280 var $ViewSourceScriptPath = '.'; 281 282 /** 283 * View Source script path 284 * 285 * @var string $ViewSourceScripName 286 * @access public 287 */ 288 var $ViewSourceScriptName = 'source.php'; 289 290 /** 291 * Color for DebugType : $DebugType => Color Code of text 292 * 293 * @var array $CellColors 294 * @access public 295 */ 296 var $CellColors = array ( DBGLINE_STD => '#000000', 297 DBGLINE_QUERY => '#FFA500', 298 DBGLINE_QUERY_REL => '#228B22', 299 DBGLINE_ENV => '#FF0000', 300 DBGLINE_CURRENTFILE => '#000000', 301 DBGLINE_APPERROR => '#FF0000', 302 DBGLINE_CREDITS => '#000000', 303 DBGLINE_SEARCH => '#000000', 304 DBGLINE_OBJECT => '#000000', 305 DBGLINE_PROCESSPERF => '#000000', 306 DBGLINE_TEMPLATES => '#000000', 307 DBGLINE_PAGEACTION => '#708090', 308 DBGLINE_ARRAY => '#000000' ); 309 310 /** 311 * Bold style for DebugType : $DebugType => Bold Style 312 * 313 * @var array $CellBoldStatus 314 * @access public 315 */ 316 var $CellBoldStatus = array ( DBGLINE_STD => false, 317 DBGLINE_QUERY => true, 318 DBGLINE_QUERY_REL => false, 319 DBGLINE_ENV => false, 320 DBGLINE_CURRENTFILE => true, 321 DBGLINE_APPERROR => true, 322 DBGLINE_CREDITS => true, 323 DBGLINE_SEARCH => true, 324 DBGLINE_OBJECT => false, 325 DBGLINE_PROCESSPERF => false, 326 DBGLINE_TEMPLATES => false, 327 DBGLINE_PAGEACTION => true, 328 DBGLINE_ARRAY => false ); 329 330 /** 331 * Bold style for DebugType : $DebugType => Bold Style 332 * 333 * @var array $CellBoldStatus 334 * @access public 335 */ 336 var $DisplayTypeInSearch = array ( DBGLINE_STD => false , 337 DBGLINE_QUERY => false , 338 DBGLINE_QUERY_REL => false , 339 DBGLINE_ENV => false , 340 DBGLINE_CURRENTFILE => true , 341 DBGLINE_APPERROR => false , 342 DBGLINE_CREDITS => true , 343 DBGLINE_SEARCH => true , 344 DBGLINE_OBJECT => false , 345 DBGLINE_PROCESSPERF => true , 346 DBGLINE_TEMPLATES => false , 347 DBGLINE_PAGEACTION => false , 348 DBGLINE_ARRAY => false ); 349 350 /** 351 * Base URL of phpmyadmin 352 * 353 * @var string $PhpMyAdminUrl 354 * @access public 355 */ 356 var $PhpMyAdminUrl = 'http://127.0.0.1/mysql'; 357 358 /** 359 * Name of database that we are working on 360 * 361 * @var string $CurrentDatabase 362 * @access public 363 */ 364 var $DatabaseName = 'mysql'; 365 366 /** 367 * Debug() : Constructor of Debug object 368 * 369 * Set debugmode, credits line and search line are added at creation 370 * if they are activated. 371 * 372 * @param integer $debugmode 373 * 374 * @return mixed Debug Object 375 * 376 * @see Debug() 377 * @since 17 Oct 2003 378 * @access public 379 */ 380 function Debug($debugmode=DBG_MODE_DEFAULT) 381 { 382 $this->DebugMode = $debugmode; 383 $this->_ProcessPerfStartGen = $this->getMicroTime(microtime()); 384 385 // Credits line 386 if ( $this->DisableCredits == false ) 387 $this->addDebug(DBG_CREDITS,DBGLINE_CREDITS); 388 389 // Search line 390 if ( $this->EnableSearch == true ) 391 $this->addDebug(STR_N,DBGLINE_SEARCH); 392 393 } 394 395 /** 396 * setDebugMode() : Set debug mode of Debug Object 397 * 398 * @param integer $debugmode 399 * 400 * @see $DebugMode 401 * @since 14 Nov 2003 402 * @access public 403 */ 404 function setDebugMode($debugmode) 405 { 406 $this->DebugMode = $debugmode; 407 } 408 409 /** 410 * getDebugMode() : Return current debug mode 411 * 412 * @see $DebugMode 413 * @since 14 Nov 2003 414 * @access public 415 */ 416 function getDebugMode() 417 { 418 return($this->DebugMode); 419 } 420 421 /** 422 * getColorCode() : Retrieve color code of the debug cell 423 * 424 * @return string 425 * 426 * @see CellColor 427 * @since 25 Oct 2003 428 * @access public 429 */ 430 function getColorCode($DebugLineType) 431 { 432 return '<font color="'. $this->CellColors[$DebugLineType] . '">'; 433 } 434 435 /** 436 * getBoldCode() : Retrieve Bold cell status of the debug cell 437 * 438 * @return string 439 * 440 * @see CellBoldStatus 441 * @since 25 Oct 2003 442 * @access public 443 */ 444 function getBoldCode($DebugLineType) 445 { 446 return ( $this->CellBoldStatus[$DebugLineType] ) ? "<b>" : STR_N; 447 } 448 449 /** 450 * getMicroTime() : Return micotime from a timestamp 451 * 452 * @param $time Timestamp to retrieve micro time 453 * @return numeric Micotime of timestamp param 454 * 455 * @see $DebugMode 456 * @since 14 Nov 2003 457 * @access public 458 */ 459 function getMicroTime($time) 460 { 461 list($usec, $sec) = explode(" ",$time); 462 return ( (float)$usec + (float)$sec ); 463 } 464 465 /** 466 * getElapsedTime() : get elapsed time between 2 timestamp 467 * 468 * @param $timeStart Start time ref 469 * @param $timeEnd End time ref 470 * @return numeric difference between the two time ref 471 * 472 * @see getProcessTime() 473 * @since 20 Oct 2003 474 * @access public 475 */ 476 function getElapsedTime($timeStart, $timeEnd) 477 { 478 return round($timeEnd - $timeStart,4); 479 } 480 481 /** 482 * getProcessTime() : Get global process time 483 * 484 * @return numeric Elapsed time between the start and end time 485 * 486 * @see getElapsedTime() 487 * @since 20 Oct 2003 488 * @access public 489 */ 490 function getProcessTime() 491 { 492 return ( $this->getElapsedTime($this->_ProcessPerfStartGen,$this->_ProcessPerfEnd) ); 493 } 494 495 /** 496 * _StopProcessTime() : Fix the end time of process 497 * 498 * @since 17 Novt 2003 499 * @access private 500 */ 501 function _StopProcessTime() 502 { 503 $this->_ProcessPerfEnd = $this->getMicroTime(microtime()); 504 } 505 506 /** 507 * DumpArr() : Display all content of an array 508 * 509 * Mode DUMP_ARR_DISP display the array 510 * Mode DUMP_ARR_STR return the infos as a string 511 * 512 * @param array $arr array Array to debug 513 * @param string $varname Name of the variable 514 * @param integer $mode Mode of function 515 * @return mixed Nothing or string depending on the mode 516 * 517 * @since 20 Oct 2003 518 * @static 519 * @access public 520 */ 521 function DumpArr($arr, $varname=DUMP_ARR_TABNAME, $mode=DUMP_ARR_DISP) 522 { 523 ob_start(); 524 print_r($arr); 525 $dbg_arrbuffer = htmlentities(ob_get_contents()); 526 ob_end_clean(); 527 528 $dbg_arrbuffer = "<br><pre><b>$varname</b> :". CR . $dbg_arrbuffer . '</pre>'; 529 530 switch($mode) 531 { 532 default: 533 case DUMP_ARR_DISP: 534 print($dbg_arrbuffer); 535 case DUMP_ARR_STR: 536 return($dbg_arrbuffer); 537 break; 538 } 539 } 540 541 /** 542 * DumpObj() : Debug an object or array with Var_Dump pear package 543 * 544 * ( Not useable with standalone version ) 545 * Mode DUMP_ARR_DISP display the array 546 * Mode DUMP_ARR_STR return the infos as a string 547 * 548 * @param array $obj Object to debug 549 * @param string $varname Name of the variable 550 * @param integer $mode Mode of function 551 * @return mixed Nothing or string depending on the mode 552 * 553 * @since 10 Nov 2003 554 * @static 555 * @access public 556 */ 557 function DumpObj($obj, $varname=DUMP_ARR_OBJNAME, $mode=DUMP_ARR_DISP) 558 { 559 // Check Pear Activation 560 if (DBG_VERSION == DBG_VERSION_STANDALONE) 561 return Debug::DumpArr($obj, $varname, $mode); 562 563 ob_start(); 564 Var_Dump::display($obj); 565 $dbg_arrbuffer = ob_get_contents(); 566 ob_end_clean(); 567 568 if ( empty($varname) ) 569 $varname = DUMP_ARR_OBJNAME; 570 571 $dbg_arrbuffer = "<br><pre><b>$varname</b> :". CR . $dbg_arrbuffer . '</pre>'; 572 573 switch($mode) 574 { 575 default: 576 case DUMP_ARR_DISP: 577 print($dbg_arrbuffer); 578 case DUMP_ARR_STR: 579 return($dbg_arrbuffer); 580 break; 581 } 582 } 583 584 /** 585 * addDebug() : Build a new debug line info. 586 * 587 * If $str is a String or an object we switch automatically to the corresponding 588 * debug info type. If debug mode is OFF does not do anything and return. 589 * Debug line is build, then it is added in the DebugLine array. 590 * 591 * @param string $str debug string/object 592 * @param integer $typeDebug Debug type of line ( Optional, Default = DBGLINE_STD ) 593 * @param string $file File of debug info ( Optional, Default = "" ) 594 * @param string $line Line of debug info ( Optional, Default = "" ) 595 * @param string $title Title of variable if applicable ( Optional, Default = "" ) 596 * 597 * @since 10 Nov 2003 598 * @access public 599 */ 600 function addDebug($str, $typeDebug=DBGLINE_STD, $file = STR_N, $line = STR_N, $title=STR_N ) 601 { 602 if ($this->DebugMode == DBG_MODE_OFF ) 603 return; 604 605 // If argument is an array change debug type 606 if ( is_array($str) && $typeDebug == DBGLINE_STD) 607 $typeDebug = DBGLINE_ARRAY; 608 609 // If argument is an object change debug type 610 if ( is_object($str) && $typeDebug == DBGLINE_STD) 611 $typeDebug = DBGLINE_OBJECT; 612 613 // Query config for query debug line type 614 $PhpMyAdminUrl = ( $typeDebug == DBGLINE_QUERY ? $this->PhpMyAdminUrl : '' ); 615 $DatabaseName = ( $typeDebug == DBGLINE_QUERY ? $this->DatabaseName : '' ); 616 617 $DbgLine = new DebugLine( $str, $typeDebug, $file , $line, $title, 618 $this->getColorCode($typeDebug), 619 $this->getBoldCode($typeDebug), 620 $PhpMyAdminUrl, 621 $DatabaseName ); 622 $this->_DebugBuffer[] = $DbgLine; 623 } 624 625 /** 626 * DebugPerf() : Get process time and stats about database processing. 627 * 628 * If $processtype is DBG_PERF_QRY then a query has been run, otherwise it 629 * is another database process. The start and end time is computed, and the 630 * global time is updated. 631 * 632 * @param integer $processtype Type of database debug query or database related. 633 * 634 * @since 20 Oct 2003 635 * @access public 636 */ 637 function DebugPerf($processtype = DBGLINE_QUERY) 638 { 639 // Lang 640 $txtPHP = 'PHP'; 641 $txtSQL = 'SQL'; 642 $txtSECOND = 's'; 643 644 switch($this->_ProcessPerfStatus) 645 { 646 // Start Timer 647 default: 648 case false: 649 $this->_ProcessPerfStart = $this->getMicroTime(microtime()); 650 $this->_ProcessPerfStatus = true; 651 652 // Additional processing depending of dataperf type request 653 switch($processtype) 654 { 655 case(DBGLINE_QUERY): 656 $this->_DataPerfQry++; 657 break; 658 659 default: 660 break; 661 } 662 663 break; 664 665 // Stop Timer and add to database perf total 666 case true; 667 $this->_ProcessPerfEnd = $this->getMicroTime(microtime()); 668 $qry_time = $this->getElapsedTime($this->_ProcessPerfStart,$this->_ProcessPerfEnd); 669 670 $this->_ProcessPerfStart = $this->_ProcessPerfEnd = 0; 671 $this->_ProcessPerfStatus = false; 672 673 // Additional processing depending of dataperf type request 674 switch($processtype) 675 { 676 default: 677 case(DBGLINE_STD); 678 $this->_DebugBuffer[$this->_getLastDebugLineID($processtype)]->DebugDisplayString .= " <b><font color=\"black\">[ $txtPHP : ". $qry_time ."$txtSECOND ]</font></b>"; 679 break; 680 681 case(DBGLINE_QUERY_REL): 682 case(DBGLINE_QUERY): 683 //Now set the Time for the query in the DebugLine info 684 $this->_DebugBuffer[$this->_getLastDebugLineID($processtype)]->DebugDisplayString .= " <b><font color=\"black\">[ $txtSQL+$txtPHP : ". $qry_time ."$txtSECOND ]</font></b>"; 685 686 // Global database perf 687 $this->_DataPerfTotal += $qry_time; 688 break; 689 } 690 break; 691 } 692 } 693 694 /** 695 * CancelPerf() : Cancel a process time monitoring, error or misc exception 696 * 697 * @param Integer $processtype Type of the process to cancel 698 * 699 * @since 13 Dec 2003 700 * @access public 701 */ 702 function CancelPerf($processtype) 703 { 704 $this->_ProcessPerfStart = $this->_ProcessPerfEnd = 0; 705 $this->_ProcessPerfStatus = false; 706 707 switch($processtype) 708 { 709 case(DBGLINE_QUERY): 710 $this->_DataPerfQry--; 711 break; 712 713 default: 714 break; 715 } 716 } 717 718 /** 719 * getLastDebugLineID : Retrieve the ID of last debugline type in _DebugBuffer array 720 * 721 * @param integer $debugtype Type of debug we want to get the last index 722 * 723 * @see DebugPerf(), _DebugBuffer 724 * @since 20 Nov 2003 725 * @access private 726 */ 727 function _getLastDebugLineID($debugtype) 728 { 729 $tmparr = $this->_DebugBuffer; 730 krsort($tmparr); 731 732 foreach ( $tmparr as $lkey => $lvalue ) 733 { 734 if ( $lvalue->DebugType == $debugtype ) 735 return $lkey; 736 } 737 } 738 739 /** 740 * _IncludeRequiredFiles() : Build debug line with all included or required files for current file. 741 * 742 * Use the get_required_files() function, then build the formatted string with 743 * links to edit and to view source of each files. Debug info line is added in 744 * current debug object. 745 * 746 * @since 20 Oct 2003 747 * @access private 748 */ 749 function _IncludeRequiredFiles() 750 { 751 // Lang 752 $txtViewSource = 'View Source'; 753 $txtEditSource = 'Edit'; 754 $txtIncRecFiles = 'Included/Required files'; 755 756 $l_reqfiles = get_required_files(); 757 $l_strinc = "<b>== $txtIncRecFiles (". count($l_reqfiles) .') :</b>'. CR; 758 $l_strinc .= '<font color="#000080">'; 759 760 foreach( $l_reqfiles as $f_file ) 761 { 762 $view_source_link = $edit_link = $f_file; 763 764 // Pattern deletion 765 if ( $this->RemoveTemplatesPattern == true && count($this->_TemplatesPattern) ) 766 $f_file = strtr( $f_file, $this->_TemplatesPattern ); 767 768 $view_source_link = ' <a href="' . $this->ViewSourceScriptPath .'/'. $this->ViewSourceScriptName .'?script='. $view_source_link . '">'. $txtViewSource .'</a>'; 769 $edit_link = ' <a href="' . $edit_link . '">'. $txtEditSource .'</a>'; 770 771 $l_strinc .= $f_file . $view_source_link . $edit_link . CR; 772 } 773 $this->addDebug($l_strinc,DBGLINE_TEMPLATES); 774 } 775 776 /** 777 * addRequiredFilesPattern() : Add a remove pattern to remove pattern array. 778 * 779 * @param string $pattern Pattern to add 780 * 781 * @since 20 Oct 2003 782 * @access public 783 */ 784 function addRequiredFilesPattern($pattern, $replace_str=STR_N) 785 { 786 $this->_TemplatesPattern[$pattern] = $replace_str; 787 } 788 789 /** 790 * delRequiredFilesPattern() : Del a remove pattern from remove pattern array. 791 * 792 * @param string $pattern Pattern to remove 793 * 794 * @since 20 Oct 2003 795 * @access public 796 */ 797 function delRequiredFilesPattern($pattern) 798 { 799 unset($this->_TemplatesPattern[$pattern]); 800 } 801 802 /** 803 * addSuperArray() : Add a super array to the debug informations 804 * 805 * @see DBG_GLOBAL, DebugDisplay() 806 * @since 12 Dec 2003 807 * @access private 808 */ 809 function _addSuperArray($SuperArrayType) 810 { 811 // Lang 812 $txtVariable = "Var"; 813 $txtNoVariable = "NO VARIABLE"; 814 815 $NoVariable = " -- $txtNoVariable -- "; 816 817 switch($SuperArrayType) 818 { 819 case(DBG_GLOBAL_GET): 820 $SuperArray = $_GET; 821 $ArrayTitle = '_GET'; 822 $Title = "$ArrayTitle $txtVariable"; 823 break; 824 825 case(DBG_GLOBAL_POST): 826 $SuperArray = $_POST; 827 $ArrayTitle = '_POST'; 828 $Title = "$ArrayTitle $txtVariable"; 829 break; 830 831 case(DBG_GLOBAL_FILES): 832 $SuperArray = $_FILES; 833 $ArrayTitle = '_FILES'; 834 $Title = "$ArrayTitle $txtVariable"; 835 break; 836 837 case(DBG_GLOBAL_COOKIE): 838 $SuperArray = $_COOKIE; 839 $ArrayTitle = '_COOKIE'; 840 $Title = "$ArrayTitle $txtVariable"; 841 break; 842 843 case(DBG_GLOBAL_REQUEST): 844 $SuperArray = $_REQUEST; 845 $ArrayTitle = '_REQUEST'; 846 $Title = "$ArrayTitle $txtVariable ( _GET + _POST + _FILES + _COOKIE )"; 847 break; 848 849 case(DBG_GLOBAL_SESSION): 850 $SuperArray = $_SESSION; 851 $ArrayTitle = '_SESSION'; 852 $Title = "$ArrayTitle $txtVariable"; 853 break; 854 855 case(DBG_GLOBAL_GLOBALS): 856 $SuperArray = $GLOBALS; 857 $ArrayTitle = 'GLOBALS'; 858 $Title = "$ArrayTitle $txtVariable"; 859 break; 860 861 default: 862 break; 863 } 864 865 $SectionBasetitle = "<b>== $Title (". count($SuperArray,COUNT_RECURSIVE) .') :'; 866 if ( count($SuperArray,COUNT_RECURSIVE) ) 867 $this->addDebug($SectionBasetitle .'</b>'. $this->DumpArr($SuperArray,$ArrayTitle,DUMP_ARR_STR),DBGLINE_ENV); 868 else 869 $this->addDebug($SectionBasetitle ."$NoVariable</b>",DBGLINE_ENV); 870 } 871 872 /** 873 * _addProcessTime() : Add the process time information to the debug infos 874 * 875 * @see DBG_GLOBAL, DebugDisplay() 876 * @since 12 Dec 2003 877 * @access private 878 */ 879 function _addProcessTime() 880 { 881 // Lang 882 $txtExecutionTime = 'Execution Time Global'; 883 $txtPHP = 'PHP'; 884 $txtSQL = 'SQL'; 885 $txtSECOND = 's'; 886 $txtOneQry = 'Query'; 887 $txtMultQry = 'Queries'; 888 $txtQuery = ( $this->_DataPerfQry > 1 ) ? $txtMultQry : $txtOneQry; 889 890 // Performance Debug 891 $ProcessTime = $this->getProcessTime(); 892 $php_time = $ProcessTime - $this->_DataPerfTotal; 893 $sql_time = $this->_DataPerfTotal; 894 895 $php_percent = round(($php_time / $ProcessTime) * 100 ,2); 896 $sql_percent = round(($sql_time / $ProcessTime) * 100 ,2); 897 898 $this->addDebug("<b>== $txtExecutionTime : " . 899 $ProcessTime . "$txtSECOND [ $txtPHP , ". $php_time ."$txtSECOND , ". $php_percent .'% ] - '. 900 "[ $txtSQL , ". $sql_time ."$txtSECOND , ". $sql_percent .'% , '. $this->_DataPerfQry ." $txtQuery ]</b>",DBGLINE_PROCESSPERF); 901 } 902 903 /** 904 * HighLightKeyWords : Highligth a keyword in the debug info 905 */ 906 function HighLightKeyWords($SearchStr) 907 { 908 if ( !empty($SearchStr) and !empty($this->_DebugBuffer) ) 909 { 910 for( $i = 0 ; $i < count($this->_DebugBuffer) ; $i++ ) 911 { 912 if ( $this->DisplayTypeInSearch[$this->_DebugBuffer[$i]->DebugType] == false ) 913 { 914 if ( !is_array($this->_DebugBuffer[$i]->_DebugString) && !is_object($this->_DebugBuffer[$i]->_DebugString) ) 915 { 916 $this->_DebugBuffer[$i]->_DebugString = eregi_replace("$SearchStr","<font color=\"#FFA500\"><b>$SearchStr</b></font>",$this->_DebugBuffer[$i]->_DebugString); 917 // PHP5 : $this->_DebugBuffer[$i]->_DebugString = str_ireplace("$SearchStr","<font color=\"#FFA500\"><b>$SearchStr</b></font>",$this->_DebugBuffer[$i]->_DebugString); 918 $this->_DebugBuffer[$i]->_BuildDisplayString(); 919 } 920 } 921 } 922 } 923 } 924 925 /** 926 * DebugDisplay() : This is the funtcion to display debug infos 927 * 928 * @param string $search_str Search string ( Optional, default = "" ) 929 * @param integer $display_mode Mode of display ( DBG_MODE_AUTO ) 930 * 931 * @since 20 Oct 2003 932 * @access public 933 */ 934 function DebugDisplay($SearchSTR=STR_N, $display_mode=DBG_MODE_AUTO) 935 { 936 // Fix end time process the sooner possible 937 $this->_StopProcessTime(); 938 939 // No Display 940 if ( $display_mode == DBG_MODE_OFF ) 941 return; 942 // Fix display mode 943 else 944 if ( $display_mode == DBG_MODE_AUTO) 945 $display_mode = $this->DebugMode; 946 947 // HTML START 948 print($this->HtmlTableStart); 949 950 // Only DBG_MODE_USERPERF is implemented for now 951 switch($display_mode) 952 { 953 default: 954 case DBG_MODE_USERPERF: 955 // Process time debug informations 956 $this->_addProcessTime(); 957 958 // Include debug of included files 959 if ( $this->ShowTemplates == true ) 960 $this->_IncludeRequiredFiles(); 961 962 // Divide Request tab 963 if ( $this->UseRequestArr == false ) 964 { 965 // Include Post Var 966 $this->_addSuperArray(DBG_GLOBAL_POST); 967 968 // Include Get Var 969 $this->_addSuperArray(DBG_GLOBAL_GET); 970 971 // Include File Var 972 $this->_addSuperArray(DBG_GLOBAL_FILES); 973 974 // Include Cookie Var 975 $this->_addSuperArray(DBG_GLOBAL_COOKIE); 976 } 977 else 978 // Only display Request Tab 979 { 980 // Include Request Var 981 $this->_addSuperArray(DBG_GLOBAL_REQUEST); 982 } 983 984 // Include Sessions Var :Check if we have Session variables 985 if ( !empty($_SESSION) ) 986 $this->_addSuperArray(DBG_GLOBAL_SESSION); 987 988 // Include Globals Var 989 if ( $this->ShowGlobals == true ) 990 $this->_addSuperArray(DBG_GLOBAL_GLOBALS); 991 992 // Highlight Keywords 993 if ( !empty($SearchSTR) ) 994 $this->HighLightKeyWords($SearchSTR); 995 996 // Display Debug cells 997 foreach ( $this->_DebugBuffer as $lkey =>$lvalue ) 998 { 999 $bufstr = $lvalue->getDebugLineString(); 1000 1001 // Display only cell that contains the search string or in force display array 1002 $ShowDebugLine = false; 1003 1004 if ( !empty($SearchSTR) ) 1005 { 1006 // Check if Brut data is not an object or array 1007 $searchInto = ( is_array($lvalue->_DebugString) || is_object($lvalue->_DebugString) ? $this->DumpArr($lvalue->_DebugString,"",DUMP_ARR_STR) : $lvalue->_DebugString ); 1008 1009 // Search string found 1010 if ( stristr($searchInto, $SearchSTR) && $lvalue->DebugType) 1011 $ShowDebugLine = true; 1012 1013 // Forced debugline in search mode 1014 if ($this->DisplayTypeInSearch[$lvalue->DebugType] == true ) 1015 $ShowDebugLine = true; 1016 } 1017 else 1018 $ShowDebugLine = true; 1019 1020 if ( $ShowDebugLine == true ) 1021 print($bufstr); 1022 } 1023 break; 1024 } 1025 1026 // Close HTML Table 1027 print($this->HtmlTableEnd); 1028 } 1029 1030 /** 1031 * UniTtests() : Make the unit tests of the debug class 1032 * 1033 * @since 22 Nov 2003 1034 * @access public 1035 */ 1036 function UnitTests($fullmode = false) 1037 { 1038 $ClassName = get_class($this); 1039 $txtTitle = "Class $ClassName Unit Tests (debug.php)"; 1040 $Title = "======== $txtTitle"; 1041 1042 print('<pre><br><br>'); 1043 print('<a name=\"'. $ClassName .'\">'); 1044 print($Title); 1045 if ($fullmode == true) 1046 Debug::DumpObj($this, $ClassName, DUMP_ARR_DISP); 1047 print('<br><br></pre>'); 1048 } 1049 } 1050 1051 1052 /** 1053 * DEBUG LINE Types 1054 * 1055 * - DBGLINE_STD : Standart debug, fonctionnal or other 1056 * - DBGLINE_QUERY : Query debug 1057 * - DBGLINE_QUERY_REL : Database related debug 1058 * - DBGLINE_ENV : Environment debug ( $GLOBALS... ) 1059 * - DBGLINE_CURRENTFILE : Output current file that is debugged 1060 * - DBGLINE_APPERROR : Debug Error 1061 * - DBGLINE_CREDITS : Class Credits 1062 * - DBGLINE_SEARCH : Search mode in debug 1063 * - DBGLINE_OBJECT : Debug object mode 1064 * - DBGLINE_PROCESSPERF : Performance analysys 1065 * - DBGLINE_TEMPLATES : Debug included templates 1066 * - DBGLINE_PAGEACTION : Debug main page action 1067 * - DBGLINE_ARRAY : Debug array mode 1068 * 1069 * @category DebugLine 1070 */ 1071 define ( 'DBGLINE_STD' , 1 ); 1072 define ( 'DBGLINE_QUERY' , 2 ); 1073 define ( 'DBGLINE_QUERY_REL' , 3 ); 1074 define ( 'DBGLINE_ENV' , 4 ); 1075 define ( 'DBGLINE_CURRENTFILE' , 5 ); 1076 define ( 'DBGLINE_APPERROR' , 6 ); 1077 define ( 'DBGLINE_CREDITS' , 7 ); 1078 define ( 'DBGLINE_SEARCH' , 8 ); 1079 define ( 'DBGLINE_OBJECT' , 9 ); 1080 define ( 'DBGLINE_PROCESSPERF' , 10 ); 1081 define ( 'DBGLINE_TEMPLATES' , 11 ); 1082 define ( 'DBGLINE_PAGEACTION' , 12 ); 1083 define ( 'DBGLINE_ARRAY' , 13 ); 1084 define ( 'DBGLINE_DEFAULT' , DBGLINE_STD ); 1085 1086 /** 1087 * DBGLINE_ERRORALERT, default error message for DBGLINE_APPERROR debug line type. 1088 * 1089 */ 1090 define ( 'DBGLINE_ERRORALERT' , "/!\\" ); 1091 1092 /** 1093 * DebugLine : Class that describe a debug line inforlations 1094 * 1095 * Descive all info and methode for a debug line, file 1096 * location, color, type of debug, debug buffer, formatted debug buffer 1097 * title of debug variable if applicable 1098 * 1099 * @package PHP_Debug 1100 * @author COil, Loic Vernet <webmaster@strangebuzz.com> 1101 * @version BETA 1.0 1102 * @since 18 oct 2003 1103 */ 1104 class DebugLine 1105 { 1106 /** 1107 * File of debug info 1108 * 1109 * @var integer $_Fine 1110 * @access private 1111 */ 1112 var $_File = ''; 1113 1114 /** 1115 * Line of debug info 1116 * 1117 * @var integer $_Line 1118 * @access private 1119 */ 1120 var $_Line = ''; 1121 1122 /** 1123 * Complete Location ( formatted ) of debug infos ( Line + File ) 1124 * 1125 * @var integer $_Location 1126 * @access private 1127 */ 1128 var $_Location = ''; 1129 1130 /** 1131 * Title of debug line ( Object var ) 1132 * 1133 * @var String $_Linetitle 1134 * @see DumpObj() 1135 * @access private 1136 */ 1137 var $_LineTitle = ''; 1138 1139 /** 1140 * String that store non formatted debug info 1141 * 1142 * @var string $_DebugString 1143 * @access private 1144 */ 1145 var $_DebugString = ''; 1146 1147 /** 1148 * Formatted Debug info 1149 * 1150 * @var string $_DebugString 1151 * @access public 1152 */ 1153 var $DebugDisplayString = ''; 1154 1155 /** 1156 * Debug Type 1157 * 1158 * @var integer $DebugType 1159 * @see DBGLINE contants 1160 * @access public 1161 */ 1162 var $DebugType = DBGLINE_DEFAULT; 1163 1164 /** 1165 * Background Color for debug info cell 1166 * 1167 * @var array $CellColor 1168 * @access public 1169 */ 1170 var $CellColor = ''; 1171 1172 /** 1173 * Base URL of phpmyadmin 1174 * 1175 * @var string $PhpMyAdminUrl 1176 * @access public 1177 */ 1178 var $PhpMyAdminUrl = ''; 1179 1180 /** 1181 * Name of database that we are working on 1182 * 1183 * @var string $CurrentDatabase 1184 * @access public 1185 */ 1186 var $DatabaseName = ''; 1187 1188 /** 1189 * Bold style for debug info cell 1190 * 1191 * @var array $CellBoldStatus 1192 * @access public 1193 */ 1194 var $CellBoldStatus = false; 1195 1196 /** 1197 * Default Backgourd cell color 1198 * 1199 * @var string $DefaultCellBackColor 1200 * @access public 1201 */ 1202 var $DefaultCellBackColor = '#F8F8FF'; 1203 1204 /** 1205 * HTML Cell start code 1206 * 1207 * @var string $HtmlPreCell 1208 * @access public 1209 */ 1210 var $HtmlPreCell = '<tr><td><pre>'; 1211 1212 /** 1213 * HTML Cell end code 1214 * 1215 * @var string $HtmlPostCell 1216 * @access public 1217 */ 1218 var $HtmlPostCell = '</td></tr>'; 1219 1220 /** 1221 * DebugLine() Constructor of class 1222 * 1223 * _Location is Automatically created at object instantation. 1224 * Then the formatted debug HTML row is created. 1225 * 1226 * @param string $str Debug Information to store 1227 * @param integer $DebugType Type of debug information 1228 * @param string $file File of debug information 1229 * @param string $line Debug of debug information 1230 * @param string $title Title of debuged var 1231 * 1232 * @return mixed DebugLine Object 1233 * 1234 * @see _BuildDebugLineLocation() 1235 * @since 17 Oct 2003 1236 * @access public 1237 */ 1238 function DebugLine($str, $DebugType, $file, $line, $title, $CellColor, $CellBoldStatus, $PhpMyAdminUrl, $DatabaseName) 1239 { 1240 $this->_DebugString = $str; 1241 $this->DebugType = $DebugType; 1242 $this->_File = $file; 1243 $this->_Line = $line; 1244 $this->_LineTitle = $title; 1245 $this->CellColor = $CellColor; 1246 $this->CellBoldStatus = $CellBoldStatus; 1247 $this->PhpMyAdminUrl = $PhpMyAdminUrl; 1248 $this->DatabaseName = $DatabaseName; 1249 1250 $this->_Location = $this->_BuildDebugLineLocation($file,$line); 1251 $this->_BuildHtmlPreCell(); 1252 $this->_BuildDisplayString(); 1253 } 1254 1255 /** 1256 * _BuildDisplayString() : Builds the formatted debug line 1257 * 1258 * Depending on the DebugType the formatted debug line is build. 1259 * DebugDisplayString is built. 1260 * One case by debug type. 1261 * 1262 * @see DebugType 1263 * @since 20 Oct 2003 1264 * @access private 1265 */ 1266 function _BuildDisplayString() 1267 { 1268 switch($this->DebugType) 1269 { 1270 // Standart output 1271 case 1: 1272 $this->DebugDisplayString = $this->_DebugString; 1273 break; 1274 1275 // Query 1276 case 2: 1277 $txtExplain = 'Explain'; 1278 $txtQuery = 'Query'; 1279 1280 $basehtml = ' </b><a target="phpmyadmin" href="'; 1281 $url_query = $this->PhpMyAdminUrl .'/read_dump.php'; 1282 $url_query .= '?is_js_confirmed=0&lang=fr&server=1&db='. $this->DatabaseName .'&pos=0&goto=db_details.php&zero_rows=&prev_sql_query=&sql_file=&sql_query='; 1283 $url_explain = $url_query .'explain '. urlencode($this->_DebugString); 1284 $url_query = $url_query . urlencode($this->_DebugString); 1285 1286 $this->DebugDisplayString = preg_replace('/\s+/',' ',$this->_DebugString); 1287 1288 // Explain Link only for select Queries. 1289 if ( stristr($this->_DebugString,'select') ) 1290 $this->DebugDisplayString .= $basehtml. $url_explain ."\">$txtExplain</a>"; 1291 1292 // Query Link 1293 $this->DebugDisplayString .= $basehtml. $url_query."\">$txtQuery</a>"; 1294 break; 1295 1296 // Database Related 1297 case 3: 1298 $this->DebugDisplayString = $this->_DebugString; 1299 break; 1300 1301 // Environnment Related 1302 case 4: 1303 $this->DebugDisplayString = $this->_DebugString; 1304 break; 1305 1306 // Current File 1307 case 5: 1308 $txtCurrentFile = 'Current File'; 1309 $this->DebugDisplayString = "<b>« $txtCurrentFile</b>"; 1310 break; 1311 1312 // App Error 1313 case 6: 1314 $this->DebugDisplayString = DBGLINE_ERRORALERT .' '. $this->_DebugString .' '. DBGLINE_ERRORALERT; 1315 break; 1316 1317 // Credits 1318 case 7: 1319 $this->DebugDisplayString = $this->_DebugString; 1320 break; 1321 1322 // Search Debug 1323 case 8: 1324 // To do, reposter toues les données qu'on a de dispo 1325 $txtSearchInDebug = 'Search in Debug Infos'; 1326 $this->DebugDisplayString = "<b><pre>== $txtSearchInDebug : ". '<form action="'. $_SERVER['PHP_SELF'] .'"><input name="DBG_SEARCH" value="'. (isset($_REQUEST["DBG_SEARCH"]) ? $_REQUEST["DBG_SEARCH"] : "") .'"><input type="SUBMIT" value="Go !"></form>'; 1327 break; 1328 1329 // Object Debug 1330 case 9: 1331 $obj_title = (empty($this->_LineTitle)) ? get_class($this->_DebugString) : $this->_LineTitle ; 1332 $this->DebugDisplayString = Debug::DumpObj($this->_DebugString,$obj_title,DUMP_ARR_STR); 1333 break; 1334 1335 // Process Perf 1336 case 10; 1337 $this->DebugDisplayString = $this->_DebugString; 1338 break; 1339 1340 // Temlates 1341 case 11; 1342 $this->DebugDisplayString = $this->_DebugString; 1343 break; 1344 1345 // Main Page Action 1346 case 12; 1347 $txtPageAction = 'Page Action'; 1348 $this->DebugDisplayString = " [ $txtPageAction : ". $this->_DebugString .' ]'; 1349 break; 1350 1351 // Array Debug 1352 case 13: 1353 $this->DebugDisplayString = Debug::DumpArr($this->_DebugString,$this->_LineTitle,DUMP_ARR_STR); 1354 break; 1355 } 1356 } 1357 1358 /** 1359 * _BuildHtmlPreCell() : Build HTML pre cell with backgroud attributes 1360 * 1361 * @since 11 Dec 2003 1362 * @see DefaultCellBackColor, HtmlPreCell 1363 * @access private 1364 */ 1365 function _BuildHtmlPreCell() 1366 { 1367 $this->HtmlPreCell = '<tr bgcolor="'. $this->DefaultCellBackColor .'"><td><pre>'; 1368 } 1369 1370 /** 1371 * getPhpMyAdminUrl() : Return url of PhpmyAdmin 1372 * 1373 * @return string PhpMyAdminUrl 1374 * 1375 * @see PhpMyAdminUrl 1376 * @since 25 Oct 2003 1377 * @access public 1378 */ 1379 function getPhpMyAdminUrl() 1380 { 1381 return $this->PhpMyAdminUrl; 1382 } 1383 1384 /** 1385 * setPhpMyAdminUrl() : Set the url of PhpmyAdmin 1386 * 1387 * @param string URL OF phpmyadmin 1388 * 1389 * @see PhpMyAdminUrl 1390 * @since 14 Nov 2003 1391 * @access public 1392 */ 1393 function setPhpMyAdminUrl($phpmyadmin_url) 1394 { 1395 $this->PhpMyAdminUrl = $phpmyadmin_url; 1396 } 1397 1398 /** 1399 * getDebugLineString() : Return Formated debug infos 1400 * 1401 * @return string The formatted string 1402 * 1403 * @since 25 Oct 2003 1404 * @access public 1405 */ 1406 function getDebugLineString() 1407 { 1408 return $this->HtmlPreCell. 1409 $this->_Location. 1410 $this->CellColor. 1411 $this->CellBoldStatus. 1412 $this->DebugDisplayString. 1413 $this->HtmlPostCell; 1414 } 1415 1416 /** 1417 * _BuildDebugLineLocation() : Retrieve Localisation of debug info 1418 * 1419 * Check is $file and $line, build the location with available 1420 * datas, if nothing return a default Info message. 1421 * 1422 * @param string $file File of debug info 1423 * @param string $line Line number of debug info 1424 * 1425 * @return string The formatted location [file,line] 1426 * 1427 * @since 25 Oct 2003 1428 * @access private 1429 */ 1430 function _BuildDebugLineLocation($file,$line) 1431 { 1432 // Lang 1433 $txtNoLocation = 'NO LOC'; 1434 1435 $l_dbgloc = STR_N; 1436 1437 if ( !empty($file) ) 1438 $l_dbgloc .= basename($file); 1439 1440 if ( !empty($line) ) 1441 { 1442 if ( !empty($l_dbgloc) ) 1443 $l_dbgloc .= ','; 1444 1445 $l_dbgloc .= $line; 1446 } 1447 1448 if ( !empty($l_dbgloc) ) 1449 $l_dbgloc = '[' . $l_dbgloc . ']'; 1450 else 1451 { 1452 if ($this->DebugType != DBGLINE_CREDITS && 1453 $this->DebugType != DBGLINE_SEARCH && 1454 $this->DebugType != DBGLINE_ENV && 1455 $this->DebugType != DBGLINE_PROCESSPERF && 1456 $this->DebugType != DBGLINE_TEMPLATES ) 1457 $l_dbgloc = "[-$txtNoLocation-]"; 1458 } 1459 return $l_dbgloc; 1460 } 1461 } 1462 ?>
|