Skip to content

Instantly share code, notes, and snippets.

@SPooCK
Created November 23, 2015 11:50
Show Gist options
  • Save SPooCK/08f1c97cf9b508ca72f9 to your computer and use it in GitHub Desktop.
Save SPooCK/08f1c97cf9b508ca72f9 to your computer and use it in GitHub Desktop.
BlackCetha original code in one PHP
<?php
require_once ("../../class2.php");
require_once (HEADERF);
$ns = e107::getRender();
class steamauthOOP {
private $settings = array(
"apikey" => "02B89A79D2310CAFDCE4739D859096DC", // Get yours today from http://steamcommunity.com/dev/apikey
"domainname" => "http://echovalleyserver.com/", // Displayed domain in the login-screen
"loginpage" => "", // Returns to last page if not set
"logoutpage" => "",
"skipAPI" => false // true = dont get the data from steam, just return the steamid64
);
function __construct() {
if (session_id() == "") session_start(); // Start a session if none exists
if ($this->settings["apikey"] == "") die("<b>SteamAuthOOP:</b> Please supply a valid API-Key!");
if ($this->settings["loginpage"] == "") $this->settings["loginpage"] = /* [ */ (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']; // Code (c) 2010 ichimonai.com, released under MIT-License
if (isset($_GET["openid_assoc_handle"]) && !isset($_SESSION["steamdata"]["steamid"])) { // Did we just return from steam login-page? If so, validate idendity and save the data
$steamid = $this->validate();
if ($steamid != "") { // ID Proven, get data from steam and save them
if ($this->settings["skipAPI"]) {
$_SESSION["steamdata"]["steamid"] = $steamid;
return; // Skip API here
}
@$apiresp = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$this->settings["apikey"]."&steamids=".$steamid),true);
foreach ($apiresp["response"]["players"][0] as $key => $value) $_SESSION["steamdata"][$key] = $value;
}
}
if (isset($_SESSION["steamdata"]["steamid"])) { // If we are logged in, make user-data accessable through $steam->var
foreach ($_SESSION["steamdata"] as $key => $value) $this->{$key} = $value;
}
}
/**
* Generate SteamLogin-URL
* @copyright loginUrl function (c) 2010 ichimonai.com, released under MIT-License
* Modified by BlackCetha for OOP use
*/
function loginUrl()
{
$params = array(
'openid.ns' => 'http://specs.openid.net/auth/2.0',
'openid.mode' => 'checkid_setup',
'openid.return_to' => $this->settings["loginpage"],
'openid.realm' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'],
'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select',
'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select',
);
return 'https://steamcommunity.com/openid/login' . '?' . http_build_query($params, '', "&");
}
/*
* Validate data against Steam-Servers
* @copyright validate function (c) 2010 ichimonai.com, released under MIT-License
* Modified by BlackCetha for OOP use
*/
private static function validate()
{
// Star off with some basic params
$params = array(
'openid.assoc_handle' => $_GET['openid_assoc_handle'],
'openid.signed' => $_GET['openid_signed'],
'openid.sig' => $_GET['openid_sig'],
'openid.ns' => 'http://specs.openid.net/auth/2.0',
);
// Get all the params that were sent back and resend them for validation
$signed = explode(',', $_GET['openid_signed']);
foreach($signed as $item)
{
$val = $_GET['openid_' . str_replace('.', '_', $item)];
$params['openid.' . $item] = get_magic_quotes_gpc() ? stripslashes($val) : $val;
}
// Finally, add the all important mode.
$params['openid.mode'] = 'check_authentication';
// Stored to send a Content-Length header
$data = http_build_query($params);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' =>
"Accept-language: en\r\n".
"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data,
),
));
$result = file_get_contents("http://steamcommunity.com/openid/login", false, $context);
// Validate wheather it's true and if we have a good ID
preg_match("#^http://steamcommunity.com/openid/id/([0-9]{17,25})#", $_GET['openid_claimed_id'], $matches);
$steamID64 = is_numeric($matches[1]) ? $matches[1] : 0;
// Return our final value
return preg_match("#is_valid\s*:\s*true#i", $result) == 1 ? $steamID64 : '';
}
function logout() {
if (!$this->loggedIn()) return false;
unset($_SESSION["steamdata"]); // Delete the users info from the cache, DOESNT DESTROY YOUR SESSION!
if (!isset($_SESSION[0])) session_destroy(); // End the session if theres no more data in it
if ($this->settings["logoutpage"] != "") header("Location: ".$this->settings["logoutpage"]); // If the logout-page is set, go there
return true;
}
function loggedIn() {
return (isset($_SESSION["steamdata"]["steamid"]) && $_SESSION["steamdata"]["steamid"] != "") ? true : false;
}
function forceReload() {
if (!isset($_SESSION["steamdata"]["steamid"])) return false; // User is not logged in, nothing to reload
@$apiresp = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$this->settings["apikey"]."&steamids=".$_SESSION["steamdata"]["steamid"]),true);
foreach ($apiresp["response"]["players"][0] as $key => $value) $_SESSION["steamdata"][$key] = $value;
foreach ($_SESSION["steamdata"] as $key => $value) $this->{$key} = $value; // Make user-data accessable through $steam->var
return true;
}
/**
* Prints debug information about steamauth
*/
function debug() {
echo "<h1>SteamAuth debug report</h1><hr><b>Settings-array:</b><br>";
echo "<pre>".print_r($this->settings,true)."</pre>";
echo "<br><br><b>Data:</b><br>";
echo "<pre>".print_r($_SESSION["steamdata"],true)."</pre>";
}
}
$steam = new steamauthOOP();
if (isset($_GET["logout"])) {
$steam->logout();
}
if(!$steam->loggedIn()) {
$text .= "<div style='margin: 30px auto; text-align: center;'>Welcome Guest! <a href='";
$text .= $steam->loginUrl();
$text .= "'>Please log in!</a></div>";
} else {
$text .= "<div style='float:left;'><a href='https://github.com/blackcetha/steamauthoop'><button type='button' class='btn btn-success' style='margin: 2px 3px;'>GitHub Repo</button></a><a href='https://github.com/blackcetha/steamauthoop/releases'><button type='button' style='margin: 2px 3px;' class='btn btn-warning'>Download</button></a></div><br><br>
<h4 style='margin-bottom: 3px; float:left;'>Steam WebAPI-Output: <small>There are more variables ready to use but not listed here as they are not always available.</small></h4><span style='float:right;'><a href='demo.php?logout'>Log out</a></span>
<table class='table table-striped'><tr><td><b>Variable name</b></td><td><b>Value</b></td><td><b>Description</b></td></tr>
<tr><td>\$steam->loggedIn()</td><td>".$steam->loggedIn()."</td><td>1 (true) - Logged in, 0 (false) - not</td></tr>
<tr><td>\$steam->steamid</td><td>".$steam->steamid."</td><td>SteamID64 of the user</td></tr>
<tr><td>\$steam->communityvisibilitystate</td><td>".$steam->communityvisibilitystate."</td><td>1 - Account not visible; 3 - Account is public (Depends on the relationship of your account to the others)</td></tr>
<tr><td>\$steam->profilestate</td><td>".$steam->profilestate."</td><td>1 - The user has a Steam Community profile; 0 - if not</td></tr>
<tr><td>\$steam->personaname</td><td>".$steam->personaname."</td><td>Public name of the user</td></tr>
<tr><td>\$steam->lastlogoff</td><td>".$steam->lastlogoff."</td><td><a href='http://www.unixtimestamp.com/' target='_blank'>Unix timestamp</a> of the user's last logoff</td></tr>
<tr><td>\$steam->profileurl</td><td>".$steam->profileurl."</td><td>Link to the user's profile</td></tr>
<tr><td>\$steam->personastate</td><td>".$steam->personastate."</td><td>0 - Offline, 1 - Online, 2 - Busy, 3 - Away, 4 - Snooze, 5 - looking to trade, 6 - looking to play</td></tr>
<tr><td>\$steam->realname</td><td>".$steam->realname."</td><td>\"Real\" name</td></tr>
<tr><td>\$steam->primaryclanid</td><td>".$steam->primaryclanid."</td><td>The ID of the user's primary group</td></tr>
<tr><td>\$steam->timecreated</td><td>".$steam->timecreated."</td><td><a href='http://www.unixtimestamp.com/' target='_blank'>Unix timestamp</a> for the time the user's account was created</td></tr>
<tr><td>\$steam->avatar</td><td><img src='".$steam->avatar."'><br>".$steam->avatar."</td><td>Adress of the user's 32x32px avatar</td></tr>
<tr><td>\$steam->avatarmedium</td><td style=''><img src='".$steam->avatarmedium."'><br>".$steam->avatarmedium."</td><td>Adress of the user's 64x64px avatar</td></tr>
<tr><td>\$steam->avatarfull</td><td><img src='".$steam->avatarfull."'><br>".$steam->avatarfull."</td><td>Adress of the user's 184x184px avatar</td></tr>
</table>";
}
$ns ->tablerender($caption, $text);
require_once (FOOTERF);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment