2016-07-21 22:01:57 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\Texture;
|
|
|
|
|
use App\Exceptions\E;
|
2016-08-06 21:53:55 +08:00
|
|
|
|
use Validate;
|
2016-07-24 09:36:34 +08:00
|
|
|
|
use Option;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
use Utils;
|
|
|
|
|
use View;
|
2016-07-24 09:36:34 +08:00
|
|
|
|
use Http;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
|
|
class SkinlibController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
private $user = null;
|
|
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
|
{
|
2016-08-16 22:52:00 +08:00
|
|
|
|
$this->user = isset($_SESSION['uid']) ? new User($_SESSION['uid']) : null;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
2016-08-13 22:31:24 +08:00
|
|
|
|
$filter = isset($_GET['filter']) ? $_GET['filter'] : "skin";
|
|
|
|
|
$sort = isset($_GET['sort']) ? $_GET['sort'] : "time";
|
2016-07-21 22:01:57 +08:00
|
|
|
|
$sort_by = ($sort == "time") ? "upload_at" : $sort;
|
2016-08-13 22:31:24 +08:00
|
|
|
|
$uid = isset($_GET['uid']) ? $_GET['uid'] : 0;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
2016-08-13 22:31:24 +08:00
|
|
|
|
$page = isset($_GET['page']) ? $_GET['page'] : 1;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
|
|
if ($filter == "skin") {
|
|
|
|
|
$textures = Texture::where(function($query) {
|
2016-07-26 13:36:24 +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
|
|
|
|
|
|
|
|
|
echo View::make('skinlib.index')->with('user', $this->user)
|
|
|
|
|
->with('sort', $sort)
|
|
|
|
|
->with('filter', $filter)
|
|
|
|
|
->with('textures', $textures)
|
|
|
|
|
->with('page', $page)
|
|
|
|
|
->with('total_pages', $total_pages)
|
|
|
|
|
->render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function search()
|
|
|
|
|
{
|
|
|
|
|
$q = isset($_GET['q']) ? $_GET['q'] : "";
|
|
|
|
|
|
|
|
|
|
$filter = isset($_GET['filter']) ? $_GET['filter'] : "skin";
|
|
|
|
|
|
|
|
|
|
$sort = isset($_GET['sort']) ? $_GET['sort'] : "time";
|
|
|
|
|
$sort_by = ($sort == "time") ? "upload_at" : $sort;
|
|
|
|
|
|
|
|
|
|
if ($filter == "skin") {
|
|
|
|
|
$textures = Texture::like('name', $q)->where(function($query) use ($q) {
|
|
|
|
|
$query->where('public', '=', '1')
|
|
|
|
|
->where('type', '=', 'steve')
|
|
|
|
|
->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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo View::make('skinlib.search')->with('user', $this->user)
|
|
|
|
|
->with('sort', $sort)
|
|
|
|
|
->with('filter', $filter)
|
|
|
|
|
->with('q', $q)
|
|
|
|
|
->with('textures', $textures)->render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show()
|
|
|
|
|
{
|
2016-07-24 09:36:34 +08:00
|
|
|
|
if (!isset($_GET['tid'])) Http::abort(404, 'No specified tid.');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
$texture = Texture::find($_GET['tid']);
|
2016-08-16 22:58:21 +08:00
|
|
|
|
|
|
|
|
|
if (!$texture || $texture && !\Storage::exist(BASE_DIR."/textures/".$texture->hash)) {
|
|
|
|
|
Http::abort(404, '请求的材质文件已经被删除,请联系管理员删除该条目');
|
|
|
|
|
}
|
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-07-24 09:36:34 +08:00
|
|
|
|
Http::abort(404, '请求的材质已经设为隐私,仅上传者和管理员可查看');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo View::make('skinlib.show')->with('texture', $texture)->with('with_out_filter', true)->with('user', $this->user)->render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function info($tid)
|
|
|
|
|
{
|
|
|
|
|
echo json_encode(Texture::find($tid)->toArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function upload()
|
|
|
|
|
{
|
|
|
|
|
echo View::make('skinlib.upload')->with('user', $this->user)->with('with_out_filter', true)->render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handleUpload()
|
|
|
|
|
{
|
|
|
|
|
$this->checkUpload(isset($_POST['type']) ? $_POST['type'] : "");
|
|
|
|
|
|
2016-07-24 09:36:34 +08:00
|
|
|
|
$t = new Texture();
|
|
|
|
|
$t->name = $_POST['name'];
|
|
|
|
|
$t->type = $_POST['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);
|
|
|
|
|
$t->public = ($_POST['public'] == 'true') ? "1" : "0";
|
|
|
|
|
$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-07-21 22:01:57 +08:00
|
|
|
|
View::json('积分不够啦', 7);
|
|
|
|
|
|
|
|
|
|
$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-07-22 10:45:36 +08:00
|
|
|
|
if ($result->type == $t->type) {
|
|
|
|
|
View::json([
|
|
|
|
|
'errno' => 0,
|
|
|
|
|
'msg' => '已经有人上传过这个材质了,直接添加到衣柜使用吧~',
|
|
|
|
|
'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-07-21 22:01:57 +08:00
|
|
|
|
View::json([
|
|
|
|
|
'errno' => 0,
|
|
|
|
|
'msg' => '材质 '.$_POST['name'].' 上传成功',
|
|
|
|
|
'tid' => $t->tid
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
|
{
|
2016-08-06 21:53:55 +08:00
|
|
|
|
Validate::checkPost(['tid']);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
2016-07-24 15:56:23 +08:00
|
|
|
|
$result = Texture::find($_POST['tid']);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
2016-07-24 15:56:23 +08:00
|
|
|
|
if (!$result)
|
|
|
|
|
View::json('Unexistent texture.', 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)
|
|
|
|
|
View::json('你不是这个材质的上传者哦', 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)
|
|
|
|
|
\Storage::remove("./textures/".$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())
|
|
|
|
|
View::json('材质已被成功删除', 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function privacy($tid)
|
|
|
|
|
{
|
|
|
|
|
$t = Texture::find($tid);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
|
|
|
|
|
if (!$t) View::json('Unexistent texture.', 1);
|
|
|
|
|
|
|
|
|
|
if ($t->uploader != $this->user->uid && !$this->user->is_admin)
|
|
|
|
|
View::json('你不是这个材质的上传者哦', 1);
|
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
|
if ($t->setPrivacy(!$t->public)) {
|
|
|
|
|
View::json([
|
|
|
|
|
'errno' => 0,
|
|
|
|
|
'msg' => '材质已被设为'.($t->public == "0" ? "隐私" : "公开"),
|
|
|
|
|
'public' => $t->public
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-24 15:56:23 +08:00
|
|
|
|
public function rename() {
|
2016-08-06 21:53:55 +08:00
|
|
|
|
Validate::checkPost(['tid', 'new_name']);
|
|
|
|
|
Validate::textureName($_POST['new_name']);
|
2016-07-24 15:56:23 +08:00
|
|
|
|
|
|
|
|
|
$t = Texture::find($_POST['tid']);
|
|
|
|
|
|
|
|
|
|
if (!$t) View::json('材质不存在', 1);
|
|
|
|
|
|
|
|
|
|
if ($t->uploader != $this->user->uid && !$this->user->is_admin)
|
|
|
|
|
View::json('你不是这个材质的上传者哦', 1);
|
|
|
|
|
|
|
|
|
|
$t->name = $_POST['new_name'];
|
|
|
|
|
|
|
|
|
|
if ($t->save()) {
|
|
|
|
|
View::json('材质名称已被成功设置为'.$_POST['new_name'], 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
|
private function checkUpload($type)
|
|
|
|
|
{
|
2016-08-06 21:53:55 +08:00
|
|
|
|
Validate::textureName(Utils::getValue('name', $_POST));
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
|
|
if (!Utils::getValue('file', $_FILES))
|
2016-07-24 15:56:23 +08:00
|
|
|
|
View::json('你还没有选择任何文件哟', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
|
|
if (!isset($_POST['public']) || ($_POST['public'] != 0 && $_POST['public'] != 1))
|
2016-08-16 22:52:00 +08:00
|
|
|
|
View::json('非法参数', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
|
|
if ($_FILES['file']['type'] == "image/png" || $_FILES['file']['type'] == "image/x-png")
|
|
|
|
|
{
|
|
|
|
|
// if error occured while uploading file
|
|
|
|
|
if ($_FILES['file']["error"] > 0)
|
2016-07-24 15:56:23 +08:00
|
|
|
|
View::json($_FILES['file']["error"], 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
|
|
$size = getimagesize($_FILES['file']["tmp_name"]);
|
|
|
|
|
$ratio = $size[0] / $size[1];
|
|
|
|
|
|
|
|
|
|
if ($type == "steve" || $type == "alex") {
|
|
|
|
|
if ($ratio != 2 && $ratio != 1)
|
2016-07-24 15:56:23 +08:00
|
|
|
|
View::json("不是有效的皮肤文件(宽 {$size[0]},高 {$size[1]})", 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
} elseif ($type == "cape") {
|
|
|
|
|
if ($ratio != 2)
|
2016-07-24 15:56:23 +08:00
|
|
|
|
View::json("不是有效的披风文件(宽 {$size[0]},高 {$size[1]})", 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
} else {
|
2016-08-16 22:52:00 +08:00
|
|
|
|
View::json('非法参数', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if (Utils::getValue('file', $_FILES)) {
|
2016-07-24 15:56:23 +08:00
|
|
|
|
View::json('文件格式不对哦', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
} else {
|
2016-07-24 15:56:23 +08:00
|
|
|
|
View::json('No file selected.', 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|