steamid_converter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. include 'util_config.php';
  25. session_name($sessionName);
  26. session_start();
  27. if(!empty($_SESSION['user_id']) && $_SERVER['REQUEST_METHOD'] === 'POST'){
  28. function toCommunityID($id){
  29. if(preg_match('/^STEAM_/', $id)){
  30. $parts = explode(':', $id);
  31. return bcadd(bcadd(bcmul($parts[2], '2'), '76561197960265728'), $parts[1]);
  32. }elseif(is_numeric($id) && strlen($id) < 16){
  33. return bcadd($id, '76561197960265728');
  34. }else{
  35. return $id;
  36. }
  37. }
  38. function toSteamID($id){
  39. if(is_numeric($id) && strlen($id) >= 16){
  40. $z = bcdiv(bcsub($id, '76561197960265728'), '2');
  41. }elseif(is_numeric($id)){
  42. $z = bcdiv($id, '2');
  43. }else{
  44. return $id;
  45. }
  46. $y = bcmod($id, '2');
  47. return 'STEAM_0:'.$y.':'.floor($z);
  48. }
  49. function toUserID($id){
  50. if(preg_match('/^STEAM_/', $id)){
  51. $split = explode(':', $id);
  52. return $split[2] * 2 + $split[1];
  53. }elseif(preg_match('/^765/', $id) && strlen($id) > 15){
  54. return bcsub($id, '76561197960265728');
  55. }else{
  56. return $id;
  57. }
  58. }
  59. function getSteamId($input){
  60. $xml = simplexml_load_file('http://steamcommunity.com/id/'.$input.'?xml=1');
  61. $steamId64 = $xml->steamID64;
  62. if(preg_match('/^765/', $steamId64) === 0){
  63. return false;
  64. }else{
  65. return (string)$steamId64; // Cast it to a string, otherwise an SimpleXMLElement Object will be returned.
  66. }
  67. }
  68. function convert($id){
  69. if(preg_match('/^\[U:1:[0-9]+\]/', $id)){
  70. $id = substr($id, 5, -1);
  71. }
  72. // Check if the input is in legacyId, communityId, or SteamId3 format.
  73. // If it's not, assume it's a custom Id and attempt to get the communityId from what the user entered.
  74. if(preg_match('/^STEAM_[01]:[01]:\d+$/', $id) === 0 && preg_match('/^[0-9]/', $id) === 0 && preg_match('/^765/', $id) === 0){
  75. if(($steamId = getSteamId($id)) === false){
  76. return json_encode(array());
  77. }
  78. $id = $steamId;
  79. }
  80. return json_encode(array(
  81. 'communityId' => toCommunityID($id),
  82. 'steamId' => toSteamID($id),
  83. 'steamId3' => '[U:1:'.toUserID($id).']',
  84. 'steamProfile' => '<a href="http://steamcommunity.com/profiles/'.toCommunityID($id).'">Steam Profile</a>',
  85. ));
  86. }
  87. echo convert($_POST['steam_input']);
  88. }else{//_SESSION check.
  89. header('HTTP/1.0 403 Forbidden');
  90. exit;
  91. }
  92. ?>