From 54f6740a4aeeae3463759031674922acfe421748 Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 18 Nov 2005 23:06:11 +0000 Subject: --- framework/Exceptions/TErrorHandler.php | 116 +++++++++++++++++---------------- framework/Exceptions/error.en | 28 -------- framework/Exceptions/error.tpl | 28 ++++++++ framework/Exceptions/error404-en.tpl | 29 +++++++++ framework/Exceptions/error404-zh.tpl | 29 +++++++++ framework/Exceptions/error404.en | 29 --------- framework/Exceptions/error404.tpl | 29 +++++++++ framework/Exceptions/error500.tpl | 29 +++++++++ framework/Exceptions/exception.en | 37 ----------- framework/Exceptions/exception.tpl | 37 +++++++++++ 10 files changed, 242 insertions(+), 149 deletions(-) delete mode 100644 framework/Exceptions/error.en create mode 100644 framework/Exceptions/error.tpl create mode 100644 framework/Exceptions/error404-en.tpl create mode 100644 framework/Exceptions/error404-zh.tpl delete mode 100644 framework/Exceptions/error404.en create mode 100644 framework/Exceptions/error404.tpl create mode 100644 framework/Exceptions/error500.tpl delete mode 100644 framework/Exceptions/exception.en create mode 100644 framework/Exceptions/exception.tpl (limited to 'framework') diff --git a/framework/Exceptions/TErrorHandler.php b/framework/Exceptions/TErrorHandler.php index 29daad74..fe756e32 100644 --- a/framework/Exceptions/TErrorHandler.php +++ b/framework/Exceptions/TErrorHandler.php @@ -2,6 +2,9 @@ class TErrorHandler extends TComponent implements IModule { + const ERROR_FILE_NAME='error'; + const EXCEPTION_FILE_NAME='exception'; + /** * @var string module ID */ @@ -11,9 +14,9 @@ class TErrorHandler extends TComponent implements IModule */ private $_application; /** - * @var array list of pages for displaying various HTTP errors + * @var string error template directory */ - private $_errorPages=array(); + private $_templatePath=null; /** * Initializes the module. @@ -26,13 +29,6 @@ class TErrorHandler extends TComponent implements IModule $this->_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'); - } } /** @@ -51,10 +47,28 @@ class TErrorHandler extends TComponent implements IModule $this->_id=$value; } + public function getErrorTemplatePath() + { + return $this->_templatePath; + } + + public function setErrorTemplatePath($value) + { + if(($templatePath=Prado::getPathOfNamespace($this->_templatePath))!==null && is_dir($templatePath)) + $this->_templatePath=$templatePath; + else + throw new TConfigurationException('errorhandler_errortemplatepath_invalid',$value); + } + public function handleError($sender,$param) { static $handling=false; - if($handling) + // We need to restore error and exception handlers, + // otherwise because errors occured in error handler + // will not be handled properly. + restore_error_handler(); + restore_exception_handler(); + if($handling) // ensure that we do not enter infinite loop of error handling $this->handleRecursiveError($param); else { @@ -75,49 +89,39 @@ class TErrorHandler extends TComponent implements IModule { 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(); - } + if($this->_templatePath===null) + $this->_templatePath=dirname(__FILE__); + $base=$this->_templatePath.'/'.self::ERROR_FILE_NAME; + $lang=array_shift(explode('-',array_shift(Prado::getUserLanguages()))); + if(!empty($lang) && !ctype_alpha($lang)) + die('No hack attempt please.'); + if(is_file("$base$statusCode-$lang.tpl")) + $errorFile="$base$statusCode-$lang.tpl"; + else if(is_file("$base$statusCode.tpl")) + $errorFile="$base$statusCode.tpl"; + else if(is_file("$base-$lang.tpl")) + $errorFile="$base-$lang.tpl"; 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'."); + $errorFile="$base.tpl"; + 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); - } + $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) @@ -171,12 +175,14 @@ class TErrorHandler extends TComponent implements IModule $_SERVER['SERVER_SOFTWARE'].' PRADO/'.Prado::getVersion(), strftime('%Y-%m-%d %H:%m',time()) ); - $languages=Prado::getUserLanguages(); - $exceptionFile=dirname(__FILE__).'/exception.'.$languages[0]; + $lang=array_shift(explode('-',array_shift(Prado::getUserLanguages()))); + if(!empty($lang) && !ctype_alpha($lang)) + die('No hack attempt please.'); + $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'-'.$lang.'.tpl'; if(!is_file($exceptionFile)) - $exceptionFile=dirname(__FILE__).'/exception.en'; + $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'.tpl'; if(($content=@file_get_contents($exceptionFile))===false) - die("Unable to open exception template file '$errorFile'."); + die("Unable to open exception template file '$exceptionFile'."); echo str_replace($fields,$values,$content); } } diff --git a/framework/Exceptions/error.en b/framework/Exceptions/error.en deleted file mode 100644 index cff0f910..00000000 --- a/framework/Exceptions/error.en +++ /dev/null @@ -1,28 +0,0 @@ - - -%%ErrorMessage%% - - - - -

Error %%StatusCode%%

-

%%ErrorMessage%%

-

-The above error happened when the server was processing your request. -

-

-If you think this is a server error, please contact the webmaster. -

-
-%%Version%%
-%%Time%% -
- - \ No newline at end of file diff --git a/framework/Exceptions/error.tpl b/framework/Exceptions/error.tpl new file mode 100644 index 00000000..cff0f910 --- /dev/null +++ b/framework/Exceptions/error.tpl @@ -0,0 +1,28 @@ + + +%%ErrorMessage%% + + + + +

Error %%StatusCode%%

+

%%ErrorMessage%%

+

+The above error happened when the server was processing your request. +

+

+If you think this is a server error, please contact the webmaster. +

+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file diff --git a/framework/Exceptions/error404-en.tpl b/framework/Exceptions/error404-en.tpl new file mode 100644 index 00000000..11585661 --- /dev/null +++ b/framework/Exceptions/error404-en.tpl @@ -0,0 +1,29 @@ + + +Page Not Found + + + + +

%%ErrorMessage%%

+

Error 404

+

+The requested URL was not found on this server. +If you entered the URL manually please check your spelling and try again. +

+

+If you think this is a server error, please contact the webmaster. +

+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file diff --git a/framework/Exceptions/error404-zh.tpl b/framework/Exceptions/error404-zh.tpl new file mode 100644 index 00000000..98ec04d8 --- /dev/null +++ b/framework/Exceptions/error404-zh.tpl @@ -0,0 +1,29 @@ + + +无法找到页面 + + + + +

%%ErrorMessage%%

+

错误代码404

+

+服务器无法找到您所请求的页面。 +如果您是手工输入页面地址的,请检查拼写是否正确。 +

+

+如果您确认这是服务器错误,请联系系统管理员。 +

+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file diff --git a/framework/Exceptions/error404.en b/framework/Exceptions/error404.en deleted file mode 100644 index 11585661..00000000 --- a/framework/Exceptions/error404.en +++ /dev/null @@ -1,29 +0,0 @@ - - -Page Not Found - - - - -

%%ErrorMessage%%

-

Error 404

-

-The requested URL was not found on this server. -If you entered the URL manually please check your spelling and try again. -

-

-If you think this is a server error, please contact the webmaster. -

-
-%%Version%%
-%%Time%% -
- - \ No newline at end of file diff --git a/framework/Exceptions/error404.tpl b/framework/Exceptions/error404.tpl new file mode 100644 index 00000000..11585661 --- /dev/null +++ b/framework/Exceptions/error404.tpl @@ -0,0 +1,29 @@ + + +Page Not Found + + + + +

%%ErrorMessage%%

+

Error 404

+

+The requested URL was not found on this server. +If you entered the URL manually please check your spelling and try again. +

+

+If you think this is a server error, please contact the webmaster. +

+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file diff --git a/framework/Exceptions/error500.tpl b/framework/Exceptions/error500.tpl new file mode 100644 index 00000000..2f0901fb --- /dev/null +++ b/framework/Exceptions/error500.tpl @@ -0,0 +1,29 @@ + + +Internal Server Error + + + + +

Internal Server Error

+

%%ErrorMessage%%

+

+An internal error occurred while the Web server was handling your request. +Please contact the webmaster to report this problem. +

+

+Thank you. +

+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file diff --git a/framework/Exceptions/exception.en b/framework/Exceptions/exception.en deleted file mode 100644 index 1d76c7f2..00000000 --- a/framework/Exceptions/exception.en +++ /dev/null @@ -1,37 +0,0 @@ - - -%%ErrorType%% - - - - -

%%ErrorType%%

-

Description

-

%%ErrorMessage%%

-

-

Source File

-

%%SourceFile%%

-
-%%SourceCode%% -
-

Stack Trace

-
-
-%%StackTrace%%
-
-
-
-%%Version%%
-%%Time%% -
- - \ No newline at end of file diff --git a/framework/Exceptions/exception.tpl b/framework/Exceptions/exception.tpl new file mode 100644 index 00000000..1d76c7f2 --- /dev/null +++ b/framework/Exceptions/exception.tpl @@ -0,0 +1,37 @@ + + +%%ErrorType%% + + + + +

%%ErrorType%%

+

Description

+

%%ErrorMessage%%

+

+

Source File

+

%%SourceFile%%

+
+%%SourceCode%% +
+

Stack Trace

+
+
+%%StackTrace%%
+
+
+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file -- cgit v1.2.3