このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
076server/app/Http/Controllers/UserController.php

908 行
25 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\ForUser;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Support\Facades\Log;
use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Http\Controllers\AuthController;
class UserController extends Controller {
private $objAuth;
public function __construct() {
$this->objAuth = new AuthController();
}
// User
public function getUsersOnline() { // /api/rpc/user/user/getusersonline
$cols = $this->getGroupColours()->toArray();
$get = DB::table('users')
->join('usr_details', 'usr_details.user_id', '=', 'users.id')
->join('usr_profile', 'usr_profile.user_id', '=', 'users.id')
->join('usr_perm_id', 'usr_perm_id.user_id', '=', 'users.id')
->orderBy('display_name', 'asc')
->orderBy('username', 'asc')
->get(array(
'id',
'username',
'gender',
'perm_id',
'avatar',
'name_style',
'display_name',
'ostatus',
'ontime'
));
$res = array();
foreach ($get as $i) {
$showName = "";
$showCol = "";
$showStatus = "";
$showAva = "";
if ($i->display_name !== '') {
$showName = $i->display_name;
}
else {
$showName = $i->username;
}
if ($i->name_style !== '') {
$showCol = $i->name_style;
}
else {
foreach($cols as $cl) {
if ($cl->id === $i->perm_id) {
if ($i->gender === 1) $showCol = $cl->colour_m;
else if ($i->gender === 2) $showCol = $cl->colour_f;
else $showCol = $cl->colour_u;
}
}
}
if ($i->ostatus == 1) {
$showStatus = "green";
}
else if ($i->ostatus == 2) {
$showStatus = "red";
}
else if ($i->ostatus == 3) {
$showStatus = "yellow";
}
else {
$showStatus = "dark";
}
if (empty($i->avatar)) {
$showAva = "assets/avatars/haznoavaz.png";
}
else {
$showAva = $i->avatar;
}
$res[] = array(
'id' => $i->id,
'display_name' => $showName,
'name_style' => $showCol,
'avatar' => $showAva,
'status' => $showStatus,
'ontime' => $i->ontime
);
}
return $res;
}
public function updateUserStatus(Request $r) { // /api/rpc/user/user/updateuserstatus
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check != 0) {
return DB::table('usr_profile')
->where('user_id', $check)
->update([
'ostatus' => $r->ostatus
]);
}
}
public function updateUserOnline(Request $r) { // /api/rpc/user/user/updateuseronline
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check != 0) {
return DB::table('usr_details')
->where('user_id', $check)
->update([
'ontime' => time()
]);
}
}
public function getUsers(Request $r) { // /api/rpc/user/user/getusers
$getting = array(
'users.id',
'usr_perm_module.name as group_name',
'users.username',
'usr_perm_id.perm_id',
'usr_details.reg_date',
'usr_profile.gender',
'usr_profile.avatar',
'usr_profile.name_style',
'usr_profile.display_name',
'usr_profile.country'
);
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_emailshow'] == 1) {
array_push($getting, 'users.email');
}
if ($valid['usr_ipshow'] == 1) {
array_push($getting, 'usr_profile.ip_address');
}
return DB::table('users')
->join('usr_details', 'usr_details.user_id', '=', 'users.id')
->join('usr_profile', 'usr_profile.user_id', '=', 'users.id')
->join('usr_perm_id', 'usr_perm_id.user_id', '=', 'users.id')
->join('usr_perm_module', 'usr_perm_module.id', 'usr_perm_id.perm_id')
->orderBy('usr_details.reg_date', 'asc')
->get($getting);
}
public function getUser($id, Request $r) { // /api/rpc/user/user/getuser/id/uid
$getting = array(
'users.id',
'users.username',
'usr_perm_module.name as group_name',
'usr_profile.display_name',
'usr_profile.name_style',
'usr_perm_id.perm_id',
'usr_profile.member_title',
'usr_details.total_posts',
'usr_details.total_threads',
'usr_details.reg_date',
'usr_contacts.website_link',
'usr_contacts.website_name',
'usr_profile.post_style as poststyle',
'usr_profile.gender',
'usr_profile.location',
'usr_profile.birthday',
'usr_profile.bio',
'usr_profile.avatar',
'usr_profile.country',
'usr_contacts.website_link',
'usr_contacts.website_name',
'usr_contacts.youtube_link',
'usr_contacts.youtube_name',
'usr_contacts.niconico',
'usr_contacts.pixiv',
'usr_contacts.discord',
'usr_contacts.mastodon',
'usr_contacts.twitter'
);
$check = $this->objAuth->checkLegit($r->kero_token);
$valid = $this->objAuth->getPermissions($r->kero_token);
$cols = $this->getGroupColours()->toArray();
if ($valid['usr_editother'] == 1 || $id == $check) {
array_push($getting, 'users.password');
}
if ($valid['usr_emailshow'] == 1 || $id == $check) {
array_push($getting, 'users.email');
}
if ($valid['usr_ipshow'] == 1 || $id == $check) {
array_push($getting, 'usr_profile.ip_address');
}
if ($valid['usr_canwarn'] == 1 || $id == $check) {
array_push($getting, 'usr_details.strikes');
}
return DB::table('users')
->join('usr_details', 'usr_details.user_id', '=', 'users.id')
->join('usr_profile', 'usr_profile.user_id', '=', 'users.id')
->join('usr_contacts', 'usr_contacts.user_id', '=', 'users.id')
->join('usr_perm_id', 'usr_perm_id.user_id', '=', 'users.id')
->join('usr_perm_module', 'usr_perm_module.id', 'usr_perm_id.perm_id')
->where('users.id', $id)
->get($getting);
}
public function getExist($username, $email) { // /api/rpc/user/user/getexist/username/email
$isExist = DB::table('users')
->select('username', 'email')
->where('username', mb_strtolower($username))
->orWhere('email', mb_strtolower($email))
->get();
return $isExist->count();
}
public function getPostStyle($id) { // /api/rpc/user/user/getpoststyle/id
return DB::table('users')
->select('header', 'footer')
->where('id', $id)
->get();
}
public function getGroups() { // /api/rpc/user/user/getgroups
return DB::table('usr_perm_module')
->select('id', 'name', 'colour_m', 'colour_f', 'colour_u')
->get();
}
public function getGroupName($id) { // /api/rpc/user/user/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',
// 'badge' (this is a pipeline feature, please don't uncomment for now!)
));
}
public function getGroupColours() { // /api/rpc/user/user/getgroupcolours
return DB::table('usr_perm_module')
->select(
'id',
'colour_m',
'colour_f',
'colour_u'
)
->get();
}
// Owner
public function getOwnerList() { // /api/rpc/user/owner/getownerlist
$user_data = DB::table('users')
->join('usr_profile', 'usr_profile.user_id', '=', 'users.id')
->orderBy('id', 'asc')
->get(array('id', 'username', 'display_name'));
$result = array();
foreach ($user_data as $u) {
$name = '';
if (!empty($u->display_name)) {
$name = $u->display_name;
}
else {
$name = $u->username;
}
array_push($result, [
'value' => $u->id,
'label' => $name,
]);
}
return $result;
}
public function countOwnersOfEntry($file_id) { // /api/rpc/user/owner/countownersofentry/id
return DB::table('str_owners')
->where('file_id', $file_id)
->count();
}
public function getOwnersOfEntry($file_id) { // /api/rpc/user/owner/getownersofentry/id
return DB::table('str_owners')
->join('str_file', 'str_owners.file_id', '=', 'str_file.id')
->join('users', 'str_owners.user_id', '=', 'users.id')
->join('usr_details', 'usr_details.user_id', '=', 'str_owners.user_id')
->join('usr_profile', 'usr_profile.user_id', '=', 'str_owners.user_id')
->join('usr_perm_id', 'usr_perm_id.user_id', '=', 'str_owners.user_id')
->where('file_id', $file_id)
->get(array(
'users.id',
'title',
'version',
'views',
'downloads',
'submit_date',
'last_date',
'username',
'avatar',
'perm_id',
'gender',
'display_name',
'name_style',
));
}
public function countEntriesOfOwner($user_id) { // /api/rpc/user/owner/countentriesofowner/id
return DB::table('str_owners')
->where('user_id', $user_id)
->count();
}
public function getEntriesOfOwner($user_id) { // /api/rpc/user/owner/getentriesofowner/id
return DB::table('str_owners')
->join('str_file', 'str_owners.file_id', '=', 'str_file.id')
->join('users', 'str_owners.user_id', '=', 'users.id')
->join('usr_details', 'usr_details.user_id', '=', 'str_owners.user_id')
->join('usr_profile', 'usr_profile.user_id', '=', 'str_owners.user_id')
->join('usr_perm_id', 'usr_perm_id.user_id', '=', 'str_owners.user_id')
->where('user_id', $user_id)
->get(array(
'users.id',
'title',
'version',
'views',
'downloads',
'submit_date',
'last_date',
'username',
'avatar',
'perm_id',
'gender',
'display_name',
'name_style',
));
}
public function getTotalPostCount($id) { // /api/rpc/user/user/gettotalpostcount/id
return DB::table('usr_details')
->select('total_posts')
->where('user_id', $id)
->first()->total_posts;
}
public function getTotalTopicCount($id) { // /api/rpc/user/user/gettotaltopiccount/id
return DB::table('usr_details')
->select('total_threads')
->where('user_id', $id)
->first()->total_threads;
}
public function addOwner(Request $r) { // /api/rpc/user/owner/addowner
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$add = DB::table('str_owners')
->insert([
'user_id' => $r->user_id,
'file_id' => $r->file_id
]);
return \Response::json($add);
}
}
public function updateTotalPostCount(Request $r) { // /api/rpc/user/user/updatetotalpostcount
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$getPC = $this->getTotalPostCount($check);
$getPC++;
return DB::table('usr_details')
->where('user_id', $check)
->update([
'total_posts' => $getPC
]);
}
}
public function updateTotalTopicCount(Request $r) { // /api/rpc/user/user/updatetotaltopiccount
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$getPC = $this->getTotalPostCount($r->user_id);
$getTC = $this->getTotalTopicCount($r->user_id);
$getPC++;
$getTC++;
return DB::table('usr_details')
->where('user_id', $r->user_id)
->update([
'total_posts' => $getPC,
'total_threads' => $getTC
]);
}
}
public function getCountries() { // /api/rpc/user/user/getcountries
$flags = File::files('assets/flags');
$res = array();
foreach ($flags as $flag) {
$protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';
$res[] = array(
'value' => $flag->getBasename('.png'),
'label' => '<img src="'.$protocol.'://'.$_SERVER['SERVER_NAME'].'/'.$flag->getPathname().'" /> '.$flag->getBasename('.png')
);
}
return $res;
}
public function avatarUpload(Request $r) { // /api/rpc/user/user/avatarupload
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$valid = $this->objAuth->getPermissions($r->kero_token);
$user = 0;
if ($valid['usr_editother'] == 1) $user = $r->id;
else $user = $check;
if ($valid['usr_editprofile'] == 1) {
if (isset($r->filename)) {
if (!is_dir("assets/avatars/".$check)) {
if (!mkdir("assets/avatars/".$check, 0755, true)) {
return "Could not make folder ".$check."<br />";
}
}
$img_dir = "assets/avatars/".$check."/";
$image = $img_dir . $r->filename;
$imageFileType = array(
'image/png',
'image/jpeg',
'image/gif'
);
if (!in_array($r->filetype, $imageFileType)) {
return "Only JPG, PNG, JPEG, and GIF are allowed.";
}
$fname = 'assets/avatars/'.$user.'/'.$r->filename;
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $r->thefile));
Storage::disk('public')->put($fname, $data);
return $r->filename;
}
}
else {
return 'Permission denied.';
}
}
}
public function editUser(Request $r) { // /api/rpc/user/user/edit
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_editprofile'] == 1) {
if (isset($r->newPassword)) {
DB::table('users')
->where('id', $r->id)
->update([
'password' => $r->newPassword
]);
}
if (isset($r->email)) {
DB::table('users')
->where('id', $r->id)
->update([
'email' => $r->email
]);
}
if (
isset($r->website_link) ||
isset($r->website_name) ||
isset($r->youtube_link) ||
isset($r->youtube_name) ||
isset($r->niconico) ||
isset($r->pixiv) ||
isset($r->discord) ||
isset($r->mastodon) ||
isset($r->twitter)
) {
DB::table('usr_contacts')
->where('user_id', $r->id)
->update([
'website_link' => ($r->website_link != '' ? $r->website_link : ''),
'website_name' => ($r->website_name != '' ? $r->website_name : ''),
'youtube_link' => ($r->youtube_link != '' ? $r->youtube_link : ''),
'youtube_name' => ($r->youtube_name != '' ? $r->youtube_name : ''),
'niconico' => ($r->niconico != '' ? $r->niconico : ''),
'pixiv' => ($r->pixiv != '' ? $r->pixiv : ''),
'discord' => ($r->discord != '' ? $r->discord : ''),
'mastodon' => ($r->mastodon != '' ? $r->mastodon : ''),
'twitter' => ($r->twitter != '' ? $r->twitter : '')
]);
}
if (isset($r->group) && $valid['usr_editother']) {
DB::table('usr_perm_id')
->where('user_id', $r->id)
->update([
'perm_id' => $r->group
]);
}
if (isset($r->avatar)) {
DB::table('usr_profile')
->where('user_id', $r->id)
->update([
'avatar' => ($r->avatar != '' ? $r->avatar : '')
]);
}
if (isset($r->avatarRemove)) {
if ($r->avatarRemove) {
DB::table('usr_profile')
->where('user_id', $r->id)
->update([
'avatar' => ''
]);
}
}
if (
isset($r->gender) ||
isset($r->aboutSelf) ||
isset($r->signature) ||
isset($r->poststyle) ||
isset($r->nameStyle) ||
isset($r->displayName) ||
isset($r->memberTitle) ||
isset($r->birthDay) ||
isset($r->country)
) {
DB::table('usr_profile')
->where('user_id', $r->id)
->update([
'gender' => $r->gender,
'bio' => ($r->aboutSelf != '' ? $r->aboutSelf : ''),
'post_style' => ($r->poststyle != '' ? $r->poststyle : ''),
'signature' => ($r->signature != '' ? $r->signature : ''),
'name_style' => ($r->nameStyle != '' ? $r->nameStyle : ''),
'display_name' => ($r->displayName != '' ? $r->displayName : ''),
'member_title' => ($r->memberTitle != '' ? $r->memberTitle : ''),
'birthday' => ($r->birthDay != 0 ? $r->birthDay : 0),
'country' => $r->country
]);
}
return 'Success!';
}
else {
return 'Permission denied.';
}
}
}
public function countComments($id) { // /api/rpc/user/comment/count/id
return DB::table('usr_comments')
->where('profile_id', $id)
->count();
}
public function getComments ($id, Request $r) { // /api/rpc/user/comment/get/id
$cols = $this->getGroupColours()->toArray();
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_viewcomment'] == 1) {
$come = array();
$get = DB::table('usr_comments')
->where('profile_id', $id)
->where('reply_id', 0)
->orderBy('usr_comments.postdate', 'asc')
->get(array(
'usr_comments.id as come_id',
'user_id',
'postdate',
'message',
'isEdit',
'isDel'
));
// Foreach, new getter but check on replies, and only if isDel is 0.
foreach ($get as $g) {
$user = $this->getUser($g->user_id, $r)->toArray();
$showName = "";
$showCol = "";
if ($user[0]->display_name !== '') {
$showName = $user[0]->display_name;
}
else {
$showName = $user[0]->username;
}
if ($user[0]->name_style !== '') {
$showCol = $user[0]->name_style;
}
else {
foreach($cols as $cl) {
if ($cl->id === $user[0]->perm_id) {
if ($user[0]->gender === 1) $showCol = $cl->colour_m;
else if ($user[0]->gender === 2) $showCol = $cl->colour_f;
else $showCol = $cl->colour_u;
}
}
}
setlocale(LC_ALL, 'ja_JP.utf8');
$come[] = array(
'come_id' => $g->come_id,
'user_id' => $g->user_id,
'name' => $showName,
'avatar' => ($user[0]->avatar != '' ? $user[0]->avatar : 'assets/avatars/haznoavaz.png'),
'col' => $showCol,
'message' => $g->message,
'postdate' => strftime('%Y/%m/%d(%a) %H:%M:%S %Z', $g->postdate),
'isEdit' => $g->isEdit,
'isDel' => $g->isDel
);
}
return $come;
}
else {
return 'Permission denied.';
}
}
public function getReplies ($id, Request $r) { // /api/rpc/user/comment/reply/id
$cols = $this->getGroupColours()->toArray();
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_viewcomment'] == 1) {
$come = array();
$get = DB::table('usr_comments')
->where('reply_id', $id)
->orderBy('usr_comments.postdate', 'asc')
->get(array(
'reply_id',
'usr_comments.id as come_id',
'user_id',
'postdate',
'message',
'isEdit',
'isDel'
));
// Foreach, new getter but check on replies, and only if isDel is 0.
foreach ($get as $g) {
$user = $this->getUser($g->user_id, $r)->toArray();
$showName = "";
$showCol = "";
if ($user[0]->display_name !== '') {
$showName = $user[0]->display_name;
}
else {
$showName = $user[0]->username;
}
if ($user[0]->name_style !== '') {
$showCol = $user[0]->name_style;
}
else {
foreach($cols as $cl) {
if ($cl->id === $user[0]->perm_id) {
if ($user[0]->gender === 1) $showCol = $cl->colour_m;
else if ($user[0]->gender === 2) $showCol = $cl->colour_f;
else $showCol = $cl->colour_u;
}
}
}
setlocale(LC_ALL, 'ja_JP.utf8');
$come[] = array(
'reply_id' => $g->reply_id,
'come_id' => $g->come_id,
'user_id' => $g->user_id,
'name' => $showName,
'avatar' => ($user[0]->avatar != '' ? $user[0]->avatar : 'assets/avatars/haznoavaz.png'),
'col' => $showCol,
'message' => $g->message,
'postdate' => strftime('%Y/%m/%d(%a) %H:%M:%S %Z', $g->postdate),
'isEdit' => $g->isEdit,
'isDel' => $g->isDel
);
}
return $come;
}
else {
return 'Permission denied.';
}
}
public function addComment (Request $r) { // /api/rpc/user/comment/add
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_addcomment'] == 1) {
$add = DB::table('usr_comments')
->insertGetId([
'user_id' => $check,
'profile_id' => $r->profile_id,
'reply_id' => ($r->reply_id > 0 ? $r->reply_id : 0),
'postdate' => time(),
'message' => $r->message,
'isEdit' => 0,
'isDel' => 0
]);
if ($check != $r->profile_id) $this->addNotification($r, $r->profile_id, 2, '新規プロファイルコメント', 'profile/'.$r->profile_id, 'comment-'.$add);
return \Response::json($add);
}
else {
return 'Permission denied.';
}
}
}
public function editComment (Request $r) { // /api/rpc/user/comment/edit
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_editowncomment'] == 1) {
DB::table('usr_comments')
->where('profile_id', $r->profile_id)
->update([
'message' => $r->message,
'isEdit' => 1
]);
return 'Success!';
}
else {
return 'Permission denied.';
}
}
}
public function deleteComment (Request $r) { // /api/rpc/user/comment/delete
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_delcomment'] == 1) {
DB::table('usr_comments')
->where('id', $r->id)
->update([
'isDel' => 1
]);
return 'Success!';
}
else {
return 'Permission denied.';
}
}
}
public function undeleteComment (Request $r) { // /api/rpc/user/comment/undelete
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check == 0) {
return 'Err!';
}
else {
$valid = $this->objAuth->getPermissions($r->kero_token);
if ($valid['usr_delcomment'] == 1) {
DB::table('usr_comments')
->where('id', $r->id)
->update([
'isDel' => 0
]);
return 'Success!';
}
else {
return 'Permission denied.';
}
}
}
public function getNotification(Request $r) { // /api/rpc/user/notification/get
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check != 0) {
$get = DB::table('usr_notification')
->select('id', 'app_id', 'text', 'section', 'goto')
->where('user_id', $check)
->get();
$res = array();
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;
$res[] = array(
'id' => $g->id,
'text' => $g->text,
'url' => 'http'.($prot == 1 ? 's' : '').'://'.$goto.'/#/'.$g->section
);
}
return $res;
}
else {
return array();
}
}
public function addNotification(Request $r, $uid, $aid, $txt, $sec, $goto) { // /api/rpc/user/notification/add
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check != 0) {
$add = DB::table('usr_notification')
->insert([
'user_id' => $uid,
'app_id' => $aid,
'text' => $txt,
'section' => $sec,
'goto' => $goto
]);
return 1;
}
}
public function delNotification(Request $r) { // /api/rpc/user/notification/del
$check = $this->objAuth->checkLegit($r->kero_token);
if ($check != 0) {
return DB::table('usr_notification')
->where('id', $r->id)
->where('user_id', $check)
->delete();
}
}
}