diff --git a/admin/admin_ajax.php b/admin/admin_ajax.php index e63c3392..164e06cd 100644 --- a/admin/admin_ajax.php +++ b/admin/admin_ajax.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-02-04 13:53:55 * @Last Modified by: printempw - * @Last Modified time: 2016-03-26 22:28:22 + * @Last Modified time: 2016-03-27 11:29:16 */ require "../libraries/session.inc.php"; @@ -30,7 +30,7 @@ if (isset($_GET['action'])) { $json['msg'] = "出现了奇怪的错误。。请联系作者"; } } else { - Utils::raise(1, '你没有选择任何文件哦'); + throw new E('你没有选择任何文件哦', 1); } } else if ($action == "change") { if (User::checkValidPwd($_POST['passwd'])) { @@ -62,7 +62,7 @@ if (isset($_GET['action'])) { $json['errno'] = 0; $json['msg'] = "成功地将用户 ".$_GET['uname']." 的优先皮肤模型更改为 ".$_POST['model']." 。"; } else { - Utils::raise(1, '非法参数。'); + throw new E('非法参数。', 1); } } else if ($action == "color") { if (isset($_POST['color_scheme'])) { @@ -71,10 +71,10 @@ if (isset($_GET['action'])) { $json['errno'] = 0; $json['msg'] = "修改配色成功。"; } else { - Utils::raise(1, '非法参数。'); + throw new E('非法参数。', 1); } } else { - Utils::raise(1, '非法参数。'); + throw new E('非法参数。', 1); } } diff --git a/ajax.php b/ajax.php index 97f8ecf9..51ce93ee 100644 --- a/ajax.php +++ b/ajax.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: printempw - * @Last Modified time: 2016-03-26 22:28:22 + * @Last Modified time: 2016-03-27 11:25:46 * * - login, register, logout * - upload, change, delete @@ -24,10 +24,10 @@ if (isset($_POST['uname'])) { if (User::checkValidUname($uname)) { $user = new User($_POST['uname']); } else { - Utils::raise(1, '无效的用户名。用户名只能包含数字,字母以及下划线。'); + throw new E('无效的用户名。用户名只能包含数字,字母以及下划线。', 1); } } else { - Utils::raise('1', '空用户名。'); + throw new E('空用户名。', 1); } $action = isset($_GET['action']) ? $_GET['action'] : null; $json = null; diff --git a/get.php b/get.php index dbf0e760..4adeb0d3 100644 --- a/get.php +++ b/get.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-02-02 20:56:42 * @Last Modified by: printempw - * @Last Modified time: 2016-03-27 10:57:45 + * @Last Modified time: 2016-03-27 11:25:36 * * All textures requests of legacy link will be handle here. */ @@ -15,7 +15,7 @@ if (isset($_GET['type']) && isset($_GET['uname'])) { $user = new User($_GET['uname']); if (!$user->is_registered) { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); - Utils::raise(1, 'Non-existent user.'); + throw new E('Non-existent user.', 1); } // Cache friendly $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? @@ -44,8 +44,8 @@ if (isset($_GET['type']) && isset($_GET['uname'])) { $size = (isset($_GET['size']) && $_GET['size'] != "") ? (int)$_GET['size'] : 128; $user->getAvatar($size); } else { - Utils::raise(1, 'Illegal parameters.'); + throw new E('Illegal parameters.', 1); } } else { - Utils::raise(1, 'Illegal parameters.'); + throw new E('Illegal parameters.', 1); } diff --git a/libraries/Database/Database.class.php b/libraries/Database/Database.class.php index adb244c9..5def9fa9 100644 --- a/libraries/Database/Database.class.php +++ b/libraries/Database/Database.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-02-02 21:59:06 * @Last Modified by: printempw - * @Last Modified time: 2016-03-26 19:04:39 + * @Last Modified time: 2016-03-27 11:32:04 */ namespace Database; @@ -12,6 +12,7 @@ use Database\EncryptInterface; use Database\SyncInterface; use Utils; use Mysqli; +use E; class Database implements EncryptInterface, SyncInterface { @@ -32,12 +33,11 @@ class Database implements EncryptInterface, SyncInterface error_reporting(E_ALL ^ E_NOTICE); if ($conn->connect_error) - Utils::showErrorPage($conn->connect_errno, - "无法连接至 MySQL 服务器。请确认 config.php 中的配置是否正确:".$conn->connect_error); + throw new E("无法连接至 MySQL 服务器。请确认 config.php 中的配置是否正确:".$conn->connect_error, $conn->connect_errno, true); if (!self::checkTableExist($conn)) - Utils::showErrorPage(-1, "数据库中不存在 ".DB_PREFIX."users 或 ".DB_PREFIX."options 表。请先运行 /admin/install.php 进行安装。"); + throw new E("数据库中不存在 ".DB_PREFIX."users 或 ".DB_PREFIX."options 表。请先访问 /admin/install.php 进行安装。", -1, true); if (!is_dir(BASE_DIR."/textures/")) - Utils::showErrorPage(-1, "textures 文件夹不存在。请先运行 /admin/install.php 进行安装,或者手动放置一个。"); + throw new E("textures 文件夹不存在。请先运行 /admin/install.php 进行安装,或者手动放置一个。", -1, true); $conn->query("SET names 'utf8'"); return $conn; @@ -58,7 +58,7 @@ class Database implements EncryptInterface, SyncInterface if (!$this->connection->error) { return $result; } - Utils::raise(-1, "Database query error: ".$this->connection->error); + throw new E("Database query error: ".$this->connection->error, -1); } public function fetchArray($sql) { diff --git a/libraries/E.class.php b/libraries/E.class.php new file mode 100644 index 00000000..cf97a2cb --- /dev/null +++ b/libraries/E.class.php @@ -0,0 +1,40 @@ +showErrorPage(); + } else { + $this->showErrorJson(); + } + } + + private function showErrorJson() { + $exception['errno'] = $this->code; + $exception['msg'] = $this->message; + header('Content-type: application/json; charset=utf-8'); + exit(json_encode($exception)); + } + + private function showErrorPage() { + $message = $this->message; + $code = $this->code; + require BASE_DIR."/templates/error.tpl.php"; + exit; + } +} diff --git a/libraries/Option.class.php b/libraries/Option.class.php index 544c1fb8..ac7663a5 100644 --- a/libraries/Option.class.php +++ b/libraries/Option.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-03-18 14:02:12 * @Last Modified by: printempw - * @Last Modified time: 2016-03-26 22:53:06 + * @Last Modified time: 2016-03-27 11:28:24 */ use Database\Database; @@ -15,7 +15,7 @@ class Option $sql = "SELECT * FROM ".DB_PREFIX."options WHERE `option_name` = '$key'"; $result = $conn->query($sql); if ($conn->error) - Utils::raise(-1, "Database query error: ".$conn->error); + throw new E("Database query error: ".$conn->error, -1); return $result->fetch_array()['option_value']; } @@ -27,7 +27,7 @@ class Option $sql = "UPDATE ".DB_PREFIX."options SET `option_value`='$value' WHERE `option_name`='$key'"; $result = $conn->query($sql); if ($conn->error) - Utils::raise(-1, "Database query error: ".$conn->error); + throw new E("Database query error: ".$conn->error, -1); else return true; } @@ -40,7 +40,7 @@ class Option $sql = "INSERT INTO ".DB_PREFIX."options (`option_name`, `option_value`) VALUES ('$key', '$value')"; $result = $conn->query($sql); if ($conn->error) - Utils::raise(-1, "Database query error: ".$conn->error); + throw new E("Database query error: ".$conn->error, -1); else return true; } else { @@ -65,7 +65,7 @@ class Option $sql = "DELETE FROM ".DB_PREFIX."options WHERE `option_name`='$key'"; $result = $conn->query($sql); if ($conn->error) - Utils::raise(-1, "Database query error: ".$conn->error); + throw new E("Database query error: ".$conn->error, -1); else return true; } else { diff --git a/libraries/User.class.php b/libraries/User.class.php index f2954c04..50aa29b1 100644 --- a/libraries/User.class.php +++ b/libraries/User.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: printempw - * @Last Modified time: 2016-03-27 10:58:10 + * @Last Modified time: 2016-03-27 11:27:06 */ use Database\Database; @@ -47,9 +47,9 @@ class User public static function checkValidPwd($passwd) { if (strlen($passwd) > 16 || strlen($passwd) < 5) { - Utils::raise(1, '无效的密码。密码长度应该大于 6 并小于 15。'); + throw new E('无效的密码。密码中包含了奇怪的字符。', 1); } else if (Utils::convertString($passwd) != $passwd) { - Utils::raise(1, '无效的密码。密码中包含了奇怪的字符。'); + throw new E('无效的密码。密码长度应该大于 6 并小于 15。', 1); } return true; } @@ -113,11 +113,11 @@ class User return Utils::fread($filename); } else { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); - Utils::showErrorPage(404, '请求的贴图已被删除。'); + throw new E('请求的贴图已被删除。', 404, true); } } else { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); - Utils::showErrorPage(404, '该用户尚未上传请求的贴图类型 '.$type.'。'); + throw new E('该用户尚未上传请求的贴图类型 '.$type.'。', 404, true); } } @@ -190,7 +190,7 @@ class User } $json['cape'] = $this->getTexture('cape'); } else { - Utils::showErrorPage(-1, '配置文件错误:不支持的 API_TYPE。'); + throw new E('配置文件错误:不支持的 API_TYPE。', -1, true); } } else { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); diff --git a/libraries/Utils.class.php b/libraries/Utils.class.php index 1b64b408..7d3c7a17 100644 --- a/libraries/Utils.class.php +++ b/libraries/Utils.class.php @@ -3,30 +3,11 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: printempw - * @Last Modified time: 2016-03-27 08:15:03 + * @Last Modified time: 2016-03-27 11:33:59 */ class Utils { - /** - * Custom error handler - * - * @param int $errno - * @param string $msg, message to show - * @return void - */ - public static function raise($errno = -1, $msg = "Error occured.") { - $exception['errno'] = $errno; - $exception['msg'] = $msg; - header('Content-type: application/json; charset=utf-8'); - die(json_encode($exception)); - } - - public static function showErrorPage($errno = -1, $msg = "Error occured.") { - require BASE_DIR."/templates/error.tpl.php"; - die(); - } - /** * Rename uploaded file * diff --git a/libraries/View.class.php b/libraries/View.class.php index 087a0af7..c398f2b5 100644 --- a/libraries/View.class.php +++ b/libraries/View.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-03-18 20:49:48 * @Last Modified by: printempw - * @Last Modified time: 2016-03-19 09:53:49 + * @Last Modified time: 2016-03-27 11:23:25 */ class View @@ -13,7 +13,7 @@ class View if (file_exists($filename)) { include $filename; } else { - Utils::showErrorPage('2', "未找到模板文件 ".$tpl_name.".tpl.php"); + throw new E("未找到模板文件 ".$tpl_name.".tpl.php", 2, true); } } } diff --git a/libraries/autoloader.php b/libraries/autoloader.php index b60ab545..d0590e24 100644 --- a/libraries/autoloader.php +++ b/libraries/autoloader.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-02-02 21:17:59 * @Last Modified by: printempw - * @Last Modified time: 2016-03-26 18:53:05 + * @Last Modified time: 2016-03-27 11:19:58 */ function __autoload($classname) { @@ -14,5 +14,5 @@ function __autoload($classname) { require_once($filename); } if (!file_exists($dir.'/config.php')) - Utils::showErrorPage(-1, '未找到 `config.php`,请确认配置文件是否存在。'); + throw new E('未找到 `config.php`,请确认配置文件是否存在。', -1, true); require "$dir/config.php"; diff --git a/templates/error.tpl.php b/templates/error.tpl.php index f7d081fb..ef100261 100644 --- a/templates/error.tpl.php +++ b/templates/error.tpl.php @@ -14,72 +14,28 @@
错误码:
-详细信息:
+错误码:
+详细信息: