diff options
Diffstat (limited to 'framework')
-rw-r--r-- | framework/Exceptions/TErrorHandler.php | 116 | ||||
-rw-r--r-- | framework/Exceptions/error.tpl (renamed from framework/Exceptions/error.en) | 0 | ||||
-rw-r--r-- | framework/Exceptions/error404-en.tpl (renamed from framework/Exceptions/error404.en) | 0 | ||||
-rw-r--r-- | framework/Exceptions/error404-zh.tpl | 29 | ||||
-rw-r--r-- | framework/Exceptions/error404.tpl | 29 | ||||
-rw-r--r-- | framework/Exceptions/error500.tpl | 29 | ||||
-rw-r--r-- | framework/Exceptions/exception.tpl (renamed from framework/Exceptions/exception.en) | 0 |
7 files changed, 148 insertions, 55 deletions
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'].' <a href="http://www.pradosoft.com/">PRADO</a>/'.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'].' <a href="http://www.pradosoft.com/">PRADO</a>/'.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'].' <a href="http://www.pradosoft.com/">PRADO</a>/'.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.tpl index cff0f910..cff0f910 100644 --- a/framework/Exceptions/error.en +++ b/framework/Exceptions/error.tpl diff --git a/framework/Exceptions/error404.en b/framework/Exceptions/error404-en.tpl index 11585661..11585661 100644 --- a/framework/Exceptions/error404.en +++ b/framework/Exceptions/error404-en.tpl 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 @@ +<html>
+<head>
+<title>无法找到页面</title>
+<style>
+body {font-family:"Verdana";font-weight:normal;color:black;}
+h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
+h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
+h3 {font-family:"Verdana";font-weight:bold;font-size:11pt}
+p {font-family:"Verdana";font-weight:normal;color:black;font-size:9pt;margin-top: -5px}
+.version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;}
+</style>
+</head>
+
+<body bgcolor="white">
+<h1>%%ErrorMessage%%</h1>
+<h2>错误代码404</h2>
+<p>
+服务器无法找到您所请求的页面。
+如果您是手工输入页面地址的,请检查拼写是否正确。
+</p>
+<p>
+如果您确认这是服务器错误,请联系<a href="mailto:%%ServerAdmin%%">系统管理员</a>。
+</p>
+<div class="version">
+%%Version%%<br/>
+%%Time%%
+</div>
+</body>
+</html>
\ 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 @@ +<html>
+<head>
+<title>Page Not Found</title>
+<style>
+body {font-family:"Verdana";font-weight:normal;color:black;}
+h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
+h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
+h3 {font-family:"Verdana";font-weight:bold;font-size:11pt}
+p {font-family:"Verdana";font-weight:normal;color:black;font-size:9pt;margin-top: -5px}
+.version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;}
+</style>
+</head>
+
+<body bgcolor="white">
+<h1>%%ErrorMessage%%</h1>
+<h2>Error 404</h2>
+<p>
+The requested URL was not found on this server.
+If you entered the URL manually please check your spelling and try again.
+</p>
+<p>
+If you think this is a server error, please contact the <a href="mailto:%%ServerAdmin%%">webmaster</a>.
+</p>
+<div class="version">
+%%Version%%<br/>
+%%Time%%
+</div>
+</body>
+</html>
\ 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 @@ +<html>
+<head>
+<title>Internal Server Error</title>
+<style>
+body {font-family:"Verdana";font-weight:normal;color:black;}
+h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
+h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
+h3 {font-family:"Verdana";font-weight:bold;font-size:11pt}
+p {font-family:"Verdana";font-weight:normal;color:black;font-size:9pt;margin-top: -5px}
+.version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;}
+</style>
+</head>
+
+<body bgcolor="white">
+<h1>Internal Server Error</h1>
+<h2>%%ErrorMessage%%</h2>
+<p>
+An internal error occurred while the Web server was handling your request.
+Please contact the <a href="mailto:%%ServerAdmin%%">webmaster</a> to report this problem.
+</p>
+<p>
+Thank you.
+</p>
+<div class="version">
+%%Version%%<br/>
+%%Time%%
+</div>
+</body>
+</html>
\ No newline at end of file diff --git a/framework/Exceptions/exception.en b/framework/Exceptions/exception.tpl index 1d76c7f2..1d76c7f2 100644 --- a/framework/Exceptions/exception.en +++ b/framework/Exceptions/exception.tpl |