diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index ef8d106a..bee624ec 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -46,6 +46,13 @@ class Handler extends ExceptionHandler $e = new NotFoundHttpException($e->getMessage(), $e); } + if ($e instanceof PrettyPageException && PHP_SAPI != "cli") { + echo \View::make('errors.e')->with('code', $e->getCode()) + ->with('message', $e->getMessage()) + ->render(); + exit; + } + if (config('app.debug')) { foreach ($this->dontReport as $type) { if ($e instanceof $type) { diff --git a/app/Exceptions/PrettyPageException.php b/app/Exceptions/PrettyPageException.php new file mode 100644 index 00000000..e06f9c2b --- /dev/null +++ b/app/Exceptions/PrettyPageException.php @@ -0,0 +1,8 @@ +is_registered) - throw new E('用户不存在', 1); + View::json('用户不存在', 1); if ($action == "email") { Validate::checkPost(['email']); @@ -210,7 +210,7 @@ class AdminController extends BaseController View::json('账号已被成功删除', 0); } else { - throw new E('非法参数', 1); + View::json('非法参数', 1); } } @@ -263,7 +263,7 @@ class AdminController extends BaseController if (PlayerModel::where('pid', $_POST['pid'])->delete()) View::json('角色已被成功删除', 0); } else { - throw new E('非法参数', 1); + View::json('非法参数', 1); } } diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index bcd0ce40..d92a5e9e 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Routing\Controller as BaseController; use App\Models\User; use App\Models\UserModel; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Validate; use Mail; use View; @@ -76,7 +76,7 @@ class AuthController extends BaseController View::json('登出成功~', 0); } else { - throw new E('并没有有效的 session', 1); + View::json('并没有有效的 session', 1); } } @@ -85,7 +85,7 @@ class AuthController extends BaseController if (Option::get('user_can_register') == 1) { return view('auth.register'); } else { - throw new E('残念。。本皮肤站已经关闭注册咯 QAQ', 7, true); + throw new PrettyPageException('残念。。本皮肤站已经关闭注册咯 QAQ', 7); } } @@ -137,7 +137,7 @@ class AuthController extends BaseController if ($_ENV['MAIL_HOST'] != "") { return view('auth.forgot'); } else { - throw new E('本站已关闭重置密码功能', 8, true); + throw new PrettyPageException('本站已关闭重置密码功能', 8); } } diff --git a/app/Http/Controllers/ClosetController.php b/app/Http/Controllers/ClosetController.php index a4bd4b59..425dd207 100644 --- a/app/Http/Controllers/ClosetController.php +++ b/app/Http/Controllers/ClosetController.php @@ -7,7 +7,7 @@ use App\Models\User; use App\Models\Texture; use App\Models\Closet; use App\Models\ClosetModel; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use View; use Option; @@ -59,7 +59,7 @@ class ClosetController extends BaseController public function remove() { if (!is_numeric(\Utils::getValue('tid', $_POST))) - throw new E('非法参数', 1); + View::json('非法参数', 1); if ($this->closet->remove($_POST['tid'])) { $t = Texture::find($_POST['tid']); diff --git a/app/Http/Controllers/PlayerController.php b/app/Http/Controllers/PlayerController.php index a7e723ee..14f3d199 100644 --- a/app/Http/Controllers/PlayerController.php +++ b/app/Http/Controllers/PlayerController.php @@ -7,7 +7,7 @@ use App\Models\User; use App\Models\Player; use App\Models\PlayerModel; use App\Models\Texture; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Validate; use Utils; use Option; @@ -87,7 +87,7 @@ class PlayerController extends BaseController $new_player_name = Utils::getValue('new_player_name', $_POST); if (!$new_player_name) - throw new E('非法参数', 1); + View::json('非法参数', 1); if (!Validate::playerName($new_player_name)) { @@ -114,7 +114,7 @@ class PlayerController extends BaseController $tid = Utils::getValue('tid', $_POST); if (!is_numeric($tid)) - throw new E('非法参数', 1); + View::json('非法参数', 1); if (!($texture = Texture::find($tid))) View::json('Unexistent texture.', 6); @@ -140,7 +140,7 @@ class PlayerController extends BaseController if (!isset($_POST['preference']) || ($_POST['preference'] != "default" && $_POST['preference'] != "slim")) { - throw new E('非法参数', 1); + View::json('非法参数', 1); } $this->player->setPreference($_POST['preference']); diff --git a/app/Http/Controllers/SkinlibController.php b/app/Http/Controllers/SkinlibController.php index d689e8c1..471bd46f 100644 --- a/app/Http/Controllers/SkinlibController.php +++ b/app/Http/Controllers/SkinlibController.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Routing\Controller as BaseController; use App\Models\User; use App\Models\Texture; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Validate; use Option; use Utils; diff --git a/app/Http/Controllers/TextureController.php b/app/Http/Controllers/TextureController.php index e793f48c..d33e952c 100644 --- a/app/Http/Controllers/TextureController.php +++ b/app/Http/Controllers/TextureController.php @@ -8,7 +8,7 @@ use App\Events\GetAvatarPreview; use App\Models\User; use App\Models\Player; use App\Models\Texture; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Storage; use Minecraft; use Option; diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 43aa9423..793dd11d 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Routing\Controller as BaseController; use App\Models\User; use App\Models\Texture; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Utils; use View; @@ -48,7 +48,7 @@ class UserController extends BaseController { // handle changing nickname if ($this->action == "nickname") { - if (!isset($_POST['new_nickname'])) throw new E('非法参数'); + if (!isset($_POST['new_nickname'])) View::json('非法参数', 1); if (Utils::convertString($_POST['new_nickname']) != $_POST['new_nickname']) View::json('无效的昵称。昵称中包含了奇怪的字符。', 1); @@ -58,7 +58,7 @@ class UserController extends BaseController // handle changing password } elseif ($this->action == "password") { if (!(isset($_POST['current_password']) && isset($_POST['new_password']))) - throw new E('非法参数'); + View::json('非法参数', 1); if (!$this->user->checkPasswd($_POST['current_password'])) View::json('原密码错误', 1); @@ -70,7 +70,7 @@ class UserController extends BaseController // handle changing email } elseif ($this->action == "email") { if (!(isset($_POST['new_email']) && isset($_POST['password']))) - throw new E('非法参数'); + View::json('非法参数', 1); if (!filter_var($_POST['new_email'], FILTER_VALIDATE_EMAIL)) { View::json('邮箱格式错误', 3); @@ -85,7 +85,7 @@ class UserController extends BaseController // handle deleting account } elseif ($this->action == "delete") { if (!isset($_POST['password'])) - throw new E('非法参数'); + View::json('非法参数', 1); if (!$this->user->checkPasswd($_POST['password'])) View::json('密码错误', 1); @@ -109,17 +109,18 @@ class UserController extends BaseController public function setAvatar() { - if (!isset($_POST['tid'])) throw new E('Empty tid.'); + if (!isset($_POST['tid'])) + View::json('Empty tid.', 1); $result = Texture::find($_POST['tid']); if ($result) { - if ($result->type == "cape") throw new E('披风可不能设置为头像哦~', 1); + if ($result->type == "cape") View::json('披风可不能设置为头像哦~', 1); if ((new User(session('uid')))->setAvatar($_POST['tid'])) { View::json('设置成功!', 0); } } else { - throw new E('材质不存在。', 1); + View::json('材质不存在。', 1); } } diff --git a/app/Http/Middleware/CheckAuthenticated.php b/app/Http/Middleware/CheckAuthenticated.php index 0afb5d5e..0dd8fe20 100644 --- a/app/Http/Middleware/CheckAuthenticated.php +++ b/app/Http/Middleware/CheckAuthenticated.php @@ -4,7 +4,7 @@ namespace App\Http\Middleware; use App\Models\User; use App\Models\UserModel; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use View; use Http; use Session; @@ -31,7 +31,7 @@ class CheckAuthenticated Session::flush(); Session::save(); - throw new E('你已经被本站封禁啦,请联系管理员解决', 5, true); + throw new PrettyPageException('你已经被本站封禁啦,请联系管理员解决', 5); } // ask for filling email diff --git a/app/Http/Middleware/CheckPostMiddleware.php b/app/Http/Middleware/CheckPostMiddleware.php index 42de6e2d..3f01d880 100644 --- a/app/Http/Middleware/CheckPostMiddleware.php +++ b/app/Http/Middleware/CheckPostMiddleware.php @@ -2,7 +2,7 @@ namespace App\Http\Middleware; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Validate; use Utils; use View; diff --git a/app/Models/Closet.php b/app/Models/Closet.php index f7c860c6..1f045355 100644 --- a/app/Models/Closet.php +++ b/app/Models/Closet.php @@ -2,8 +2,9 @@ namespace App\Models; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Utils; +use View; class Closet { @@ -93,7 +94,7 @@ class Closet { foreach ($this->textures as $item) { if ($item['tid'] == $tid) - throw new E('你已经收藏过这个材质啦', 1); + View::json('你已经收藏过这个材质啦', 1); } $this->textures[] = array( @@ -137,7 +138,7 @@ class Closet $offset++; } - throw new E('The texture is not in the closet.', 1); + View::json('The texture is not in the closet.', 1); } private function checkTextureExist($tid) diff --git a/app/Models/Player.php b/app/Models/Player.php index c8a5ba68..a3abbcf7 100644 --- a/app/Models/Player.php +++ b/app/Models/Player.php @@ -4,9 +4,10 @@ namespace App\Models; use App\Events\PlayerProfileUpdated; use App\Events\GetPlayerJson; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Event; use Utils; +use View; class Player { @@ -68,7 +69,7 @@ class Player { if (!isset($tids['tid_steve']) && !isset($tids['tid_alex']) && !isset($tids['tid_cape'])) { - throw new E('非法参数', 1); + View::json('非法参数', 1); } $this->model->tid_steve = isset($tids['tid_steve']) ? $tids['tid_steve'] : $this->model['tid_steve']; @@ -144,7 +145,7 @@ class Player if ($api_type == self::CSL_API || $api_type == self::USM_API) { return \Storage::disk('cache')->get("json/{$this->pid}-{$api_type}"); } else { - throw new E('不支持的 API_TYPE。', -1, true); + throw new PrettyPageException('不支持的 API_TYPE。', -1); } } diff --git a/app/Providers/BootServiceProvider.php b/app/Providers/BootServiceProvider.php index e34dfd6a..b8036616 100644 --- a/app/Providers/BootServiceProvider.php +++ b/app/Providers/BootServiceProvider.php @@ -3,7 +3,7 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; class BootServiceProvider extends ServiceProvider { @@ -15,10 +15,18 @@ class BootServiceProvider extends ServiceProvider public function boot() { \View::addExtension('tpl', 'blade'); + $this->checkFileExists(); $this->checkDbConfig(); $this->checkInstallation(); } + protected function checkFileExists() + { + if (!file_exists(BASE_DIR."/.env")) { + throw new PrettyPageException('.env 文件不存在', -1); + } + } + protected function checkDbConfig() { // use error control to hide shitty connect warnings @@ -31,7 +39,7 @@ class BootServiceProvider extends ServiceProvider ); if ($conn->connect_error) - throw new E("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno, true); + throw new PrettyPageException("无法连接至 MySQL 服务器,请检查你的配置:".$conn->connect_error, $conn->connect_errno); return true; } @@ -44,7 +52,7 @@ class BootServiceProvider extends ServiceProvider if (!is_dir(BASE_DIR.'/storage/textures/')) { if (!mkdir(BASE_DIR.'/storage/textures/')) - throw new E('textures 文件夹创建失败,请确认目录权限是否正确,或者手动放置一个。', -1); + throw new PrettyPageException('textures 文件夹创建失败,请确认目录权限是否正确,或者手动放置一个。', -1); } if (config('app.version') != \Option::get('version', '')) { diff --git a/app/Services/Database/Database.php b/app/Services/Database/Database.php index cf047f41..284a6e5f 100644 --- a/app/Services/Database/Database.php +++ b/app/Services/Database/Database.php @@ -2,13 +2,10 @@ namespace App\Services\Database; -use \App\Exceptions\E; -use \Blessing\Config; - /** * Light-weight database helper * - * @author + * @author */ class Database { @@ -52,7 +49,7 @@ class Database ); if ($this->connection->connect_error) - throw new E("Could not connect to MySQL database. Check your configuration:". + throw new \InvalidArgumentException("Could not connect to MySQL database. Check your configuration:". $this->connection->connect_error, $this->connection->connect_errno, true); $this->connection->query("SET names 'utf8'"); @@ -75,7 +72,7 @@ class Database $result = $this->connection->query($sql); if ($this->connection->error) - throw new E("Database query error: ".$this->connection->error.", Statement: ".$sql, -1); + throw new \Exception("Database query error: ".$this->connection->error.", Statement: ".$sql, -1); return $result; } @@ -90,9 +87,9 @@ class Database * * @param string $key * @param string $value - * @param array $condition, see function `where` - * @param string $table, which table to operate - * @param boolean $dont_fetch_array, return resources if true + * @param array $condition See function `where` + * @param string $table Which table to operate + * @param bool $dont_fetch_array Return resources if true * @return array|resources */ public function select($key, $value, $condition = null, $table = null, $dont_fetch_array = false) @@ -173,7 +170,7 @@ class Database /** * Generate where statement * - * @param array $condition, e.g. array('where'=>'username="shit"', 'limit'=>10, 'order'=>'uid') + * @param array $condition e.g. array('where'=>'username="shit"', 'limit'=>10, 'order'=>'uid') * @return string */ private function where($condition) diff --git a/app/Services/Utils.php b/app/Services/Utils.php index d04ff634..b99d8f3b 100644 --- a/app/Services/Utils.php +++ b/app/Services/Utils.php @@ -2,7 +2,7 @@ namespace App\Services; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; use Storage; class Utils diff --git a/app/Services/Validate.php b/app/Services/Validate.php index 0d5bdf4a..53287215 100644 --- a/app/Services/Validate.php +++ b/app/Services/Validate.php @@ -2,7 +2,8 @@ namespace App\Services; -use App\Exceptions\E; +use App\Exceptions\PrettyPageException; +use View; class Validate { @@ -17,7 +18,7 @@ class Validate foreach ($keys as $key) { if (!isset($_POST[$key])) { if ($silent) return false; - throw new E('非法参数', 1); + View::json('非法参数', 1); } } return true; @@ -43,9 +44,9 @@ class Validate public static function textureName($texture_name) { if (strlen($texture_name) > 32 || strlen($texture_name) < 1) { - throw new E('无效的材质名称。材质名长度应该小于 32。', 2); + View::json('无效的材质名称。材质名长度应该小于 32。', 2); } else if (Utils::convertString($texture_name) != $texture_name) { - throw new E('无效的材质名称。材质名称中包含了奇怪的字符。', 2); + View::json('无效的材质名称。材质名称中包含了奇怪的字符。', 2); } return true; } @@ -54,10 +55,10 @@ class Validate { if (strlen($password) > 16 || strlen($password) < 8) { if ($silent) return false; - throw new E('无效的密码。密码长度应该大于 8 并小于 16。', 2); + View::json('无效的密码。密码长度应该大于 8 并小于 16。', 2); } else if (Utils::convertString($password) != $password) { if ($silent) return false; - throw new E('无效的密码。密码中包含了奇怪的字符。', 2); + View::json('无效的密码。密码中包含了奇怪的字符。', 2); } return true; }