_application=$application; $application->attachEventHandler('Error',array($this,'handleError')); $application->setErrorHandler($this); foreach($config->getElementsByTagName('error') as $node) { if(($code=$node->getAttribute('code'))===null || $code==='*') $code=0; else $this->_errorPages[$node->getAttribute('code')]=$node->getAttribute('page'); } } /** * @return string id of this module */ public function getID() { return $this->_id; } /** * @param string id of this module */ public function setID($value) { $this->_id=$value; } public function handleError($sender,$param) { static $handling=false; if($handling) $this->handleRecursiveError($param); else { $handling=true; if(($response=Prado::getApplication()->getResponse())!==null) $response->clear(); if($param instanceof THttpException) $this->handleExternalError($param->getStatusCode(),$param); else if(Prado::getApplication()->getMode()==='Debug') $this->displayException($param); else $this->handleExternalError(500,$param); } exit(1); } protected function handleExternalError($statusCode,$exception) { if(!($exception instanceof THttpException)) error_log($exception->__toString()); if(isset($this->_errorPages["$statusCode"]) || isset($this->_errorPages[0])) { if(isset($this->_errorPages["$statusCode"])) $page=Prado::createComponent($this->_errorPages["$statusCode"]); else $page=Prado::createComponent($this->_errorPages[0]); $writer=new THtmlTextWriter($this->_application->getResponse()); $page->run($writer); $writer->flush(); } else { $base=dirname(__FILE__).'/error'; $languages=Prado::getUserLanguages(); $lang=$languages[0]; if(is_file("$base$statusCode.$lang")) $errorFile="$base$statusCode.$lang"; else if(is_file("$base$statusCode.en")) $errorFile="$base$statusCode.en"; else if(is_file("$base.$lang")) $errorFile="$base.$lang"; else $errorFile="$base.en"; if(($content=@file_get_contents($errorFile))===false) die("Unable to open error template file '$errorFile'."); $serverAdmin=isset($_SERVER['SERVER_ADMIN'])?$_SERVER['SERVER_ADMIN']:''; $fields=array( '%%StatusCode%%', '%%ErrorMessage%%', '%%ServerAdmin%%', '%%Version%%', '%%Time%%' ); $values=array( "$statusCode", htmlspecialchars($exception->getMessage()), $serverAdmin, $_SERVER['SERVER_SOFTWARE'].' PRADO/'.Prado::getVersion(), strftime('%Y-%m-%d %H:%m',time()) ); echo str_replace($fields,$values,$content); } } protected function handleRecursiveError($exception) { if(Prado::getApplication()->getMode()==='Debug') { echo "
".$exception."\n"; echo ""; } else { error_log("Error happened while processing an existing error:\n".$param->__toString()); header('HTTP/1.0 500 Internal Error'); } } protected function displayException($exception) { $lines=file($exception->getFile()); $errorLine=$exception->getLine(); $beginLine=$errorLine-9>=0?$errorLine-9:0; $endLine=$errorLine+8<=count($lines)?$errorLine+8:count($lines); $source=''; for($i=$beginLine;$i<$endLine;++$i) { if($i===$errorLine-1) { $line=highlight_string(sprintf("Line %4d: %s",$i+1,$lines[$i]),true); $source.="