Skip to content

Commit

Permalink
more done
Browse files Browse the repository at this point in the history
  • Loading branch information
mukunda- committed Sep 30, 2014
1 parent 9d5c612 commit c4192ed
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
steamapikey
86 changes: 61 additions & 25 deletions lib/steamid.php
Expand Up @@ -3,7 +3,6 @@
/*!
* SteamID Parser
*
*
* Copyright 2014 Mukunda Johnson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -69,6 +68,7 @@ class SteamID {
// this is a raw steamid index that overflows
// into negative bitspace.
const FORMAT_RAW = 5; // Raw index. like 64-bit minus the base value.
const FORMAT_VANITY= 6; // Vanity URL name. Forward conversion only.


const STEAMID64_BASE = '76561197960265728';
Expand All @@ -87,8 +87,8 @@ class SteamID {
* @see http://steamcommunity.com/dev/apikey
*/
public static function SetSteamAPIKey( $key ) {
if( empty($key) ) $steam_api_key = FALSE;
$steam_api_key = $key;
if( empty($key) ) self::$steam_api_key = FALSE;
self::$steam_api_key = $key;
}

/** -----------------------------------------------------------------------
Expand All @@ -107,7 +107,7 @@ private function __construct( $raw ) {
* @param string $url URL to request.
* @return string|false Contents of result or FALSE if the request failed.
*/
private function Curl( $url ) {
private static function Curl( $url ) {

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
Expand All @@ -126,11 +126,15 @@ private function Curl( $url ) {
* @param string $input Input to parse.
* @param int $format Input formatting, see FORMAT_ constants.
* Defaults to FORMAT_AUTO which detects the format.
* @param bool $resolve_vanity Detect and resolve vanity URLs. (only used
* with FORMAT_AUTO.
*
* @return SteamID|false SteamID instance or FALSE if the input is invalid
* or unsupported.
*/
public static function Parse( $input, $format = self::FORMAT_AUTO ) {
public static function Parse( $input,
$format = self::FORMAT_AUTO,
$resolve_vanity = false ) {
switch( $format ) {

case self::FORMAT_32BIT:
Expand Down Expand Up @@ -211,6 +215,17 @@ public static function Parse( $input, $format = self::FORMAT_AUTO ) {
// sanity range check
if( bccomp( $input, self::MAX_VALUE, 0 ) > 0 ) return FALSE;
return new self( $input );

case self::FORMAT_VANITY:

// validate characters.
if( !preg_match( '/^[a-zA-Z0-9_-]{2,}$/', $input ) ) return FALSE;

$result = self::ConvertVanityURL( $input );
if( $result !== FALSE ) {
$result->formatted[ self::FORMAT_VANITY ] = $input;
return $result;
}
}

// Auto detect format:
Expand All @@ -222,10 +237,6 @@ public static function Parse( $input, $format = self::FORMAT_AUTO ) {
if( $result !== FALSE ) return $result;
$result = self::Parse( $input, self::FORMAT_V3 );
if( $result !== FALSE ) return $result;
$result = self::Parse( $input, self::FORMAT_S32 );
if( $result !== FALSE ) return $result;
$result = self::Parse( $input, self::FORMAT_RAW );
if( $result !== FALSE ) return $result;

if( preg_match(
'/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/profiles\/([0-9]+)$/',
Expand All @@ -234,15 +245,27 @@ public static function Parse( $input, $format = self::FORMAT_AUTO ) {
if( $result !== FALSE ) return $result;
}

// TODO find out what characters are valid in customURLs.
if( preg_match(
'/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/id\/([a-zA-Z0-9]+)$/',
$input, $matches ) ) {

$result = self::ConvertVanityURL( $matches[1] );
if( $resolve_vanity ) {

// try the name directly
$result = self::Parse( $input, self::FORMAT_VANITY );
if( $result !== FALSE ) return $result;

// try a full URL.
if( preg_match(
'/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/id\/([a-zA-Z0-9_-]{2,})$/',
$input, $matches ) ) {

$result = self::ConvertVanityURL( $matches[1] );
if( $result !== FALSE ) return $result;
}
}

$result = self::Parse( $input, self::FORMAT_S32 );
if( $result !== FALSE ) return $result;
$result = self::Parse( $input, self::FORMAT_RAW );
if( $result !== FALSE ) return $result;

// unknown stem
return FALSE;
}
Expand All @@ -258,10 +281,12 @@ public static function Parse( $input, $format = self::FORMAT_AUTO ) {
public static function ConvertVanityURL( $vanity_url_name ) {
if( empty($vanity_url_name) ) return FALSE;

if( $steam_api_key !== FALSE ) {
$result = Curl(
"http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=$steam_api_key&vanityurl=$vanity_url_name" );
if( $result === FALSE ) {
if( self::$steam_api_key !== FALSE ) {
$response = self::Curl(
"http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key="
.self::$steam_api_key
."&vanityurl=$vanity_url_name" );
if( $response === FALSE ) {
throw new SteamIDResolutionException(
SteamIDResolutionException::CURL_FAILURE,
'CURL Request Failed.' );
Expand All @@ -272,14 +297,16 @@ public static function ConvertVanityURL( $vanity_url_name ) {
SteamIDResolutionException::VANITYURL_FAILED,
'Steam failure.' );
}

$response = json_decode( $response );
if( $response === FALSE ) {
throw new SteamIDResolutionException(
SteamIDResolutionException::VANITYURL_FAILED,
'Steam failure.' );
}

$response = $response->response;

if( $response->success == 42 ) {
throw new SteamIDResolutionException(
SteamIDResolutionException::VANITYURL_NOTFOUND,
Expand All @@ -299,8 +326,7 @@ public static function ConvertVanityURL( $vanity_url_name ) {
} else {
// fallback to xml parsing method.

$result = Curl(
"http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=$steam_api_key&vanityurl=$vanity_url_name" );
$result = self::Curl( "http://steamcommunity.com/id/$vanity_url_name?xml=1" );
if( $result === FALSE ) {
throw new SteamIDResolutionException(
SteamIDResolutionException::CURL_FAILURE,
Expand All @@ -312,16 +338,26 @@ public static function ConvertVanityURL( $vanity_url_name ) {
$indexes = array();
xml_parse_into_struct( $parser, $result, $values, $indexes );
xml_parser_free($parser);
$steamid = $indexes['STEAMID64'];
if( is_null( $steamid ) ) {
if( !isset( $indexes['STEAMID64'] ) || is_null( $indexes['STEAMID64'] ) ) {

if( isset( $indexes['ERROR'] ) &&
trim($values[ $indexes['ERROR'][0] ]['value']) ==
'The specified profile could not be found.' ) {

throw new SteamIDResolutionException(
SteamIDResolutionException::VANITYURL_NOTFOUND,
'Vanity URL doesn\'t exist.' );
}

throw new SteamIDResolutionException(
SteamIDResolutionException::VANITYURL_FAILED,
'Invalid Vanity URL or Steam failure.' );
}
$steamid = $indexes['STEAMID64'];
$steamid = $values[ $steamid[0] ]['value'];
}

return Parse( $steamid, self::FORMAT_64BIT );
return self::Parse( $steamid, self::FORMAT_64BIT );
}

/** -----------------------------------------------------------------------
Expand Down
88 changes: 79 additions & 9 deletions test.php
@@ -1,22 +1,24 @@
<?php

// oh man a testing thing

require_once 'lib/steamid.php';

header( 'Content-Type: text/plain' );

//-----------------------------------------------------------------------------
// ****************************************************************************
function PrintLine( $text ) {
echo $text . "\r\n";
flush();
}
//-----------------------------------------------------------------------------
// ****************************************************************************
function PrintSubTest( $text ) {
echo " $text" . "\r\n";
flush();
}


//-----------------------------------------------------------------------------
// ****************************************************************************
class Test {
public $name;
public $method;
Expand All @@ -33,7 +35,7 @@ function __construct( $name, $method ) {
}
}

//-----------------------------------------------------------------------------
// ****************************************************************************
final class Tests {

public static $tests = array();
Expand Down Expand Up @@ -63,9 +65,10 @@ public static function Run() {
PrintLine( "---" );
PrintLine( "Running all tests." );
foreach( self::$tests as $test ) {
PrintLine( "" );
PrintLine( "\"$test->name\"" );
if( $test->Run() ) {
PrintLine( "Passed." );
PrintLine( "--- Passed. ---" );
} else {
PrintLine( "*** Failed! ***" );
}
Expand All @@ -77,7 +80,7 @@ public static function Add( $name, $method ) {
}
}

//-----------------------------------------------------------------------------
// ****************************************************************************
Tests::Add( "Conversion Test", function() {

PrintSubTest( "32-bit detect" );
Expand Down Expand Up @@ -173,7 +176,7 @@ public static function Add( $name, $method ) {
return TRUE;
} );


// ****************************************************************************
Tests::Add( "Large SteamID conversions", function () {

for( $i = 0; $i < 255; $i++ ) {
Expand Down Expand Up @@ -208,9 +211,76 @@ public static function Add( $name, $method ) {

return TRUE;
} );

bcscale( 32 );

// ****************************************************************************
function VanityTest() {

$buildname = function( $len ) {
$name = "";
for( $i = 0; $i < $len; $i++ ) {
$name .= chr( mt_rand( 97, 122 ) );
}
return $name;
};
PrintSubTest( "simple/direct" );
$steamid = SteamID::Parse( "prayspray", SteamID::FORMAT_AUTO, true );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;


for( $i = 0; $i < 20; $i++ ) {
$name = $buildname( $i );

try {
$steamid = SteamID::Parse( $name, SteamID::FORMAT_AUTO, true );
} catch( SteamIDResolutionException $e ) {
switch( $e->reason ) {
case SteamIDResolutionException::UNKNOWN:
PrintSubTest( "failure: UNKNOWN. $name" );
PrintSubTest( $e->getMessage() );
return FALSE;
case SteamIDResolutionException::CURL_FAILURE:
PrintSubTest( "CURL_FAILURE $name" );
PrintSubTest( $e->getMessage() );
return FALSE;
case SteamIDResolutionException::VANITYURL_NOTFOUND:
// normal operation
continue;
case SteamIDResolutionException::VANITYURL_FAILED:
PrintSubTest( "STEAM FAILURE $name" );
PrintSubTest( $e->getMessage() );
return FALSE;
}
}
}
return TRUE;
}

// ****************************************************************************
Tests::Add( "VanityURL Resolution via XML", function () {

return VanityTest();

} );

// ****************************************************************************
Tests::Add( "VanityURL Resolution via Steam API", function () {
$steamapikey = file_get_contents( "steamapikey" );
if($steamapikey=="") {
PrintSubTest(
"Steam API Key missing, please create a file called
\"steamapikey\" and set the contents to your Steam API key!" );
PrintSubTest( "http://steamcommunity.com/dev/apikey" );
return FALSE;
}

SteamID::SetSteamAPIKey( $steamapikey );

return VanityTest();
} );

// ****************************************************************************
bcscale( 32 );
Tests::Run();

?>

0 comments on commit c4192ed

Please sign in to comment.