From 58e5eaaa9426f7893073f698b777b23cf9d82f61 Mon Sep 17 00:00:00 2001 From: Astrologies Date: Thu, 23 Dec 2021 01:02:34 -0500 Subject: [PATCH] User impl update --- globals/Dependencies/Users/User.php | 107 +++++++++++++++++++++++ globals/functions.php | 95 -------------------- html_api/settings/update/joinprivacy.php | 5 +- html_api/user/feed/index.php | 5 +- html_api/users/profile/info.php | 9 +- html_api/users/profile/inventory.php | 5 +- html_api/users/siteusers.php | 5 +- 7 files changed, 129 insertions(+), 102 deletions(-) diff --git a/globals/Dependencies/Users/User.php b/globals/Dependencies/Users/User.php index ce94086..d13a67f 100644 --- a/globals/Dependencies/Users/User.php +++ b/globals/Dependencies/Users/User.php @@ -2,10 +2,32 @@ namespace Alphaland\Users { + use Alphaland\Assets\Asset; use PDO; class User { + public static function UserExists(int $userid) + { + $get = $GLOBALS['pdo']->prepare("SELECT COUNT(*) FROM users WHERE id = :i"); + $get->bindParam(":i", $userid, PDO::PARAM_INT); + $get->execute(); + if($get->fetchColumn() > 0) { + return true; + } + return false; + } + + public static function GetUserInfo(int $userid) + { + $user = $GLOBALS['pdo']->prepare("SELECT * FROM users WHERE id = :u"); + $user->bindParam(":u", $userid, PDO::PARAM_STR); + $user->execute(); + if($user->rowCount() > 0) { + return $user->fetch(PDO::FETCH_OBJ); + } + } + public static function ValidatePassword(int $userid, string $password) { $userpassword = $GLOBALS['pdo']->prepare("SELECT pwd FROM users WHERE id = :i"); @@ -66,5 +88,90 @@ namespace Alphaland\Users { } return $wearingassets; } + + public static function SetCanJoinUser(int $userid, int $status) + { + if ($status <= 2) { + $setstatus = $GLOBALS['pdo']->prepare("UPDATE users SET canJoin = :c WHERE id = :u"); + $setstatus->bindParam(":c", $status, PDO::PARAM_INT); + $setstatus->bindParam(":u", $userid, PDO::PARAM_INT); + $setstatus->execute(); + if ($setstatus->rowCount() > 0) { + return true; + } + } + return false; + } + + public static function CanJoinUser(int $targetuser) //TODO: fix when friends class is implemented + { + /* + 0 = no one + 1 = friends + 2 = everyone + */ + + $canjoin = User::GetUserInfo($targetuser)->canJoin; + if($canjoin == 1) { + if (friendsWith($targetuser)) { + return true; + } + } else if ($canjoin == 2) { + return true; + } + return false; + } + + public static function SiteStatus(int $userid) + { + $p = $GLOBALS['pdo']->prepare("SELECT * FROM game_presence WHERE uid = :i AND (lastPing + 50) > UNIX_TIMESTAMP()"); + $p->bindParam(":i", $userid, PDO::PARAM_INT); + $p->execute(); + $userinfo = User::GetUserInfo($userid); + + if($p->rowCount() > 0) { + if (User::CanJoinUser($userinfo->id)) { + return cleanOutput(Asset::GetAssetInfo($p->fetch(PDO::FETCH_OBJ)->placeid)->Name); + } else { + return 'In-Game'; + } + } + else { + if (($userinfo->lastseen + 120) > time()) { + return 'Online'; + } else { + return 'Offline'; + } + } + } + + public static function UserPlaying(int $userid) + { + $p = $GLOBALS['pdo']->prepare("SELECT * FROM game_presence WHERE uid = :i AND (lastPing + 50) > UNIX_TIMESTAMP()"); + $p->bindParam(":i", $userid, PDO::PARAM_INT); + $p->execute(); + + if($p->rowCount() > 0) { + if (User::CanJoinUser($userid)) { + $playingInfo = $p->fetch(PDO::FETCH_OBJ); + return array ( + "placeid" => $playingInfo->placeid, + "jobid" => $playingInfo->jobid + ); + } + } + return array ( + "placeid" => null, + "jobid" => null + ); + } + + public static function IsInventoryPrivate(int $userid) + { + if (User::GetUserInfo($userid)->privateInventory && !$GLOBALS['user']->IsAdmin()) { + return true; + } + return false; + } } } \ No newline at end of file diff --git a/globals/functions.php b/globals/functions.php index 6f03e2b..b9d19cc 100644 --- a/globals/functions.php +++ b/globals/functions.php @@ -2073,40 +2073,6 @@ function playerOwnsAsset($id, $userid=NULL) //user functions -function userPlaying($userid) -{ - $p = $GLOBALS['pdo']->prepare("SELECT * FROM game_presence WHERE uid = :i AND (lastPing + 50) > UNIX_TIMESTAMP()"); - $p->bindParam(":i", $userid, PDO::PARAM_INT); - $p->execute(); - - if($p->rowCount() > 0) //if the ingame check has any results - { - if (canJoinUser($userid)) - { - $playingInfo = $p->fetch(PDO::FETCH_OBJ); - $info = array ( - "placeid" => $playingInfo->placeid, - "jobid" => $playingInfo->jobid - ); - return $info; - } - } - $info = array ( - "placeid" => null, - "jobid" => null - ); - return $info; -} - -function isUserInventoryPrivate($userid) -{ - if(userInfo($userid)->privateInventory && !$GLOBALS['user']->IsAdmin()) - { - return true; - } - return false; -} - function chatFilterInfractionLimit($userid, $limit, $seconds) { $infractions = $GLOBALS['pdo']->prepare("SELECT * FROM chat_logs WHERE whoSent = :uid AND (whenSent + :seconds) > UNIX_TIMESTAMP() AND trippedFilter = 1"); @@ -2132,50 +2098,6 @@ function kickUserIfInGame($userid, $message) } } -function siteStatus($userid) -{ - $p = $GLOBALS['pdo']->prepare("SELECT * FROM game_presence WHERE uid = :i AND (lastPing + 50) > UNIX_TIMESTAMP()"); - $p->bindParam(":i", $userid, PDO::PARAM_INT); - $p->execute(); - - $userinfo = $GLOBALS['pdo']->prepare('SELECT * FROM `users` WHERE id = :uid'); - $userinfo->bindParam(':uid', $userid, PDO::PARAM_INT); - $userinfo->execute(); - $userinfo = $userinfo->fetch(PDO::FETCH_OBJ); - - if($p->rowCount() > 0) //if the ingame check has any results - { - $serverInfo = $p->fetch(PDO::FETCH_OBJ); - - $g = $GLOBALS['pdo']->prepare("SELECT * FROM assets WHERE id = :i"); - $g->bindParam(":i", $serverInfo->placeid, PDO::PARAM_INT); - $g->execute(); - - $gameInfo = $g->fetch(PDO::FETCH_OBJ); - - if (canJoinUser($userinfo->id)) //depending on the user's settings, show what game they are playing (might wanna also pass the userID variable if there are options such as everyone, friends only, etc) - { - //user viewing profile has permission to see what game they are in - return cleanOutput($gameInfo->Name); - } - else - { - //no perms - return 'In-Game'; - } - } - else //if no ingame result, check if the user has pinged the site in a while - { - if (($userinfo->lastseen + 120) > time()) - { - return 'Online'; - } - else - { - return 'Offline'; - } - } -} // ... //friend request button check @@ -2258,23 +2180,6 @@ function placeAssetComment($aid, $comment) //1 = comment placed, 2 = cooldown, 3 //canjoin stuff { -function setCanJoinUser($status) -{ - $localuser = $GLOBALS['user']->id; - $maxcanjoinstatus = 2; - - if ($status <= $maxcanjoinstatus) - { - $setstatus = $GLOBALS['pdo']->prepare("UPDATE users SET canJoin = :c WHERE id = :u"); - $setstatus->bindParam(":c", $status, PDO::PARAM_INT); - $setstatus->bindParam(":u", $localuser, PDO::PARAM_INT); - $setstatus->execute(); - - return true; - } - return false; -} - function canJoinUser($uid) // { $canjoinstatusquery = $GLOBALS['pdo']->prepare("SELECT canJoin FROM users WHERE id = :i"); diff --git a/html_api/settings/update/joinprivacy.php b/html_api/settings/update/joinprivacy.php index e5bd91e..a48b78f 100644 --- a/html_api/settings/update/joinprivacy.php +++ b/html_api/settings/update/joinprivacy.php @@ -5,6 +5,9 @@ Alphaland 2021 */ //headers + +use Alphaland\Users\User; + header("Access-Control-Allow-Origin: https://www.alphaland.cc"); header("access-control-allow-credentials: true"); @@ -19,5 +22,5 @@ else { $privacy = $data->preference; header('Content-Type: application/json'); - echo json_encode(array("success" => setCanJoinUser($privacy))); + echo json_encode(array("success" => User::SetCanJoinUser($user->id, $privacy))); } \ No newline at end of file diff --git a/html_api/user/feed/index.php b/html_api/user/feed/index.php index 359c8a6..eb4ff06 100644 --- a/html_api/user/feed/index.php +++ b/html_api/user/feed/index.php @@ -6,6 +6,9 @@ Alphaland 2021 */ //headers + +use Alphaland\Users\User; + header("Access-Control-Allow-Origin: https://www.alphaland.cc"); header("access-control-allow-credentials: true"); @@ -84,7 +87,7 @@ foreach($shouts as $shout) //{ $whenshout = date("m/d/Y", $timestamp); //} - $sitestatus = siteStatus($userid); + $sitestatus = User::SiteStatus($user->id); $shoutInfo = array( "userid" => $userid, diff --git a/html_api/users/profile/info.php b/html_api/users/profile/info.php index 639bfba..a77b8ba 100644 --- a/html_api/users/profile/info.php +++ b/html_api/users/profile/info.php @@ -6,6 +6,9 @@ Alphaland 2021 */ //headers + +use Alphaland\Users\User; + header("Access-Control-Allow-Origin: https://www.alphaland.cc"); header("access-control-allow-credentials: true"); @@ -37,14 +40,14 @@ $usershout = userShout($userquery->id); $blurb = cleanOutput($userquery->blurb); $joindate = date("m/d/Y", $userquery->joindate); $placevisits = userPlaceVisits($userquery->id); -$privateinventory = isUserInventoryPrivate($userquery->id); +$privateinventory = User::IsInventoryPrivate($userquery->id); $playerender = getPlayerRender($userquery->id); -$playingInfo = userPlaying($userquery->id); +$playingInfo = User::UserPlaying($userquery->id); $userInfo = array ( array( "userid" => $userquery->id, - "siteStatus" => siteStatus($userquery->id), + "siteStatus" => User::SiteStatus($userquery->id), "gameAssetId" => $playingInfo['placeid'], "gameJobId" => $playingInfo['jobid'], "username" => $username, diff --git a/html_api/users/profile/inventory.php b/html_api/users/profile/inventory.php index 7621714..a145d6f 100644 --- a/html_api/users/profile/inventory.php +++ b/html_api/users/profile/inventory.php @@ -7,6 +7,9 @@ TODO: UNGHETTO */ //headers + +use Alphaland\Users\User; + header("Access-Control-Allow-Origin: https://www.alphaland.cc"); header("access-control-allow-credentials: true"); header('Content-Type: application/json'); @@ -88,7 +91,7 @@ foreach($items as $item) } // ... -if (!isUserInventoryPrivate($userid)) { +if (!User::IsInventoryPrivate($userid)) { die(json_encode($jsonData)); } else { die(json_encode(["message"=>"User's inventory is private"])); diff --git a/html_api/users/siteusers.php b/html_api/users/siteusers.php index 0972a64..da2a51e 100644 --- a/html_api/users/siteusers.php +++ b/html_api/users/siteusers.php @@ -6,6 +6,9 @@ Alphaland 2021 //headers + +use Alphaland\Users\User; + header("Access-Control-Allow-Origin: https://www.alphaland.cc"); header("access-control-allow-credentials: true"); @@ -70,7 +73,7 @@ foreach($users as $user) $id = $user['id']; $username = $user['username']; $blurb = cleanOutput($user['blurb'], false); //pass false to not add html linebreaks - $sitestatus = siteStatus($id); + $sitestatus = User::SiteStatus($id); $lastseen = date("m/d/Y", $user['lastseen']); $thumbnail = getPlayerRender($user['id']);