Created
December 24, 2016 08:53
-
-
Save moonexpr/c838d626e50a08cbf69acf9bf5193791 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once ("yaml.class.php"); | |
function SteamIDFrom64 ($id) { | |
// Thanks Gio!? <https://facepunch.com/member.php?u=423489> | |
// https://facepunch.com/showthread.php?t=1238157 | |
function parseInt ($string) { | |
if (preg_match ('/(\d+)/', $string, $array)) { | |
return $array[1]; | |
} else { | |
return 0; | |
} | |
} | |
$steamY = parseInt($id); | |
$steamY = $steamY - 76561197960265728; | |
$steamX = 0; | |
if ($steamY % 2 == 1){ | |
$steamX = 1; | |
} else { | |
$steamX = 0; | |
} | |
$steamY = (($steamY - $steamX) / 2); | |
return sprintf ("STEAM_0:%s:%s", (string) $steamX, (string) $steamY); | |
} | |
/** | |
* Account Class | |
* @version RC5 | |
* @author Poatofactory <mrpotatofactory@gmail.com> | |
* @package cloud-db <YAML, JSON, XML> | |
*/ | |
class Account { | |
private $parser; | |
private $path; | |
public $data = array (); | |
public $primary = array ( | |
"m_strSteamID64" => "", | |
"m_strSteamID32" => "", | |
"m_iRegisterDate" => "0", | |
"m_iUpdateLast" => "0", | |
); | |
function __construct ($strSteam64) { | |
$this->parser = new Spyc; | |
$this->primary["m_strSteamID64"] = $strSteam64; | |
$this->primary["m_strSteamID32"] = SteamIDFrom64 ($strSteam64); | |
$this->path = sprintf ("/var/www/~hidden/account-db/%s.yaml", $strSteam64); | |
$this->load (); | |
} | |
private function valAccount () { | |
if (file_exists ($this->path)) { | |
return (bool) $this->parser->YAMLLoad ($this->path); | |
} else { | |
return false; | |
} | |
} | |
public function load () { | |
$this->data = $this->getAccount (); | |
} | |
public function getAccount ($type = "array") { | |
$arrAccount = array (); | |
if ($this->valAccount ()) { | |
$arrAccount = $this->parser->YAMLLoad ($this->path); | |
} else { | |
$this->primary["m_iRegisterDate"] = Date ("U"); | |
$arrAccount[".primary"] = $this->primary; | |
} | |
switch ($type) { | |
case "json": | |
return json_encode ($arrAccount, true); | |
break; | |
case "yaml": | |
return $this->parser->dump ($arrAccount); | |
break; | |
case "array": | |
return $arrAccount; | |
break; | |
default: | |
return $arrAccount; | |
break; | |
} | |
} | |
public function getSteamID32 () { | |
return $this->primary["m_strSteamID32"]; | |
} | |
public function getSteamID64 () { | |
return $this->primary["m_strSteamID64"]; | |
} | |
public function getPrimary () { | |
return $this->primary; | |
} | |
public function setData ($key, $value) { | |
return $this->data[$key] = $value; | |
} | |
public function getData ($key) { | |
return isset ($this->data[$key]) ? $this->data[$key] : null; | |
} | |
public function save () { | |
if (!isset ($this->data[".primary"])) { | |
die ("{\"message\": \"error: cannot save account (missing primary data)\"}"); | |
} else { | |
$this->primary["m_iUpdateLast"] = Date ("U"); | |
$strParsedYAML = $this->parser->dump ($this->data, true, true); | |
file_put_contents ($this->path, $strParsedYAML); | |
} | |
} | |
} | |
/** | |
* Steam Account Class | |
* @version RC5 | |
* @author Poatofactory <mrpotatofactory@gmail.com> | |
* @package cloud-db <YAML, JSON, XML> | |
*/ | |
class SteamAccount extends Account | |
{ | |
public $API = array ( | |
"key" => "", | |
"formats" => array ( | |
"GetPlayerSummaries" => "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s&format=json", | |
"GetFriendList" => "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=%s&steamid=%s&relationship=friend&format=json", | |
"GetPlayerAchievements" => "http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?key=%s&steamid=%s&appid=%s&format=json", | |
"GetPlayerBans" => "http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=%s&steamids=%s&format=json", | |
"GetRecentlyPlayedGames" => "http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=%s&steamid=%s&format=json", | |
), | |
); | |
private function Validate () { | |
if | |
( | |
!isset ($this->primary["m_strSteamID64"]) | |
|| $this->primary["m_strSteamID64"] == "" | |
|| !isset ($this->primary["m_strSteamID32"]) | |
|| $this->primary["m_strSteamID32"] == "" | |
) | |
{ | |
echo ("{\"message\": \"cannot fetch SteamAPI data from user (missing primary data)\"}"); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public $APICache = array (); | |
private function FetchURL ($strURL) { | |
return json_decode (file_get_contents ($strURL), true); | |
} | |
public function GetPlayerSummaries () { | |
if (!$this->Validate ()) { | |
return; | |
} | |
$url = sprintf ($this->API["formats"]["GetPlayerSummaries"], | |
$this->API["key"], | |
$this->primary["m_strSteamID64"] | |
); | |
$result = $this->FetchURL ($url)["response"]["players"][0]; | |
$APICache["GetPlayerSummaries"] = $result; | |
return $result; | |
} | |
public function GetFriendList () { | |
if (!$this->Validate ()) { | |
return; | |
} | |
$url = sprintf ($this->API["formats"]["GetFriendList"], | |
$this->API["key"], | |
$this->primary["m_strSteamID64"] | |
); | |
$result = $this->FetchURL ($url)["response"]["players"][0]; | |
$APICache["GetFriendList"] = $result; | |
return $result; | |
} | |
public function GetPlayerAchievements ($appid) { | |
if (!$this->Validate ()) { | |
return; | |
} | |
$url = sprintf ($this->API["formats"]["GetPlayerAchievements"], | |
$this->API["key"], | |
$this->primary["m_strSteamID64"], | |
$appid | |
); | |
$result = $this->FetchURL ($url)["response"]["players"][0]; | |
$APICache["GetPlayerAchievements"] = $result; | |
return $result; | |
} | |
public function GetPlayerBans () { | |
if (!$this->Validate ()) { | |
return; | |
} | |
$url = sprintf ($this->API["formats"]["GetPlayerBans"], | |
$this->API["key"], | |
$this->primary["m_strSteamID64"] | |
); | |
$result = $this->FetchURL ($url)["response"]["players"][0]; | |
$APICache["GetPlayerBans"] = $result; | |
return $result; | |
} | |
public function GetRecentlyPlayedGames () { | |
if (!$this->Validate ()) { | |
return; | |
} | |
$url = sprintf ($this->API["formats"]["GetRecentlyPlayedGames"], | |
$this->API["key"], | |
$this->primary["m_strSteamID64"] | |
); | |
$result = $this->FetchURL ($url)["response"]["players"][0]; | |
$APICache["GetRecentlyPlayedGames"] = $result; | |
return $result; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment