2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2016-07-29 12:46:19 +08:00
|
|
|
use Utils;
|
2016-10-16 21:30:40 +08:00
|
|
|
use Option;
|
|
|
|
use Carbon\Carbon;
|
2016-07-29 12:46:19 +08:00
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
class User
|
|
|
|
{
|
|
|
|
public $uid = "";
|
|
|
|
public $email = "";
|
|
|
|
|
|
|
|
private $password = "";
|
|
|
|
private $token = "";
|
|
|
|
|
|
|
|
private $storage_used = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of App\Services\Cipher\{cipher}
|
|
|
|
* @var null
|
|
|
|
*/
|
|
|
|
private $cipher = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of App\Models\UserModel
|
|
|
|
* @var null
|
|
|
|
*/
|
2016-08-16 13:27:06 +08:00
|
|
|
private $model = null;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of App\Models\Closet
|
|
|
|
* @var null
|
|
|
|
*/
|
|
|
|
public $closet = null;
|
|
|
|
|
|
|
|
public $is_registered = false;
|
|
|
|
public $is_admin = false;
|
|
|
|
|
|
|
|
/**
|
2016-08-16 13:27:06 +08:00
|
|
|
* Pass uid or an array to instantiate a user
|
2016-07-21 22:01:57 +08:00
|
|
|
*
|
2016-08-16 13:27:06 +08:00
|
|
|
* $info = [
|
|
|
|
* 'username' => 'foo',
|
|
|
|
* 'email' => 'foo@bar.com'
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* @param int $uid
|
|
|
|
* @param array $info
|
2016-07-21 22:01:57 +08:00
|
|
|
*/
|
2016-08-16 22:52:00 +08:00
|
|
|
public function __construct($uid, Array $info = [])
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
// Construct user with uid|email|player_name
|
2016-08-16 22:52:00 +08:00
|
|
|
if ($uid !== null) {
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->uid = $uid;
|
|
|
|
$this->model = UserModel::find($uid);
|
|
|
|
} else {
|
|
|
|
if (isset($info['email'])) {
|
2016-09-30 20:53:43 +08:00
|
|
|
$this->email = e($info['email']);
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model = UserModel::where('email', $this->email)->first();
|
|
|
|
} elseif (isset($info['username'])) {
|
2016-10-16 18:16:15 +08:00
|
|
|
$player = Player::where('player_name', $info['username'])->first();
|
2016-08-16 22:52:00 +08:00
|
|
|
$this->uid = $player ? $player['uid'] : 0;
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model = UserModel::find($this->uid);
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException('Invalid arguments');
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-29 16:41:28 +08:00
|
|
|
$class_name = "App\Services\Cipher\\".config('secure.cipher');
|
2016-07-21 22:01:57 +08:00
|
|
|
$this->cipher = new $class_name;
|
|
|
|
|
2016-08-16 13:27:06 +08:00
|
|
|
if (!is_null($this->model)) {
|
2016-07-21 22:01:57 +08:00
|
|
|
$this->is_registered = true;
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->uid = $this->model->uid;
|
|
|
|
$this->email = $this->model->email;
|
|
|
|
$this->password = $this->model->password;
|
2016-08-29 16:41:28 +08:00
|
|
|
$this->token = md5($this->email . $this->password . config('secure.salt'));
|
2016-07-21 22:01:57 +08:00
|
|
|
$this->closet = new Closet($this->uid);
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->is_admin = $this->model->permission == 1 || $this->model->permission == 2;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkPasswd($raw_passwd)
|
|
|
|
{
|
2016-08-29 16:41:28 +08:00
|
|
|
return ($this->cipher->encrypt($raw_passwd, config('secure.salt')) == $this->password);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function changePasswd($new_passwd)
|
|
|
|
{
|
2016-08-29 16:41:28 +08:00
|
|
|
$this->model->password = $this->cipher->encrypt($new_passwd, config('secure.salt'));
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-07-23 15:20:10 +08:00
|
|
|
public function getPermission()
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->permission;
|
2016-07-23 15:20:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set user permission
|
|
|
|
* @param int $permission
|
|
|
|
* -1 - banned
|
|
|
|
* 0 - normal
|
|
|
|
* 1 - admin
|
2016-07-29 12:46:19 +08:00
|
|
|
* 2 - super admin
|
2016-07-23 15:20:10 +08:00
|
|
|
*/
|
|
|
|
public function setPermission($permission)
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->update(['permission' => $permission]);
|
2016-07-23 15:20:10 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public function setEmail($new_email)
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model->email = $new_email;
|
|
|
|
return $this->model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getNickName()
|
|
|
|
{
|
2016-07-24 11:12:52 +08:00
|
|
|
if (!$this->is_registered) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return trans('general.unexistent-user');
|
2016-07-24 11:12:52 +08:00
|
|
|
} else {
|
2016-08-16 13:27:06 +08:00
|
|
|
return ($this->model->nickname == "") ? $this->email : $this->model->nickname;
|
2016-07-24 11:12:52 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setNickName($new_nickname)
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model->nickname = $new_nickname;
|
|
|
|
return $this->model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 23:09:32 +08:00
|
|
|
public function getToken($refresh = false)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-26 13:22:48 +08:00
|
|
|
if ($this->is_registered && ($this->token === "" || $refresh)) {
|
2016-08-29 16:41:28 +08:00
|
|
|
$this->token = md5($this->model->email . $this->model->password . config('secure.salt'));
|
2016-08-26 13:22:48 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
return $this->token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getScore()
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->score;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setScore($score, $mode = "set")
|
|
|
|
{
|
|
|
|
switch ($mode) {
|
|
|
|
case 'set':
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model->score = $score;
|
2016-07-21 22:01:57 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'plus':
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model->score += $score;
|
2016-07-21 22:01:57 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'minus':
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model->score -= $score;
|
2016-07-21 22:01:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getStorageUsed()
|
|
|
|
{
|
|
|
|
if (is_null($this->storage_used)) {
|
|
|
|
$this->storage_used = 0;
|
|
|
|
// recalculate
|
2016-08-25 13:26:53 +08:00
|
|
|
$sql = "SELECT SUM(`size`) AS total_size FROM `{table}` WHERE uploader = {$this->uid}";
|
2016-08-28 10:05:21 +08:00
|
|
|
$result = \Database::table('textures')->fetchArray($sql)['total_size'];
|
2016-08-25 13:26:53 +08:00
|
|
|
$this->storage_used = $result ?: 0;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
return $this->storage_used;
|
|
|
|
}
|
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
public function checkIn()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-24 22:49:20 +08:00
|
|
|
if ($this->canCheckIn()) {
|
2016-07-29 12:46:19 +08:00
|
|
|
$sign_score = explode(',', Option::get('sign_score'));
|
|
|
|
$aquired_score = rand($sign_score[0], $sign_score[1]);
|
2016-07-21 22:01:57 +08:00
|
|
|
$this->setScore($aquired_score, 'plus');
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model->last_sign_at = Utils::getTimeFormatted();
|
|
|
|
$this->model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
return $aquired_score;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
public function canCheckIn($return_remaining_time = false)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
|
|
|
// convert to timestamp
|
2016-10-16 21:30:40 +08:00
|
|
|
$last_sign_at = strtotime($this->getLastSignTime());
|
2016-07-29 13:57:03 +08:00
|
|
|
|
2016-09-27 22:32:15 +08:00
|
|
|
if (Option::get('sign_after_zero') == "1") {
|
2016-10-16 21:30:40 +08:00
|
|
|
$remaining_time = (Carbon::tomorrow()->timestamp - time()) / 3600;
|
|
|
|
$can_check_in = $last_sign_at <= Carbon::today()->timestamp;
|
2016-07-29 13:57:03 +08:00
|
|
|
} else {
|
2016-10-16 21:30:40 +08:00
|
|
|
$remaining_time = ($last_sign_at + Option::get('sign_gap_time') * 3600 - time()) / 3600;
|
|
|
|
$can_check_in = $remaining_time <= 0;
|
2016-07-29 13:57:03 +08:00
|
|
|
}
|
2016-10-16 21:30:40 +08:00
|
|
|
|
|
|
|
return $return_remaining_time ? round($remaining_time) : $can_check_in;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getLastSignTime()
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->last_sign_at;
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new user
|
|
|
|
* @param string $password
|
|
|
|
* @param string $ip
|
|
|
|
* @return object, instance of App\Models\User
|
|
|
|
*/
|
|
|
|
public function register($password, $ip)
|
|
|
|
{
|
|
|
|
$user = new UserModel();
|
|
|
|
|
|
|
|
$user->email = $this->email;
|
2016-08-29 16:41:28 +08:00
|
|
|
$user->password = $this->cipher->encrypt($password, config('secure.salt'));
|
2016-07-21 22:01:57 +08:00
|
|
|
$user->ip = $ip;
|
2016-07-29 12:46:19 +08:00
|
|
|
$user->score = Option::get('user_initial_score');
|
|
|
|
$user->register_at = Utils::getTimeFormatted();
|
|
|
|
$user->last_sign_at = Utils::getTimeFormatted(time() - 86400);
|
2016-07-21 22:01:57 +08:00
|
|
|
$user->permission = 0;
|
|
|
|
$user->save();
|
|
|
|
|
2016-10-16 22:06:31 +08:00
|
|
|
$closet = new Closet($user->uid);
|
2016-08-26 13:22:48 +08:00
|
|
|
$this->model = $user;
|
|
|
|
$this->uid = $user->uid;
|
|
|
|
$this->is_registered = true;
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPlayers()
|
|
|
|
{
|
2016-10-16 18:16:15 +08:00
|
|
|
return Player::where('uid', $this->uid)->get();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-07 16:56:54 +08:00
|
|
|
public function getAvatarId()
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->avatar;
|
2016-08-07 16:56:54 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public function setAvatar($tid)
|
|
|
|
{
|
2016-08-16 13:27:06 +08:00
|
|
|
$this->model->avatar = $tid;
|
|
|
|
return $this->model->save();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete()
|
|
|
|
{
|
2016-10-16 18:16:15 +08:00
|
|
|
Player::where('uid', $this->uid)->delete();
|
2016-10-16 22:06:31 +08:00
|
|
|
DB::table('closets')->where('uid', $this->uid)->delete();
|
2016-08-16 13:27:06 +08:00
|
|
|
return $this->model->delete();
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserModel extends \Illuminate\Database\Eloquent\Model
|
|
|
|
{
|
|
|
|
public $primaryKey = 'uid';
|
|
|
|
protected $table = 'users';
|
|
|
|
public $timestamps = false;
|
2016-07-22 19:36:24 +08:00
|
|
|
|
2016-07-23 15:20:10 +08:00
|
|
|
protected $fillable = ['email', 'nickname', 'permission'];
|
|
|
|
|
2016-07-22 19:36:24 +08:00
|
|
|
public function scopeLike($query, $field, $value)
|
|
|
|
{
|
|
|
|
return $query->where($field, 'LIKE', "%$value%");
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|