use custom helpers instead of util classes

This commit is contained in:
printempw 2016-08-29 23:31:43 +08:00
parent 0dd6d0125c
commit 9f02cf26c1
16 changed files with 87 additions and 106 deletions

View File

@ -98,15 +98,17 @@ class AuthController extends BaseController
if (!$user->is_registered) {
if (Option::get('user_can_register') == 1) {
if (Validate::password($_POST['password'])) {
$ip = get_real_ip();
// If amount of registered accounts of IP is more than allowed amounts,
// then reject the register.
if (UserModel::where('ip', Http::getRealIP())->count() < Option::get('regs_per_ip'))
if (UserModel::where('ip', $ip)->count() < Option::get('regs_per_ip'))
{
if (Validate::nickname(Utils::getValue('nickname', $_POST)))
View::json('无效的昵称,昵称不能包含奇怪的字符', 1);
// register new user
$user = $user->register($_POST['password'], Http::getRealIP());
$user = $user->register($_POST['password'], $ip);
$user->setNickName($_POST['nickname']);
// set cookies

View File

@ -26,7 +26,7 @@ class PlayerController extends BaseController
if (isset($_POST['pid'])) {
$this->player = new Player($_POST['pid']);
if (!$this->player)
\Http::abort(404, '角色不存在');
abort(404, '角色不存在');
}
}

View File

@ -96,7 +96,7 @@ class SkinlibController extends BaseController
public function show()
{
if (!isset($_GET['tid']))
Http::abort(404, 'No specified tid.');
abort(404, 'No specified tid.');
$texture = Texture::find($_GET['tid']);
@ -105,14 +105,14 @@ class SkinlibController extends BaseController
if ($texture)
$texture->delete();
Http::abort(404, '请求的材质文件已经被删除');
abort(404, '请求的材质文件已经被删除');
}
Http::abort(404, '请求的材质文件已经被删除,请联系管理员删除该条目');
abort(404, '请求的材质文件已经被删除,请联系管理员删除该条目');
}
if ($texture->public == "0") {
if (is_null($this->user) || ($this->user->uid != $texture->uploader && !$this->user->is_admin))
Http::abort(404, '请求的材质已经设为隐私,仅上传者和管理员可查看');
abort(404, '请求的材质已经设为隐私,仅上传者和管理员可查看');
}
echo View::make('skinlib.show')->with('texture', $texture)->with('with_out_filter', true)->with('user', $this->user)->render();

View File

@ -23,7 +23,7 @@ class TextureController extends BaseController
$player = new Player(0, $player_name);
if ($player->is_banned)
Http::abort(404, '该角色拥有者已被本站封禁。');
abort(404, '该角色拥有者已被本站封禁。');
if ($api == "csl") {
return response($player->getJsonProfile(Player::CSL_API))
@ -35,7 +35,7 @@ class TextureController extends BaseController
return response($player->getJsonProfile(Option::get('api_type')))
->header('Content-type', 'application/json');
} else {
Http::abort(404, '不支持的 API_TYPE。');
abort(404, '不支持的 API_TYPE。');
}
}
@ -64,7 +64,7 @@ class TextureController extends BaseController
$player = new Player(0, $player_name);
if ($player->is_banned)
Http::abort(404, '该角色拥有者已被本站封禁。');
abort(404, '该角色拥有者已被本站封禁。');
if (!$this->checkCache($player_name)) {
$model_preference = ($player->getPreference() == "default") ? "steve" : "alex";
@ -80,7 +80,7 @@ class TextureController extends BaseController
$player = new Player(0, $player_name);
if ($player->is_banned)
Http::abort(404, '该角色拥有者已被本站封禁。');
abort(404, '该角色拥有者已被本站封禁。');
if (!$this->checkCache($player_name)) {
echo $player->getBinaryTexture('cape');
@ -146,10 +146,10 @@ class TextureController extends BaseController
if (Storage::disk('textures')->has($t->hash)) {
return response(Storage::disk('textures')->get($t->hash))->header('Content-Type', 'image/png');
} else {
Http::abort(404, '请求的材质文件已经被删除');
abort(404, '请求的材质文件已经被删除');
}
} else {
Http::abort(404, '材质不存在');
abort(404, '材质不存在');
}
}

View File

@ -17,7 +17,7 @@ class CheckPlayerExistMiddleware
$player_name = urldecode($matches[1]);
if (PlayerModel::where('player_name', $player_name)->get()->isEmpty()) {
\Http::abort(404, '角色不存在');
abort(404, '角色不存在');
}
return $next($request);

View File

@ -37,7 +37,7 @@ class Player
}
if (!$this->model) {
\Http::abort(404, '角色不存在');
abort(404, '角色不存在');
} else {
$this->pid = $this->model->pid;
}
@ -102,10 +102,10 @@ class Player
->header('Last-Modified', gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT')
->header('Content-Length', filesize($path));
} else {
\Http::abort(404, '请求的贴图已被删除。');
abort(404, '请求的贴图已被删除。');
}
} else {
\Http::abort(404, '该用户尚未上传请求的贴图类型 '.$type);
abort(404, '该用户尚未上传请求的贴图类型 '.$type);
}
}

View File

@ -14,6 +14,8 @@ class AppServiceProvider extends ServiceProvider
public function boot()
{
\View::addExtension('tpl', 'blade');
require_once BASE_DIR."/app/helpers.php";
}
/**

View File

@ -1,74 +0,0 @@
<?php
namespace App\Services;
use Session;
class Http
{
/**
* 301 Moved Permanently
*
* @param string $url
* @return void
*/
public static function redirectPermanently($url)
{
http_response_code(301);
header('Location: '.$url);
exit;
}
public static function getRealIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
public static function setUri($uri)
{
$_SERVER["REQUEST_URI"] = $uri;
return true;
}
public static function getUri()
{
return $_SERVER["REQUEST_URI"];
}
public static function getBaseUrl()
{
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
$base_url .= $_SERVER["SERVER_NAME"];
$base_url .= ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $base_url;
}
public static function getCurrentUrl()
{
return self::getBaseUrl().$_SERVER["REQUEST_URI"];
}
public static function abort($code, $msg = "Something happened.", $is_json = false)
{
http_response_code((int)$code);
if ($is_json) {
View::json($msg, $code);
} else {
$config = require BASE_DIR."/config/view.php";
if (View::exists("errors.$code")) {
echo View::make('errors.'.$code)->with('code', $code)->with('message', $msg);
} else {
echo View::make('errors.e')->with('code', $code)->with('message', $msg);
}
exit;
}
}
}

36
app/helpers.php Normal file
View File

@ -0,0 +1,36 @@
<?php
if (! function_exists('get_real_ip')) {
function get_real_ip()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}
if (! function_exists('get_base_url')) {
function get_base_url()
{
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
$base_url .= $_SERVER["SERVER_NAME"];
$base_url .= ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $base_url;
}
}
if (! function_exists('get_current_url')) {
function get_current_url()
{
return get_base_url().$_SERVER["REQUEST_URI"];
}
}

View File

@ -222,7 +222,6 @@ return [
'Validate' => App\Services\Validate::class,
'Updater' => App\Services\Updater::class,
'Database' => App\Services\Facades\Database::class,
'Http' => App\Services\Http::class
],

View File

@ -5,7 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,nofollow" />
<title>@yield('title') - Blessing Skin Server</title>
<link rel="stylesheet" type="text/css" href="{{ \Http::getBaseUrl() }}/assets/css/style.css">
<link rel="stylesheet" type="text/css" href="{{ url('assets/css/style.css') }}">
</head>
<body class="container">

View File

@ -65,17 +65,17 @@
<?php
if (Validate::checkPost(['v2_table_name', 'texture_name_pattern'], true)) {
if ($_POST['v2_table_name'] == "") {
Http::redirect('index.php?action=import-v2-both&step=1', 'v2 users 表名不能为空');
redirect_to('index.php?action=import-v2-both&step=1', 'v2 users 表名不能为空');
} else {
if (Utils::convertString($_POST['v2_table_name']) != $_POST['v2_table_name'])
Http::redirect('index.php?action=import-v2-both&step=1', "表名 {$_POST['v2_table_name']} 中含有无效字符");
redirect_to('index.php?action=import-v2-both&step=1', "表名 {$_POST['v2_table_name']} 中含有无效字符");
if (!Database::hasTable($_POST['v2_table_name'])) {
Http::redirect('index.php?action=import-v2-both&step=1', "数据表 {$_POST['v2_table_name']} 不存在");
redirect_to('index.php?action=import-v2-both&step=1', "数据表 {$_POST['v2_table_name']} 不存在");
}
}
} else {
Http::redirect('index.php?action=import-v2-both&step=1', '表单信息不完整');
redirect_to('index.php?action=import-v2-both&step=1', '表单信息不完整');
}
?>

View File

@ -73,19 +73,19 @@
<?php
if (Validate::checkPost(['v2_table_name', 'uploader_uid', 'texture_name_pattern'], true)) {
if ($_POST['v2_table_name'] == "") {
Http::redirect('index.php?action=import-v2-textures&step=1', 'v2 users 表名不能为空');
redirect_to('index.php?action=import-v2-textures&step=1', 'v2 users 表名不能为空');
} else {
$_POST['uploader_uid'] = ($_POST['uploader_uid'] == "") ? 0 : (int)$_POST['uploader_uid'];
if (Utils::convertString($_POST['v2_table_name']) != $_POST['v2_table_name'])
Http::redirect('index.php?action=import-v2-textures&step=1', "表名 {$_POST['v2_table_name']} 中含有无效字符");
redirect_to('index.php?action=import-v2-textures&step=1', "表名 {$_POST['v2_table_name']} 中含有无效字符");
if (!Database::hasTable($_POST['v2_table_name'])) {
Http::redirect('index.php?action=import-v2-textures&step=1', "数据表 {$_POST['v2_table_name']} 不存在");
redirect_to('index.php?action=import-v2-textures&step=1', "数据表 {$_POST['v2_table_name']} 不存在");
}
}
} else {
Http::redirect('index.php?action=import-v2-textures&step=1', '表单信息不完整');
redirect_to('index.php?action=import-v2-textures&step=1', '表单信息不完整');
}
?>

View File

@ -43,17 +43,17 @@
<?php
if (Validate::checkPost(['v2_table_name'], true)) {
if ($_POST['v2_table_name'] == "") {
Http::redirect('index.php?action=import-v2-users&step=1', 'v2 users 表名不能为空');
redirect_to('index.php?action=import-v2-users&step=1', 'v2 users 表名不能为空');
} else {
if (Utils::convertString($_POST['v2_table_name']) != $_POST['v2_table_name'])
Http::redirect('index.php?action=import-v2-users&step=1', "表名 {$_POST['v2_table_name']} 中含有无效字符");
redirect_to('index.php?action=import-v2-users&step=1', "表名 {$_POST['v2_table_name']} 中含有无效字符");
if (!Database::hasTable($_POST['v2_table_name'])) {
Http::redirect('index.php?action=import-v2-users&step=1', "数据表 {$_POST['v2_table_name']} 不存在");
redirect_to('index.php?action=import-v2-users&step=1', "数据表 {$_POST['v2_table_name']} 不存在");
}
}
} else {
Http::redirect('index.php?action=import-v2-users&step=1', '表单信息不完整');
redirect_to('index.php?action=import-v2-users&step=1', '表单信息不完整');
}
?>

View File

@ -146,7 +146,7 @@
<div class="box-body">
@if (Option::get('comment_script') != "")
<!-- Comment Start -->
{!! Utils::getStringReplaced(Option::get('comment_script'), ['{tid}' => $texture->tid, '{name}' => $texture->name, '{url}' => Http::getCurrentUrl()]) !!}
{!! Utils::getStringReplaced(Option::get('comment_script'), ['{tid}' => $texture->tid, '{name}' => $texture->name, '{url}' => get_current_url()]) !!}
<!-- Comment End -->
@else
<p style="text-align: center; margin: 30px 0;">本站未开启评论服务</p>

View File

@ -82,4 +82,20 @@ function checkTableExist() {
return true;
}
function redirect_to($url, $msg = "") {
if ($msg !== "") {
if (app()->bound('session')) {
Session::flash('msg', $msg);
Session::save();
} else {
$_SESSION['msg'] = $msg;
}
}
if (!headers_sent()) {
header('Location: '.$url);
} else {
echo "<meta http-equiv='Refresh' content='0; URL=$url'>";
}
exit;
}