added custom Exception class E

This commit is contained in:
printempw 2016-03-27 11:38:27 +08:00
parent 2c043824a8
commit d0c67d7110
11 changed files with 88 additions and 111 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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 表。请先访问 <a href='./admin/install.php'>/admin/install.php</a> 进行安装。", -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) {

40
libraries/E.class.php Normal file
View File

@ -0,0 +1,40 @@
<?php
/**
* @Author: printempw
* @Date: 2016-03-27 11:04:14
* @Last Modified by: printempw
* @Last Modified time: 2016-03-27 11:36:57
*/
class E extends Exception
{
/**
* Custom error handler
*
* @param string $message
* @param integer $code
* @param boolean $render, to show a error page
*/
function __construct($message = "Error occured.", $code = -1, $render = false) {
parent::__construct($message, $code);
if ($render) {
$this->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;
}
}

View File

@ -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 {

View File

@ -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");

View File

@ -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
*

View File

@ -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);
}
}
}

View File

@ -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";

View File

@ -14,72 +14,28 @@
<h1>出现了一些错误:</h1>
<p>错误码:<?php echo $errno; ?></p>
<p>详细信息:<?php echo $msg; ?></p>
<p>错误码:<?php echo $code; ?></p>
<p>详细信息:<?php echo $message; ?></p>
</body>
<style type="text/css">
html {
background: #f1f1f1;
margin: 0 20px;
font-weight: 400;
}
body {
background: #FFF none repeat scroll 0% 0%;
color: #444;
font-family: Ubuntu, 'Microsoft Yahei', 'Microsoft Jhenghei', sans-serif;
margin: 140px auto 25px;
padding: 20px 20px 10px;
max-width: 700px;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.13);
}
h1, h2 {
border-bottom: 1px solid #DEDEDE;
clear: both;
color: #666;
font-size: 24px;
}
#logo a, p, h1, h2 {
font-weight: 400;
}
#logo, h1, h2 {
padding: 0px 0px 7px;
}
p {
padding-bottom: 2px;
font-size: 14px;
line-height: 1.5;
}
#logo a {
font-family: Minecraft, sans-serif;
font-size: 50px;
color: #666;
height: 84px;
line-height: 1.3em;
margin: -130px auto 25px;
padding: 0;
outline: 0;
text-decoration: none;
overflow: hidden;
display: block;
}
html { background: #f1f1f1; margin: 0 20px; font-weight: 400; }
body { background: #FFF none repeat scroll 0% 0%; color: #444; font-family: Ubuntu, 'Microsoft Yahei', 'Microsoft Jhenghei', sans-serif; margin: 140px auto 25px; padding: 20px 20px 10px; max-width: 700px; box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.13); }
h1, h2 { border-bottom: 1px solid #DEDEDE; clear: both; color: #666; font-size: 24px; }
a:active, a:hover { color: #00a0d2; }
a { color: #0073aa; }
#logo a, p, h1, h2 { font-weight: 400; }
#logo, h1, h2 { padding: 0px 0px 7px; }
p { padding-bottom: 2px; font-size: 14px; line-height: 1.5; }
#logo a { font-family: Minecraft, sans-serif; font-size: 50px; color: #666; height: 84px; line-height: 1.3em; margin: -130px auto 25px; padding: 0; outline: 0; text-decoration: none; overflow: hidden; display: block; }
#logo { margin: 6px 0 14px; border-bottom: none; text-align: center; }
/* Mobile phone */
@media (max-width: 48em) {
#logo a {
font-size: 40px;
}
#logo a { font-size: 40px; }
}
@media (max-width: 35.5em) {
#logo a {
font-size: 30px;
}
}
#logo {
margin: 6px 0 14px;
border-bottom: none;
text-align: center;
#logo a { font-size: 30px; }
}
</style>
</html>