Current file: /home/greg/workspace/PHPDS-dev-trunk/PHPDevShell/stable/trunk/includes/PHPDS_errorHandler.class.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00%0.00%
0.00% 0 / 2
5.88%5.88%
5.88% 1 / 17 CRAP
1.96%1.96%
1.96% 6 / 306
 
PHPDS_errorHandler
0.00%0.00%
0.00% 0 / 1
9.09%9.09%
9.09% 1 / 11
2.14%2.14%
2.14% 4 / 187
 construct()
0.00%0.00%
0.00% 0 / 1 229.43
5.88%5.88%
5.88% 2 / 34
 getFirePHP()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 doHandleShutdown()
0.00%0.00%
0.00% 0 / 1 12
0.00%0.00%
0.00% 0 / 6
 doHandleException(Exception $ex)
0.00%0.00%
0.00% 0 / 1 240
0.00%0.00%
0.00% 0 / 42
 doHandleError($errno, $errstr, $errfile, $errline)
0.00%0.00%
0.00% 0 / 1 90
0.00%0.00%
0.00% 0 / 13
 conductor($msg, $level = 0, $label = '')
0.00%0.00%
0.00% 0 / 1 812
0.00%0.00%
0.00% 0 / 45
 textualize($text)
0.00%0.00%
0.00% 0 / 1 2
0.00%0.00%
0.00% 0 / 2
 error_log($prefix, $data)
0.00%0.00%
0.00% 0 / 1 12
0.00%0.00%
0.00% 0 / 3
 getArgument ($arg)
0.00%0.00%
0.00% 0 / 1 42
0.00%0.00%
0.00% 0 / 14
 message ($message, $trace = '')
0.00%0.00%
0.00% 0 / 1 6
0.00%0.00%
0.00% 0 / 2
 showException(Exception $e = null)
0.00%0.00%
0.00% 0 / 1 110
0.00%0.00%
0.00% 0 / 24
PHPDS_backtrace
0.00%0.00%
0.00% 0 / 1
0.00%0.00%
0.00% 0 / 6
0.85%0.85%
0.85% 1 / 118
 lastLine($backtrace = null)
0.00%0.00%
0.00% 0 / 1 6
0.00%0.00%
0.00% 0 / 4
 asText($ignore = 0, $backtrace = null)
0.00%0.00%
0.00% 0 / 1 132
0.00%0.00%
0.00% 0 / 35
 asArray($ignore = 0, $backtrace = null)
0.00%0.00%
0.00% 0 / 1 2
0.00%0.00%
0.00% 0 / 1
 asHTML($ignore = 0, $backtrace = null)
0.00%0.00%
0.00% 0 / 1 240
0.00%0.00%
0.00% 0 / 54
 fetchCodeFragment($filepath, $lineno)
0.00%0.00%
0.00% 0 / 1 30
0.00%0.00%
0.00% 0 / 17
 highlightString($string, $lineno = null)
0.00%0.00%
0.00% 0 / 1 12
0.00%0.00%
0.00% 0 / 6


       1                 : <?php                                                                                                                                                                                                                                                                                                                                                                                                                                            
       2                 : /**                                                                                                                                                                                                                                                                                                                                                                                                                                              
       3                 :  * PHPDevShell is a RAD Framework aimed at developing administrative applications.                                                                                                                                                                                                                                                                                                                                                               
       4                 :  *                                                                                                                                                                                                                                                                                                                                                                                                                                               
       5                 :  * @package PHPDevShell                                                                                                                                                                                                                                                                                                                                                                                                                          
       6                 :  * @link http://www.phpdevshell.org                                                                                                                                                                                                                                                                                                                                                                                                              
       7                 :  * @copyright Copyright (C) 2007 Jason Schoeman, All rights reserved.                                                                                                                                                                                                                                                                                                                                                                            
       8                 :  * @license GNU/LGPL, see readme/licensed_under_lgpl or http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html                                                                                                                                                                                                                                                                                                                                   
       9                 :  * @author Jason Schoeman, Contact: titan [at] phpdevshell [dot] org.                                                                                                                                                                                                                                                                                                                                                                            
      10                 :  *                                                                                                                                                                                                                                                                                                                                                                                                                                               
      11                 :  * Copyright notice: See readme/notice                                                                                                                                                                                                                                                                                                                                                                                                           
      12                 :  * By using PHPDevShell you agree to notice and license, if you dont agree to this notice/license you are not allowed to use PHPDevShell.                                                                                                                                                                                                                                                                                                        
      13                 :  */                                                                                                                                                                                                                                                                                                                                                                                                                                              
      14                 : /**                                                                                                                                                                                                                                                                                                                                                                                                                                              
      15                 :  * Error handling function with use of FirePHP                                                                                                                                                                                                                                                                                                                                                                                                   
      16                 :  *                                                                                                                                                                                                                                                                                                                                                                                                                                               
      17                 :  * @author Grzegorz Godlewski redone by Jason Schoeman                                                                                                                                                                                                                                                                                                                                                                                           
      18                 :  */                                                                                                                                                                                                                                                                                                                                                                                                                                              
      19               1 : include_once dirname(__FILE__).'/PHPDS_exception.class.php';                                                                                                                                                                                                                                                                                                                                                                                     
      20                 : /**                                                                                                                                                                                                                                                                                                                                                                                                                                              
      21                 :  * Error handler class                                                                                                                                                                                                                                                                                                                                                                                                                           
      22                 :  *                                                                                                                                                                                                                                                                                                                                                                                                                                               
      23                 :  */                                                                                                                                                                                                                                                                                                                                                                                                                                              
      24                 : class PHPDS_errorHandler extends PHPDS_dependant                                                                                                                                                                                                                                                                                                                                                                                                 
      25               1 : {                                                                                                                                                                                                                                                                                                                                                                                                                                                
      26                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
      27                 :      * Error handler options                                                                                                                                                                                                                                                                                                                                                                                                                     
      28                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
      29                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      30                 :     protected $ignore_notices = false; // - if set to true Error handler ignores notices                                                                                                                                                                                                                                                                                                                                                         
      31                 :     protected $ignore_warnings = false; // - if set to true Error handler ignores warnings                                                                                                                                                                                                                                                                                                                                                       
      32                 :     protected $warningsAreFatal = true; // if true warning are handled as Exceptions                                                                                                                                                                                                                                                                                                                                                             
      33                 :     protected $noticesAreFatal = false; // if true, notices are handled as Exceptions                                                                                                                                                                                                                                                                                                                                                            
      34                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      35                 :     protected $serverlog = true; // log to syslog using error_log()                                                                                                                                                                                                                                                                                                                                                                              
      36                 :     protected $file = ''; // - log file                                                                                                                                                                                                                                                                                                                                                                                                          
      37                 :     protected $mail = ''; // - log mail                                                                                                                                                                                                                                                                                                                                                                                                          
      38                 :     protected $display = true; // - if set to true Error handler display error to output                                                                                                                                                                                                                                                                                                                                                         
      39                 :     protected $firebug = false; // - if set to true Error handler send error to firebug                                                                                                                                                                                                                                                                                                                                                          
      40                 :     protected $firephp = null; // - firephp object                                                                                                                                                                                                                                                                                                                                                                                               
      41                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      42                 :     protected $I_give_up = false; // if this is true something serious is wrong.                                                                                                                                                                                                                                                                                                                                                                 
      43                 :     protected $production = false; // is this a system in production                                                                                                                                                                                                                                                                                                                                                                             
      44                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      45                 :     public $error_backtrace = false; // - Should a backtrace be created. (Causes problems some times)                                                                                                                                                                                                                                                                                                                                            
      46                 :     //public $template;                                                                                                                                                                                                                                                                                                                                                                                                                          
      47                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      48                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
      49                 :      * Construtor                                                                                                                                                                                                                                                                                                                                                                                                                                
      50                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
      51                 :      * @date 20100402 (v1.0.1) (greg) fix a typo regarding firephp config field                                                                                                                                                                                                                                                                                                                                                                  
      52                 :      * @date 20100927 (v1.0.2) (greg) using the new constructor                                                                                                                                                                                                                                                                                                                                                                                  
      53                 :      * @date 20110808 (v1.0.3) (greg) don't do anything if we're running embedded (for example unit testing)                                                                                                                                                                                                                                                                                                                                     
      54                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
      55                 :      * @version 1.0.3                                                                                                                                                                                                                                                                                                                                                                                                                            
      56                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
      57                 :     public function construct()                                                                                                                                                                                                                                                                                                                                                                                                                  
      58                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
      59               1 :         if ($this->PHPDS_dependance()->isEmbedded()) {                                                                                                                                                                                                                                                                                                                                                                                           
      60               1 :             return;                                                                                                                                                                                                                                                                                                                                                                                                                              
      61                 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
      62                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      63               0 :         $flags = E_ALL;                                                                                                                                                                                                                                                                                                                                                                                                                          
      64                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      65               0 :         $this->production = !empty($this->configuration['production']);                                                                                                                                                                                                                                                                                                                                                                          
      66                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      67               0 :         if (isset($this->configuration['error'])) {                                                                                                                                                                                                                                                                                                                                                                                              
      68               0 :             $configuration = $this->configuration['error'];                                                                                                                                                                                                                                                                                                                                                                                      
      69                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      70               0 :             if ($configuration['mask']) $flags = intval($configuration['mask']);                                                                                                                                                                                                                                                                                                                                                                 
      71                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      72               0 :             if (isset($configuration['ignore_notices'])) $this->ignore_notices = !empty($configuration['ignore_notices']);                                                                                                                                                                                                                                                                                                                       
      73               0 :             if (isset($configuration['ignore_warnings'])) $this->ignore_warnings = !empty($configuration['ignore_warnings']);                                                                                                                                                                                                                                                                                                                    
      74               0 :             if (isset($configuration['warningsAreFatal'])) $this->warningsAreFatal = !empty($configuration['warningsAreFatal']);                                                                                                                                                                                                                                                                                                                 
      75               0 :             if (isset($configuration['noticesAreFatal'])) $this->noticesAreFatal = !empty($configuration['noticesAreFatal']);                                                                                                                                                                                                                                                                                                                    
      76                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      77                 :             // Backends                                                                                                                                                                                                                                                                                                                                                                                                                          
      78               0 :             if (!empty($configuration['serverlog'])) $this->serverlog = !empty($configuration['serverlog']);                                                                                                                                                                                                                                                                                                                                     
      79               0 :             if (!empty($configuration['file_log_dir'])) $this->file = $configuration['file_log_dir']; // TODO: check file is writable                                                                                                                                                                                                                                                                                                            
      80               0 :             if (!empty($configuration['email_critical'])) $this->mail = $configuration['email_critical'];                                                                                                                                                                                                                                                                                                                                        
      81               0 :             if (isset($configuration['display'])) $this->display = !empty($configuration['display']);                                                                                                                                                                                                                                                                                                                                            
      82               0 :             if (isset($configuration['firePHP'])) $this->firebug = !empty($configuration['firePHP']);                                                                                                                                                                                                                                                                                                                                            
      83                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      84               0 :             if ($this->firebug) {                                                                                                                                                                                                                                                                                                                                                                                                                
      85               0 :                 require_once ('debug/FirePHPCore/fb.php');                                                                                                                                                                                                                                                                                                                                                                                       
      86               0 :                 $this->firephp = FirePHP::getInstance(true);                                                                                                                                                                                                                                                                                                                                                                                     
      87               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
      88                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      89               0 :             if (! empty($this->ignore_notices)) {                                                                                                                                                                                                                                                                                                                                                                                                
      90               0 :                 $flags = $flags ^ E_NOTICE;                                                                                                                                                                                                                                                                                                                                                                                                      
      91               0 :                 $flags = $flags ^ E_USER_NOTICE;                                                                                                                                                                                                                                                                                                                                                                                                 
      92               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
      93               0 :             if (!empty($this->ignore_warnings)) {                                                                                                                                                                                                                                                                                                                                                                                                
      94               0 :                 $flags = $flags ^ E_WARNING;                                                                                                                                                                                                                                                                                                                                                                                                     
      95               0 :                 $flags = $flags ^ E_USER_WARNING;                                                                                                                                                                                                                                                                                                                                                                                                
      96               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
      97               0 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
      98                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
      99               0 :         error_reporting($flags);                                                                                                                                                                                                                                                                                                                                                                                                                 
     100               0 :         set_error_handler(array($this, "doHandleError"), $flags);                                                                                                                                                                                                                                                                                                                                                                                
     101               0 :         set_exception_handler(array($this, "doHandleException"));                                                                                                                                                                                                                                                                                                                                                                                
     102               0 :         register_shutdown_function(array($this, "doHandleShutdown"));                                                                                                                                                                                                                                                                                                                                                                            
     103               0 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     104                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     105                 :     public function getFirePHP()                                                                                                                                                                                                                                                                                                                                                                                                                 
     106                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     107               1 :         return $this->firephp;                                                                                                                                                                                                                                                                                                                                                                                                                   
     108                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     109                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     110                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     111                 :      * Handle critical errors (if set to)                                                                                                                                                                                                                                                                                                                                                                                                        
     112                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     113                 :     public function doHandleShutdown()                                                                                                                                                                                                                                                                                                                                                                                                           
     114                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     115               0 :         if ($this->I_give_up) return;                                                                                                                                                                                                                                                                                                                                                                                                            
     116                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     117               0 :         $error = error_get_last();                                                                                                                                                                                                                                                                                                                                                                                                               
     118               0 :         $errmask = error_reporting();                                                                                                                                                                                                                                                                                                                                                                                                            
     119               0 :         if ($errmask & $error['type']) $this->doHandleException(new PHPDS_fatalError());                                                                                                                                                                                                                                                                                                                                                         
     120                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     121               0 :         $this->I_give_up = true;                                                                                                                                                                                                                                                                                                                                                                                                                 
     122               0 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     123                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     124                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     125                 :      * Exception handler                                                                                                                                                                                                                                                                                                                                                                                                                         
     126                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     127                 :      * @param Exception $ex Exception                                                                                                                                                                                                                                                                                                                                                                                                            
     128                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     129                 :     public function doHandleException(Exception $ex)                                                                                                                                                                                                                                                                                                                                                                                             
     130                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     131               0 :         if ($this->I_give_up) return;                                                                                                                                                                                                                                                                                                                                                                                                            
     132                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     133                 :         try {                                                                                                                                                                                                                                                                                                                                                                                                                                    
     134               0 :             $errMsg = $ex->getMessage();                                                                                                                                                                                                                                                                                                                                                                                                         
     135               0 :             $backtrace = $ex->getTrace();                                                                                                                                                                                                                                                                                                                                                                                                        
     136               0 :             if (! $ex instanceof errorHandler) {                                                                                                                                                                                                                                                                                                                                                                                                 
     137               0 :                 $errMsg = get_class($ex) . ': ' . $errMsg;                                                                                                                                                                                                                                                                                                                                                                                       
     138               0 :                 array_unshift($backtrace, array('file' => $ex->getFile(), 'line' => $ex->getLine(), 'function' => 'throw ' . get_class($ex), 'args' => array($errMsg, $ex->getCode())));                                                                                                                                                                                                                                                         
     139               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     140               0 :             $errMsg .= ' | ' . date("Y-m-d H:i:s");                                                                                                                                                                                                                                                                                                                                                                                              
     141               0 :             if (empty($_SERVER['HTTP_HOST'])) {                                                                                                                                                                                                                                                                                                                                                                                                  
     142               0 :                 $errMsg .= ' | ' . implode(' ', $_SERVER['argv']);                                                                                                                                                                                                                                                                                                                                                                               
     143               0 :             } else {                                                                                                                                                                                                                                                                                                                                                                                                                             
     144               0 :                 $errMsg .= ' | ' . $_SERVER['HTTP_HOST'] . " (" . $_SERVER['SERVER_ADDR'] . ":" . $_SERVER['SERVER_PORT'] . ")" . "\n";                                                                                                                                                                                                                                                                                                          
     145                 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     146               0 :             if ($this->error_backtrace == true)    {                                                                                                                                                                                                                                                                                                                                                                                             
     147               0 :                 $trace = PHPDS_backtrace::asText(2, $backtrace);                                                                                                                                                                                                                                                                                                                                                                                 
     148               0 :             } else {                                                                                                                                                                                                                                                                                                                                                                                                                             
     149               0 :                 $trace = false;                                                                                                                                                                                                                                                                                                                                                                                                                  
     150                 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     151                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     152                 :             // This will take care of Firebug (textual), in-page alerts, and syslog                                                                                                                                                                                                                                                                                                                                                              
     153               0 :             $this->conductor($errMsg);                                                                                                                                                                                                                                                                                                                                                                                                           
     154                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     155                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     156                 :             // SENDING THROUGH FIREBUG (extended info with backtrace)                                                                                                                                                                                                                                                                                                                                                                            
     157               0 :             if ($this->firephp && !$this->production && !headers_sent())                                                                                                                                                                                                                                                                                                                                                                         
     158               0 :                 $this->firephp->fb($ex);                                                                                                                                                                                                                                                                                                                                                                                                         
     159                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     160                 :             // SENDING AN EMAIL                                                                                                                                                                                                                                                                                                                                                                                                                  
     161               0 :             if ($this->mail) {                                                                                                                                                                                                                                                                                                                                                                                                                   
     162               0 :                 $headers = 'MIME-Version: 1.0' . "\n" . 'Content-type: text/plain; charset=UTF-8' . "\n" . 'From: Error Handler <' . $this->mail . ">\n";                                                                                                                                                                                                                                                                                        
     163               0 :                 @mail("$this->mail", "$errMsg", "$errMsg\r\n$trace\r\n", $headers);                                                                                                                                                                                                                                                                                                                                                              
     164               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     165                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     166                 :             ///// WRITING TO A LOG FILE                                                                                                                                                                                                                                                                                                                                                                                                          
     167               0 :             if ($this->file) {                                                                                                                                                                                                                                                                                                                                                                                                                   
     168               0 :                 $filepath = realpath(BASEPATH.$this->file);                                                                                                                                                                                                                                                                                                                                                                                      
     169               0 :                 if ($filepath) {                                                                                                                                                                                                                                                                                                                                                                                                                 
     170               0 :                     $filepath .=  '/error.' . date('Y-m-d') . '.log';                                                                                                                                                                                                                                                                                                                                                                            
     171               0 :                     $fp = fopen($filepath, "a+");                                                                                                                                                                                                                                                                                                                                                                                                
     172               0 :                     if (flock($fp, LOCK_EX)) {                                                                                                                                                                                                                                                                                                                                                                                                   
     173               0 :                         fwrite($fp, "----\n" . $errMsg . "\n" . $trace . "\n");                                                                                                                                                                                                                                                                                                                                                                  
     174               0 :                         flock($fp, LOCK_UN);                                                                                                                                                                                                                                                                                                                                                                                                     
     175               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     176               0 :                     fclose($fp);                                                                                                                                                                                                                                                                                                                                                                                                                 
     177               0 :                 }                                                                                                                                                                                                                                                                                                                                                                                                                                
     178               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     179                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     180               0 :         } catch (Exception $e) {                                                                                                                                                                                                                                                                                                                                                                                                                 
     181                 :             // something bad happend in the exception handler, we build a new exception to describe that in the error page                                                                                                                                                                                                                                                                                                                       
     182               0 :             $ex = new PHPDS_Exception('Oops! An exception occured in the exception handler.', 0, $e);                                                                                                                                                                                                                                                                                                                                            
     183               0 :             $this->I_give_up = true;                                                                                                                                                                                                                                                                                                                                                                                                             
     184                 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     185                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     186                 :         try {                                                                                                                                                                                                                                                                                                                                                                                                                                    
     187                 :             ///// DISPLAYING ON THE WEB PAGE (only if not in production)                                                                                                                                                                                                                                                                                                                                                                         
     188               0 :             $this->showException($this->production ? null : $ex);                                                                                                                                                                                                                                                                                                                                                                                
     189               0 :         } catch (Exception $e) {                                                                                                                                                                                                                                                                                                                                                                                                                 
     190               0 :             error_log('Oops! An exception occured in the exception display.'.$e);                                                                                                                                                                                                                                                                                                                                                                
     191                 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     192                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     193               0 :         exit;                                                                                                                                                                                                                                                                                                                                                                                                                                    
     194                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     195                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     196                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     197                 :      * Error handler                                                                                                                                                                                                                                                                                                                                                                                                                             
     198                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     199                 :      * @param int $errno Error code                                                                                                                                                                                                                                                                                                                                                                                                              
     200                 :      * @param string $errstr Error message                                                                                                                                                                                                                                                                                                                                                                                                       
     201                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     202                 :     public function doHandleError($errno, $errstr, $errfile, $errline)                                                                                                                                                                                                                                                                                                                                                                           
     203                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     204               0 :         if ($this->I_give_up) return;                                                                                                                                                                                                                                                                                                                                                                                                            
     205                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     206               0 :         $errmask = error_reporting();                                                                                                                                                                                                                                                                                                                                                                                                            
     207               0 :         if (!($errmask & $errno)) { // if error has been masked with error_reporting() or suppressed with an @                                                                                                                                                                                                                                                                                                                                   
     208               0 :             return;                                                                                                                                                                                                                                                                                                                                                                                                                              
     209                 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     210                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     211               0 :         if ((E_WARNING == $errno) && $this->warningsAreFatal) throw new ErrorException($errstr, 0, $errno, $errfile, $errline);                                                                                                                                                                                                                                                                                                                  
     212               0 :         if ((E_NOTICE == $errno) && $this->noticesAreFatal) throw new ErrorException($errstr, 0, $errno, $errfile, $errline);                                                                                                                                                                                                                                                                                                                    
     213                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     214                 :         /*$errorTypes = array(E_ERROR => 'ERROR', E_WARNING => 'WARNING', E_PARSE => 'PARSING ERROR', E_NOTICE => 'NOTICE', E_CORE_ERROR => 'CORE ERROR', E_CORE_WARNING => 'CORE WARNING', E_COMPILE_ERROR => 'COMPILE ERROR', E_COMPILE_WARNING => 'COMPILE WARNING', E_USER_ERROR => 'USER ERROR', E_USER_WARNING => 'USER WARNING', E_USER_NOTICE => 'USER NOTICE', E_STRICT => 'STRICT NOTICE', E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR');
     215                 :         $errMsg = empty($errorTypes[$errno]) ? 'Unknown error' : $errorTypes[$errno];*/                                                                                                                                                                                                                                                                                                                                                          
     216               0 :         $errMsg = $errstr . " ($errfile line $errline )";                                                                                                                                                                                                                                                                                                                                                                                        
     217                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     218                 :         switch ($errno) {                                                                                                                                                                                                                                                                                                                                                                                                                        
     219               0 :             case E_WARNING: $level = PHPDS_debug::WARN; break;                                                                                                                                                                                                                                                                                                                                                                                   
     220               0 :             case E_NOTICE: $level = PHPDS_debug::INFO; break;                                                                                                                                                                                                                                                                                                                                                                                    
     221               0 :             default: $level = 0;                                                                                                                                                                                                                                                                                                                                                                                                                 
     222               0 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     223                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     224               0 :         $this->conductor($errMsg, $level);                                                                                                                                                                                                                                                                                                                                                                                                       
     225                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     226               0 :         return true; // to reset internal error                                                                                                                                                                                                                                                                                                                                                                                                  
     227                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     228                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     229                 :     public function conductor($msg, $level = 0, $label = '')                                                                                                                                                                                                                                                                                                                                                                                     
     230                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     231               0 :         if ($this->production) return;                                                                                                                                                                                                                                                                                                                                                                                                           
     232                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     233               0 :         $template = $this->PHPDS_dependance()->PHPDS_template(false);                                                                                                                                                                                                                                                                                                                                                                            
     234                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     235               0 :         $emsg = empty($label) ? $msg : "($label) $msg";                                                                                                                                                                                                                                                                                                                                                                                          
     236                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     237                 :         switch ($level) {                                                                                                                                                                                                                                                                                                                                                                                                                        
     238               0 :             case PHPDS_debug::ERROR:                                                                                                                                                                                                                                                                                                                                                                                                             
     239               0 :                     if ($this->display) {                                                                                                                                                                                                                                                                                                                                                                                                        
     240               0 :                         if (method_exists($template,'error')) $template->error($emsg);                                                                                                                                                                                                                                                                                                                                                           
     241               0 :                         else echo $this->message($emsg);                                                                                                                                                                                                                                                                                                                                                                                         
     242               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     243                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     244               0 :                     if ($this->firephp) $this->firephp->error($msg, $label);                                                                                                                                                                                                                                                                                                                                                                     
     245                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     246               0 :                     if ($this->serverlog) $this->error_log('ERROR', $emsg);                                                                                                                                                                                                                                                                                                                                                                      
     247               0 :                 break;                                                                                                                                                                                                                                                                                                                                                                                                                           
     248                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     249               0 :             case PHPDS_debug::WARN:                                                                                                                                                                                                                                                                                                                                                                                                              
     250               0 :                     if ($this->display) {                                                                                                                                                                                                                                                                                                                                                                                                        
     251               0 :                         if (method_exists($template,'warning')) $template->warning($emsg);                                                                                                                                                                                                                                                                                                                                                       
     252               0 :                         else echo $this->message($emsg);                                                                                                                                                                                                                                                                                                                                                                                         
     253               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     254                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     255               0 :                     if ($this->firephp) $this->firephp->warn($msg, $label);                                                                                                                                                                                                                                                                                                                                                                      
     256                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     257               0 :                     if ($this->serverlog) $this->error_log('WARNING', $emsg);                                                                                                                                                                                                                                                                                                                                                                    
     258               0 :                 break;                                                                                                                                                                                                                                                                                                                                                                                                                           
     259                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     260               0 :             case PHPDS_debug::INFO:                                                                                                                                                                                                                                                                                                                                                                                                              
     261               0 :                     if ($this->display) {                                                                                                                                                                                                                                                                                                                                                                                                        
     262               0 :                         if (method_exists($template,'notice')) $template->notice($emsg);                                                                                                                                                                                                                                                                                                                                                         
     263               0 :                         else echo $this->message($emsg);                                                                                                                                                                                                                                                                                                                                                                                         
     264               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     265                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     266               0 :                     if ($this->firephp) $this->firephp->info($msg, $label);                                                                                                                                                                                                                                                                                                                                                                      
     267                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     268               0 :                     if ($this->serverlog) $this->error_log('NOTICE', $emsg);                                                                                                                                                                                                                                                                                                                                                                     
     269               0 :                 break;                                                                                                                                                                                                                                                                                                                                                                                                                           
     270                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     271               0 :             case PHPDS_debug::DEBUG:                                                                                                                                                                                                                                                                                                                                                                                                             
     272               0 :                     if ($this->display) {                                                                                                                                                                                                                                                                                                                                                                                                        
     273               0 :                         if (method_exists($template,'debug')) $template->debug($emsg);                                                                                                                                                                                                                                                                                                                                                           
     274               0 :                         else echo $this->message($emsg);                                                                                                                                                                                                                                                                                                                                                                                         
     275               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     276                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     277               0 :                     if ($this->firephp) $this->firephp->log($msg, $label);                                                                                                                                                                                                                                                                                                                                                                       
     278                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     279               0 :                     if ($this->serverlog) $this->error_log('DEBUG', $emsg);                                                                                                                                                                                                                                                                                                                                                                      
     280               0 :                 break;                                                                                                                                                                                                                                                                                                                                                                                                                           
     281                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     282               0 :             default:                                                                                                                                                                                                                                                                                                                                                                                                                             
     283               0 :                     if ($this->display) {                                                                                                                                                                                                                                                                                                                                                                                                        
     284               0 :                         if (method_exists($template,'note')) $template->note($emsg);                                                                                                                                                                                                                                                                                                                                                             
     285               0 :                         else echo $this->message($emsg);                                                                                                                                                                                                                                                                                                                                                                                         
     286               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     287                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     288               0 :                     if ($this->firephp && !headers_sent()) $this->firephp->log($msg, $label);                                                                                                                                                                                                                                                                                                                                                    
     289                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     290               0 :                     if ($this->serverlog) $this->error_log('LOG', $emsg);                                                                                                                                                                                                                                                                                                                                                                        
     291               0 :                 break;                                                                                                                                                                                                                                                                                                                                                                                                                           
     292               0 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     293               0 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     294                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     295                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     296                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     297                 :      * Cleans a string for outputing on plain text devices (such as log files)                                                                                                                                                                                                                                                                                                                                                                   
     298                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     299                 :      * @param $text        the string to clean                                                                                                                                                                                                                                                                                                                                                                                                   
     300                 :      * @return $text                                                                                                                                                                                                                                                                                                                                                                                                                             
     301                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     302                 :     function textualize($text)                                                                                                                                                                                                                                                                                                                                                                                                                   
     303                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     304               0 :         $text = preg_replace('/[\x00-\x1F]+/', ' ', $text);                                                                                                                                                                                                                                                                                                                                                                                      
     305               0 :         return $text;                                                                                                                                                                                                                                                                                                                                                                                                                            
     306                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     307                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     308                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     309                 :      * Write data to the error log using Apache flow                                                                                                                                                                                                                                                                                                                                                                                             
     310                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     311                 :      * @param $prefix     A string to add at the beginning                                                                                                                                                                                                                                                                                                                                                                                       
     312                 :      * @param $data        An array of strings to output                                                                                                                                                                                                                                                                                                                                                                                         
     313                 :      * @return void                                                                                                                                                                                                                                                                                                                                                                                                                              
     314                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     315                 :     function error_log($prefix, $data)                                                                                                                                                                                                                                                                                                                                                                                                           
     316                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     317               0 :         if (is_array($data)) foreach($data as $text) $this->error_log('-', $text);                                                                                                                                                                                                                                                                                                                                                               
     318               0 :         else error_log('[ PHPDS ] '.$prefix.': '.$this->textualize($data));                                                                                                                                                                                                                                                                                                                                                                      
     319               0 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     320                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     321                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     322                 :      * Converts variable into short text                                                                                                                                                                                                                                                                                                                                                                                                         
     323                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     324                 :      * @param mixed $arg Variable                                                                                                                                                                                                                                                                                                                                                                                                                
     325                 :      * @return string                                                                                                                                                                                                                                                                                                                                                                                                                            
     326                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     327                 :     public static function getArgument ($arg)                                                                                                                                                                                                                                                                                                                                                                                                    
     328                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     329               0 :         switch (strtolower(gettype($arg))) {                                                                                                                                                                                                                                                                                                                                                                                                     
     330               0 :             case 'string':                                                                                                                                                                                                                                                                                                                                                                                                                       
     331               0 :                 return ('"' . str_replace(array("\n", "\""), array('', '"'), $arg) . '"');                                                                                                                                                                                                                                                                                                                                                       
     332               0 :             case 'boolean':                                                                                                                                                                                                                                                                                                                                                                                                                      
     333               0 :                 return (bool) $arg;                                                                                                                                                                                                                                                                                                                                                                                                              
     334               0 :             case 'object':                                                                                                                                                                                                                                                                                                                                                                                                                       
     335               0 :                 return 'object(' . get_class($arg) . ')';                                                                                                                                                                                                                                                                                                                                                                                        
     336               0 :             case 'array':                                                                                                                                                                                                                                                                                                                                                                                                                        
     337               0 :                 return 'array[' . count($arg) . ']';                                                                                                                                                                                                                                                                                                                                                                                             
     338               0 :             case 'resource':                                                                                                                                                                                                                                                                                                                                                                                                                     
     339               0 :                 return 'resource(' . get_resource_type($arg) . ')';                                                                                                                                                                                                                                                                                                                                                                              
     340               0 :             default:                                                                                                                                                                                                                                                                                                                                                                                                                             
     341               0 :                 return var_export($arg, true);                                                                                                                                                                                                                                                                                                                                                                                                   
     342               0 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     343                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     344                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     345                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     346                 :      * Quick independent message styling, just to make it look better yea.                                                                                                                                                                                                                                                                                                                                                                       
     347                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     348                 :      * @param string $message                                                                                                                                                                                                                                                                                                                                                                                                                    
     349                 :      * @return string                                                                                                                                                                                                                                                                                                                                                                                                                            
     350                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     351                 :     public function message ($message, $trace = '')                                                                                                                                                                                                                                                                                                                                                                                              
     352                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     353                 :         // Simple styled message.                                                                                                                                                                                                                                                                                                                                                                                                                
     354               0 :         if (! empty($trace)) $trace = "=>[$trace]";                                                                                                                                                                                                                                                                                                                                                                                              
     355               0 :         return $this->textualize($message)."<br />$trace<br />";                                                                                                                                                                                                                                                                                                                                                                                 
     356                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     357                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     358                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     359                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     360                 :      * Display an Exception                                                                                                                                                                                                                                                                                                                                                                                                                      
     361                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     362                 :      * This function will load a predefined template page (in PHP form) in order to warn the user something has gone wrong.                                                                                                                                                                                                                                                                                                                      
     363                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     364                 :      * If an exception is provided, it will be detailed as much as possible ; if not, only a generic message will be displayed                                                                                                                                                                                                                                                                                                                   
     365                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     366                 :      * @date 20100918                                                                                                                                                                                                                                                                                                                                                                                                                            
     367                 :      * @version 1.0.1                                                                                                                                                                                                                                                                                                                                                                                                                            
     368                 :      * @author greg                                                                                                                                                                                                                                                                                                                                                                                                                              
     369                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     370                 :      * @param Exception $e (optional)                                                                                                                                                                                                                                                                                                                                                                                                            
     371                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     372                 :     public function showException(Exception $e = null)                                                                                                                                                                                                                                                                                                                                                                                           
     373                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     374                 :         // we stop on the first unhandled error                                                                                                                                                                                                                                                                                                                                                                                                  
     375               0 :         $this->I_give_up = true;                                                                                                                                                                                                                                                                                                                                                                                                                 
     376                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     377               0 :         if ($this->PHPDS_dependance()->isEmbedded()) return;                                                                                                                                                                                                                                                                                                                                                                                     
     378                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     379               0 :         PU_cleanBuffers();                                                                                                                                                                                                                                                                                                                                                                                                                       
     380                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     381               0 :         if (is_a($e, 'Exception')) {                                                                                                                                                                                                                                                                                                                                                                                                             
     382               0 :             $lineno = $e->getLine();                                                                                                                                                                                                                                                                                                                                                                                                             
     383               0 :             $filepath = $e->getFile();                                                                                                                                                                                                                                                                                                                                                                                                           
     384                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     385               0 :             $trace = (is_a($e, 'PHPDS_exception')) ? $e->getExtendedTrace() : $e->getTrace();                                                                                                                                                                                                                                                                                                                                                    
     386               0 :             $ignore = (is_a($e, 'PHPDS_exception')) ? $e->getIgnoreLines() : 1;                                                                                                                                                                                                                                                                                                                                                                  
     387                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     388               0 :             $filefragment = PHPDS_backtrace::fetchCodeFragment($filepath, $lineno);                                                                                                                                                                                                                                                                                                                                                              
     389                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     390               0 :             $message = $e->getMessage();                                                                                                                                                                                                                                                                                                                                                                                                         
     391               0 :             $extendedMessage = (is_a($e, 'PHPDS_exception')) ? $e->getExtendedMessage() : '';                                                                                                                                                                                                                                                                                                                                                    
     392               0 :             $config = $this->configuration;                                                                                                                                                                                                                                                                                                                                                                                                      
     393               0 :             if (!empty($config)) {                                                                                                                                                                                                                                                                                                                                                                                                               
     394               0 :                 if (isset($config['config_files_used']))                                                                                                                                                                                                                                                                                                                                                                                         
     395               0 :                     $conf['used'] = PU_dumpArray($config['config_files_used']);                                                                                                                                                                                                                                                                                                                                                                  
     396                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     397               0 :                 if (isset($config['config_files_missing']))                                                                                                                                                                                                                                                                                                                                                                                      
     398               0 :                     $conf['missing'] = PU_dumpArray($config['config_files_missing']);                                                                                                                                                                                                                                                                                                                                                            
     399               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     400               0 :             $bt = PHPDS_backtrace::asHTML($ignore, $trace);                                                                                                                                                                                                                                                                                                                                                                                      
     401               0 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     402               0 :         $protocol = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';                                                                                                                                                                                                                                                                                                                                                                           
     403                 :         // Need this for absolute URL configuration to be sef safe.                                                                                                                                                                                                                                                                                                                                                                              
     404               0 :         $aurl = $protocol . $_SERVER['SERVER_NAME'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']);                                                                                                                                                                                                                                                                                                                                       
     405                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     406                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     407                 :         // Load error page: $e is the handled exception                                                                                                                                                                                                                                                                                                                                                                                          
     408               0 :         require_once BASEPATH.'themes/cloud/error.php';                                                                                                                                                                                                                                                                                                                                                                                          
     409               0 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     410                 : }                                                                                                                                                                                                                                                                                                                                                                                                                                                
     411                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     412                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     413                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     414                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     415                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     416                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     417                 :     /**                                                                                                                                                                                                                                                                                                                                                                                                                                          
     418                 :      * Generate a pretty (formated to be read) backtrace, skipping the first lines if asked                                                                                                                                                                                                                                                                                                                                                      
     419                 :      *                                                                                                                                                                                                                                                                                                                                                                                                                                           
     420                 :      * @param $ignore                integer, number of frames to skip (optional, defaults to 0)                                                                                                                                                                                                                                                                                                                                                 
     421                 :      * @param $backtrace        the backtrace (optional)                                                                                                                                                                                                                                                                                                                                                                                         
     422                 :      * @return string                                                                                                                                                                                                                                                                                                                                                                                                                            
     423                 :      */                                                                                                                                                                                                                                                                                                                                                                                                                                          
     424                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     425                 : class PHPDS_backtrace                                                                                                                                                                                                                                                                                                                                                                                                                            
     426               1 : {                                                                                                                                                                                                                                                                                                                                                                                                                                                
     427                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     428                 :     public static function lastLine($backtrace = null)                                                                                                                                                                                                                                                                                                                                                                                           
     429                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     430               0 :         if (empty($backtrace)) $backtrace = debug_backtrace();                                                                                                                                                                                                                                                                                                                                                                                   
     431                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     432               0 :         $b = $backtrace[1];                                                                                                                                                                                                                                                                                                                                                                                                                      
     433               0 :         $result = 'at line '.$b['line'].' of file "'.$b['file'].'"';                                                                                                                                                                                                                                                                                                                                                                             
     434                 :         //if ($b['function']) $result .= ' in function "'.$b['function'].'"';                                                                                                                                                                                                                                                                                                                                                                    
     435                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     436               0 :         return $result;                                                                                                                                                                                                                                                                                                                                                                                                                          
     437                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     438                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     439                 :     public static function asText($ignore = 0, $backtrace = null)                                                                                                                                                                                                                                                                                                                                                                                
     440                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     441               0 :         if (empty($backtrace)) $backtrace = debug_backtrace();                                                                                                                                                                                                                                                                                                                                                                                   
     442                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     443               0 :         $ignore = intval($ignore);                                                                                                                                                                                                                                                                                                                                                                                                               
     444                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     445               0 :         $trace = '';                                                                                                                                                                                                                                                                                                                                                                                                                             
     446               0 :         foreach ($backtrace as $v) {                                                                                                                                                                                                                                                                                                                                                                                                             
     447               0 :             if (empty($v['file'])) $v['file'] = '';                                                                                                                                                                                                                                                                                                                                                                                              
     448               0 :             if (empty($v['line'])) $v['line'] = '';                                                                                                                                                                                                                                                                                                                                                                                              
     449               0 :             $v['file'] = preg_replace('!^'.$_SERVER['DOCUMENT_ROOT'].'!', '' ,$v['file']);                                                                                                                                                                                                                                                                                                                                                       
     450               0 :             $trace .= $v['file']."\t".$v['line']."\t";                                                                                                                                                                                                                                                                                                                                                                                           
     451               0 :             if (isset($v['class'])) {                                                                                                                                                                                                                                                                                                                                                                                                            
     452               0 :                     $trace .= $v['class'].'::'.$v['function'].'(';                                                                                                                                                                                                                                                                                                                                                                               
     453               0 :                     if (isset($v['args'])) {                                                                                                                                                                                                                                                                                                                                                                                                     
     454               0 :                             $errRow[] = $v['args'];                                                                                                                                                                                                                                                                                                                                                                                              
     455               0 :                             $separator = '';                                                                                                                                                                                                                                                                                                                                                                                                     
     456               0 :                             foreach($v['args'] as $arg ) {                                                                                                                                                                                                                                                                                                                                                                                       
     457               0 :                                     $trace .= $separator.PHPDS_errorHandler::getArgument($arg);                                                                                                                                                                                                                                                                                                                                                  
     458               0 :                                     $separator = ', ';                                                                                                                                                                                                                                                                                                                                                                                           
     459               0 :                             }                                                                                                                                                                                                                                                                                                                                                                                                                    
     460               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     461               0 :                     $trace .= ')';                                                                                                                                                                                                                                                                                                                                                                                                               
     462               0 :             } elseif (isset($v['function'])) {                                                                                                                                                                                                                                                                                                                                                                                                   
     463               0 :                     $trace .= $v['function'].'(';                                                                                                                                                                                                                                                                                                                                                                                                
     464               0 :                     $errRow[] = $v['function'];                                                                                                                                                                                                                                                                                                                                                                                                  
     465               0 :                     if (!empty($v['args'])) {                                                                                                                                                                                                                                                                                                                                                                                                    
     466               0 :                             $errRow[] = $v['args'];                                                                                                                                                                                                                                                                                                                                                                                              
     467               0 :                             $separator = '';                                                                                                                                                                                                                                                                                                                                                                                                     
     468               0 :                             foreach($v['args'] as $arg ) {                                                                                                                                                                                                                                                                                                                                                                                       
     469               0 :                                     $trace .= $separator.PHPDS_errorHandler::getArgument($arg);                                                                                                                                                                                                                                                                                                                                                  
     470               0 :                                     $separator = ', ';                                                                                                                                                                                                                                                                                                                                                                                           
     471               0 :                             }                                                                                                                                                                                                                                                                                                                                                                                                                    
     472               0 :                     }                                                                                                                                                                                                                                                                                                                                                                                                                            
     473               0 :                     $trace .= ')';                                                                                                                                                                                                                                                                                                                                                                                                               
     474               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     475               0 :             $trace .= "\n";                                                                                                                                                                                                                                                                                                                                                                                                                      
     476               0 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     477                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     478               0 :         return $trace;                                                                                                                                                                                                                                                                                                                                                                                                                           
     479                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     480                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     481                 :     public function asArray($ignore = 0, $backtrace = null)                                                                                                                                                                                                                                                                                                                                                                                      
     482                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     483                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     484               0 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     485                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     486                 :     public function asHTML($ignore = 0, $backtrace = null)                                                                                                                                                                                                                                                                                                                                                                                       
     487                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     488               0 :         if (empty($backtrace)) $backtrace = debug_backtrace();                                                                                                                                                                                                                                                                                                                                                                                   
     489                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     490               0 :         $ignore = intval($ignore);                                                                                                                                                                                                                                                                                                                                                                                                               
     491                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     492               0 :         $internals = get_defined_functions();                                                                                                                                                                                                                                                                                                                                                                                                    
     493               0 :         $internals = $internals['internal'];                                                                                                                                                                                                                                                                                                                                                                                                     
     494                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     495               0 :         $protocol = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';                                                                                                                                                                                                                                                                                                                                                                           
     496                 :         // Need this for absolute URL configuration to be sef safe.                                                                                                                                                                                                                                                                                                                                                                              
     497               0 :         $aurl = $protocol . $_SERVER['SERVER_NAME'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']);                                                                                                                                                                                                                                                                                                                                       
     498                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     499               0 :         $trace = '';                                                                                                                                                                                                                                                                                                                                                                                                                             
     500               0 :         foreach ($backtrace as $v) {                                                                                                                                                                                                                                                                                                                                                                                                             
     501               0 :             if ($ignore-- > 0) continue;                                                                                                                                                                                                                                                                                                                                                                                                         
     502                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     503               0 :             $class = (0 == $ignore) ? 'ui-state-active ui-corner-all' : '';                                                                                                                                                                                                                                                                                                                                                                      
     504                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     505               0 :             $trace .= '<tr class="'.$class.'">';                                                                                                                                                                                                                                                                                                                                                                                                 
     506                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     507               0 :             if (empty($v['file'])) $v['file'] = '';                                                                                                                                                                                                                                                                                                                                                                                              
     508               0 :             if (empty($v['line'])) $v['line'] = '';                                                                                                                                                                                                                                                                                                                                                                                              
     509               0 :             $filepath = preg_replace('!^'.$_SERVER['DOCUMENT_ROOT'].'/!', '<span class="note">...</span>' ,$v['file']);                                                                                                                                                                                                                                                                                                                          
     510                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     511               0 :             $trace .= '<td>'.$filepath.'</td><td>'.$v['line'].'</td><td>';                                                                                                                                                                                                                                                                                                                                                                       
     512                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     513               0 :             if (isset($v['class'])) {                                                                                                                                                                                                                                                                                                                                                                                                            
     514               0 :                 $fct = $v['class'].'::'.$v['function'];                                                                                                                                                                                                                                                                                                                                                                                          
     515               0 :                 $call = $fct.'(';                                                                                                                                                                                                                                                                                                                                                                                                                
     516               0 :                 if (isset($v['args'])) {                                                                                                                                                                                                                                                                                                                                                                                                         
     517               0 :                         $errRow[] = $v['args'];                                                                                                                                                                                                                                                                                                                                                                                                  
     518               0 :                         $separator = '';                                                                                                                                                                                                                                                                                                                                                                                                         
     519               0 :                         foreach($v['args'] as $arg ) {                                                                                                                                                                                                                                                                                                                                                                                           
     520               0 :                                 $call .= $separator.PHPDS_errorHandler::getArgument($arg);                                                                                                                                                                                                                                                                                                                                                       
     521               0 :                                 $separator = ', ';                                                                                                                                                                                                                                                                                                                                                                                               
     522               0 :                         }                                                                                                                                                                                                                                                                                                                                                                                                                        
     523               0 :                 }                                                                                                                                                                                                                                                                                                                                                                                                                                
     524               0 :                 $call .= ')';                                                                                                                                                                                                                                                                                                                                                                                                                    
     525               0 :                 $call = PHPDS_backtrace::highlightString($call);                                                                                                                                                                                                                                                                                                                                                                                 
     526               0 :                 if (substr($v['class'], 0, 5) == 'PHPDS') {                                                                                                                                                                                                                                                                                                                                                                                      
     527               0 :                     $call = '<a href="http://doc.phpdevshell.org/PHPDevShell/'.$v['class'].'.html#'.$v['function'].'" target="_blank"><img src="' . $aurl . '/themes/cloud/images/icons-16/book-question.png" /></a>&nbsp;'.$call;                                                                                                                                                                                                               
     528               0 :                 }                                                                                                                                                                                                                                                                                                                                                                                                                                
     529               0 :                 $trace .= $call;                                                                                                                                                                                                                                                                                                                                                                                                                 
     530               0 :             } elseif (isset($v['function'])) {                                                                                                                                                                                                                                                                                                                                                                                                   
     531               0 :                 $fct = $v['function'];                                                                                                                                                                                                                                                                                                                                                                                                           
     532               0 :                 $call = $fct.'(';                                                                                                                                                                                                                                                                                                                                                                                                                
     533               0 :                 $errRow[] = $v['function'];                                                                                                                                                                                                                                                                                                                                                                                                      
     534               0 :                 if (!empty($v['args'])) {                                                                                                                                                                                                                                                                                                                                                                                                        
     535               0 :                         $errRow[] = $v['args'];                                                                                                                                                                                                                                                                                                                                                                                                  
     536               0 :                         $separator = '';                                                                                                                                                                                                                                                                                                                                                                                                         
     537               0 :                         foreach($v['args'] as $arg ) {                                                                                                                                                                                                                                                                                                                                                                                           
     538               0 :                                 $call .= $separator.PHPDS_errorHandler::getArgument($arg);                                                                                                                                                                                                                                                                                                                                                       
     539               0 :                                 $separator = ', ';                                                                                                                                                                                                                                                                                                                                                                                               
     540               0 :                         }                                                                                                                                                                                                                                                                                                                                                                                                                        
     541               0 :                 }                                                                                                                                                                                                                                                                                                                                                                                                                                
     542               0 :                 $call .= ')';                                                                                                                                                                                                                                                                                                                                                                                                                    
     543               0 :                 $call = PHPDS_backtrace::highlightString($call);                                                                                                                                                                                                                                                                                                                                                                                 
     544               0 :                 /*if (!empty($internals[$fct]))*/ $call = '<a href="http://www.php.net/manual-lookup.php?lang=en&pattern='.urlencode($fct).'" target="_blank"><img src="' . $aurl . '/themes/cloud/images/icons-16/book-question.png" /></a>&nbsp;'.$call;                                                                                                                                                                                       
     545               0 :                 $trace .= $call;                                                                                                                                                                                                                                                                                                                                                                                                                 
     546                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     547               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     548               0 :             $trace .= '</td></tr>';                                                                                                                                                                                                                                                                                                                                                                                                              
     549               0 :             $trace .= '<tr class="'.$class.'">';                                                                                                                                                                                                                                                                                                                                                                                                 
     550               0 :             $trace .= '<td colspan="10">'.PHPDS_backtrace::fetchCodeFragment($v['file'], $v['line']).'</td></tr>';                                                                                                                                                                                                                                                                                                                               
     551               0 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     552                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     553                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     554               0 :         return $trace;                                                                                                                                                                                                                                                                                                                                                                                                                           
     555                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     556                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     557                 :     public static function fetchCodeFragment($filepath, $lineno)                                                                                                                                                                                                                                                                                                                                                                                 
     558                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     559               0 :         if (!empty($filepath)) {                                                                                                                                                                                                                                                                                                                                                                                                                 
     560               0 :             $filecontent = file($filepath);                                                                                                                                                                                                                                                                                                                                                                                                      
     561               0 :             $start = max($lineno - 7, 0);                                                                                                                                                                                                                                                                                                                                                                                                        
     562               0 :             $end = min($lineno + 7, count($filecontent));                                                                                                                                                                                                                                                                                                                                                                                        
     563                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     564               0 :             $fragment = '<div class="ui-state-default ui-corner-all"></div><div class="toggle">';                                                                                                                                                                                                                                                                                                                                                
     565               0 :             for($loop = $start; $loop < $end; $loop++) {                                                                                                                                                                                                                                                                                                                                                                                         
     566               0 :                 if (!empty($filecontent[$loop])) {                                                                                                                                                                                                                                                                                                                                                                                               
     567               0 :                     $line = $filecontent[$loop];                                                                                                                                                                                                                                                                                                                                                                                                 
     568               0 :                     $line = preg_replace('#\n$#', '', $line);                                                                                                                                                                                                                                                                                                                                                                                    
     569               0 :                     $line = PHPDS_backtrace::highlightString($line, $loop + 1);                                                                                                                                                                                                                                                                                                                                                                  
     570               0 :                 }                                                                                                                                                                                                                                                                                                                                                                                                                                
     571               0 :                 if ($loop == $lineno - 1) $line = '<span class="ui-state-highlight ui-corner-all">'.$line.'</span>';                                                                                                                                                                                                                                                                                                                             
     572                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     573               0 :                 $fragment .= $line.'<br />'."\n";                                                                                                                                                                                                                                                                                                                                                                                                
     574               0 :             }                                                                                                                                                                                                                                                                                                                                                                                                                                    
     575               0 :                         $fragment .= '</div>';                                                                                                                                                                                                                                                                                                                                                                                                   
     576               0 :             return $fragment;                                                                                                                                                                                                                                                                                                                                                                                                                    
     577               0 :         } else return '<p>Missing file...</p>';                                                                                                                                                                                                                                                                                                                                                                                                  
     578                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     579                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     580                 :     public static function highlightString($string, $lineno = null)                                                                                                                                                                                                                                                                                                                                                                              
     581                 :     {                                                                                                                                                                                                                                                                                                                                                                                                                                            
     582                 :         try {                                                                                                                                                                                                                                                                                                                                                                                                                                    
     583               0 :             $string = preg_replace('/&lt;\?php/', '', highlight_string('<?php' . $string, true));                                                                                                                                                                                                                                                                                                                                                
     584               0 :             $string = preg_replace('/<code><span ([^>]*)>\n<span [^>]*>&lt;\?<br \/>(.*)\n<\/span>\n<\/code>/', '<span $1>$2</span>', $string);                                                                                                                                                                                                                                                                                                  
     585               0 :         } catch (Exception $e) {                                                                                                                                                                                                                                                                                                                                                                                                                 
     586                 :             // only mask the exception and prevent it from bubbling                                                                                                                                                                                                                                                                                                                                                                              
     587               0 :             $string = '???????????????????????????';                                                                                                                                                                                                                                                                                                                                                                                             
     588                 :         }                                                                                                                                                                                                                                                                                                                                                                                                                                        
     589               0 :         if ($lineno) $string = '<span class="note">'.$lineno.'</span>&nbsp;&nbsp;'.$string;                                                                                                                                                                                                                                                                                                                                                      
     590               0 :         return $string;                                                                                                                                                                                                                                                                                                                                                                                                                          
     591                 :     }                                                                                                                                                                                                                                                                                                                                                                                                                                            
     592                 : }                                                                                                                                                                                                                                                                                                                                                                                                                                                
     593                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     594                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     595                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     596                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     597                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     598                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     599                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     600                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     601                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     602                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     603                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     604                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     605                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     606                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     607                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     608                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     609                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     610                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     611                 :                                                                                                                                                                                                                                                                                                                                                                                                                                                  

Generated by PHP_CodeCoverage 1.0.4 using PHP 5.3.6 and PHPUnit 3.5.14 at Mon Aug 8 22:35:38 PDT 2011.