00001 <?php 00002 00006 class oxExceptionHandler 00007 { 00013 protected $_sFileName = 'EXCEPTION_LOG.txt'; 00014 00020 protected $_iDebug = 0; 00021 00027 public function __construct( $iDebug = 0 ) 00028 { 00029 $this->_iDebug = (int) $iDebug; 00030 } 00031 00039 public function setIDebug($iDebug) 00040 { 00041 $this->_iDebug = $iDebug; 00042 } 00043 00051 public function setLogFileName($sFile) 00052 { 00053 $this->_sFileName = $sFile; 00054 } 00055 00061 public function getLogFileName() 00062 { 00063 return $this->_sFileName; 00064 } 00065 00073 public function handleUncaughtException( Exception $oEx ) 00074 { 00075 // split between php or shop exception 00076 if ( !$oEx instanceof oxException ) { 00077 $this->_dealWithNoOxException( $oEx ); 00078 return; // Return straight away ! (in case of unit testing) 00079 } 00080 00081 $oEx->setLogFileName( $this->_sFileName ); // set common log file ... 00082 00083 $this->_uncaughtException( $oEx ); // Return straight away ! (in case of unit testing) 00084 } 00085 00094 protected function _uncaughtException( oxException $oEx ) 00095 { 00096 // exception occured in function processing 00097 $oEx->setNotCaught(); 00098 // general log entry for all exceptions here 00099 $oEx->debugOut(); 00100 00101 if ( defined( 'OXID_PHP_UNIT' ) ) { 00102 return $oEx->getString(); 00103 } elseif ( 0 != $this->_iDebug ) { 00104 oxUtils::getInstance()->showMessageAndExit( $oEx->getString() ); 00105 } 00106 00107 //simple safe redirect in productive mode 00108 $this->_safeShopRedirectAndExit( "offline.html" ); 00109 00110 //should not be reached 00111 return ; 00112 } 00113 00122 protected function _dealWithNoOxException( Exception $oEx ) 00123 { 00124 if ( 0 != $this->_iDebug ) { 00125 $sLogMsg = date( 'Y-m-d H:i:s' ) . $oEx . "\n---------------------------------------------\n"; 00126 oxUtils::getInstance()->writeToLog( $sLogMsg, $this->getLogFileName() ); 00127 if ( defined( 'OXID_PHP_UNIT' ) ) { 00128 return; 00129 } elseif ( 0 != $this->_iDebug ) { 00130 oxUtils::getInstance()->showMessageAndExit( $sLogMsg ); 00131 } 00132 } 00133 00134 $this->_safeShopRedirectAndExit( 'offline.html' ); 00135 00136 } 00137 00147 protected function _safeShopRedirectAndExit($sUrl) 00148 { 00149 // No redirects in unit testing .. as redirection ends also unit testing script.. 00150 if ( defined('OXID_PHP_UNIT')) { 00151 return ; 00152 } 00153 00154 //make the redirect directly to be independetn from other objects 00155 header("HTTP/1.1 500 Internal Server Error"); 00156 header("Location: ".$sUrl); 00157 header("Connection: close"); 00158 00159 } 00160 00172 public function __call( $sMethod, $aArgs) 00173 { 00174 if ( defined( 'OXID_PHP_UNIT' ) ) { 00175 if ( substr( $sMethod, 0, 4) == "UNIT") { 00176 $sMethod = str_replace( "UNIT", "_", $sMethod); 00177 } 00178 if ( method_exists( $this, $sMethod)) { 00179 return call_user_func_array( array( & $this, $sMethod), $aArgs ); 00180 } 00181 } 00182 00183 throw new oxSystemComponentException( "Function '$sMethod' does not exist or is not accessable! (".__CLASS__.")".PHP_EOL); 00184 } 00185 }