diff --git a/var/Typecho/Common.php b/var/Typecho/Common.php index 1e96392f..c38f5827 100644 --- a/var/Typecho/Common.php +++ b/var/Typecho/Common.php @@ -287,64 +287,39 @@ class Typecho_Common spl_autoload_register(['Typecho_Common', '__autoLoad']); /** 设置异常截获函数 */ - set_exception_handler(['Typecho_Common', 'exceptionHandle']); - } - - /** - * 异常截获函数 - * - * @access public - * - * @param $exception 截获的异常 - * - * @return void - */ - public static function exceptionHandle($exception) - { - if (defined('__TYPECHO_DEBUG__') && __TYPECHO_DEBUG__) { - echo '
';
- echo '' . htmlspecialchars($exception->getMessage()) . '
';
- echo htmlspecialchars($exception->__toString());
- echo '
';
- } else {
- @ob_end_clean();
- if (404 == $exception->getCode() && !empty(self::$exceptionHandle)) {
- $handleClass = self::$exceptionHandle;
- new $handleClass($exception);
+ set_exception_handler(function (Throwable $exception) {
+ if (defined('__TYPECHO_DEBUG__') && __TYPECHO_DEBUG__) {
+ echo '';
+ echo '' . htmlspecialchars($exception->getMessage()) . '
';
+ echo htmlspecialchars($exception->__toString());
+ echo '
';
} else {
- self::error($exception);
+ @ob_end_clean();
+ if (404 == $exception->getCode() && !empty(self::$exceptionHandle)) {
+ $handleClass = self::$exceptionHandle;
+ new $handleClass($exception);
+ } else {
+ self::error($exception);
+ }
}
- }
- exit;
+ exit(1);
+ });
}
/**
* 输出错误页面
*
- * @access public
- *
- * @param mixed $exception 错误信息
- *
- * @return void
+ * @param Throwable $exception 错误信息
*/
- public static function error($exception)
+ public static function error(Throwable $exception)
{
- $isException = is_object($exception);
- $message = '';
-
- if ($isException) {
- $code = $exception->getCode();
- $message = $exception->getMessage();
- } else {
- $code = $exception;
- }
-
+ $code = $exception->getCode() ?: 500;
+ $message = $exception->getMessage();
$charset = self::$charset;
- if ($isException && $exception instanceof Typecho_Db_Exception) {
+ if ($exception instanceof Typecho_Db_Exception) {
$code = 500;
- @error_log($message);
//覆盖原始错误信息
$message = 'Database Server Error';
@@ -355,23 +330,8 @@ class Typecho_Common
} elseif ($exception instanceof Typecho_Db_Query_Exception) {
$message = 'Database Query Error';
}
- } else {
- switch ($code) {
- case 500:
- $message = 'Server Error';
- break;
-
- case 404:
- $message = 'Page Not Found';
- break;
-
- default:
- $code = 'Error';
- break;
- }
}
-
/** 设置http code */
if (is_numeric($code) && $code > 200) {
Typecho_Response::setStatus($code);
@@ -428,7 +388,7 @@ class Typecho_Common
EOF;
}
- exit;
+ exit(1);
}
/**
diff --git a/var/Typecho/Widget.php b/var/Typecho/Widget.php
index fbef5289..ff81488e 100644
--- a/var/Typecho/Widget.php
+++ b/var/Typecho/Widget.php
@@ -1,11 +1,4 @@
execute();
- self::$_widgetPool[$alias] = $widget;
+ self::$widgetPool[$alias] = $widget;
}
- return self::$_widgetPool[$alias];
+ return self::$widgetPool[$alias];
}
/**
@@ -190,8 +187,8 @@ abstract class Typecho_Widget
*/
public static function destroy(string $alias)
{
- if (isset(self::$_widgetPool[$alias])) {
- unset(self::$_widgetPool[$alias]);
+ if (isset(self::$widgetPool[$alias])) {
+ unset(self::$widgetPool[$alias]);
}
}
@@ -243,8 +240,11 @@ abstract class Typecho_Widget
public function parse(string $format)
{
while ($this->next()) {
- echo preg_replace_callback("/\{([_a-z0-9]+)\}/i",
- [$this, '__parseCallback'], $format);
+ echo preg_replace_callback(
+ "/\{([_a-z0-9]+)\}/i",
+ [$this, '__parseCallback'],
+ $format
+ );
}
}
diff --git a/var/Widget/Archive.php b/var/Widget/Archive.php
index 2e63292b..3bdc750d 100644
--- a/var/Widget/Archive.php
+++ b/var/Widget/Archive.php
@@ -1380,7 +1380,7 @@ class Widget_Archive extends Widget_Abstract_Contents
/** 文件不存在 */
if (!$validated) {
- Typecho_Common::error(500);
+ throw new Typecho_Widget_Exception(_t('文件不存在'), 500);
}
/** 挂接插件 */
diff --git a/var/Widget/Do.php b/var/Widget/Do.php
index 3bae6f8f..92106b6c 100644
--- a/var/Widget/Do.php
+++ b/var/Widget/Do.php
@@ -1,12 +1,8 @@
'Widget_Ajax',
- 'login' => 'Widget_Login',
- 'logout' => 'Widget_Logout',
- 'register' => 'Widget_Register',
- 'upgrade' => 'Widget_Upgrade',
- 'upload' => 'Widget_Upload',
- 'service' => 'Widget_Service',
- 'xmlrpc' => 'Widget_XmlRpc',
- 'comments-edit' => 'Widget_Comments_Edit',
- 'contents-page-edit' => 'Widget_Contents_Page_Edit',
- 'contents-post-edit' => 'Widget_Contents_Post_Edit',
+ private $map = [
+ 'ajax' => 'Widget_Ajax',
+ 'login' => 'Widget_Login',
+ 'logout' => 'Widget_Logout',
+ 'register' => 'Widget_Register',
+ 'upgrade' => 'Widget_Upgrade',
+ 'upload' => 'Widget_Upload',
+ 'service' => 'Widget_Service',
+ 'xmlrpc' => 'Widget_XmlRpc',
+ 'comments-edit' => 'Widget_Comments_Edit',
+ 'contents-page-edit' => 'Widget_Contents_Page_Edit',
+ 'contents-post-edit' => 'Widget_Contents_Post_Edit',
'contents-attachment-edit' => 'Widget_Contents_Attachment_Edit',
- 'metas-category-edit' => 'Widget_Metas_Category_Edit',
- 'metas-tag-edit' => 'Widget_Metas_Tag_Edit',
- 'options-discussion' => 'Widget_Options_Discussion',
- 'options-general' => 'Widget_Options_General',
- 'options-permalink' => 'Widget_Options_Permalink',
- 'options-reading' => 'Widget_Options_Reading',
- 'plugins-edit' => 'Widget_Plugins_Edit',
- 'themes-edit' => 'Widget_Themes_Edit',
- 'users-edit' => 'Widget_Users_Edit',
- 'users-profile' => 'Widget_Users_Profile',
- 'backup' => 'Widget_Backup'
+ 'metas-category-edit' => 'Widget_Metas_Category_Edit',
+ 'metas-tag-edit' => 'Widget_Metas_Tag_Edit',
+ 'options-discussion' => 'Widget_Options_Discussion',
+ 'options-general' => 'Widget_Options_General',
+ 'options-permalink' => 'Widget_Options_Permalink',
+ 'options-reading' => 'Widget_Options_Reading',
+ 'plugins-edit' => 'Widget_Plugins_Edit',
+ 'themes-edit' => 'Widget_Themes_Edit',
+ 'users-edit' => 'Widget_Users_Edit',
+ 'users-profile' => 'Widget_Users_Profile',
+ 'backup' => 'Widget_Backup'
];
/**
* 入口函数,初始化路由器
*
- * @access public
- * @return void
- * @throws Typecho_Widget_Exception
+ * @throws Typecho_Widget_Exception|Typecho_Exception
*/
public function execute()
{
@@ -69,7 +63,7 @@ class Widget_Do extends Typecho_Widget
}
} else {
/** 判断是否为plugin */
- $actionTable = array_merge($this->_map, unserialize($this->widget('Widget_Options')->actionTable));
+ $actionTable = array_merge($this->map, unserialize($this->widget('Widget_Options')->actionTable));
if (isset($actionTable[$action])) {
$widgetName = $actionTable[$action];