Permalink
Browse files

renames

  • Loading branch information...
mukunda- committed Sep 30, 2014
1 parent 67aaba2 commit 4806028d3d8b933a74a05098385f881b315e9a1e
Showing with 67 additions and 41 deletions.
  1. +21 −0 LICENSE.TXT
  2. +29 −24 lib/steamid.php
  3. +17 −17 test.php
View
@@ -0,0 +1,21 @@
SteamID Parser
Copyright 2014 Mukunda Johnson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
View
@@ -59,16 +59,21 @@ class SteamID {
// by Format or Parse.
public $formatted;
const FORMAT_AUTO = 0; // Auto-detect format --- this also supports other
// unlisted formats such as profile URLs.
const FORMAT_32BIT = 1; // Classic STEAM_x:y:zzzzzz | x = 0/1
const FORMAT_64BIT = 2; // SteamID64: 7656119xxxxxxxxxx
const FORMAT_V3 = 3; // SteamID3 format: [U:1:xxxxxx]
const FORMAT_S32 = 4; // Raw 32-bit SIGNED format.
// 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 FORMAT_AUTO = 0; // Auto-detect format --- this also supports
// other unlisted formats such as
// full profile URLs.
const FORMAT_STEAMID32 = 1; // Classic STEAM_x:y:zzzzzz | x = 0/1
const FORMAT_STEAMID64 = 2; // SteamID64: 7656119xxxxxxxxxx
const FORMAT_STEAMID3 = 3; // SteamID3 format: [U:1:xxxxxx]
const FORMAT_S32 = 4; // Raw 32-bit SIGNED format.
// this is a raw steamid index that overflows
// into negative bitspace.
// This is the format that SourceMod returns
// with GetSteamAccountID, and will always
// fit into a 32-bit signed variable. (e.g.
// a 32-bit PHP integer).
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';
@@ -137,7 +142,7 @@ public static function Parse( $input,
$resolve_vanity = false ) {
switch( $format ) {
case self::FORMAT_32BIT:
case self::FORMAT_STEAMID32:
// validate STEAM_0/1:y:zzzzzz
if( !preg_match(
@@ -152,10 +157,10 @@ public static function Parse( $input,
$a = bcadd( $a, $matches[1], 0 );
$result = new self( $a );
$result->formatted[ self::FORMAT_32BIT ] = $input;
$result->formatted[ self::FORMAT_STEAMID32 ] = $input;
return $result;
case self::FORMAT_64BIT:
case self::FORMAT_STEAMID64:
// allow digits only
if( !preg_match( '/^[0-9]+$/', $input ) ) return FALSE;
@@ -168,10 +173,10 @@ public static function Parse( $input,
if( bccomp( $a, self::MAX_VALUE, 0 ) > 0 ) return FALSE;
$result = new self( $a );
$result->formatted[ self::FORMAT_64BIT ] = $input;
$result->formatted[ self::FORMAT_STEAMID64 ] = $input;
return $result;
case self::FORMAT_V3:
case self::FORMAT_STEAMID3:
// validate [U:1:xxxxxx]
if( !preg_match( '/^\[U:1:([0-9]+)\]$/', $input, $matches ) ) {
@@ -183,7 +188,7 @@ public static function Parse( $input,
// sanity range check.
if( bccomp( $a, self::MAX_VALUE, 0 ) > 0 ) return FALSE;
$result = new self( $a );
$result->formatted[ self::FORMAT_V3 ] = $input;
$result->formatted[ self::FORMAT_STEAMID3 ] = $input;
return $result;
case self::FORMAT_S32:
@@ -231,17 +236,17 @@ public static function Parse( $input,
// Auto detect format:
$input = trim( $input );
$result = self::Parse( $input, self::FORMAT_32BIT );
$result = self::Parse( $input, self::FORMAT_STEAMID32 );
if( $result !== FALSE ) return $result;
$result = self::Parse( $input, self::FORMAT_64BIT );
$result = self::Parse( $input, self::FORMAT_STEAMID64 );
if( $result !== FALSE ) return $result;
$result = self::Parse( $input, self::FORMAT_V3 );
$result = self::Parse( $input, self::FORMAT_STEAMID3 );
if( $result !== FALSE ) return $result;
if( preg_match(
'/^(?:https?:\/\/)?(?:www.)?steamcommunity.com\/profiles\/([0-9]+)$/',
$input, $matches ) ) {
$result = self::Parse( $matches[1], self::FORMAT_64BIT );
$result = self::Parse( $matches[1], self::FORMAT_STEAMID64 );
if( $result !== FALSE ) return $result;
}
@@ -357,7 +362,7 @@ public static function ConvertVanityURL( $vanity_url_name ) {
$steamid = $values[ $steamid[0] ]['value'];
}
return self::Parse( $steamid, self::FORMAT_64BIT );
return self::Parse( $steamid, self::FORMAT_STEAMID64 );
}
/** -----------------------------------------------------------------------
@@ -374,20 +379,20 @@ public function Format( $format ) {
}
switch( $format ) {
case self::FORMAT_32BIT:
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_64BIT:
case self::FORMAT_STEAMID64:
$formatted = bcadd( $this->value, self::STEAMID64_BASE, 0 );
$this->formatted[$format] = $formatted;
return $formatted;
case self::FORMAT_V3:
case self::FORMAT_STEAMID3:
$formatted = "[U:1:$this->value]";
$this->formatted[$format] = $formatted;
return $formatted;
View
@@ -87,24 +87,24 @@ public static function Add( $name, $method ) {
$steamid = SteamID::Parse( " STEAM_1:1:54499221 " );
if( $steamid === FALSE ) return FALSE;
PrintSubTest( "32-bit direct" );
$steamid = SteamID::Parse( "STEAM_0:1:54499221", SteamID::FORMAT_32BIT );
$steamid = SteamID::Parse( "STEAM_0:1:54499221", SteamID::FORMAT_STEAMID32 );
if( $steamid === FALSE ) return FALSE;
PrintSubTest( "32-bit as 64 error" );
$steamid = SteamID::Parse( "STEAM_1:1:54499221", SteamID::FORMAT_64BIT );
$steamid = SteamID::Parse( "STEAM_1:1:54499221", SteamID::FORMAT_STEAMID64 );
if( $steamid !== FALSE ) return FALSE;
PrintSubTest( "64-bit detect" );
$steamid = SteamID::Parse( "76561198069264171" );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "64-bit direct" );
$steamid = SteamID::Parse( "76561198069264171", SteamID::FORMAT_64BIT );
$steamid = SteamID::Parse( "76561198069264171", SteamID::FORMAT_STEAMID64 );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "64-bit errornous" );
$steamid = SteamID::Parse( "76533611981069263334171", SteamID::FORMAT_64BIT );
$steamid = SteamID::Parse( "76533611981069263334171", SteamID::FORMAT_STEAMID64 );
if( $steamid !== FALSE ) return FALSE;
PrintSubTest( "64-bit as raw" );
@@ -114,44 +114,44 @@ public static function Add( $name, $method ) {
PrintSubTest( "v3 detect" );
$steamid = SteamID::Parse( "[U:1:108998443]" );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "v3 direct" );
$steamid = SteamID::Parse( "[U:1:108998443]", SteamID::FORMAT_V3 );
$steamid = SteamID::Parse( "[U:1:108998443]", SteamID::FORMAT_STEAMID3 );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "s32 detect" );
$steamid = SteamID::Parse( "108998443" );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "s32 direct" );
$steamid = SteamID::Parse( "108998443", SteamID::FORMAT_S32 );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "raw direct" );
$steamid = SteamID::Parse( "108998443", SteamID::FORMAT_RAW );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "community URL 1" );
$steamid = SteamID::Parse( "http://www.steamcommunity.com/profiles/76561198069264171" );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "community URL 2" );
$steamid = SteamID::Parse( "http://steamcommunity.com/profiles/76561198069264171" );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "community URL 3" );
$steamid = SteamID::Parse( "www.steamcommunity.com/profiles/76561198069264171" );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "community URL 4" );
$steamid = SteamID::Parse( "steamcommunity.com/profiles/76561198069264171" );
if( $steamid === FALSE ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_32BIT ) != "STEAM_1:1:54499221" ) return FALSE;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
PrintSubTest( "random conversions..." );
for( $i = 0; $i < 5000; $i++ ) {
@@ -225,7 +225,7 @@ function VanityTest() {
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;
if( $steamid->Format( SteamID::FORMAT_STEAMID32 ) != "STEAM_1:1:54499221" ) return FALSE;
for( $i = 0; $i < 20; $i++ ) {

0 comments on commit 4806028

Please sign in to comment.