2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Controllers;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
use View;
|
|
|
|
use Utils;
|
|
|
|
use Option;
|
|
|
|
use Storage;
|
|
|
|
use Session;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Texture;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
class SkinlibController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
|
|
|
private $user = null;
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function __construct()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->user = Session::has('uid') ? new User(session('uid')) : null;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function index(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$filter = $request->input('filter', 'skin');
|
|
|
|
$sort = $request->input('sort', 'time');
|
|
|
|
$uid = $request->input('uid', 0);
|
|
|
|
$page = $request->input('page', 1);
|
2016-09-27 22:31:37 +08:00
|
|
|
$page = $page <= 0 ? 1 : $page;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$sort_by = ($sort == "time") ? "upload_at" : $sort;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
if ($filter == "skin") {
|
|
|
|
$textures = Texture::where(function($query) {
|
2016-09-24 22:49:20 +08:00
|
|
|
$query->where('type', '=', 'steve')
|
2016-07-21 22:01:57 +08:00
|
|
|
->orWhere('type', '=', 'alex');
|
2016-07-24 11:12:52 +08:00
|
|
|
})->orderBy($sort_by, 'desc');
|
2016-07-26 13:36:24 +08:00
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
} elseif ($filter == "user") {
|
2016-08-13 22:31:24 +08:00
|
|
|
$textures = Texture::where('uploader', $uid)->orderBy($sort_by, 'desc');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
} else {
|
2016-07-26 13:36:24 +08:00
|
|
|
$textures = Texture::where('type', $filter)->orderBy($sort_by, 'desc');
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-13 22:31:24 +08:00
|
|
|
if (!is_null($this->user)) {
|
|
|
|
// show private textures when show uploaded textures of current user
|
|
|
|
if ($uid != $this->user->uid && !$this->user->is_admin)
|
|
|
|
$textures = $textures->where('public', '1');
|
|
|
|
} else {
|
2016-07-26 13:36:24 +08:00
|
|
|
$textures = $textures->where('public', '1');
|
2016-08-13 22:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$total_pages = ceil($textures->count() / 20);
|
2016-07-24 11:12:52 +08:00
|
|
|
|
2016-07-26 13:36:24 +08:00
|
|
|
$textures = $textures->skip(($page - 1) * 20)->take(20)->get();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('skinlib.index')->with('user', $this->user)
|
|
|
|
->with('sort', $sort)
|
|
|
|
->with('filter', $filter)
|
|
|
|
->with('textures', $textures)
|
|
|
|
->with('page', $page)
|
|
|
|
->with('total_pages', $total_pages);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function search(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$q = $request->input('q', '');
|
|
|
|
$filter = $request->input('filter', 'skin');
|
|
|
|
$sort = $request->input('sort', 'time');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
$sort_by = ($sort == "time") ? "upload_at" : $sort;
|
|
|
|
|
|
|
|
if ($filter == "skin") {
|
|
|
|
$textures = Texture::like('name', $q)->where(function($query) use ($q) {
|
|
|
|
$query->where('public', '=', '1')
|
2016-09-04 15:35:12 +08:00
|
|
|
->where('type', '=', 'steve')
|
2016-07-21 22:01:57 +08:00
|
|
|
->orWhere('type', '=', 'alex');
|
|
|
|
})->orderBy($sort_by, 'desc')->get();
|
|
|
|
} else {
|
|
|
|
$textures = Texture::like('name', $q)
|
|
|
|
->where('type', $filter)
|
|
|
|
->where('public', '1')
|
|
|
|
->orderBy($sort_by, 'desc')->get();
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('skinlib.search')->with('user', $this->user)
|
|
|
|
->with('sort', $sort)
|
|
|
|
->with('filter', $filter)
|
|
|
|
->with('q', $q)
|
|
|
|
->with('textures', $textures);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function show(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2016-09-24 22:49:20 +08:00
|
|
|
'tid' => 'required|integer'
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2016-08-28 20:33:35 +08:00
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
$texture = Texture::find($_GET['tid']);
|
2016-08-20 21:08:46 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if (!$texture || $texture && !Storage::disk('textures')->has($texture->hash)) {
|
2016-08-17 18:08:51 +08:00
|
|
|
if (Option::get('auto_del_invalid_texture') == "1") {
|
2016-08-28 20:33:35 +08:00
|
|
|
if ($texture)
|
|
|
|
$texture->delete();
|
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
abort(404, trans('skinlib.show.deleted'));
|
2016-08-17 18:08:51 +08:00
|
|
|
}
|
2016-09-24 22:49:20 +08:00
|
|
|
abort(404, trans('skinlib.show.deleted').trans('skinlib.show.contact-admin'));
|
2016-08-16 22:58:21 +08:00
|
|
|
}
|
2016-08-20 21:08:46 +08:00
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
if ($texture->public == "0") {
|
|
|
|
if (is_null($this->user) || ($this->user->uid != $texture->uploader && !$this->user->is_admin))
|
2016-09-24 22:49:20 +08:00
|
|
|
abort(404, trans('skinlib.show.private'));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('skinlib.show')->with('texture', $texture)->with('with_out_filter', true)->with('user', $this->user);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function info($tid)
|
|
|
|
{
|
2016-09-10 21:39:45 +08:00
|
|
|
return json(Texture::find($tid)->toArray());
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function upload()
|
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
return view('skinlib.upload')->with('user', $this->user)->with('with_out_filter', true);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function handleUpload(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->checkUpload($request);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-24 09:36:34 +08:00
|
|
|
$t = new Texture();
|
2016-09-04 15:35:12 +08:00
|
|
|
$t->name = $request->input('name');
|
|
|
|
$t->type = $request->input('type');
|
2016-08-14 13:31:56 +08:00
|
|
|
$t->likes = 1;
|
2016-08-17 13:16:46 +08:00
|
|
|
$t->hash = Utils::upload($_FILES['file']);
|
2016-07-24 09:36:34 +08:00
|
|
|
$t->size = ceil($_FILES['file']['size'] / 1024);
|
2016-09-04 15:35:12 +08:00
|
|
|
$t->public = ($request->input('public') == 'true') ? "1" : "0";
|
2016-07-24 09:36:34 +08:00
|
|
|
$t->uploader = $this->user->uid;
|
|
|
|
$t->upload_at = Utils::getTimeFormatted();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-13 23:25:10 +08:00
|
|
|
$cost = $t->size * (($t->public == "1") ? Option::get('score_per_storage') : Option::get('private_score_per_storage'));
|
|
|
|
|
|
|
|
if ($this->user->getScore() < $cost)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.lack-score'), 7);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
$results = Texture::where('hash', $t->hash)->get();
|
2016-08-13 23:25:10 +08:00
|
|
|
|
|
|
|
if (!$results->isEmpty()) {
|
2016-07-21 22:01:57 +08:00
|
|
|
foreach ($results as $result) {
|
2016-09-30 16:31:45 +08:00
|
|
|
// if the texture already uploaded was setted to private,
|
|
|
|
// then allow to re-upload it.
|
|
|
|
if ($result->type == $t->type && $result->public == "1") {
|
|
|
|
return json(trans('skinlib.upload.repeated'), 0, [
|
2016-07-22 10:45:36 +08:00
|
|
|
'tid' => $result->tid
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$t->save();
|
|
|
|
|
2016-08-13 23:25:10 +08:00
|
|
|
$this->user->setScore($cost, 'minus');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-23 21:43:42 +08:00
|
|
|
if ($this->user->closet->add($t->tid, $t->name)) {
|
2016-09-30 16:31:45 +08:00
|
|
|
return json(trans('skinlib.upload.success', ['name' => $request->input('name')]), 0, [
|
2016-07-21 22:01:57 +08:00
|
|
|
'tid' => $t->tid
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function delete(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$result = Texture::find($request->tid);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-24 15:56:23 +08:00
|
|
|
if (!$result)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-24 15:56:23 +08:00
|
|
|
if ($result->uploader != $this->user->uid && !$this->user->is_admin)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.no-permission'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-24 15:56:23 +08:00
|
|
|
// check if file occupied
|
|
|
|
if (Texture::where('hash', $result['hash'])->count() == 1)
|
2016-09-04 15:35:12 +08:00
|
|
|
Storage::delete($result['hash']);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-29 12:46:19 +08:00
|
|
|
$this->user->setScore($result->size * Option::get('score_per_storage'), 'plus');
|
2016-07-24 15:56:23 +08:00
|
|
|
|
|
|
|
if ($result->delete())
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.delete.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function privacy($tid, Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$t = Texture::find($request->tid);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if (!$t)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
|
|
|
if ($t->uploader != $this->user->uid && !$this->user->is_admin)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.no-permission'), 1);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
if ($t->setPrivacy(!$t->public)) {
|
2016-09-10 21:39:45 +08:00
|
|
|
return json([
|
2016-07-21 22:01:57 +08:00
|
|
|
'errno' => 0,
|
2016-09-24 22:49:20 +08:00
|
|
|
'msg' => trans('skinlib.privacy.success', ['privacy' => ($t->public == "0" ? trans('general.private') : trans('general.public'))]),
|
2016-07-21 22:01:57 +08:00
|
|
|
'public' => $t->public
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function rename(Request $request) {
|
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer',
|
|
|
|
'new_name' => 'required|no_special_chars'
|
|
|
|
]);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$t = Texture::find($request->input('tid'));
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if (!$t)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
|
|
|
if ($t->uploader != $this->user->uid && !$this->user->is_admin)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.no-permission'), 1);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$t->name = $request->input('new_name');
|
2016-07-24 15:56:23 +08:00
|
|
|
|
|
|
|
if ($t->save()) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.rename.success', ['name' => $request->input('new_name')]), 0);
|
2016-07-24 15:56:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
|
|
|
* Check Uploaded Files
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function checkUpload(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2016-09-22 22:00:12 +08:00
|
|
|
'name' => 'required|no_special_chars',
|
2016-10-16 20:18:40 +08:00
|
|
|
'file' => 'required|max:'.option('max_upload_file_size'),
|
2016-09-04 15:35:12 +08:00
|
|
|
'public' => 'required'
|
|
|
|
]);
|
|
|
|
|
2016-09-22 22:00:12 +08:00
|
|
|
if ($_FILES['file']['type'] != "image/png" || $_FILES['file']['type'] != "image/x-png") {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.type-error'), 1);
|
2016-09-22 22:00:12 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
// if error occured while uploading file
|
|
|
|
if ($_FILES['file']["error"] > 0)
|
2016-09-10 21:39:45 +08:00
|
|
|
return json($_FILES['file']["error"], 1);
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
$type = $request->input('type');
|
|
|
|
$size = getimagesize($_FILES['file']["tmp_name"]);
|
|
|
|
$ratio = $size[0] / $size[1];
|
|
|
|
|
|
|
|
if ($type == "steve" || $type == "alex") {
|
|
|
|
if ($ratio != 2 && $ratio != 1)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.invalid-size', ['type' => trans('general.skin'), 'width' => $size[0], 'height' => $size[1]]), 1);
|
2016-09-04 15:35:12 +08:00
|
|
|
} elseif ($type == "cape") {
|
|
|
|
if ($ratio != 2)
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.invalid-size', ['type' => trans('general.cape'), 'width' => $size[0], 'height' => $size[1]]), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
} else {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|