blessing-skin-server/app/Http/Controllers/AdminController.php

461 lines
16 KiB
PHP
Raw Normal View History

2016-07-22 19:36:24 +08:00
<?php
2016-08-28 10:05:21 +08:00
namespace App\Http\Controllers;
2016-07-22 19:36:24 +08:00
2016-11-12 23:50:41 +08:00
use Option;
use Datatables;
2016-11-19 22:02:02 +08:00
use App\Events;
use Carbon\Carbon;
2016-07-22 19:36:24 +08:00
use App\Models\User;
2016-07-23 15:20:10 +08:00
use App\Models\Player;
use App\Models\Texture;
use Illuminate\Http\Request;
2016-12-31 23:28:09 +08:00
use App\Services\OptionForm;
use Illuminate\Support\Facades\Auth;
2016-10-23 11:41:52 +08:00
use App\Services\Repositories\UserRepository;
2016-07-22 19:36:24 +08:00
class AdminController extends Controller
2016-07-22 19:36:24 +08:00
{
public function index()
{
$today = Carbon::today()->timestamp;
// Prepare data for the graph
$data = [];
$labels = [];
for ($i = 6; $i >= 0; $i--) {
$time = Carbon::createFromTimestamp($today - $i * 86400);
$labels[] = $time->format('m-d');
$data['user_registration'][] = User::like('register_at', $time->toDateString())->count();
$data['texture_uploads'][] = Texture::like('upload_at', $time->toDateString())->count();
}
2018-02-24 17:08:32 +08:00
$datasets = [
[
'label' => trans('admin.index.user-registration'),
'backgroundColor' => 'rgba(60, 141, 188, 0.6)',
'borderColor' => '#3c8dbc',
'pointRadius' => 0,
'pointBorderColor' => '#3c8dbc',
'pointBackgroundColor' => '#3c8dbc',
'pointHoverBackgroundColor' => '#3c8dbc',
'pointHoverBorderColor' => '#3c8dbc',
'data' => $data['user_registration'],
],
[
'label' => trans('admin.index.texture-uploads'),
'backgroundColor' => 'rgba(210, 214, 222, 0.6)',
'borderColor' => '#d2d6de',
'pointRadius' => 0,
'pointBorderColor' => '#c1c7d1',
'pointBackgroundColor' => '#c1c7d1',
'pointHoverBackgroundColor' => '#c1c7d1',
'pointHoverBorderColor' => '#c1c7d1',
'data' => $data['texture_uploads'],
]
];
$options = [
'tooltips' => [
'intersect' => false,
'mode' => 'index'
]
];
2018-02-24 17:08:32 +08:00
return view('admin.index', ['chartOptions' => compact('labels', 'datasets', 'options')]);
2016-07-22 19:36:24 +08:00
}
public function customize(Request $request)
2016-07-23 15:20:10 +08:00
{
if ($request->input('action') == "color") {
$this->validate($request, [
'color_scheme' => 'required'
]);
$color_scheme = str_replace('_', '-', $request->input('color_scheme'));
option(['color_scheme' => $color_scheme]);
2016-12-31 23:28:09 +08:00
return json(trans('admin.customize.change-color.success'), 0);
}
2016-12-31 23:28:09 +08:00
$homepage = Option::form('homepage', OptionForm::AUTO_DETECT, function($form)
2016-12-27 23:05:09 +08:00
{
2017-01-18 22:57:15 +08:00
$form->text('home_pic_url')->hint();
2016-12-27 23:05:09 +08:00
2017-01-18 22:57:15 +08:00
$form->text('favicon_url')->hint()->description();
2016-12-31 13:36:46 +08:00
2016-12-31 23:28:09 +08:00
$form->select('copyright_prefer')
2016-12-30 20:55:33 +08:00
->option('0', 'Powered with ❤ by Blessing Skin Server.')
->option('1', 'Powered by Blessing Skin Server.')
2016-12-31 23:28:09 +08:00
->option('2', 'Proudly powered by Blessing Skin Server.')
->option('3', '由 Blessing Skin Server 强力驱动.')
->option('4', '自豪地采用 Blessing Skin Server.')
2017-01-18 22:57:15 +08:00
->description();
2017-01-18 22:57:15 +08:00
$form->textarea('copyright_text')->rows(6)->description();
2016-12-27 23:05:09 +08:00
})->handle(function () {
Option::set('copyright_prefer_'.config('app.locale'), request('copyright_prefer'));
Option::set('copyright_text_'.config('app.locale'), request('copyright_text'));
});
2016-12-27 23:05:09 +08:00
2016-12-31 23:28:09 +08:00
$customJsCss = Option::form('customJsCss', OptionForm::AUTO_DETECT, function($form)
{
$form->textarea('custom_css', 'CSS')->rows(6);
$form->textarea('custom_js', 'JavaScript')->rows(6);
2017-01-18 22:57:15 +08:00
})->addMessage()->handle();
return view('admin.customize', ['forms' => compact('homepage', 'customJsCss')]);
2016-07-23 15:20:10 +08:00
}
2016-07-29 11:52:45 +08:00
public function score()
{
2016-12-31 23:28:09 +08:00
$rate = Option::form('rate', OptionForm::AUTO_DETECT, function($form)
{
2017-01-18 22:57:15 +08:00
$form->group('score_per_storage')->text('score_per_storage')->addon();
2016-12-31 23:28:09 +08:00
$form->group('private_score_per_storage')
2017-01-18 22:57:15 +08:00
->text('private_score_per_storage')->addon()->hint();
2016-12-31 23:28:09 +08:00
$form->group('score_per_closet_item')
2017-01-18 22:57:15 +08:00
->text('score_per_closet_item')->addon();
2017-01-18 22:57:15 +08:00
$form->checkbox('return_score')->label();
2017-01-18 22:57:15 +08:00
$form->group('score_per_player')->text('score_per_player')->addon();
2016-12-31 23:28:09 +08:00
$form->text('user_initial_score');
})->handle();
2017-07-14 09:17:42 +08:00
$sign = Option::form('sign', OptionForm::AUTO_DETECT, function($form)
{
2016-12-31 23:28:09 +08:00
$form->group('sign_score')
2017-07-14 09:17:42 +08:00
->text('sign_score_from')->addon(trans('options.sign.sign_score.addon1'))
->text('sign_score_to')->addon(trans('options.sign.sign_score.addon2'));
$form->group('sign_gap_time')->text('sign_gap_time')->addon();
$form->checkbox('sign_after_zero')->label()->hint();
2017-11-02 16:50:00 +08:00
})->after(function() {
$sign_score = request('sign_score_from').','.request('sign_score_to');
Option::set('sign_score', $sign_score);
})->with([
'sign_score_from' => @explode(',', option('sign_score'))[0],
'sign_score_to' => @explode(',', option('sign_score'))[1]
2017-11-02 16:50:00 +08:00
])->handle();
2017-07-14 09:17:42 +08:00
return view('admin.score', ['forms' => compact('rate', 'sign')]);
2016-07-29 11:52:45 +08:00
}
2016-07-23 15:20:10 +08:00
public function options()
{
2016-12-31 23:28:09 +08:00
$general = Option::form('general', OptionForm::AUTO_DETECT, function($form)
2016-11-12 23:50:41 +08:00
{
2016-12-31 23:28:09 +08:00
$form->text('site_name');
$form->text('site_description')->description();
2017-11-02 16:50:00 +08:00
$form->text('site_url')
->hint()
->format(function ($url) {
if (ends_with($url, '/')) {
2017-11-02 16:50:00 +08:00
$url = substr($url, 0, -1);
}
if (ends_with($url, '/index.php')) {
$url = substr($url, 0, -10);
}
2017-11-02 16:50:00 +08:00
return $url;
});
2016-11-12 23:50:41 +08:00
$form->checkbox('user_can_register')->label();
2016-11-12 23:50:41 +08:00
2016-12-31 23:28:09 +08:00
$form->text('regs_per_ip');
2016-11-12 23:50:41 +08:00
$form->select('ip_get_method')
->option('0', trans('options.general.ip_get_method.HTTP_X_FORWARDED_FOR'))
->option('1', trans('options.general.ip_get_method.REMOTE_ADDR'))
->hint();
2016-12-31 23:28:09 +08:00
$form->group('max_upload_file_size')
2016-12-28 23:28:15 +08:00
->text('max_upload_file_size')->addon('KB')
->hint(trans('options.general.max_upload_file_size.hint', ['size' => ini_get('upload_max_filesize')]));
2016-11-12 23:50:41 +08:00
$form->select('player_name_rule')
->option('official', trans('options.general.player_name_rule.official'))
->option('cjk', trans('options.general.player_name_rule.cjk'))
->option('custom', trans('options.general.player_name_rule.custom'));
$form->text('custom_player_name_regexp')->hint()->placeholder();
$form->group('player_name_length')
->addon(trans('options.general.player_name_length.addon1'))
->text('player_name_length_min')
->addon(trans('options.general.player_name_length.addon2'))
->text('player_name_length_max')
->addon(trans('options.general.player_name_length.addon3'));
2016-11-12 23:50:41 +08:00
2016-12-31 23:28:09 +08:00
$form->select('api_type')
2016-12-28 23:28:15 +08:00
->option('0', 'CustomSkinLoader API')
->option('1', 'UniversalSkinAPI');
2016-11-12 23:50:41 +08:00
$form->checkbox('auto_del_invalid_texture')->label()->hint();
2016-12-28 23:28:15 +08:00
$form->checkbox('allow_downloading_texture')->label();
$form->text('texture_name_regexp')->hint()->placeholder();
$form->textarea('comment_script')->rows(6)->description();
2016-11-12 23:50:41 +08:00
$form->checkbox('allow_sending_statistics')->label()->hint();
})->handle(function () {
Option::set('site_name_'.config('app.locale'), request('site_name'));
Option::set('site_description_'.config('app.locale'), request('site_description'));
});
2016-11-12 23:50:41 +08:00
$announ = Option::form('announ', OptionForm::AUTO_DETECT, function ($form) {
$form->textarea('announcement')->rows(10)->description();
})->renderWithOutTable()->handle(function () {
Option::set('announcement_'.config('app.locale'), request('announcement'));
});
$resources = Option::form('resources', OptionForm::AUTO_DETECT, function($form)
2016-11-12 23:50:41 +08:00
{
$form->checkbox('force_ssl')->label()->hint();
$form->checkbox('auto_detect_asset_url')->label()->description();
$form->checkbox('return_204_when_notfound')->label()->description();
2016-11-12 23:50:41 +08:00
2016-12-31 23:28:09 +08:00
$form->text('cache_expire_time')->hint(OptionForm::AUTO_DETECT);
2016-11-12 23:50:41 +08:00
2016-12-31 23:28:09 +08:00
})->type('warning')->hint(OptionForm::AUTO_DETECT)->handle();
2016-11-12 23:50:41 +08:00
return view('admin.options')->with('forms', compact('general', 'resources', 'announ'));
2016-07-23 15:20:10 +08:00
}
2017-04-27 18:02:05 +08:00
public function getUserData(Request $request)
{
2017-04-27 18:02:05 +08:00
$users = collect();
if ($request->has('uid')) {
$users = User::select(['uid', 'email', 'nickname', 'score', 'permission', 'register_at'])
->where('uid', intval($request->input('uid')));
} else {
$users = User::select(['uid', 'email', 'nickname', 'score', 'permission', 'register_at']);
}
return Datatables::of($users)->editColumn('email', function ($user) {
return $user->email ?: 'EMPTY';
})
->setRowId('uid')
->addColumn('operations', Auth::user()->permission)
2017-04-27 18:02:05 +08:00
->addColumn('players_count', function ($user) {
2018-07-16 10:22:19 +08:00
return $user->players->count();
2017-04-27 18:02:05 +08:00
})
->make(true);
2016-07-23 15:20:10 +08:00
}
2017-04-27 18:02:05 +08:00
public function getPlayerData(Request $request)
2016-12-31 13:07:00 +08:00
{
2017-04-27 18:02:05 +08:00
$players = collect();
if ($request->has('uid')) {
$players = Player::select(['pid', 'uid', 'player_name', 'preference', 'tid_steve', 'tid_alex', 'tid_cape', 'last_modified'])
->where('uid', intval($request->input('uid')));
} else {
$players = Player::select(['pid', 'uid', 'player_name', 'preference', 'tid_steve', 'tid_alex', 'tid_cape', 'last_modified']);
}
2016-12-31 13:07:00 +08:00
2017-04-22 18:36:22 +08:00
return Datatables::of($players)->setRowId('pid')->make(true);
2016-07-23 15:20:10 +08:00
}
/**
* Handle ajax request from /admin/users
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
2016-07-23 15:20:10 +08:00
*/
2016-10-23 11:41:52 +08:00
public function userAjaxHandler(Request $request, UserRepository $users)
2016-07-22 19:36:24 +08:00
{
$action = $request->input('action');
$user = $users->get($request->input('uid'));
$currentUser = Auth::user();
if (! $user) {
return json(trans('admin.users.operations.non-existent'), 1);
}
if ($user->uid !== $currentUser->uid) {
if ($user->permission >= $currentUser->permission) {
2017-08-06 22:53:02 +08:00
return json(trans('admin.users.operations.no-permission'), 1);
}
}
2016-07-22 19:36:24 +08:00
if ($action == "email") {
$this->validate($request, [
'email' => 'required|email'
]);
2016-07-22 19:36:24 +08:00
if ($users->get($request->input('email'), 'email')) {
return json(trans('admin.users.operations.email.existed', ['email' => $request->input('email')]), 1);
}
$user->setEmail($request->input('email'));
return json(trans('admin.users.operations.email.success'), 0);
2016-07-23 15:20:10 +08:00
} elseif ($action == "nickname") {
$this->validate($request, [
'nickname' => 'required|no_special_chars'
]);
2016-07-22 19:36:24 +08:00
$user->setNickName($request->input('nickname'));
return json(trans('admin.users.operations.nickname.success', [
'new' => $request->input('nickname')
]), 0);
2016-07-23 15:20:10 +08:00
} elseif ($action == "password") {
$this->validate($request, [
'password' => 'required|min:8|max:16'
]);
2016-07-22 19:36:24 +08:00
2018-07-19 10:31:44 +08:00
$user->changePassword($request->input('password'));
return json(trans('admin.users.operations.password.success'), 0);
2016-07-23 15:20:10 +08:00
} elseif ($action == "score") {
$this->validate($request, [
'score' => 'required|integer'
]);
2016-07-22 19:36:24 +08:00
$user->setScore($request->input('score'));
return json(trans('admin.users.operations.score.success'), 0);
2016-07-23 15:20:10 +08:00
} elseif ($action == "ban") {
2016-10-23 11:41:52 +08:00
$permission = $user->getPermission() == User::BANNED ? User::NORMAL : User::BANNED;
$user->setPermission($permission);
return json([
'errno' => 0,
'msg' => trans('admin.users.operations.ban.'.($permission == User::BANNED ? 'ban' : 'unban').'.success'),
'permission' => $user->getPermission()
]);
2016-07-23 15:20:10 +08:00
} elseif ($action == "admin") {
2016-10-23 11:41:52 +08:00
$permission = $user->getPermission() == User::ADMIN ? User::NORMAL : User::ADMIN;
$user->setPermission($permission);
return json([
'errno' => 0,
'msg' => trans('admin.users.operations.admin.'.($permission == User::ADMIN ? 'set' : 'unset').'.success'),
'permission' => $user->getPermission()
]);
2016-07-23 15:20:10 +08:00
} elseif ($action == "delete") {
$user->delete();
2016-07-23 15:20:10 +08:00
return json(trans('admin.users.operations.delete.success'), 0);
2017-11-02 16:50:00 +08:00
} else {
return json(trans('admin.users.operations.invalid'), 1);
2016-07-22 19:36:24 +08:00
}
}
2016-07-23 15:20:10 +08:00
/**
* Handle ajax request from /admin/players
*/
2016-10-23 11:41:52 +08:00
public function playerAjaxHandler(Request $request, UserRepository $users)
2016-07-22 19:36:24 +08:00
{
2017-11-02 16:50:00 +08:00
$action = $request->input('action');
$currentUser = Auth::user();
2016-10-16 18:16:15 +08:00
$player = Player::find($request->input('pid'));
if (! $player) {
2017-11-02 16:50:00 +08:00
return json(trans('general.unexistent-player'), 1);
}
if ($player->user()->first()->uid !== $currentUser->uid) {
if ($player->user->permission >= $currentUser->permission) {
2017-08-06 22:53:02 +08:00
return json(trans('admin.players.no-permission'), 1);
}
}
2016-07-22 19:36:24 +08:00
2016-07-23 15:20:10 +08:00
if ($action == "preference") {
$this->validate($request, [
'preference' => 'required|preference'
]);
2016-07-22 19:36:24 +08:00
$player->setPreference($request->input('preference'));
return json(trans('admin.players.preference.success', ['player' => $player->player_name, 'preference' => $request->input('preference')]), 0);
2016-07-22 19:36:24 +08:00
2016-07-23 15:20:10 +08:00
} elseif ($action == "texture") {
$this->validate($request, [
'model' => 'required|model',
'tid' => 'required|integer'
]);
2016-07-23 15:20:10 +08:00
2018-02-16 17:31:04 +08:00
if (! Texture::find($request->tid) && $request->tid != 0)
return json(trans('admin.players.textures.non-existent', ['tid' => $request->tid]), 1);
$player->setTexture(['tid_'.$request->model => $request->tid]);
2016-07-23 15:20:10 +08:00
return json(trans('admin.players.textures.success', ['player' => $player->player_name]), 0);
2016-07-23 15:20:10 +08:00
} elseif ($action == "owner") {
$this->validate($request, [
2016-09-10 17:52:33 +08:00
'uid' => 'required|integer'
]);
2016-07-23 15:20:10 +08:00
2016-10-23 11:41:52 +08:00
$user = $users->get($request->input('uid'));
2016-07-23 15:20:10 +08:00
2018-02-16 17:31:04 +08:00
if (! $user)
return json(trans('admin.users.operations.non-existent'), 1);
2016-07-23 15:20:10 +08:00
$player->setOwner($request->input('uid'));
return json(trans('admin.players.owner.success', ['player' => $player->player_name, 'user' => $user->getNickName()]), 0);
2016-07-23 15:20:10 +08:00
2016-07-23 21:46:20 +08:00
} elseif ($action == "delete") {
$player->delete();
return json(trans('admin.players.delete.success'), 0);
} elseif ($action == "name") {
2017-11-02 16:50:00 +08:00
$this->validate($request, [
'name' => 'required'
]);
$player->rename($request->input('name'));
return json(trans('admin.players.name.success', ['player' => $player->player_name]), 0, ['name' => $player->player_name]);
2017-11-02 16:50:00 +08:00
} else {
return json(trans('admin.users.operations.invalid'), 1);
2016-07-23 15:20:10 +08:00
}
2016-07-22 19:36:24 +08:00
}
/**
* Get one user information
*
* @param string $uid
2017-11-02 16:50:00 +08:00
* @param UserRepository $users
* @return \Illuminate\Http\JsonResponse
*/
public function getOneUser($uid, UserRepository $users)
{
$user = $users->get(intval($uid));
if ($user) {
return json('success', 0, ['user' => $user->makeHidden([
'password', 'ip', 'last_sign_at', 'register_at', 'remember_token'
])->toArray()]);
} else {
return json('No such user.', 1);
}
}
2016-07-22 19:36:24 +08:00
}