2018-01-24 01:36:47 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-01-24 19:26:00 +09:00
|
|
|
use App\Models\ForUser;
|
|
|
|
|
2018-01-24 05:33:53 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2018-06-23 19:22:16 +09:00
|
|
|
use Illuminate\Support\Facades\File;
|
2018-06-22 19:43:14 +09:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-12-05 18:32:20 +09:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2018-01-24 01:36:47 +09:00
|
|
|
use Illuminate\Http\Request;
|
2018-03-06 21:34:19 +09:00
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory;
|
2018-01-24 01:36:47 +09:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-02-07 00:58:54 +09:00
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
2018-01-24 01:36:47 +09:00
|
|
|
|
2018-04-12 18:02:24 +09:00
|
|
|
use App\Http\Controllers\AuthController;
|
2018-03-02 00:59:26 +09:00
|
|
|
|
2018-01-24 01:36:47 +09:00
|
|
|
class UserController extends Controller {
|
2019-01-19 11:01:33 +09:00
|
|
|
private $objAuth;
|
2020-09-23 14:15:07 +09:00
|
|
|
private $objSite;
|
2019-01-19 11:01:33 +09:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->objAuth = new AuthController();
|
2020-09-23 14:15:07 +09:00
|
|
|
$this->menu = getPagesInMenu();
|
|
|
|
$this->cook = (isset($_COOKIE['kero_token']) ? $_COOKIE['kero_token'] : '');
|
|
|
|
$this->id = $this->objAuth->checkLegit($this->cook);
|
|
|
|
$this->user = $this->getLoggedUser($this->id, $this->cook);
|
2019-01-19 11:01:33 +09:00
|
|
|
}
|
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
public function getLoggedUser ($id, $kero) {
|
2020-01-06 18:24:32 +09:00
|
|
|
$check = $this->objAuth->checkLegit($kero);
|
2020-02-02 13:39:53 +09:00
|
|
|
if ($check == 0) return 0;
|
|
|
|
|
2020-01-06 18:24:32 +09:00
|
|
|
$valid = $this->objAuth->getPermissions($kero);
|
|
|
|
$cols = $this->getGroupColours()->toArray();
|
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
$get = DB::table('users')->where('id', $id)->first();
|
|
|
|
$get->profile = DB::table('usr_profile')->where('user_id', $id)->first();
|
|
|
|
$get->profile->showname = (!empty($get->profile->display_name) && !is_null($get->profile->display_name) ? $get->profile->display_name : $get->username);
|
2020-07-24 22:07:01 +09:00
|
|
|
$get->perm = $valid;
|
2020-06-22 13:48:34 +09:00
|
|
|
if (empty($get->avatar) || $get->avatar == '') $get->avatar = '/img/noicon.webp';
|
2020-01-06 18:24:32 +09:00
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
return $get;
|
|
|
|
}
|
2020-01-06 18:24:32 +09:00
|
|
|
|
2020-02-02 14:21:33 +09:00
|
|
|
public function getGroupName($id) {
|
|
|
|
return DB::table('usr_perm_module')->join('usr_perm_id', 'usr_perm_id.perm_id', '=', 'usr_perm_module.id')->where('user_id', $id)->get(array('name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGroupColours() {
|
|
|
|
return DB::table('usr_perm_module')->select('id', 'colour_m', 'colour_f', 'colour_u')->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUser ($id, $kero) {
|
2020-02-02 13:39:53 +09:00
|
|
|
$check = $this->objAuth->checkLegit($kero);
|
2020-01-06 18:24:32 +09:00
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
$valid = $this->objAuth->getPermissions($kero);
|
|
|
|
$cols = $this->getGroupColours()->toArray();
|
2020-01-06 18:24:32 +09:00
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
$get = DB::table('users')->where('id', $id)->first();
|
|
|
|
$get->details = DB::table('usr_details')->where('user_id', $id)->first();
|
|
|
|
$get->profile = DB::table('usr_profile')->where('user_id', $id)->first();
|
|
|
|
$get->contacts = DB::table('usr_contacts')->where('user_id', $id)->first();
|
|
|
|
$get->perm_id = DB::table('usr_perm_id')->where('user_id', $id)->first();
|
|
|
|
$get->perm_module = DB::table('usr_perm_module')->where('usr_perm_id', $get->perm_id->usr_per_id)->first();
|
2020-03-05 08:35:04 +09:00
|
|
|
if (is_null($get->profile->avatar) || empty($get->profile->avatar) || $get->profile->avatar == '') {
|
2020-06-22 13:48:34 +09:00
|
|
|
$get->profile->avatar = 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].'/img/noicon.webp';
|
2020-03-05 08:35:04 +09:00
|
|
|
}
|
|
|
|
else $get->profile->avatar = '/'.$get->profile->avatar;
|
2020-01-06 18:24:32 +09:00
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
$get->profile->gender_name = '不明';
|
|
|
|
if ($get->profile->gender == 1) $get->profile->gender_name = '男性';
|
|
|
|
else if ($get->profile->gender == 2) $get->profile->gender_name = '女性';
|
2020-01-07 22:48:29 +09:00
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
$get->details->reg_date = strftime('%Y年%m月%d日(%a)', $get->details->reg_date);
|
|
|
|
$get->profile->showname = (!empty($get->profile->display_name) && !is_null($get->profile->display_name) ? $get->profile->display_name : $get->username);
|
|
|
|
|
|
|
|
if ($id != $check) {
|
|
|
|
unset($get->password);
|
|
|
|
unset($get->kero_token);
|
|
|
|
unset($get->salt);
|
2020-01-06 18:24:32 +09:00
|
|
|
}
|
|
|
|
|
2020-02-02 13:39:53 +09:00
|
|
|
if ($valid['usr_emailshow'] == 0 || $id != $check) unset($get->email);
|
|
|
|
if ($valid['usr_ipshow'] == 0 || $id != $check) unset($get->profile->ip_address);
|
|
|
|
if ($valid['usr_canwarn'] == 0 || $id != $check) unset($get->details->strikes);
|
|
|
|
|
2020-01-06 18:24:32 +09:00
|
|
|
return $get;
|
|
|
|
}
|
|
|
|
|
2020-03-01 22:12:50 +09:00
|
|
|
public function getCountries () {
|
|
|
|
$flags = DB::table('nhn_country')->orderBy('id', 'asc')->get();
|
2019-01-19 11:01:33 +09:00
|
|
|
$res = array();
|
2018-03-02 00:59:26 +09:00
|
|
|
|
2019-01-19 11:01:33 +09:00
|
|
|
foreach ($flags as $flag) {
|
|
|
|
$protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';
|
2018-03-02 00:59:26 +09:00
|
|
|
|
2019-01-19 11:01:33 +09:00
|
|
|
$res[] = array(
|
2020-03-01 22:12:50 +09:00
|
|
|
'value' => $flag->nameEng,
|
|
|
|
'label' => $flag->name
|
2019-01-19 11:01:33 +09:00
|
|
|
);
|
2018-01-30 18:38:55 +09:00
|
|
|
}
|
|
|
|
|
2019-01-19 11:01:33 +09:00
|
|
|
return $res;
|
|
|
|
}
|
2018-02-15 22:43:45 +09:00
|
|
|
|
2020-02-02 14:21:33 +09:00
|
|
|
public function avatarUpload(Request $r) {
|
2019-05-25 09:52:37 +09:00
|
|
|
$check = $this->objAuth->checkLegit($r->kero_token);
|
2018-02-15 22:43:45 +09:00
|
|
|
|
2020-02-02 14:21:33 +09:00
|
|
|
if ($check == 0) return 'Err!';
|
2019-01-19 11:01:33 +09:00
|
|
|
else {
|
2019-05-25 09:52:37 +09:00
|
|
|
$valid = $this->objAuth->getPermissions($r->kero_token);
|
2019-01-19 11:01:33 +09:00
|
|
|
$user = 0;
|
|
|
|
|
2019-05-25 09:52:37 +09:00
|
|
|
if ($valid['usr_editother'] == 1) $user = $r->id;
|
2019-01-19 11:01:33 +09:00
|
|
|
else $user = $check;
|
|
|
|
|
|
|
|
if ($valid['usr_editprofile'] == 1) {
|
2019-05-25 09:52:37 +09:00
|
|
|
if (isset($r->filename)) {
|
2020-05-26 09:54:01 +09:00
|
|
|
if (!is_dir('/usericon/'.$check)) {
|
|
|
|
if (!mkdir('/usericon/'.$check, 0755, true)) return 'Could not make folder '.$check.'<br />';
|
2019-01-19 11:01:33 +09:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:54:01 +09:00
|
|
|
$img_dir = '/usericon/'.$check.'/';
|
2019-05-25 09:52:37 +09:00
|
|
|
$image = $img_dir . $r->filename;
|
2020-02-02 14:21:33 +09:00
|
|
|
$imageFileType = array('image/png', 'image/jpeg', 'image/gif');
|
|
|
|
|
|
|
|
if (!in_array($r->filetype, $imageFileType)) return "Only JPG, PNG, JPEG, and GIF are allowed.";
|
2019-01-19 11:01:33 +09:00
|
|
|
|
2020-05-26 09:54:01 +09:00
|
|
|
$fname = '/usericon/'.$user.'/'.$r->filename;
|
2019-05-25 09:52:37 +09:00
|
|
|
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $r->thefile));
|
2019-01-19 11:01:33 +09:00
|
|
|
Storage::disk('public')->put($fname, $data);
|
|
|
|
|
2019-05-25 09:52:37 +09:00
|
|
|
return $r->filename;
|
2019-01-19 11:01:33 +09:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 14:21:33 +09:00
|
|
|
else return 'Permission denied.';
|
2018-08-03 14:28:41 +09:00
|
|
|
}
|
2019-01-19 11:01:33 +09:00
|
|
|
}
|
2019-03-17 07:00:22 +09:00
|
|
|
|
2020-09-23 14:15:07 +09:00
|
|
|
public function profile ($id) {
|
|
|
|
$res = $this->getUser($id, $this->cook);
|
|
|
|
return view('pages.site.profile', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function editProfile ($id) {
|
|
|
|
$err = '';
|
|
|
|
|
|
|
|
if ($this->user) {
|
|
|
|
$res = $this->getUser($id, $this->cook);
|
|
|
|
$cnt = $this->getCountries();
|
|
|
|
return view('pages.site.profileedit', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user, 'err' => $err, 'cnt' => $cnt]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function login (Request $r) {
|
|
|
|
if (isset($_COOKIE['kero_token'])) return redirect('');
|
|
|
|
$res = array();
|
|
|
|
$err = '';
|
|
|
|
|
|
|
|
if (isset($r->username) && isset($r->password)) {
|
|
|
|
$res = $this->objAuth->login($r);
|
|
|
|
if (isset($res['kero_token'])) return redirect('');
|
|
|
|
$err = $res['err'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('pages.site.login', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user, 'err' => $err]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function register (Request $r) {
|
|
|
|
if (isset($_COOKIE['kero_token'])) return redirect('');
|
|
|
|
$res = $this->getCountries();
|
|
|
|
$err = '';
|
|
|
|
|
|
|
|
if (isset($r->username) && isset($r->password) && isset($r->email) && isset($r->password_check)) {
|
|
|
|
$reg = $this->objAuth->register($r);
|
|
|
|
if (isset($reg['kero_token'])) return redirect('');
|
|
|
|
$err = $reg['err'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('pages.site.register', ['res' => $res, 'menu' => $this->menu, 'user' => $this->user, 'err' => $err]);
|
|
|
|
}
|
|
|
|
|
2019-12-05 18:32:20 +09:00
|
|
|
public function getNotification (Request $r) { // /api/rpc/user/notification/get
|
2019-05-25 09:52:37 +09:00
|
|
|
$check = $this->objAuth->checkLegit($r->kero_token);
|
2019-12-05 18:32:20 +09:00
|
|
|
$res = null;
|
2019-03-17 07:00:22 +09:00
|
|
|
|
|
|
|
if ($check != 0) {
|
2019-12-05 18:32:20 +09:00
|
|
|
if (Cache::has('getNotification')) $get = Cache::get('getNotification');
|
|
|
|
else {
|
2020-02-02 14:21:33 +09:00
|
|
|
$get = DB::table('usr_notification')->select('id', 'app_id', 'text', 'section', 'goto')->where('user_id', $check)->get();
|
2019-12-05 18:32:20 +09:00
|
|
|
$res = array();
|
2019-03-17 07:00:22 +09:00
|
|
|
|
2019-12-05 18:32:20 +09:00
|
|
|
foreach ($get as $g) {
|
|
|
|
$prot = DB::table('sys_settings')->select('protocol')->first()->protocol;
|
|
|
|
$goto = DB::table('sys_apps')->select('url')->where('id', $g->app_id)->first()->url;
|
2019-03-17 07:00:22 +09:00
|
|
|
|
2020-02-02 14:21:33 +09:00
|
|
|
$res[] = array('id' => $g->id, 'text' => $g->text, 'url' => 'http'.($prot == 1 ? 's' : '').'://'.$goto.'/#/'.$g->section);
|
2019-12-05 18:32:20 +09:00
|
|
|
Cache::put('getNotification', $get);
|
|
|
|
}
|
2019-03-17 07:00:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2019-12-05 18:32:20 +09:00
|
|
|
else return array();
|
2019-03-17 07:00:22 +09:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:21:33 +09:00
|
|
|
public function addNotification(Request $r, $uid, $aid, $txt, $sec, $goto) {
|
2019-05-25 09:52:37 +09:00
|
|
|
$check = $this->objAuth->checkLegit($r->kero_token);
|
2019-03-17 07:00:22 +09:00
|
|
|
|
|
|
|
if ($check != 0) {
|
2020-02-02 14:21:33 +09:00
|
|
|
$add = DB::table('usr_notification')->insert(['user_id' => $uid, 'app_id' => $aid, 'text' => $txt, 'section' => $sec, 'goto' => $goto]);
|
2019-12-05 18:32:20 +09:00
|
|
|
if (Cache::has('getNotification')) Cache::forget('getNotification');
|
2019-03-17 07:00:22 +09:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:21:33 +09:00
|
|
|
public function delNotification(Request $r) {
|
2019-05-25 09:52:37 +09:00
|
|
|
$check = $this->objAuth->checkLegit($r->kero_token);
|
2019-03-17 07:00:22 +09:00
|
|
|
|
|
|
|
if ($check != 0) {
|
2020-02-02 14:21:33 +09:00
|
|
|
$del = DB::table('usr_notification')->where('id', $r->id)->where('user_id', $check)->delete();
|
2019-12-05 18:32:20 +09:00
|
|
|
if (Cache::has('getNotification')) Cache::forget('getNotification');
|
2020-02-02 14:21:33 +09:00
|
|
|
return $del;
|
2019-03-17 07:00:22 +09:00
|
|
|
}
|
|
|
|
}
|
2018-01-24 01:36:47 +09:00
|
|
|
}
|