private function updateUsingAPI() { $getSmallId = []; foreach ($this->refreshProfiles as $profile) { $smallId = $profile['profile']['small_id']; $key = $profile['profile_key']; $getSmallId[] = (int) $smallId; $toSaveKey[$smallId] = $key; } /* grab 'info' from web api and handle errors */ $steamAPI = new SteamAPI('info'); $steamAPI->setSmallId($getSmallId); $steamInfos = $steamAPI->run(); if ($steamAPI->error()) { return ['error' => $steamAPI->errorMessage()]; } if (!isset($steamInfos->response->players[0])) { return ['error' => 'profile_null']; } // simplify the variable $steamInfos = $steamInfos->response->players; /* grab 'ban' from web api and handle errors */ $steamAPI = new SteamAPI('ban'); $steamAPI->setSmallId($getSmallId); $steamBans = $steamAPI->run(); if ($steamAPI->error()) { return ['error' => $steamAPI->errorMessage()]; } if (!isset($steamBans->players[0])) { return ['error' => 'profile_null']; } $steamBans = $steamBans->players; // whereIn('profile.small_id', $getSmallId)-> $profiles = Profile::whereIn('profile.small_id', $getSmallId)->groupBy('profile.id')->leftjoin('profile_ban', 'profile_ban.profile_id', '=', 'profile.id')->leftjoin('users', 'profile.small_id', '=', 'users.small_id')->leftjoin('user_list_profile', 'user_list_profile.profile_id', '=', 'profile.id')->whereNull('user_list_profile.deleted_at')->get(['profile.id', 'profile.small_id', 'profile.display_name', 'profile.privacy', 'profile.avatar_thumb', 'profile.avatar', 'profile.profile_created', 'profile.alias', 'profile.created_at', 'profile_ban.community', 'profile_ban.vac', 'profile_ban.trade', 'profile_ban.unban', 'users.site_admin', 'users.donation', 'users.beta', \DB::raw('max(user_list_profile.created_at) as last_added_created_at'), \DB::raw('count(user_list_profile.id) as total')]); $indexSave = []; foreach ($steamInfos as $k => $info) { $indexSave[Steam::toSmallId($info->steamid)] = ['steamInfos' => $k]; } foreach ($steamBans as $k => $ban) { // Lets just not update if api didn't return for this user if (!isset($indexSave[Steam::toSmallId($ban->SteamId)])) { continue; } $indexSave[Steam::toSmallId($ban->SteamId)]['steamBans'] = $k; } $newProfiles = []; foreach ($getSmallId as $k => $smallId) { // api didn't give values for this user if (!isset($indexSave[$smallId])) { continue; } $keys = $indexSave[$smallId]; $steamInfo = $steamInfos[$keys['steamInfos']]; if (!isset($keys['steamBans'])) { continue; } // right now dont let this user get through. Figure out a better method $steamBan = $steamBans[$keys['steamBans']]; $profile = $profiles->where('small_id', $smallId)->first(); if (is_null($profile)) { $profile = Profile::whereSmallId($smallId)->first(); if (!isset($profile->id)) { $profile = new Profile(); $profile->small_id = $smallId; } if (isset($steamInfo->timecreated)) { $profile->profile_created = $steamInfo->timecreated; } } $profile->display_name = $steamInfo->personaname; $profile->avatar = Steam::imgToHTTPS($steamInfo->avatarfull); $profile->avatar_thumb = Steam::imgToHTTPS($steamInfo->avatar); $profile->privacy = $steamInfo->communityvisibilitystate; $profile->save(); $profileBan = $profile->ProfileBan; // Dont update the profile_ban if there is nothing to update // This has to do with in the future when I check for new bans to notify/email $skipProfileBan = false; $newVacBanDate = new DateTime(); $newVacBanDate->sub(new DateInterval("P{$steamBan->DaysSinceLastBan}D")); $combinedBan = (int) $steamBan->NumberOfVACBans + (int) $steamBan->NumberOfGameBans; if (!isset($profileBan->id)) { $profileBan = new ProfileBan(); $profileBan->profile_id = $profile->id; $profileBan->unban = false; } else { $skipProfileBan = $profileBan->skipProfileBanUpdate($steamBan); if ($profileBan->vac != (int) $steamBan->NumberOfVACBans + (int) $steamBan->NumberOfGameBans && $profileBan->vac_banned_on->format('Y-m-d') !== $newVacBanDate->format('Y-m-d')) { $skipProfileBan = false; $profileBan->timestamps = false; } if ($profileBan->vac > $combinedBan) { $skipProfileBan = false; $profileBan->timestamps = false; $profileBan->unban = true; } } $profileBan->vac = $combinedBan; $profileBan->community = $steamBan->CommunityBanned; $profileBan->trade = $steamBan->EconomyBan != 'none'; $profileBan->vac_banned_on = $newVacBanDate->format('Y-m-d'); if (!$skipProfileBan) { $profile->ProfileBan()->save($profileBan); } /* Time to do profile_old_alias */ /* Checks to make sure if there is already a same name before inserting new name */ $profileOldAlias = $profile->ProfileOldAlias()->whereProfileId($profile->id)->orderBy('id', 'desc')->get(); if ($profileOldAlias->count() == 0) { $profileOldAlias = new ProfileOldAlias(); $profileOldAlias->profile_id = $profile->id; $profileOldAlias->seen = time(); $profileOldAlias->seen_alias = $profile->display_name; $profileOldAlias->save(); } else { $match = false; $recent = 0; foreach ($profileOldAlias as $oldAlias) { if (!is_object($oldAlias)) { continue; } if ($oldAlias->seen_alias == $profile->display_name) { $match = true; break; } $recent = $oldAlias->compareTime($recent); } if (!$match && $recent + Steam::$UPDATE_TIME < time()) { $newAlias = new ProfileOldAlias(); $newAlias->profile_id = $profile->id; $newAlias->seen = time(); $newAlias->seen_alias = $profile->display_name; $profile->ProfileOldAlias()->save($newAlias); } } $steam64BitId = Steam::to64Bit($profile->small_id); $vacBanDate = new DateTime(); $vacBanDate->sub(new DateInterval("P{$steamBan->DaysSinceLastBan}D")); $oldAliasArray = []; foreach ($profileOldAlias as $k => $oldAlias) { if (!is_object($oldAlias)) { $oldAliasArray[] = ["newname" => $profileOldAlias->seen_alias, "timechanged" => $profileOldAlias->seen->format("M j Y")]; break; } $oldAliasArray[] = ["newname" => $oldAlias->seen_alias, "timechanged" => $oldAlias->seen->format("M j Y")]; } $profileCheckCache = "profile_checked_"; $currentProfileCheck = ['number' => 0, 'time' => date("M j Y", time())]; if (Cache::has($profileCheckCache . $profile->smallId)) { $currentProfileCheck = Cache::get($profileCheckCache . $profile->smallId); } $newProfileCheck = ['number' => $currentProfileCheck['number'] + 1, 'time' => date("M j Y", time())]; Cache::forever($profileCheckCache . $profile->smallId, $newProfileCheck); $return = ['id' => $profile->id, 'display_name' => $steamInfo->personaname, 'avatar' => Steam::imgToHTTPS($steamInfo->avatarfull), 'avatar_thumb' => Steam::imgToHTTPS($steamInfo->avatar), 'small_id' => $profile->small_id, 'steam_64_bit' => $steam64BitId, 'steam_32_bit' => Steam::to32Bit($steam64BitId), 'profile_created' => isset($steamInfo->timecreated) ? date("M j Y", $steamInfo->timecreated) : "Unknown", 'privacy' => $steamInfo->communityvisibilitystate, 'alias' => Steam::friendlyAlias(json_decode($profile->alias)), 'created_at' => $profile->created_at ? $profile->created_at->format("M j Y") : null, 'vac' => $combinedBan, 'vac_banned_on' => $vacBanDate->format("M j Y"), 'community' => $steamBan->CommunityBanned, 'trade' => $steamBan->EconomyBan != 'none', 'site_admin' => (int) $profile->site_admin ?: 0, 'donation' => (int) $profile->donation ?: 0, 'beta' => (int) $profile->beta ?: 0, 'profile_old_alias' => $oldAliasArray, 'times_checked' => $currentProfileCheck, 'times_added' => ['number' => (int) $profile->total ?: 0, 'time' => (new DateTime($profile->last_added_created_at))->format("M j Y")]]; $newProfiles[$toSaveKey[$profile->small_id]] = $return; $this->updateCache($profile->small_id, $return); } // Send somewhere else to update alias // This takes too long for many profiles $randomString = str_random(12); $updateAliasCacheName = "update_alias_"; if (Cache::has($updateAliasCacheName . $randomString)) { while (Cache::has($updateAliasCacheName . $randomString)) { $randomString = str_random(12); } } Cache::forever($updateAliasCacheName . $randomString, $getSmallId); shell_exec('php artisan update:alias ' . $randomString . ' > /dev/null 2>/dev/null &'); return $newProfiles; }
private function updateUsingAPI() { /* Time to follow that great guide to updating via API above */ /* grab 'info' from web api and handle errors */ $steamAPI = new SteamAPI('info'); $steamAPI->setSmallId($this->smallId); $steamInfo = $steamAPI->run(); if ($steamAPI->error()) { return ['error' => $steamAPI->errorMessage()]; } if (!isset($steamInfo->response->players[0])) { return ['error' => 'profile_null']; } $steamInfo = $steamInfo->response->players[0]; /* grab 'ban' from web api and handle errors */ $steamAPI = new SteamAPI('ban'); $steamAPI->setSmallId($this->smallId); $steamBan = $steamAPI->run(); if ($steamAPI->error()) { return ['error' => $steamAPI->errorMessage()]; } if (!isset($steamBan->players[0])) { return ['error' => 'profile_null']; } $steamBan = $steamBan->players[0]; /* grab 'alias' from old web api but do not break on errors */ $steamAPI = new SteamAPI('alias'); $steamAPI->setSmallId($this->smallId); $steamAlias = $steamAPI->run(); if ($steamAPI->error()) { $steamAlias = []; } else { usort($steamAlias, array('VacStatus\\Steam\\Steam', 'aliasSort')); } /* Successfully passed steam's not very reliable api servers */ /* Lets hope we got the alias as well :))) */ /* Lets start up with profile table */ $profile = Profile::whereSmallId($this->smallId)->first(); if (!isset($profile->id)) { $profile = new Profile(); $profile->small_id = $this->smallId; if (isset($steamInfo->timecreated)) { $profile->profile_created = $steamInfo->timecreated; } } else { // Make sure to update if this was private and now suddenly public if (empty($profile->profile_created) && isset($steamInfo->timecreated)) { $profile->profile_created = $steamInfo->timecreated; } } $profile->display_name = $steamInfo->personaname; $profile->avatar = Steam::imgToHTTPS($steamInfo->avatarfull); $profile->avatar_thumb = Steam::imgToHTTPS($steamInfo->avatar); $profile->privacy = $steamInfo->communityvisibilitystate; $profile->alias = json_encode($steamAlias); if (!$profile->save()) { return ['error' => 'profile_save_error']; } /* Now to do profile_ban table */ $profileBan = $profile->ProfileBan; // Dont update the profile_ban if there is nothing to update // This has to do with in the future when I check for new bans to notify/email $skipProfileBan = false; $newVacBanDate = new DateTime(); $newVacBanDate->sub(new DateInterval("P{$steamBan->DaysSinceLastBan}D")); $combinedBan = (int) $steamBan->NumberOfVACBans + (int) $steamBan->NumberOfGameBans; if (!isset($profileBan->id)) { $profileBan = new ProfileBan(); $profileBan->profile_id = $profile->id; $profileBan->unban = false; } else { $skipProfileBan = $profileBan->skipProfileBanUpdate($steamBan); if ($profileBan->vac != (int) $steamBan->NumberOfVACBans + (int) $steamBan->NumberOfGameBans && $profileBan->vac_banned_on->format('Y-m-d') !== $newVacBanDate->format('Y-m-d')) { $skipProfileBan = false; $profileBan->timestamps = false; } if ($profileBan->vac > $combinedBan) { $profileBan->timestamps = false; $profileBan->unban = true; } } $profileBan->vac = $combinedBan; $profileBan->community = $steamBan->CommunityBanned; $profileBan->trade = $steamBan->EconomyBan != 'none'; $profileBan->vac_banned_on = $newVacBanDate->format('Y-m-d'); if (!$skipProfileBan) { if (!$profile->ProfileBan()->save($profileBan)) { return ['error' => 'profile_ban_save_error']; } } /* Time to do profile_old_alias */ /* Checks to make sure if there is already a same name before inserting new name */ $profileOldAlias = $profile->ProfileOldAlias()->whereProfileId($profile->id)->orderBy('id', 'desc')->get(); $currentTime = new DateTime(); if ($profileOldAlias->count() == 0) { $profileOldAlias = new ProfileOldAlias(); $profileOldAlias->profile_id = $profile->id; $profileOldAlias->seen = $currentTime->format('Y-m-d'); $profileOldAlias->seen_alias = $profile->display_name; $profileOldAlias->save(); } else { $match = false; $recent = 0; foreach ($profileOldAlias as $oldAlias) { if (!is_object($oldAlias)) { continue; } if ($oldAlias->seen_alias == $profile->display_name) { $match = true; break; } $recent = $oldAlias->compareTime($recent); } if (!$match && $recent + Steam::$UPDATE_TIME < time()) { $currentTime = new DateTime(); $newAlias = new ProfileOldAlias(); $newAlias->profile_id = $profile->id; $newAlias->seen = $currentTime->format('Y-m-d'); $newAlias->seen_alias = $profile->display_name; $profile->ProfileOldAlias()->save($newAlias); } } $profileOldAlias = $profile->ProfileOldAlias()->whereProfileId($profile->id)->orderBy('id', 'desc')->get(); /* Finished inserting / updating into the DB! */ /* Check to see if this user has an account in vacstatus */ $user = User::where('small_id', $this->smallId)->first(); /* getting the number of times checked and added */ $gettingCount = UserListProfile::whereProfileId($profile->id)->orderBy('id', 'desc')->get(); $profileTimesAdded = ['number' => $gettingCount->count(), 'time' => isset($gettingCount[0]) ? (new DateTime($gettingCount[0]->created_at))->format("M j Y") : null]; $profileCheckCache = "profile_checked_"; $currentProfileCheck = ['number' => 0, 'time' => date("M j Y", time())]; if (Cache::has($profileCheckCache . $this->smallId)) { $currentProfileCheck = Cache::get($profileCheckCache . $this->smallId); } $newProfileCheck = ['number' => $currentProfileCheck['number'] + 1, 'time' => date("M j Y", time())]; Cache::forever($profileCheckCache . $this->smallId, $newProfileCheck); /* Writing the return array for the single profile */ $steam64BitId = Steam::to64Bit($profile->small_id); $oldAliasArray = []; foreach ($profileOldAlias as $k => $oldAlias) { if ($oldAlias === true) { $oldAliasArray[] = ["newname" => $profileOldAlias->seen_alias, "timechanged" => $profileOldAlias->seen->format("M j Y")]; break; } $oldAliasArray[] = ["newname" => $oldAlias->seen_alias, "timechanged" => $oldAlias->seen->format("M j Y")]; } $return = ['id' => $profile->id, 'display_name' => $steamInfo->personaname, 'avatar' => Steam::imgToHTTPS($steamInfo->avatarfull), 'avatar_thumb' => Steam::imgToHTTPS($steamInfo->avatar), 'small_id' => $this->smallId, 'steam_64_bit' => $steam64BitId, 'steam_32_bit' => Steam::to32Bit($steam64BitId), 'profile_created' => isset($profile->profile_created) ? date("M j Y", $profile->profile_created) : "Unknown", 'privacy' => $steamInfo->communityvisibilitystate, 'alias' => Steam::friendlyAlias($steamAlias), 'created_at' => $profile->created_at->format("M j Y"), 'vac' => $combinedBan, 'vac_banned_on' => $newVacBanDate->format("M j Y"), 'community' => $steamBan->CommunityBanned, 'trade' => $steamBan->EconomyBan != 'none', 'site_admin' => (int) isset($user->id) ? $user->site_admin : 0, 'donation' => (int) isset($user->id) ? $user->donation : 0, 'beta' => (int) isset($user->id) ? $user->beta : 0, 'profile_old_alias' => $oldAliasArray, 'times_checked' => $currentProfileCheck, 'times_added' => $profileTimesAdded]; /* YAY nothing broke :D time to return the data (and update cache) */ $this->updateCache($return); return $return; }