Applied fixes from StyleCI

This commit is contained in:
shavit 2016-06-03 10:56:19 -04:00 committed by StyleCI Bot
parent 1943c57205
commit 472d8f2da2
4 changed files with 651 additions and 624 deletions

View File

@ -1,46 +1,46 @@
<?php <?php
// ip address to the mysql server // ip address to the mysql server
define("DB_HOST", "localhost"); define('DB_HOST', 'localhost');
// mysql username // mysql username
define("DB_USER", "root"); define('DB_USER', 'root');
// mysql password // mysql password
define("DB_PASSWORD", ""); define('DB_PASSWORD', '');
// mysql database (schema) name // mysql database (schema) name
define("DB_SCHEMA", "shavit"); define('DB_SCHEMA', 'shavit');
// amount of records that can be displayed // amount of records that can be displayed
define("RECORD_LIMIT", "100"); define('RECORD_LIMIT', '100');
// the page's title as seen in the homepage // the page's title as seen in the homepage
define("HOMEPAGE_TITLE", "shavit's bhoptimer"); define('HOMEPAGE_TITLE', "shavit's bhoptimer");
// title for the top left side of the screen // title for the top left side of the screen
define("TOPLEFT_TITLE", "bhoptimer"); define('TOPLEFT_TITLE', 'bhoptimer');
// mysql table prefix, leave empty unless changed in the server // mysql table prefix, leave empty unless changed in the server
define("MYSQL_PREFIX", ""); define('MYSQL_PREFIX', '');
// header title // header title
define("HEADER_TITLE", "Welcome!"); define('HEADER_TITLE', 'Welcome!');
// page styling // page styling
define("PAGE_STYLE", "0"); // 0 - Default | 1 - Red/Black define('PAGE_STYLE', '0'); // 0 - Default | 1 - Red/Black
// setup multi styles here // setup multi styles here
$styles = array( $styles = [
"Forwards", // 0 'Forwards', // 0
"Sideways", // 1 'Sideways', // 1
"W-Only", // 2 'W-Only', // 2
"Scroll", // 3 'Scroll', // 3
"400 Velocity", // 4 '400 Velocity', // 4
"Half-Sideways" // 5 'Half-Sideways', // 5
); ];
define("DEFAULT_STYLE", 0); // 0 - forwards define('DEFAULT_STYLE', 0); // 0 - forwards
// amount of records that can be displayed in 'latest records' // amount of records that can be displayed in 'latest records'
define("RECORD_LIMIT_LATEST", "10"); define('RECORD_LIMIT_LATEST', '10');
?>

View File

@ -1,89 +1,67 @@
<?php <?php
function formattoseconds($time) function formattoseconds($time)
{ {
$iTemp = floor($time); $iTemp = floor($time);
$iHours = 0; $iHours = 0;
if($iTemp > 3600) if ($iTemp > 3600) {
{ $iHours = floor($iTemp / 3600.0);
$iHours = floor($iTemp / 3600.0); $iTemp %= 3600;
$iTemp %= 3600; }
}
$sHours = ""; $sHours = '';
if($iHours < 10) if ($iHours < 10) {
{ $sHours = '0'.$iHours;
$sHours = "0" . $iHours; } else {
} $sHours = $iHours;
}
else $iMinutes = 0;
{
$sHours = $iHours;
}
$iMinutes = 0; if ($iTemp >= 60) {
$iMinutes = floor($iTemp / 60.0);
$iTemp %= 60;
}
if($iTemp >= 60) $sMinutes = '';
{
$iMinutes = floor($iTemp / 60.0);
$iTemp %= 60;
}
$sMinutes = ""; if ($iMinutes < 10) {
$sMinutes = '0'.$iMinutes;
} else {
$sMinutes = $iMinutes;
}
if($iMinutes < 10) $fSeconds = (($iTemp) + $time - floor($time));
{
$sMinutes = "0" . $iMinutes;
}
else $sSeconds = '';
{
$sMinutes = $iMinutes;
}
$fSeconds = (($iTemp) + $time - floor($time)); if ($fSeconds < 10) {
$sSeconds = '0'.number_format($fSeconds, 3);
} else {
$sSeconds = number_format($fSeconds, 3);
}
$sSeconds = ""; if ($iHours > 0) {
$newtime = $sHours.':'.$sMinutes.':'.$sSeconds.'s';
} elseif ($iMinutes > 0) {
$newtime = $sMinutes.':'.$sSeconds.'s';
} else {
$newtime = number_format($fSeconds, 3).'s';
}
if($fSeconds < 10) return $newtime;
{
$sSeconds = "0" . number_format($fSeconds, 3);
}
else
{
$sSeconds = number_format($fSeconds, 3);
}
if($iHours > 0)
{
$newtime = $sHours . ":" . $sMinutes . ":" . $sSeconds . "s";
}
else if($iMinutes > 0)
{
$newtime = $sMinutes . ":" . $sSeconds . "s";
}
else
{
$newtime = number_format($fSeconds, 3) . "s";
}
return $newtime;
} }
function removeworkshop($mapname) function removeworkshop($mapname)
{ {
if(strpos($mapname, "workshop/") !== false) if (strpos($mapname, 'workshop/') !== false) {
{ $pieces = explode('/', $mapname);
$pieces = explode("/", $mapname);
return $pieces[2]; return $pieces[2];
} }
return $mapname; return $mapname;
} }
?>

View File

@ -1,26 +1,24 @@
<?php <?php
require("config.php"); require 'config.php';
require("functions.php"); require 'functions.php';
require("steamid.php"); require 'steamid.php';
$connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_SCHEMA); $connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_SCHEMA);
$connection->set_charset("utf8"); $connection->set_charset('utf8');
$style = 0; $style = 0;
if(isset($_REQUEST["style"])) if (isset($_REQUEST['style'])) {
{ $style = $_REQUEST['style'];
$style = $_REQUEST["style"];
} }
$map = ""; $map = '';
if(isset($_REQUEST["map"])) if (isset($_REQUEST['map'])) {
{ $map = $_REQUEST['map'];
$map = $_REQUEST["map"];
} }
$rr = isset($_REQUEST["rr"]); $rr = isset($_REQUEST['rr']);
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
@ -35,15 +33,11 @@ $rr = isset($_REQUEST["rr"]);
<link href="assets/icons/favicon.ico" rel="icon" type="image/x-icon" /> <link href="assets/icons/favicon.ico" rel="icon" type="image/x-icon" />
<?php <?php
if(!$map) if (!$map) {
{ echo '<title>'.HOMEPAGE_TITLE.'</title>';
echo("<title>" . HOMEPAGE_TITLE . "</title>"); } else {
} echo '<title>'.removeworkshop($_GET['map']).'</title>';
} ?>
else
{
echo("<title>".removeworkshop($_GET["map"])."</title>");
} ?>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]> <!--[if lt IE 9]>
@ -89,7 +83,7 @@ $rr = isset($_REQUEST["rr"]);
<nav class="navbar navbar-inverse navbar-fixed-top"> <nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container"> <div class="container">
<div class="navbar-header"> <div class="navbar-header">
<a class="navbar-brand" href="index.php"><?php echo("<i class=\"fa fa-clock-o\"></i> ".TOPLEFT_TITLE); ?></a> <a class="navbar-brand" href="index.php"><?php echo '<i class="fa fa-clock-o"></i> '.TOPLEFT_TITLE; ?></a>
<a class="navbar-brand" href="index.php?rr=1">Recent Records</a> <a class="navbar-brand" href="index.php?rr=1">Recent Records</a>
</div> </div>
<div id="navbar" class="navbar-collapse collapse"> <div id="navbar" class="navbar-collapse collapse">
@ -97,9 +91,14 @@ $rr = isset($_REQUEST["rr"]);
<div class="form-group"> <div class="form-group">
<select name="style" class="form-control"> <select name="style" class="form-control">
<?php <?php
for($i = 0; $i < count($styles); $i++) for ($i = 0; $i < count($styles); $i++) {
{ ?> <option value="<?php echo $i;
?> <option value="<?php echo($i); ?>" <?php if($i == DEFAULT_STYLE || $style == $i) echo("selected=\"selected\""); ?>><?php echo($styles[$i]); ?></option> <?php ?>" <?php if ($i == DEFAULT_STYLE || $style == $i) {
echo 'selected="selected"';
}
?>><?php echo $styles[$i];
?></option> <?php
} }
?> ?>
</select> </select>
@ -108,16 +107,20 @@ $rr = isset($_REQUEST["rr"]);
<select name="map" class="form-control" required> <select name="map" class="form-control" required>
<option value="" selected="selected">None</option> <option value="" selected="selected">None</option>
<?php <?php
$result = mysqli_query($connection, "SELECT DISTINCT ".MYSQL_PREFIX."map FROM mapzones ORDER BY map ASC;"); $result = mysqli_query($connection, 'SELECT DISTINCT '.MYSQL_PREFIX.'map FROM mapzones ORDER BY map ASC;');
if($result->num_rows > 0) if ($result->num_rows > 0) {
{ while ($row = $result->fetch_assoc()) {
while($row = $result->fetch_assoc())
{
// $row["map"] - including workshop // $row["map"] - including workshop
// removeworkshop($row["map"]) - no workshop // removeworkshop($row["map"]) - no workshop
?> <option value="<?php echo($row["map"]); ?>" <?php if($row["map"] == $map) echo("selected=\"selected\""); ?>><?php echo(removeworkshop($row["map"])); ?></option> <?php ?> <option value="<?php echo $row['map'];
} ?>" <?php if ($row['map'] == $map) {
echo 'selected="selected"';
}
?>><?php echo removeworkshop($row['map']);
?></option> <?php
}
} }
?> ?>
</select> </select>
@ -134,51 +137,46 @@ $rr = isset($_REQUEST["rr"]);
<div class="col-md-10 col-md-offset-1"> <div class="col-md-10 col-md-offset-1">
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading cam-heading center"> <div class="panel-heading cam-heading center">
<?php echo(HOMEPAGE_TITLE);?> <strong>-</strong> Record Database <?php echo HOMEPAGE_TITLE; ?> <strong>-</strong> Record Database
</div> </div>
<div class="panel-body table-responsive"> <div class="panel-body table-responsive">
<?php <?php
if(!isset($_REQUEST["map"]) && !$rr) if (!isset($_REQUEST['map']) && !$rr) {
{
?> ?>
<h1><?php echo(HEADER_TITLE); ?></h1> <h1><?php echo HEADER_TITLE;
?></h1>
<p> <p>
To show the records of any map, please select it using the menu at the top right of this page.<br/> To show the records of any map, please select it using the menu at the top right of this page.<br/>
Don't forget to select a style if you wish, and then tap 'Submit'!</p> Don't forget to select a style if you wish, and then tap 'Submit'!</p>
<p> <p>
Alternatively, you may click <a href="index.php?rr=1">Recent Records</a> to view the latest <?php echo(RECORD_LIMIT_LATEST); ?> records. Alternatively, you may click <a href="index.php?rr=1">Recent Records</a> to view the latest <?php echo RECORD_LIMIT_LATEST;
?> records.
</p> </p>
<?php <?php
}
else } else {
{
$results = false; $results = false;
$stmt = FALSE; $stmt = false;
if($rr && $stmt = $connection->prepare("SELECT p.map, u.name, p.style, p.time, p.jumps, u.auth, p.date FROM ".MYSQL_PREFIX."playertimes p JOIN (SELECT style, MIN(time) time FROM ".MYSQL_PREFIX."playertimes GROUP BY style, map) s ON p.style = s.style AND p.time = s.time JOIN ".MYSQL_PREFIX."users u ON p.auth = u.auth GROUP BY style, map ORDER BY date DESC;")) if ($rr && $stmt = $connection->prepare('SELECT p.map, u.name, p.style, p.time, p.jumps, u.auth, p.date FROM '.MYSQL_PREFIX.'playertimes p JOIN (SELECT style, MIN(time) time FROM '.MYSQL_PREFIX.'playertimes GROUP BY style, map) s ON p.style = s.style AND p.time = s.time JOIN '.MYSQL_PREFIX.'users u ON p.auth = u.auth GROUP BY style, map ORDER BY date DESC;')) {
{
echo $connection->error; echo $connection->error;
$stmt->execute(); $stmt->execute();
$stmt->store_result(); $stmt->store_result();
$results = ($rows = $stmt->num_rows) > 0; $results = ($rows = $stmt->num_rows) > 0;
$stmt->bind_result($map, $name, $style, $time, $jumps, $auth, $date); $stmt->bind_result($map, $name, $style, $time, $jumps, $auth, $date);
if($rows > 0) if ($rows > 0) {
{
$records = 0; $records = 0;
$first = true; $first = true;
while($row = $stmt->fetch()) while ($row = $stmt->fetch()) {
{ if ($first) {
if($first)
{
?> ?>
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
<thead id="ignore"> <thead id="ignore">
@ -198,52 +196,54 @@ $rr = isset($_REQUEST["rr"]);
?> ?>
<tr> <tr>
<td><?php echo(removeworkshop($map)); ?></td> <td><?php echo removeworkshop($map);
<td><?php echo($name); ?></td> ?></td>
<td><?php echo($styles[$style]); ?></td> <td><?php echo $name;
<td><?php echo(formattoseconds($time)); ?></td> ?></td>
<td><?php echo(number_format($jumps)); ?></td> <td><?php echo $styles[$style];
?></td>
<td><?php echo formattoseconds($time);
?></td>
<td><?php echo number_format($jumps);
?></td>
<td><?php <td><?php
$steamid = SteamID::Parse($auth, SteamID::FORMAT_STEAMID3); $steamid = SteamID::Parse($auth, SteamID::FORMAT_STEAMID3);
echo("<a href=\"http://steamcommunity.com/profiles/" . $steamid->Format(SteamID::FORMAT_STEAMID64) . "/\" target=\"_blank\">" . $auth . "</a>"); echo '<a href="http://steamcommunity.com/profiles/'.$steamid->Format(SteamID::FORMAT_STEAMID64).'/" target="_blank">'.$auth.'</a>';
?></td> ?></td>
<td><?php echo($date); ?></td> <td><?php echo $date;
?></td>
</tr> </tr>
<?php <?php
if(++$records > RECORD_LIMIT_LATEST) if (++$records > RECORD_LIMIT_LATEST) {
{
break; break;
} }
} }
} }
} } elseif ($stmt = $connection->prepare('SELECT p.id, u.auth, u.name, p.time, p.jumps, p.date FROM '.MYSQL_PREFIX.'playertimes p JOIN '.MYSQL_PREFIX.'users u ON p.auth = u.auth WHERE map = ? AND style = ? ORDER BY time ASC;')) {
$stmt->bind_param('ss', $map, $style);
$stmt->execute();
else if($stmt = $connection->prepare("SELECT p.id, u.auth, u.name, p.time, p.jumps, p.date FROM ".MYSQL_PREFIX."playertimes p JOIN ".MYSQL_PREFIX."users u ON p.auth = u.auth WHERE map = ? AND style = ? ORDER BY time ASC;")) $stmt->store_result();
{
$stmt->bind_param("ss", $map, $style);
$stmt->execute();
$stmt->store_result(); $results = ($rows = $stmt->num_rows) > 0;
$results = ($rows = $stmt->num_rows) > 0; $stmt->bind_result($id, $auth, $name, $time, $jumps, $date);
$stmt->bind_result($id, $auth, $name, $time, $jumps, $date); if ($rows > 0) {
if($rows > 0)
{
$first = true; $first = true;
$rank = 1; $rank = 1;
while($row = $stmt->fetch()) while ($row = $stmt->fetch()) {
{ if ($first) {
if($first) ?>
{ <p><span class="mark"><?php echo $styles[$style];
?> ?></span> Records (<?php echo number_format($rows);
<p><span class="mark"><?php echo($styles[$style]); ?></span> Records (<?php echo(number_format($rows)); ?>) for <i><?php echo(removeworkshop($map)); ?></i>:</p> ?>) for <i><?php echo removeworkshop($map);
?></i>:</p>
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
<thead id="ignore"><th>Rank</th> <thead id="ignore"><th>Rank</th>
@ -257,85 +257,86 @@ $rr = isset($_REQUEST["rr"]);
<?php <?php
$first = false; $first = false;
} }
?> ?>
<?php if($rank == 1) <?php if ($rank == 1) {
{
?> ?>
<tr class="warning"> <tr class="warning">
<?php <?php
}
else } else {
{
?> ?>
<tr class="default"> <tr class="default">
<?php <?php
} }
?> ?>
<td> <td>
<?php switch($rank) <?php switch ($rank) {
{
case 1: case 1:
{ {
echo("<i class=\"fa fa-trophy\" style=\"color:#C98910\"></i>"); echo '<i class="fa fa-trophy" style="color:#C98910"></i>';
break; break;
} }
case 2: case 2:
{ {
echo("<i class=\"fa fa-trophy\" style=\"color:#A8A8A8\"></i>"); echo '<i class="fa fa-trophy" style="color:#A8A8A8"></i>';
break; break;
} }
case 3: case 3:
{ {
echo("<i class=\"fa fa-trophy\" style=\"color:#965A38\"></i>"); echo '<i class="fa fa-trophy" style="color:#965A38"></i>';
break; break;
} }
default: default:
{ {
echo("#".$rank); echo '#'.$rank;
break; break;
} }
} }
?></td> ?></td>
<td><?php echo($id); ?></td> <td><?php echo $id;
?></td>
<td><?php <td><?php
$steamid = SteamID::Parse($auth, SteamID::FORMAT_STEAMID3); $steamid = SteamID::Parse($auth, SteamID::FORMAT_STEAMID3);
echo("<a href=\"http://steamcommunity.com/profiles/" . $steamid->Format(SteamID::FORMAT_STEAMID64) . "/\" target=\"_blank\">" . $auth . "</a>"); ?></td> echo '<a href="http://steamcommunity.com/profiles/'.$steamid->Format(SteamID::FORMAT_STEAMID64).'/" target="_blank">'.$auth.'</a>';
<td><?php echo($name); ?></td> ?></td>
<td><?php echo $name;
?></td>
<td> <td>
<?php <?php
echo(formattoseconds($time)); echo formattoseconds($time);
?></td> ?></td>
<td><?php echo(number_format($jumps)); ?></td> <td><?php echo number_format($jumps);
<td><?php echo($date); ?></td></tr> ?></td>
<td><?php echo $date;
?></td></tr>
<?php <?php
if(++$rank > RECORD_LIMIT) if (++$rank > RECORD_LIMIT) {
{
break; break;
} }
} }
?> </table> <?php ?> </table> <?php
}
}
if($stmt != FALSE) }
{ }
$stmt->close();
}
if(!$results) if ($stmt != false) {
{ $stmt->close();
}
if (!$results) {
?> <h1>No results!</h1> ?> <h1>No results!</h1>
<p>Try another map, there may be some records!</p> <?php <p>Try another map, there may be some records!</p> <?php
} }
} }
?> ?>
@ -354,13 +355,10 @@ $rr = isset($_REQUEST["rr"]);
<!-- Custom styles for this template, if we'll ever use it --> <!-- Custom styles for this template, if we'll ever use it -->
<?php <?php
if(PAGE_STYLE =='0') if (PAGE_STYLE == '0') {
{ echo '<link rel="stylesheet" href="timer.css">';
echo('<link rel="stylesheet" href="timer.css">'); } else {
} echo '<link rel="stylesheet" href="timer-red.css">';
else
{
echo('<link rel="stylesheet" href="timer-red.css">');
} }
?> ?>

View File

@ -1,4 +1,5 @@
<?php <?php
// https://github.com/mukunda-/steamidparser/blob/master/lib/steamid.php // https://github.com/mukunda-/steamidparser/blob/master/lib/steamid.php
/*! /*!
* SteamID Parser * SteamID Parser
@ -26,432 +27,482 @@
/** /**
* Exception thrown on resolution failure * Exception thrown on resolution failure
* (only used when resolving vanity URLs.) * (only used when resolving vanity URLs.).
*/ */
class SteamIDResolutionException extends Exception { class SteamIDResolutionException extends Exception
const UNKNOWN = 0; // Unknown error. {
const CURL_FAILURE = 1; // cURL/network related error. const UNKNOWN = 0; // Unknown error.
const VANITYURL_NOTFOUND = 2; // The vanity URL given was invalid. const CURL_FAILURE = 1; // cURL/network related error.
const VANITYURL_FAILED = 3; // Steam failure when trying to resolve vanity URL. const VANITYURL_NOTFOUND = 2; // The vanity URL given was invalid.
const VANITYURL_FAILED = 3; // Steam failure when trying to resolve vanity URL.
public $reason; public $reason;
function __construct( $reason, $text ) { public function __construct($reason, $text)
$this->reason = $reason; {
parent::__construct( $text ); $this->reason = $reason;
} parent::__construct($text);
}
} }
/** --------------------------------------------------------------------------- /** ---------------------------------------------------------------------------
* SteamID * SteamID.
* *
* Contains a User Steam ID. * Contains a User Steam ID.
* *
* @author Mukunda Johnson * @author Mukunda Johnson
*/ */
class SteamID { class SteamID
{
// RAW Steam ID value as a string. (a plain number.) // RAW Steam ID value as a string. (a plain number.)
public $value; public $value;
// Array of converted values. Indexed by FORMAT_xxx // Array of converted values. Indexed by FORMAT_xxx
// this is a cache of formatted values, filled in // this is a cache of formatted values, filled in
// by Format or Parse. // by Format or Parse.
public $formatted; public $formatted;
const FORMAT_AUTO = 0; // Auto-detect format --- this also supports const FORMAT_AUTO = 0; // Auto-detect format --- this also supports
// other unlisted formats such as // other unlisted formats such as
// full profile URLs. // full profile URLs.
const FORMAT_STEAMID32 = 1; // Classic STEAM_x:y:zzzzzz | x = 0/1 const FORMAT_STEAMID32 = 1; // Classic STEAM_x:y:zzzzzz | x = 0/1
const FORMAT_STEAMID64 = 2; // SteamID64: 7656119xxxxxxxxxx const FORMAT_STEAMID64 = 2; // SteamID64: 7656119xxxxxxxxxx
const FORMAT_STEAMID3 = 3; // SteamID3 format: [U:1:xxxxxx] const FORMAT_STEAMID3 = 3; // SteamID3 format: [U:1:xxxxxx]
const FORMAT_S32 = 4; // Raw 32-bit SIGNED format. const FORMAT_S32 = 4; // Raw 32-bit SIGNED format.
// this is a raw steamid index that overflows // this is a raw steamid index that overflows
// into negative bitspace. // into negative bitspace.
// This is the format that SourceMod returns // This is the format that SourceMod returns
// with GetSteamAccountID, and will always // with GetSteamAccountID, and will always
// fit into a 32-bit signed variable. (e.g. // fit into a 32-bit signed variable. (e.g.
// a 32-bit PHP integer). // a 32-bit PHP integer).
const FORMAT_RAW = 5; // Raw index. like 64-bit minus the base value. const FORMAT_RAW = 5; // Raw index. like 64-bit minus the base value.
const FORMAT_VANITY = 6; // Vanity URL name. Forward conversion only. const FORMAT_VANITY = 6; // Vanity URL name. Forward conversion only.
const STEAMID64_BASE = '76561197960265728'; const STEAMID64_BASE = '76561197960265728';
// max allowed value. (sanity check) // max allowed value. (sanity check)
// 2^36; update this in approx 2,400,000 years // 2^36; update this in approx 2,400,000 years
const MAX_VALUE = '68719476736'; const MAX_VALUE = '68719476736';
private static $steam_api_key = FALSE; private static $steam_api_key = false;
private static $default_detect_raw = FALSE; private static $default_detect_raw = false;
private static $default_resolve_vanity = FALSE; private static $default_resolve_vanity = false;
/** ----------------------------------------------------------------------- /** -----------------------------------------------------------------------
* Set an API key to use for resolving Custom URLs. If this isn't set * Set an API key to use for resolving Custom URLs. If this isn't set
* custom URL resolution will be done by parsing the profile XML. * custom URL resolution will be done by parsing the profile XML.
* *
* @param string $key API Key * @param string $key API Key
* @see http://steamcommunity.com/dev/apikey *
*/ * @see http://steamcommunity.com/dev/apikey
public static function SetSteamAPIKey( $key ) { */
if( empty($key) ) self::$steam_api_key = FALSE; public static function SetSteamAPIKey($key)
self::$steam_api_key = $key; {
} if (empty($key)) {
self::$steam_api_key = false;
/** ----------------------------------------------------------------------- }
* Set the default setting for $detect_raw for Parse() self::$steam_api_key = $key;
* }
* @param bool $parseraw Default $detect_raw value, see Parse function.
*/ /** -----------------------------------------------------------------------
public static function SetParseRawDefault( $parseraw ) { * Set the default setting for $detect_raw for Parse().
self::$default_detect_raw = $parseraw; *
} * @param bool $parseraw Default $detect_raw value, see Parse function.
*/
/** ----------------------------------------------------------------------- public static function SetParseRawDefault($parseraw)
* Set the default setting for $resolve_vanity for Parse() {
* self::$default_detect_raw = $parseraw;
* @param bool $resolve_vanity Default $resolve_vanity value, }
* see Parse function.
*/ /** -----------------------------------------------------------------------
public static function SetResolveVanityDefault( $resolve_vanity ) { * Set the default setting for $resolve_vanity for Parse().
self::$default_resolve_vanity = $resolve_vanity; *
} * @param bool $resolve_vanity Default $resolve_vanity value,
* see Parse function.
/** ----------------------------------------------------------------------- */
* Construct an instance. public static function SetResolveVanityDefault($resolve_vanity)
* {
* @param string $raw Raw value of Steam ID. self::$default_resolve_vanity = $resolve_vanity;
*/ }
private function __construct( $raw ) {
$this->value = $raw; /** -----------------------------------------------------------------------
$this->formatted[ self::FORMAT_RAW ] = $raw; * Construct an instance.
} *
* @param string $raw Raw value of Steam ID.
/** ----------------------------------------------------------------------- */
* Make a cURL request and return the contents. private function __construct($raw)
* {
* @param string $url URL to request. $this->value = $raw;
* @return string|false Contents of result or FALSE if the request failed. $this->formatted[self::FORMAT_RAW] = $raw;
*/ }
private static function Curl( $url ) {
/** -----------------------------------------------------------------------
$ch = curl_init(); * Make a cURL request and return the contents.
curl_setopt( $ch, CURLOPT_URL, $url ); *
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); * @param string $url URL to request.
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); *
* @return string|false Contents of result or FALSE if the request failed.
$data = curl_exec($ch); */
curl_close($ch); private static function Curl($url)
{
return $data; $ch = curl_init();
} curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/** ----------------------------------------------------------------------- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
* Parse a Steam ID.
* $data = curl_exec($ch);
* @param string $input Input to parse. curl_close($ch);
* @param int $format Input formatting, see FORMAT_ constants.
* Defaults to FORMAT_AUTO which detects the format. return $data;
* @param bool $resolve_vanity Detect and resolve vanity URLs. (only used }
* with FORMAT_AUTO. Default option set with
* SetResolveVanityDefault. /** -----------------------------------------------------------------------
* @param bool $detect_raw Detect and parse RAW values. (only used with * Parse a Steam ID.
* FORMAT_AUTO. e.g "123" will resolve to the *
* SteamID with the raw value 123, and not a * @param string $input Input to parse.
* vanity-url named "123". Default option set with * @param int $format Input formatting, see FORMAT_ constants.
* SetParseRawDefault. * Defaults to FORMAT_AUTO which detects the format.
* * @param bool $resolve_vanity Detect and resolve vanity URLs. (only used
* @return SteamID|false SteamID instance or FALSE if the input is invalid * with FORMAT_AUTO. Default option set with
* or unsupported. * SetResolveVanityDefault.
*/ * @param bool $detect_raw Detect and parse RAW values. (only used with
public static function Parse( $input, * FORMAT_AUTO. e.g "123" will resolve to the
$format = self::FORMAT_AUTO, * SteamID with the raw value 123, and not a
$resolve_vanity = null, * vanity-url named "123". Default option set with
$detect_raw = null ) { * SetParseRawDefault.
*
if( $detect_raw === null ) * @return SteamID|false SteamID instance or FALSE if the input is invalid
$detect_raw = self::$default_detect_raw; * or unsupported.
if( $resolve_vanity === null ) */
$resolve_vanity = self::$default_resolve_vanity; public static function Parse($input,
$format = self::FORMAT_AUTO,
switch( $format ) { $resolve_vanity = null,
$detect_raw = null)
case self::FORMAT_STEAMID32: {
if ($detect_raw === null) {
// validate STEAM_0/1:y:zzzzzz $detect_raw = self::$default_detect_raw;
if( !preg_match( }
'/^STEAM_[0-1]:([0-1]):([0-9]+)$/', if ($resolve_vanity === null) {
$input, $matches ) ) { $resolve_vanity = self::$default_resolve_vanity;
}
return FALSE;
} switch ($format) {
// convert to raw. case self::FORMAT_STEAMID32:
$a = bcmul( $matches[2], '2', 0 );
$a = bcadd( $a, $matches[1], 0 ); // validate STEAM_0/1:y:zzzzzz
if (!preg_match(
$result = new self( $a ); '/^STEAM_[0-1]:([0-1]):([0-9]+)$/',
$result->formatted[ self::FORMAT_STEAMID32 ] = $input; $input, $matches)) {
return $result; return false;
}
case self::FORMAT_STEAMID64:
// convert to raw.
// allow digits only $a = bcmul($matches[2], '2', 0);
if( !preg_match( '/^[0-9]+$/', $input ) ) return FALSE; $a = bcadd($a, $matches[1], 0);
// convert to raw (subtract base) $result = new self($a);
$a = bcsub( $input, self::STEAMID64_BASE, 0 ); $result->formatted[self::FORMAT_STEAMID32] = $input;
// sanity range check. return $result;
if( bccomp( $a, '0', 0 ) < 0 ) return FALSE;
if( bccomp( $a, self::MAX_VALUE, 0 ) > 0 ) return FALSE; case self::FORMAT_STEAMID64:
$result = new self( $a ); // allow digits only
$result->formatted[ self::FORMAT_STEAMID64 ] = $input; if (!preg_match('/^[0-9]+$/', $input)) {
return $result; return false;
}
case self::FORMAT_STEAMID3:
// convert to raw (subtract base)
// validate [U:1:xxxxxx] $a = bcsub($input, self::STEAMID64_BASE, 0);
if( !preg_match( '/^\[U:1:([0-9]+)\]$/', $input, $matches ) ) {
return FALSE; // sanity range check.
} if (bccomp($a, '0', 0) < 0) {
return false;
$a = $matches[1]; }
if (bccomp($a, self::MAX_VALUE, 0) > 0) {
// sanity range check. return false;
if( bccomp( $a, self::MAX_VALUE, 0 ) > 0 ) return FALSE; }
$result = new self( $a );
$result->formatted[ self::FORMAT_STEAMID3 ] = $input; $result = new self($a);
return $result; $result->formatted[self::FORMAT_STEAMID64] = $input;
case self::FORMAT_S32: return $result;
// validate signed 32-bit format case self::FORMAT_STEAMID3:
if( !preg_match( '/^(-?[0-9]+)$/', $input ) ) {
return FALSE; // validate [U:1:xxxxxx]
} if (!preg_match('/^\[U:1:([0-9]+)\]$/', $input, $matches)) {
return false;
$a = $input; }
// 32-bit range check $a = $matches[1];
if( bccomp( $a, '2147483647', 0 ) > 0 ) return FALSE;
if( bccomp( $a, '-2147483648', 0 ) < 0 ) return FALSE; // sanity range check.
if( bccomp( $a, '0', 0 ) < 0 ) { if (bccomp($a, self::MAX_VALUE, 0) > 0) {
$a = bcadd( $a, '4294967296', 0 ); return false;
} }
$result = new self( $a ); $result = new self($a);
$result->formatted[ self::FORMAT_S32 ] = $input; $result->formatted[self::FORMAT_STEAMID3] = $input;
return $result;
return $result;
case self::FORMAT_RAW:
case self::FORMAT_S32:
// validate digits only
if( !preg_match( '/^[0-9]+$/', $input ) ) { // validate signed 32-bit format
return FALSE; if (!preg_match('/^(-?[0-9]+)$/', $input)) {
} return false;
}
// sanity range check
if( bccomp( $input, self::MAX_VALUE, 0 ) > 0 ) return FALSE; $a = $input;
return new self( $input );
// 32-bit range check
case self::FORMAT_VANITY: if (bccomp($a, '2147483647', 0) > 0) {
return false;
// validate characters. }
if( !preg_match( '/^[a-zA-Z0-9_-]{2,}$/', $input ) ) return FALSE; if (bccomp($a, '-2147483648', 0) < 0) {
return false;
$result = self::ConvertVanityURL( $input ); }
if( $result !== FALSE ) { if (bccomp($a, '0', 0) < 0) {
$result->formatted[ self::FORMAT_VANITY ] = $input; $a = bcadd($a, '4294967296', 0);
return $result; }
} $result = new self($a);
} $result->formatted[self::FORMAT_S32] = $input;
// Auto detect format: return $result;
$input = trim( $input ); case self::FORMAT_RAW:
$result = self::Parse( $input, self::FORMAT_STEAMID32 );
if( $result !== FALSE ) return $result; // validate digits only
$result = self::Parse( $input, self::FORMAT_STEAMID64 ); if (!preg_match('/^[0-9]+$/', $input)) {
if( $result !== FALSE ) return $result; return false;
$result = self::Parse( $input, self::FORMAT_STEAMID3 ); }
if( $result !== FALSE ) return $result;
// sanity range check
if( preg_match( if (bccomp($input, self::MAX_VALUE, 0) > 0) {
'/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/profiles\/([0-9]+)\/*$/', return false;
$input, $matches ) ) { }
$result = self::Parse( $matches[1], self::FORMAT_STEAMID64 ); return new self($input);
if( $result !== FALSE ) return $result;
} case self::FORMAT_VANITY:
if( $resolve_vanity ) { // validate characters.
if (!preg_match('/^[a-zA-Z0-9_-]{2,}$/', $input)) {
// try the name directly return false;
$result = self::Parse( $input, self::FORMAT_VANITY ); }
if( $result !== FALSE ) return $result;
$result = self::ConvertVanityURL($input);
// try a full URL. if ($result !== false) {
if( preg_match( $result->formatted[self::FORMAT_VANITY] = $input;
'/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/id\/([a-zA-Z0-9_-]{2,})\/*$/',
$input, $matches ) ) { return $result;
}
$result = self::ConvertVanityURL( $matches[1] ); }
if( $result !== FALSE ) return $result;
} // Auto detect format:
}
$input = trim($input);
if( $detect_raw ) { $result = self::Parse($input, self::FORMAT_STEAMID32);
$result = self::Parse( $input, self::FORMAT_S32 ); if ($result !== false) {
if( $result !== FALSE ) return $result; return $result;
$result = self::Parse( $input, self::FORMAT_RAW ); }
if( $result !== FALSE ) return $result; $result = self::Parse($input, self::FORMAT_STEAMID64);
} if ($result !== false) {
return $result;
// unknown stem }
return FALSE; $result = self::Parse($input, self::FORMAT_STEAMID3);
} if ($result !== false) {
return $result;
/** ----------------------------------------------------------------------- }
* Convert a vanity URL into a SteamID instance.
* if (preg_match(
* @param string $vanity_url_name The text part of the person's vanity URL. '/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/profiles\/([0-9]+)\/*$/',
* e.g http://steamcommunity.com/id/gabelogannewell $input, $matches)) {
* would use "gabelogannewell" $result = self::Parse($matches[1], self::FORMAT_STEAMID64);
* @return SteamID|false SteamID instance or FALSE on failure. if ($result !== false) {
*/ return $result;
public static function ConvertVanityURL( $vanity_url_name ) { }
if( empty($vanity_url_name) ) return FALSE; }
if( self::$steam_api_key !== FALSE ) { if ($resolve_vanity) {
$response = self::Curl(
"http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=" // try the name directly
.self::$steam_api_key $result = self::Parse($input, self::FORMAT_VANITY);
."&vanityurl=$vanity_url_name" ); if ($result !== false) {
if( $response === FALSE ) { return $result;
throw new SteamIDResolutionException( }
SteamIDResolutionException::CURL_FAILURE,
'CURL Request Failed.' ); // try a full URL.
} if (preg_match(
'/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/id\/([a-zA-Z0-9_-]{2,})\/*$/',
if( $response == "" ) { $input, $matches)) {
throw new SteamIDResolutionException( $result = self::ConvertVanityURL($matches[1]);
SteamIDResolutionException::VANITYURL_FAILED, if ($result !== false) {
'Steam failure.' ); return $result;
} }
}
$response = json_decode( $response ); }
if( $response === FALSE ) {
throw new SteamIDResolutionException( if ($detect_raw) {
SteamIDResolutionException::VANITYURL_FAILED, $result = self::Parse($input, self::FORMAT_S32);
'Steam failure.' ); if ($result !== false) {
} return $result;
}
$response = $response->response; $result = self::Parse($input, self::FORMAT_RAW);
if ($result !== false) {
if( $response->success == 42 ) { return $result;
throw new SteamIDResolutionException( }
SteamIDResolutionException::VANITYURL_NOTFOUND, }
'Vanity URL doesn\'t exist.' );
// unknown stem
} return false;
}
if( $response->success != 1 ) {
throw new SteamIDResolutionException( /** -----------------------------------------------------------------------
SteamIDResolutionException::VANITYURL_FAILED, * Convert a vanity URL into a SteamID instance.
'Steam failure.' ); *
* @param string $vanity_url_name The text part of the person's vanity URL.
} * e.g http://steamcommunity.com/id/gabelogannewell
* would use "gabelogannewell"
$steamid = $response->steamid; *
* @return SteamID|false SteamID instance or FALSE on failure.
} else { */
// fallback to xml parsing method. public static function ConvertVanityURL($vanity_url_name)
{
$result = self::Curl( "http://steamcommunity.com/id/$vanity_url_name?xml=1" ); if (empty($vanity_url_name)) {
if( $result === FALSE ) { return false;
throw new SteamIDResolutionException( }
SteamIDResolutionException::CURL_FAILURE,
'CURL Request Failed.' ); if (self::$steam_api_key !== false) {
} $response = self::Curl(
'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key='
$parser = xml_parser_create(''); .self::$steam_api_key
$values = array(); ."&vanityurl=$vanity_url_name");
$indexes = array(); if ($response === false) {
xml_parse_into_struct( $parser, $result, $values, $indexes ); throw new SteamIDResolutionException(
xml_parser_free($parser); SteamIDResolutionException::CURL_FAILURE,
if( !isset( $indexes['STEAMID64'] ) || is_null( $indexes['STEAMID64'] ) ) { 'CURL Request Failed.');
}
if( isset( $indexes['ERROR'] ) &&
trim($values[ $indexes['ERROR'][0] ]['value']) == if ($response == '') {
'The specified profile could not be found.' ) { throw new SteamIDResolutionException(
SteamIDResolutionException::VANITYURL_FAILED,
throw new SteamIDResolutionException( 'Steam failure.');
SteamIDResolutionException::VANITYURL_NOTFOUND, }
'Vanity URL doesn\'t exist.' );
} $response = json_decode($response);
if ($response === false) {
throw new SteamIDResolutionException( throw new SteamIDResolutionException(
SteamIDResolutionException::VANITYURL_FAILED, SteamIDResolutionException::VANITYURL_FAILED,
'Invalid Vanity URL or Steam failure.' ); 'Steam failure.');
} }
$steamid = $indexes['STEAMID64'];
$steamid = $values[ $steamid[0] ]['value']; $response = $response->response;
}
if ($response->success == 42) {
return self::Parse( $steamid, self::FORMAT_STEAMID64 ); throw new SteamIDResolutionException(
} SteamIDResolutionException::VANITYURL_NOTFOUND,
'Vanity URL doesn\'t exist.');
/** ----------------------------------------------------------------------- }
* Format this SteamID to a string.
* if ($response->success != 1) {
* @param int $format Output format. See FORMAT_xxx constants. throw new SteamIDResolutionException(
* @return string|false Formatted Steam ID. FALSE if an invalid format is SteamIDResolutionException::VANITYURL_FAILED,
* given or the desired format cannot contain the 'Steam failure.');
* SteamID. }
*/
public function Format( $format ) { $steamid = $response->steamid;
if( isset( $this->formatted[$format] ) ) { } else {
return $this->formatted[$format]; // fallback to xml parsing method.
}
$result = self::Curl("http://steamcommunity.com/id/$vanity_url_name?xml=1");
switch( $format ) { if ($result === false) {
case self::FORMAT_STEAMID32: throw new SteamIDResolutionException(
$z = bcdiv( $this->value, '2', 0 ); SteamIDResolutionException::CURL_FAILURE,
$y = bcmul( $z, '2', 0 ); 'CURL Request Failed.');
$y = bcsub( $this->value, $y, 0 ); }
$formatted = "STEAM_1:$y:$z";
$this->formatted[$format] = $formatted; $parser = xml_parser_create('');
return $formatted; $values = [];
$indexes = [];
case self::FORMAT_STEAMID64: xml_parse_into_struct($parser, $result, $values, $indexes);
$formatted = bcadd( $this->value, self::STEAMID64_BASE, 0 ); xml_parser_free($parser);
$this->formatted[$format] = $formatted; if (!isset($indexes['STEAMID64']) || is_null($indexes['STEAMID64'])) {
return $formatted; if (isset($indexes['ERROR']) &&
trim($values[$indexes['ERROR'][0]]['value']) ==
case self::FORMAT_STEAMID3: 'The specified profile could not be found.') {
$formatted = "[U:1:$this->value]"; throw new SteamIDResolutionException(
$this->formatted[$format] = $formatted; SteamIDResolutionException::VANITYURL_NOTFOUND,
return $formatted; 'Vanity URL doesn\'t exist.');
}
case self::FORMAT_S32:
if( bccomp( $this->value, '4294967296', 0 ) >= 0 ) { throw new SteamIDResolutionException(
return FALSE; // too large for s32. SteamIDResolutionException::VANITYURL_FAILED,
} 'Invalid Vanity URL or Steam failure.');
}
if( bccomp( $this->value, '2147483648', 0 ) >= 0 ) { $steamid = $indexes['STEAMID64'];
$formatted = bcsub( $this->value, '4294967296', 0 ); $steamid = $values[$steamid[0]]['value'];
} else { }
$formatted = $this->value;
} return self::Parse($steamid, self::FORMAT_STEAMID64);
$this->formatted[$format] = $formatted; }
return $formatted;
/** -----------------------------------------------------------------------
// (raw is always cached) * Format this SteamID to a string.
} *
return FALSE; * @param int $format Output format. See FORMAT_xxx constants.
} *
* @return string|false Formatted Steam ID. FALSE if an invalid format is
* given or the desired format cannot contain the
* SteamID.
*/
public function Format($format)
{
if (isset($this->formatted[$format])) {
return $this->formatted[$format];
}
switch ($format) {
case self::FORMAT_STEAMID32:
$z = bcdiv($this->value, '2', 0);
$y = bcmul($z, '2', 0);
$y = bcsub($this->value, $y, 0);
$formatted = "STEAM_1:$y:$z";
$this->formatted[$format] = $formatted;
return $formatted;
case self::FORMAT_STEAMID64:
$formatted = bcadd($this->value, self::STEAMID64_BASE, 0);
$this->formatted[$format] = $formatted;
return $formatted;
case self::FORMAT_STEAMID3:
$formatted = "[U:1:$this->value]";
$this->formatted[$format] = $formatted;
return $formatted;
case self::FORMAT_S32:
if (bccomp($this->value, '4294967296', 0) >= 0) {
return false; // too large for s32.
}
if (bccomp($this->value, '2147483648', 0) >= 0) {
$formatted = bcsub($this->value, '4294967296', 0);
} else {
$formatted = $this->value;
}
$this->formatted[$format] = $formatted;
return $formatted;
// (raw is always cached)
}
return false;
}
} }
?>