Source for file jErrorHandler.lib.php

Documentation is available at jErrorHandler.lib.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage core
  5. * @author     Laurent Jouanneau
  6. * @contributor Sylvain de Vathaire
  7. * @contributor Loic Mathaud <loic@mathaud.net>
  8. * @copyright  2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau, 2007 Sylvain de Vathaire, 2007 Loic Mathaud
  9. *  This function was get originally from the Copix project (CopixErrorHandler, Copix 2.3dev20050901, http://www.copix.org)
  10. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  11. *  Initial authors of this function are Gerald Croes and Laurent Jouanneau,
  12. *  and it was adapted/improved for Jelix by Laurent Jouanneau
  13. *
  14. * @link        http://www.jelix.org
  15. * @licence    http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  16. */
  17.  
  18.  
  19. /**
  20. * Error handler for the framework.
  21. * Replace the default PHP error handler
  22. * @param   integer     $errno      error code
  23. * @param   string      $errmsg     error message
  24. * @param   string      $filename   filename where the error appears
  25. * @param   integer     $linenum    line number where the error appears
  26. * @param   array       $errcontext 
  27. */
  28. function jErrorHandler($errno, $errmsg, $filename, $linenum, $errcontext){
  29.     global $gJConfig, $gJCoord;
  30.  
  31.     if (error_reporting() == 0)
  32.         return;
  33.  
  34.     $codeString = array(
  35.         E_ERROR         => 'error',
  36.         E_RECOVERABLE_ERROR => 'error',
  37.         E_WARNING       => 'warning',
  38.         E_NOTICE        => 'notice',
  39.         E_DEPRECATED    => 'notice',
  40.         E_USER_ERROR    => 'error',
  41.         E_USER_WARNING  => 'warning',
  42.         E_USER_NOTICE   => 'notice',
  43.         E_STRICT        => 'strict'
  44.     );
  45.  
  46.     if(preg_match('/^\s*\((\d+)\)(.+)$/',$errmsg,$m)){
  47.         $code = $m[1];
  48.         $errmsg = $m[2];
  49.     }else{
  50.         $code=1;
  51.     }
  52.  
  53.     $conf = $gJConfig->error_handling;
  54.  
  55.     if (isset ($codeString[$errno])){
  56.         $codestr = $codeString[$errno];
  57.         $action = $conf[$codestr];
  58.     }else{
  59.         $action = $conf['default'];
  60.         $codestr = 'error';
  61.     }
  62.  
  63.     $doEchoByResponse = true;
  64.     if($gJCoord->request == null){
  65.         $errmsg = 'JELIX PANIC ! Error during initialization !! '.$errmsg;
  66.         $doEchoByResponse = false;
  67.         $action.= ' EXIT';
  68.     }elseif($gJCoord->response == null){
  69.         $ret = $gJCoord->initDefaultResponseOfRequest();
  70.         if(is_string($ret)){
  71.             $errmsg = 'Double error ! 1)'. $ret.'; 2)'.$errmsg;
  72.             $doEchoByResponse = false;
  73.         }
  74.     }
  75.  
  76.     // When we are in cmdline we need to fix the remoteAddr
  77.     $remoteAddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
  78.  
  79.     // formatage du message
  80.     $messageLog = strtr($conf['messageLogFormat'], array(
  81.         '%date%' => date("Y-m-d H:i:s"),
  82.         '%ip%'   => $remoteAddr,
  83.         '%typeerror%'=>$codestr,
  84.         '%code%' => $code,
  85.         '%msg%'  => $errmsg,
  86.         '%file%' => $filename,
  87.         '%line%' => $linenum,
  88.         '\t' =>"\t",
  89.         '\n' => "\n"
  90.     ));
  91.  
  92.     if(strpos($action , 'TRACE') !== false){
  93.         $arr = debug_backtrace();
  94.         $messageLog.="\ttrace:";
  95.         array_shift($arr);
  96.         foreach($arr as $k=>$t){
  97.             $messageLog.="\n\t$k\t".(isset($t['class'])?$t['class'].$t['type']:'').$t['function']."()\t";
  98.             $messageLog.=(isset($t['file'])?$t['file']:'[php]').' : '.(isset($t['line'])?$t['line']:'');
  99.         }
  100.         $messageLog.="\n";
  101.     }
  102.     $echoAsked = false;
  103.     // traitement du message
  104.     if(strpos($action , 'ECHOQUIET') !== false){
  105.         $echoAsked = true;
  106.         if(!$doEchoByResponse){
  107.             header("HTTP/1.1 500 Internal jelix error");
  108.             header('Content-type: text/plain');
  109.             echo 'JELIX PANIC ! Error during initialization !! ';
  110.         }elseif($gJCoord->addErrorMsg($codestr, $code, $conf['quietMessage'], '', '')){
  111.             $action.=' EXIT';
  112.         }
  113.     }elseif(strpos($action , 'ECHO') !== false){
  114.         $echoAsked = true;
  115.         if(!$doEchoByResponse){
  116.             header("HTTP/1.1 500 Internal jelix error");
  117.             header('Content-type: text/plain');
  118.             echo $messageLog;
  119.         }elseif($gJCoord->addErrorMsg($codestr, $code, $errmsg, $filename, $linenum)){
  120.             $action.=' EXIT';
  121.         }
  122.     }
  123.     if(strpos($action , 'LOGFILE') !== false){
  124.         @error_log($messageLog,3, JELIX_APP_LOG_PATH.$conf['logFile']);
  125.     }
  126.     if(strpos($action , 'MAIL') !== false){
  127.         error_log(wordwrap($messageLog,70),1, $conf['email'], str_replace(array('\\r','\\n'),array("\r","\n"),$conf['emailHeaders']));
  128.     }
  129.     if(strpos($action , 'SYSLOG') !== false){
  130.         error_log($messageLog,0);
  131.     }
  132.  
  133.     if(strpos($action , 'EXIT') !== false){
  134.         if($doEchoByResponse) {
  135.             if ($gJCoord->response)
  136.                 $gJCoord->response->outputErrors();
  137.             else if($echoAsked) {
  138.                 header("HTTP/1.1 500 Internal jelix error");
  139.                 header('Content-type: text/plain');
  140.                 foreach($gJCoord->errorMessages as $msg)
  141.                     echo $msg."\n";
  142.             }
  143.         }
  144.         jSession::end();
  145.         exit;
  146.     }
  147. }
  148. ?>

Documentation generated on Wed, 07 Sep 2011 13:47:15 +0200 by phpDocumentor 1.4.3