PHP_Debug
[ class tree: PHP_Debug ] [ index: PHP_Debug ] [ all elements ]

Source for file Debug.php

Documentation is available at Debug.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * PHP_Debug : A simple and fast way to debug your PHP code
  7.  * 
  8.  * The basic purpose of PHP_Debug is to provide assistance in debugging PHP
  9.  * code, by 'debug' i don't mean 'step by step debug' but program trace,
  10.  * variables display, process time, included files, queries executed, watch
  11.  * variables... These informations are gathered through the script execution and
  12.  * therefore are displayed at the end of the script (in a nice floating div or a
  13.  * html table) so that it can be read and used at any moment. (especially
  14.  * usefull during the development phase of a project or in production with a
  15.  * secure key/ip)
  16.  *
  17.  * PHP version 5 only
  18.  *
  19.  * Copyright (c) 2007 - Vernet Loïc
  20.  
  21.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  22.  * of this software and associated documentation files (the "Software"), to deal
  23.  * in the Software without restriction, including without limitation the rights
  24.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25.  * copies of the Software, and to permit persons to whom the Software is
  26.  * furnished to do so, subject to the following conditions:
  27.  * 
  28.  * The above copyright notice and this permission notice shall be included in
  29.  * all copies or substantial portions of the Software.
  30.  * 
  31.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  37.  * THE SOFTWARE.
  38.  * 
  39.  * @category   PHP
  40.  * @package    PHP_Debug
  41.  * @author     Vernet Loïc <qrf_coil[at]yahoo.fr>
  42.  * @copyright  1997-2007 The PHP Group
  43.  * @license    http://www.opensource.org/licenses/mit-license.php MIT
  44.  * @link       http://pear.php.net/package/PHP_Debug
  45.  * @link       http://phpdebug.sourceforge.net
  46.  * @link       http://www.php-debug.com
  47.  * @see          Text_Highlighter
  48.  * @see        Var_Dump, SQL_Parser
  49.  * @since      1.0.0RC1
  50.  * @version    CVS: $Id: Debug.php,v 1.6 2009/01/12 21:13:00 c0il Exp $
  51.  */
  52.  
  53. /**
  54.  * Factory class for renderer of Debug class
  55.  * 
  56.  * @see Debug/Renderer/*.php
  57.  */
  58. require_once 'PHP/DebugLine.php';
  59. require_once 'PHP/Debug/Renderer.php';
  60.  
  61. /**
  62.  * External constants
  63.  * 
  64.  * @filesource
  65.  * @package PHP_Debug
  66.  */
  67. if (!defined('CR')) 
  68.     define('CR'"\n");
  69. }
  70.  
  71. class PHP_Debug
  72. {
  73.  
  74.     /**
  75.      * Possible version of class Debug
  76.      */ 
  77.     const VERSION_STANDALONE = 0;
  78.     const VERSION_PEAR       = 1;
  79.     const VERSION_DEFAULT    = self::VERSION_STANDALONE;
  80.     const VERSION            = self::VERSION_STANDALONE;
  81.     const RELEASE            = 'V2.1.5';
  82.     const PEAR_RELEASE       = 'V1.0.3';
  83.  
  84.     /**
  85.      * These are constant for dump() and DumpObj() functions.
  86.      * 
  87.      * - DUMP_DISP : Tell the function to display the debug info.
  88.      * - DUMP_STR  : Tell the fonction to return the debug info as a string
  89.      * - DUMP_VARNAME : Default name of Array - DBG_ARR_OBJNAME : Default name
  90.      * of Object
  91.      */
  92.     const DUMP_DISP = 1;
  93.     const DUMP_STR  = 2;
  94.     const DUMP_VARNAME = 'Variable';
  95.  
  96.     /**
  97.      * These are constant for addDebug functions, they set the behaviour where
  98.      * the function should add the debug information in first or in last
  99.      * position
  100.      */
  101.     const POSITIONLAST =  0;
  102.     const POSITIONFIRST = 1;
  103.  
  104.     /**
  105.      * These are constants to define Super array environment variables
  106.      */ 
  107.     const GLOBAL_GET     = 0;
  108.     const GLOBAL_POST    = 1;
  109.     const GLOBAL_FILES   = 2;
  110.     const GLOBAL_COOKIE  = 3;
  111.     const GLOBAL_REQUEST = 4;
  112.     const GLOBAL_SESSION = 5;
  113.     const GLOBAL_GLOBALS = 6;
  114.  
  115.     /**
  116.      * Default configuration options
  117.      * 
  118.      * @since V2.0.0 - 16 apr 2006
  119.      * @see setOptions()
  120.      * @var array 
  121.      */
  122.     protected $defaultOptions = array(
  123.         'render_mode'          => 'Div',              // Renderer mode
  124.         'render_type'          => 'HTML',             // Renderer type
  125.         'restrict_access'      => false,              // Restrict or not the access
  126.         'allowed_ip'           => array('127.0.0.1')// Authorized IP to view the debug when restrict_access is true
  127.         'allow_url_access'     => false,              // Allow to access the debug with a special parameter in the url
  128.         'url_key'              => 'debug',            // Key for url instant access
  129.         'url_pass'             => 'true',             // Password for url instant access
  130.         'enable_watch'         => false,              // Enable the watch function
  131.         'replace_errorhandler' => true,               // Replace or no the PHP errorhandler
  132.         'lang'                 => 'EN',               // Language
  133.     );
  134.  
  135.     /**
  136.      * Default static options for static functions
  137.      *
  138.      * @since V2.0.0 - 16 apr 2006
  139.      * @see dump()
  140.      * @var array 
  141.      */
  142.     protected static $staticOptions = array(
  143.         'dump_method'          => 'print_r',          // print_r or var_dump
  144.         'pear_var_dump_method' => 'Var_Dump::display' // Var_Dump display funtion (not used for now)
  145.     );
  146.  
  147.     /**
  148.      * Functions from this class that must be excluded in order to have the
  149.      * correct backtrace information
  150.      *
  151.      * @see PHP_DebugLine::setTraceback()
  152.      * @since V2.0.0 - 13 apr 2006
  153.      * @var array 
  154.      */
  155.     public static $excludedBackTraceFunctions = array(
  156.         'add'
  157.         'dump',
  158.         'error'
  159.         'query'
  160.         'addDebug'
  161.         'setAction'
  162.         'addDebugFirst',
  163.         'watchesCallback',
  164.         'errorHandlerCallback'
  165.     );
  166.  
  167.     /**
  168.      * Correspondance between super array constant and variable name
  169.      * Used by renderers
  170.      *
  171.      * @since V2.0.0 - 18 apr 2006
  172.      * @var array 
  173.      */
  174.     public static $globalEnvConstantsCorresp = array(  
  175.         self::GLOBAL_GET    => '_GET',
  176.         self::GLOBAL_POST   => '_POST',
  177.         self::GLOBAL_FILES  => '_FILES',
  178.         self::GLOBAL_COOKIE => '_COOKIE',
  179.         self::GLOBAL_REQUEST=> '_REQUEST',
  180.         self::GLOBAL_SESSION=> '_SESSION',
  181.         self::GLOBAL_GLOBALS=> 'GLOBALS'
  182.     );
  183.  
  184.     /**
  185.      * Default configuration options
  186.      *
  187.      * @since V2.0.0 - 13 apr 2006
  188.      * @see setOptions()
  189.      * @var array 
  190.      */
  191.     protected $options = array();
  192.  
  193.     /**
  194.      * This is the array where the debug lines are collected.
  195.      *
  196.      * @since V2.0.0 - 11 apr 2006
  197.      * @see DebugLine
  198.      * @var array 
  199.      */
  200.     protected  $debugLineBuffer = array();
  201.     
  202.     /**
  203.      * This is the array containing all the required/included files of the
  204.      * script
  205.      *
  206.      * @since V2.0.0 - 17 apr 2006
  207.      * @see render(), PHP_DebugLine::TYPE_TEMPLATES
  208.      * @var array 
  209.      */    
  210.     protected $requiredFiles = array();
  211.  
  212.     /**
  213.      * This is the array containing all the watched variables
  214.      *
  215.      * @since V2.0.0 - 16 apr 2006
  216.      * @see watch()
  217.      * @var array 
  218.      */    
  219.     protected $watches = array();
  220.     
  221.     /** 
  222.      * Execution start time
  223.      * 
  224.      * @since V2.0.0 - 11 apr 2006
  225.      * @see __construct()
  226.      * @var float 
  227.      */
  228.     protected $startTime;
  229.         
  230.     /** 
  231.      * Exection end time
  232.      * 
  233.      * @since V2.0.0 - 11 apr 2006
  234.      * @see render()
  235.      * @var float 
  236.      */
  237.     protected $endTime;
  238.     
  239.     /** 
  240.      * Number of queries executed during script
  241.      * 
  242.      * @since V2.0.0 - 19 apr 2006
  243.      * @var integer 
  244.      */
  245.     protected $queryCount = 0;
  246.  
  247.     /**
  248.      * PHP_Debug class constructor
  249.      * 
  250.      * Here we set :
  251.      * - the execution start time
  252.      * - the options
  253.      * - the error and watch call back functions
  254.      * 
  255.      * @param array $options    Array containing options to affect to Debug
  256.      *                           object and his childs
  257.      * 
  258.      * @since V2.0.0 - 11 apr 2006
  259.      */
  260.     function __construct($options = array())
  261.     {
  262.         $this->startTime = PHP_Debug::getMicroTimeNow();
  263.         $this->options array_merge($this->defaultOptions$options);
  264.         $this->setWatchCallback();
  265.         $this->setErrorHandler();
  266.     }
  267.  
  268.     /**
  269.      * Add a debug information
  270.      *
  271.      * @param string  $info  The main debug information
  272.      *                       (may be empty for some debug line types)
  273.      * @param integer $type Type of the DebugLine
  274.      * 
  275.      * @see Debug constants
  276.      * @since V1.0.0 - 07 Apr 2006
  277.      */     
  278.     public function addDebug($info$type = PHP_DebugLine::TYPE_STD
  279.         $position = self::POSITIONLAST)
  280.     {        
  281.         // Add info
  282.         $debugLine = new PHP_DebugLine($info$type);
  283.         if ($position == self::POSITIONLAST{        
  284.             $this->debugLineBuffer[$debugLine;
  285.         else {
  286.             array_unshift($this->debugLineBuffer$debugLine);
  287.         }
  288.         
  289.         // Additional process for some types
  290.         switch ($type{
  291.             case PHP_DebugLine::TYPE_QUERY:
  292.                 $this->queryCount++;
  293.                 break;
  294.         
  295.             default:
  296.                 break;
  297.         }
  298.  
  299.         // Return debugline
  300.         return $debugLine;
  301.     }
  302.  
  303.     /**
  304.      * Add a debug info before all the existing other debug lines
  305.      * It is an alias for addDebug($info, self::POSITIONLAST)
  306.      * 
  307.      * @see addDebug
  308.      * @since V1.0.0 - 13 Apr 2006
  309.      */
  310.     public function addDebugFirst($info$type = PHP_DebugLine::TYPE_STD)
  311.     {
  312.         return $this->addDebug($info$typeself::POSITIONFIRST);
  313.     }
  314.  
  315.     /**
  316.      * This is an alias for the addDebug function
  317.      *
  318.      * @see addDebug()
  319.      * @since  V2.0.0 - 20 apr 2006
  320.      */
  321.     public function add($info$type = PHP_DebugLine::TYPE_STD)
  322.     {
  323.         return $this->addDebug($info$type);
  324.     }
  325.  
  326.     /**
  327.      * This is an alias for the addDebug function when wanting to add a query
  328.      * debug information
  329.      * 
  330.      * @see addDebug(), PHP_DebugLine::TYPE_QUERY
  331.      * @since V2.0.0 - 21 Apr 2006
  332.      */
  333.     public function query($qry)
  334.     {
  335.         return $this->addDebug($qryPHP_DebugLine::TYPE_QUERY);
  336.     }
  337.  
  338.     /**
  339.      * This is an alias for the addDebug function when wanting to add a
  340.      * database related debug info
  341.      * 
  342.      * @see addDebug(), PHP_DebugLine::TYPE_QUERYREL
  343.      * @since V2.1.0 - 3 apr 2007
  344.      */
  345.     public function queryRel($info)
  346.     {
  347.         return $this->addDebug($infoPHP_DebugLine::TYPE_QUERYREL);
  348.     }
  349.  
  350.     /**
  351.      * This is an alias for the addDebug function when wanting to add an
  352.      * application error
  353.      * 
  354.      * @see addDebug(), PHP_DebugLine::TYPE_APPERROR
  355.      * @since V2.0.0 - 21 Apr 2006
  356.      */
  357.     public function error($info)
  358.     {
  359.         return $this->addDebug($infoPHP_DebugLine::TYPE_APPERROR);
  360.     }
  361.  
  362.     /**
  363.      * This is an alias for adding the monitoring of processtime
  364.      * 
  365.      * @see addDebug(), PHP_DebugLine::TYPE_PROCESSPERF
  366.      * @since V2.1.0 - 21 Apr 2006
  367.      */
  368.     public function addProcessPerf()
  369.     {
  370.         return $this->addDebug(''PHP_DebugLine::TYPE_PROCESSPERF);
  371.     }
  372.  
  373.    /**
  374.      * This a method to dump the content of any variable and add the result in
  375.      * the debug information
  376.      * 
  377.      * @param   mixed       $var        Variable to dump
  378.      * @param   string      $varname    Name of the variable
  379.      * 
  380.      * @since V2.0.0 - 25 Apr 2006
  381.      */  
  382.     public function dump($obj$varName '')
  383.     {
  384.         $info[$varName;
  385.         $info[$obj;
  386.         return $this->addDebug($infoPHP_DebugLine::TYPE_DUMP);
  387.     }
  388.  
  389.     /**
  390.      * Set the main action of PHP script
  391.      * 
  392.      * @param string $action Name of the main action of the file
  393.      * 
  394.      * @since V2.0.0 - 25 Apr 2006
  395.      * @see PHP_DebugLine::TYPE_CURRENTFILE
  396.      */  
  397.     public function setAction($action)    
  398.     {
  399.         $this->add($actionPHP_DebugLine::TYPE_PAGEACTION);
  400.     }
  401.  
  402.     /**
  403.      * Add an application setting
  404.      * 
  405.      * @param string $action Name of the main action of the file
  406.      * 
  407.      * @since V2.1.0 - 02 Apr 2007
  408.      * @see PHP_DebugLine::TYPE_ENV
  409.      */  
  410.     public function addSetting($value$name)
  411.     {
  412.         $this->add($name': '$valuePHP_DebugLine::TYPE_ENV);
  413.     }
  414.  
  415.     /**
  416.      * Add a group of settings
  417.      * 
  418.      * @param string $action Name of the main action of the file
  419.      * 
  420.      * @since V2.1.0 - 2 Apr 2007
  421.      * @see PHP_DebugLine::TYPE_ENV
  422.      */  
  423.     public function addSettings($values$name)
  424.     {
  425.         $this->add($name': '
  426.             PHP_Debug::dumpVar(
  427.                 $values
  428.                 $name
  429.                 false
  430.                 PHP_Debug::DUMP_STR
  431.             )
  432.             PHP_DebugLine::TYPE_ENV
  433.         );
  434.     }
  435.  
  436.     /**
  437.      * Set the callback fucntion to process the watches, enabled depending of
  438.      * the options flag 'enable_watch'
  439.      * 
  440.      * @since V2.0.0 - 16 apr 2006
  441.      * @see options, watches, watchesCallback()
  442.      */
  443.     protected function setWatchCallback()
  444.     {
  445.         if ($this->options['enable_watch'== true{
  446.             if (count($this->watches=== 0{
  447.                 $watchMethod = array($this'watchesCallback');
  448.                 register_tick_function($watchMethod);
  449.             }
  450.         }
  451.     }
  452.  
  453.     /**
  454.      * Set the callback function to process replace the php error handler,
  455.      * enabled depending of the options flag 'replace_errorhandler'
  456.      * 
  457.      * @since V2.0.0 - 16 apr 2006
  458.      * @see options, errorHandlerCallback()
  459.      */
  460.     protected function setErrorHandler()
  461.     {
  462.         if ($this->options['replace_errorhandler'== true{
  463.  
  464.             $errorhandler = array(
  465.                 $this,
  466.                 'errorHandlerCallback'
  467.             );
  468.             set_error_handler($errorhandler);
  469.         }
  470.     }
  471.  
  472.     /**
  473.      * Callback function for php error handling
  474.      * 
  475.      * Warning : the only PHP error codes that are processed by this user
  476.      * handler are : E_WARNING, E_NOTICE, E_USER_ERROR
  477.      * For the other error codes the standart php handler will be used
  478.      *
  479.      * @since V2.0.0 - 17 apr 2006
  480.      * @see options, setErrorHandler()
  481.      */
  482.     public function errorHandlerCallback(
  483.     {
  484.         $details func_get_args();
  485.         $popNumber = 3;
  486.  
  487.         // We already have line & file with setBackTrace function
  488.         for ($index = 0; $index $popNumber$index++{
  489.           array_pop($details);    
  490.         }
  491.         
  492.         if ($details[0!= E_STRICT)                            
  493.             $this->addDebug($detailsPHP_DebugLine::TYPE_PHPERROR);
  494.     }
  495.  
  496.     /**
  497.      * Add a variable to the watchlist. Watched variables must be in a declare
  498.      * (ticks=n) block so that every n ticks the watched variables are checked
  499.      * for changes. If any changes were made, the new value of the variable is
  500.      * recorded
  501.      * 
  502.      * @param string $variableName      Variable to watch
  503.      * @since V2.0.0 - 17 apr 2006
  504.      * @see watchesCallback()
  505.      */
  506.     public function watch($variableName
  507.     {   
  508.         if ($this->options['enable_watch'== true{
  509.             if (isset($GLOBALS[$variableName])) {
  510.                 $this->watches[$variableName$GLOBALS[$variableName];
  511.             else {
  512.                 $this->watches[$variableName= null;
  513.             }
  514.         else {
  515.             throw new Exception('The Watch function is disabled please set the option \'enable_watch\' to \'true\' to be able to use this feature, it\'s stable with a Unix server');
  516.         }
  517.     }
  518.  
  519.     /**
  520.      * Watch callback function, process watches and add changes to the debug
  521.      * information
  522.      * 
  523.      * @since V2.0.0 - 17 apr 2006
  524.      * @see watch()
  525.      */
  526.     public function watchesCallback(
  527.     {
  528.         // Check if there are variables to watch
  529.         if (count($this->watches)) {
  530.             foreach ($this->watches as $variableName => $variableValue{
  531.                 if ($GLOBALS[$variableName!== $this->watches[$variableName]{
  532.  
  533.                     $info = array(
  534.                         $variableName,
  535.                         $this->watches[$variableName],
  536.                         $GLOBALS[$variableName]
  537.                     );
  538.                                         
  539.                     $this->watches[$variableName$GLOBALS[$variableName];
  540.                     $this->addDebug($infoPHP_DebugLine::TYPE_WATCH);
  541.                 }
  542.             }
  543.         }
  544.     }
  545.  
  546.     /**
  547.      * Get global process time
  548.      * 
  549.      * @return  float             Execution process time of the script
  550.      * 
  551.      * @see getElapsedTime()
  552.      * @since V2.0.0 - 21 Apr 2006
  553.      */ 
  554.     public function getProcessTime()
  555.     {
  556.         return $this->getElapsedTime($this->startTime$this->endTime);
  557.     }
  558.  
  559.     /**
  560.      * Get database related process time
  561.      * 
  562.      * @return  float      Execection process time of the script for all
  563.      *                         database    specific tasks
  564.      * 
  565.      * @see PHP_DebugLine::TYPE_QUERY, PHP_DebugLine::TYPE_QUERYREL
  566.      * @since V2.0.0 - 21 Apr 2006
  567.      */ 
  568.     public function getQueryTime()
  569.     {
  570.           $queryTime = 0;        
  571.         
  572.         foreach($this->debugLineBuffer as $lkey => $lvalue)  {
  573.             $properties $lvalue->getProperties();
  574.             if ($properties['type'== PHP_DebugLine::TYPE_QUERY 
  575.              || $properties['type'== PHP_DebugLine::TYPE_QUERYREL{
  576.                 if (!empty($properties['endTime'])) {
  577.                     $queryTime $queryTime 
  578.                         $this->getElapsedTime(
  579.                             $properties['startTime']
  580.                             $properties['endTime']);
  581.                 }
  582.             }
  583.         }
  584.         return $queryTime;
  585.     }
  586.  
  587.     /**
  588.      * PHP_Debug default output function, first we finish the processes and
  589.      * then a render object is created and its render method is invoked
  590.      * 
  591.      * The renderer used is set with the options, all the possible renderer
  592.      * are in the directory Debug/Renderer/*.php
  593.      * (not the files ending by '_Config.php')
  594.      * 
  595.      * @since V2.0.0 - 13 apr 2006
  596.      * @see Debug_Renderer
  597.      */
  598.     public function render()
  599.     {
  600.         // Finish process
  601.         $this->endTime = PHP_Debug::getMicroTimeNow();
  602.  
  603.         // Render output if we are allowed to
  604.         if ($this->isAllowed()) {
  605.  
  606.             // Create render object and invoke its render function
  607.             $renderer PHP_Debug_Renderer::factory($this$this->options);
  608.     
  609.             // Get required files here to have event all Debug classes
  610.             $this->requiredFiles get_required_files();
  611.     
  612.             // Call rendering
  613.             return $renderer->render();
  614.         }
  615.     }
  616.  
  617.     /**
  618.      * Alias for the render function
  619.      * 
  620.      * @since V2.0.0 - 17 apr 2006
  621.      * @see render()
  622.      */
  623.     public function display()
  624.     {
  625.         echo $this->render();
  626.     }
  627.     
  628.     /**
  629.      * Return the output without displaying it
  630.      * 
  631.      * @since V2.0.1 - 17 apr 2006
  632.      * @see render()
  633.      */
  634.     public function getOutput()
  635.     {
  636.         return $this->render();
  637.     }
  638.     
  639.     /**
  640.      * Restrict access to a list of IP
  641.      * 
  642.      * @param array $ip     Array with IP to allow access
  643.      * @since V2.0.0 - 11 Apr 2006
  644.      * @see $options, isAllowed()
  645.      */ 
  646.     function restrictAccess($ip)
  647.     {
  648.         $this->options['allowed_ip'$ip;
  649.     }
  650.  
  651.     /**
  652.      * Test if the client is allowed to access the debug information
  653.      * There are several possibilities :
  654.      * - 'restrict_access' flag is set to false
  655.      * - 'restrict_access' flag is set to true and client IP is the
  656.      * allowed ip in the options 'allowed_ip'
  657.      * - Access by url is allowed with flag 'allow_url_access' then
  658.      * the client must enter the good key and password in the url
  659.      * 
  660.      * @since V2.0.0 - 20 apr 2006
  661.      * @see $options, restrictAcess()
  662.      */ 
  663.     protected function isAllowed()
  664.     {
  665.         if ($this->options['restrict_access'== true{
  666.  
  667.             // Check if client IP is among the allowed ones
  668.             if (in_array(
  669.                 $_SERVER['REMOTE_ADDR']
  670.                 $this->options['allowed_ip']
  671.             )) {
  672.                 return true;
  673.             }
  674.             // Check if instant access is allowed and test key and password
  675.             elseif ($this->options['allow_url_access'== true{
  676.                 
  677.                 $key $this->options['url_key'];
  678.                 
  679.                 if (!empty($_GET[$key])) {
  680.                     if ($_GET[$key== $this->options['url_pass']{
  681.                         return true;
  682.                     else {
  683.                         return false;                        
  684.                     }
  685.                 }
  686.                 else {
  687.                     return false;
  688.                 }                
  689.             else {
  690.                 return false;
  691.             }
  692.         else {
  693.             // Access is not restricted
  694.             return true;
  695.         }
  696.     }
  697.  
  698.     /**
  699.      * Return microtime from a timestamp
  700.      *   
  701.      * @param $time     Timestamp to retrieve micro time
  702.      * @return numeric  Microtime of timestamp param
  703.      * 
  704.      * @since V1.1.0 - 14 Nov 2003
  705.      * @see $DebugMode
  706.      */ 
  707.     public static function getMicroTime($time)
  708.     {   
  709.         list($usec$secexplode(' '$time);
  710.         return (float)$usec + (float)$sec;
  711.     }
  712.  
  713.     /**
  714.      * Alias for getMicroTime(microtime()
  715.      *   
  716.      * @see getMicroTime()
  717.      * @since V2.0.0 - 19 apr 2006
  718.      */ 
  719.     public static function getMicroTimeNow()
  720.     {   
  721.         return PHP_Debug::getMicroTime(microtime());
  722.     }
  723.  
  724.     /**
  725.      * Get elapsed time between 2 timestamp
  726.      *   
  727.      * @param   float $timeStart    Start time
  728.      * @param   float $timeEnd      End time
  729.      * @return  float               Numeric difference between the two times
  730.      *                               ref in format 00.0000 sec
  731.      * 
  732.      * @see getMicroTime()
  733.      * @since V1.0.0 - 20 Oct 2003
  734.      */ 
  735.     public static function getElapsedTime($timeStart$timeEnd)
  736.     {           
  737.         return round($timeEnd $timeStart4);
  738.     }
  739.  
  740.    /**
  741.     * Returns Uri prefix, including protocol, hostname and server port.
  742.     *
  743.     * @return string Uniform resource identifier prefix
  744.     */
  745.     public static function getUriPrefix()
  746.     {
  747.         $pathArray $_SERVER;
  748.  
  749.         if (PHP_Debug::isSecure()) {
  750.           $standardPort '443';
  751.           $proto 'https';
  752.         else {
  753.           $standardPort '80';
  754.           $proto 'http';
  755.         }
  756.     
  757.         $port $pathArray['SERVER_PORT'== $standardPort || !$pathArray['SERVER_PORT''' ':'.$pathArray['SERVER_PORT'];    
  758.         return $proto.'://'$pathArray['SERVER_NAME']$port;
  759.     }
  760.  
  761.     /**
  762.      * Test if url is secured
  763.      * 
  764.      * @since V2.1.1 - 23 avr. 2007
  765.      */ 
  766.     public static function isSecure()
  767.     {
  768.         return $_SERVER['SERVER_PORT'!= 80;        
  769.     }
  770.  
  771.     /**
  772.      * Returns current host name.
  773.      * 
  774.      * @since    V2.1.1 - 23 avr. 2007
  775.      */
  776.     public static function getHost()
  777.     {
  778.         $pathArray $_SERVER;
  779.         return isset($pathArray['HTTP_X_FORWARDED_HOST']$pathArray['HTTP_X_FORWARDED_HOST'(isset($pathArray['HTTP_HOST']$pathArray['HTTP_HOST''');
  780.     }
  781.  
  782.     /**
  783.      * Returns current script name.
  784.      * 
  785.      * @return         string 
  786.      * @since V2.1.1 - 23 avr. 2007
  787.      */
  788.     public static function getScriptName()
  789.     {
  790.         $pathArray $_SERVER;
  791.         return isset($pathArray['SCRIPT_NAME']$pathArray['SCRIPT_NAME'(isset($pathArray['ORIG_SCRIPT_NAME']$pathArray['ORIG_SCRIPT_NAME''');
  792.     }
  793.  
  794.     /**
  795.      * Return the query string
  796.      * 
  797.      * @author Vernet Loic
  798.      * @since 2.1.1 - 23 avr. 2007
  799.      */
  800.     public static function getQueryString()
  801.     {
  802.         return $_SERVER['QUERY_STRING''?'$_SERVER['QUERY_STRING''';
  803.     }
  804.  
  805.     /**
  806.      * Return the full url
  807.      * 
  808.      * @author Vernet Loi
  809.      * @since 2.1.1 - 23 avr. 2007
  810.      */
  811.     public static function getUrl(
  812.     {
  813.         return self::getUriPrefix(). self::getScriptName(). self::getQueryString();
  814.     }
  815.  
  816.     /**
  817.      * Set the endtime for a DebugLine in order to monitor the performance
  818.      * of a part of script
  819.      *   
  820.      * @see PHP_DebugLine::endTime
  821.      * @since V2.0.0 - 19 apr 2006
  822.      */ 
  823.     public function stopTimer()
  824.     {
  825.         $this->debugLineBuffer[count($this->debugLineBuffer)-1]->setEndTime();
  826.     }
  827.  
  828.     /**
  829.      * Display the content of any kind of variable
  830.      * 
  831.      * - Mode PHP_DEBUG_DUMP_ARR_DISP display the array
  832.      * - Mode PHP_DEBUG_DUMP_ARR_STR return the infos as a string
  833.      * 
  834.      * @param   mixed       $var        Variable to dump
  835.      * @param   string      $varname    Name of the variable
  836.      * @param   integer     $mode       Mode of function
  837.      * @param   boolean     $stopExec   Stop the process after display of debug
  838.      * @return  mixed                   Nothing or string depending on the mode
  839.      * 
  840.      * @since V2.0.0 - 25 Apr 2006
  841.      */ 
  842.     public static function dumpVar(
  843.         $var
  844.         $varName = self::DUMP_VARNAME
  845.         $stopExec = false
  846.         $mode = self::DUMP_DISP{
  847.         $dumpMethod = self::$staticOptions['dump_method'];
  848.         ob_start();
  849.         $dumpMethod($var);
  850.         
  851.         $dbgBuffer htmlentities(ob_get_contents());
  852.         ob_end_clean();
  853.         
  854.         switch ($mode{
  855.             default:
  856.             case self::DUMP_DISP:
  857.  
  858.                 if (empty($varName)) {
  859.                     if (is_array($var)) {
  860.                         $varName 'Array';
  861.                     elseif (is_object($var)) {
  862.                         $varName get_class($var);
  863.                     else {
  864.                         $varName 'Variable';                              
  865.                     }
  866.                 }
  867.             
  868.                 $dbgBuffer '<pre><b>dump of \''$varName'\'</b> :'
  869.                     CR$dbgBuffer'</pre>';
  870.                 echo $dbgBuffer;
  871.                 break;
  872.  
  873.             case PHP_Debug::DUMP_STR:
  874.                 return($dbgBuffer);
  875.         }        
  876.  
  877.         // Check process stop
  878.         if ($stopExec{
  879.             $backtrace debug_backtrace();
  880.             $dieMsg  '<pre><b>Process stopped by PHP_Debug</b>'CR;
  881.             $dieMsg .= $backtrace[0]['file'?     '&raquo; file     : <b>'
  882.                 $backtrace[0]['file'.'</b>'CR : '';  
  883.             $dieMsg .= $backtrace[0]['line'?     '&raquo; line     : <b>'
  884.                 $backtrace[0]['line'.'</b>'CR : '';  
  885.             $dieMsg .= $backtrace[1]['class'?    '&raquo; class    : <b>'
  886.                 $backtrace[1]['class'.'</b>'CR : '';  
  887.             $dieMsg .= $backtrace[1]['function''&raquo; function : <b>'
  888.                 $backtrace[1]['function'.'</b>'CR : '';  
  889.             $dieMsg .= '</pre>'
  890.             die($dieMsg);
  891.         }
  892.     }
  893.  
  894.     /**
  895.      * Get one option
  896.      *
  897.      * @param string $optionsIdx Name of the option to get
  898.      * @since V2.0.0 - 13 apr 2006
  899.      */
  900.     public function getOption($optionIdx)
  901.     {
  902.         return $this->options[$optionIdx];
  903.     }
  904.  
  905.     /**
  906.      * Getter of requiredFiles property
  907.      * 
  908.      * @return array Array with the included/required files
  909.      * @since V2.0.0 - 13 apr 2006
  910.      * @see requiredFiles
  911.      */
  912.     public function getRequiredFiles()
  913.     {
  914.         return $this->requiredFiles;
  915.     }
  916.  
  917.     /**
  918.      * Getter of debugString property
  919.      * 
  920.      * @since V2.0.0 - 13 apr 2006
  921.      * @see debugLineBuffer
  922.      */
  923.     public function getDebugBuffer()
  924.     {
  925.         return $this->debugLineBuffer;           
  926.     }
  927.  
  928.     /**
  929.      * Getter of queryCount property
  930.      * 
  931.      * @since V2.0.0 - 21 Apr 2006
  932.      * @see queryCount
  933.      */
  934.     public function getQueryCount()
  935.     {
  936.         return $this->queryCount;           
  937.     }
  938.  
  939.     /**
  940.      * Debug default output function, simply uses the static dump fonction
  941.      * of this class
  942.      * 
  943.      * @since V2.0.0 - 11 apr 2006
  944.      * @see dump
  945.      */
  946.     public function __toString()
  947.     {
  948.         return '<pre>'. PHP_Debug::dumpVar(
  949.             $this
  950.             __CLASS__. ' class instance',
  951.             false
  952.             PHP_Debug::DUMP_STR
  953.         )'</pre>';  
  954.     }
  955. }

Documentation generated on Wed, 14 Jan 2009 16:00:06 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.