Advertisement
Azelphur

Untitled

Nov 23rd, 2011
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.58 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * get_steam_button
  5.  * Returns a "Sign in through steam" button that will take you to the steam website to login, and then load the callback page.
  6.  * Note that although it says openid in a lot of places, it's not following the openid spec and won't work with most if not all openid libraries
  7.  * This assumes that you have a page at /steam_openid to take the callback
  8.  */
  9.  
  10. function get_steam_button() {
  11.       return '<a id="openid_link" href="https://steamcommunity.com/openid/login?openid.ns=http://specs.openid.net/auth/2.0&openid.mode=checkid_setup&openid.return_to=' . $base_url . $base_path . 'steam_openid/;&openid.realm=' . $base_url . '&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select">
  12.  <img src="http://steamcommunity.com/public/images/signinthroughsteam/sits_small.png" />
  13.  </a>',
  14.     );
  15.   }
  16. }
  17.  
  18.  
  19. /*
  20.  * steam_openid
  21.  * This is an example callback for the steam_openid page
  22.  */
  23.  
  24. function steam_openid() {
  25.   if(!isset($_GET['openid_assoc_handle']))
  26.       return "Error attempting to validate your steam account, please try again.";
  27.  
  28.   $params = array(
  29.       'openid.assoc_handle' => $_GET['openid_assoc_handle'],
  30.       'openid.signed'           => $_GET['openid_signed'],
  31.       'openid.sig'          => $_GET['openid_sig'],
  32.       'openid.ns'               => 'http://specs.openid.net/auth/2.0',
  33.   );
  34.  
  35.   $signed = explode(',', $_GET['openid_signed']);
  36.   foreach($signed as $item)
  37.   {
  38.       $val = $_GET['openid_' . str_replace('.', '_', $item)];
  39.       $params['openid.' . $item] = get_magic_quotes_gpc() ? stripslashes($val) : $val;
  40.   }
  41.  
  42.   $params['openid.mode'] = 'check_authentication';
  43.  
  44.   $data =  http_build_query($params);
  45.   $context = stream_context_create(array(
  46.       'http' => array(
  47.           'method'  => 'POST',
  48.           'header'  =>
  49.               "Accept-language: en\r\n".
  50.               "Content-type: application/x-www-form-urlencoded\r\n" .
  51.               "Content-Length: " . strlen($data) . "\r\n",
  52.           'content' => $data,
  53.       ),
  54.   ));
  55.  
  56.   $result = file_get_contents("https://steamcommunity.com/openid/login", false, $context);
  57.  
  58.   preg_match("#^http://steamcommunity.com/openid/id/([0-9]{17,25})#", $_GET['openid_claimed_id'], $matches);
  59.   $steamid64 = is_numeric($matches[1]) ? $matches[1] : 0;
  60.  
  61.   if (preg_match("#is_valid\s*:\s*true#i", $result) != 1) {
  62.     global $base_url;
  63.     global $base_path;
  64.     header("Location: https://steamcommunity.com/openid/login?openid.ns=http://specs.openid.net/auth/2.0&openid.mode=checkid_setup&openid.return_to=' . $base_url . $base_path . 'steam_openid/&openid.realm=' . $base_url . '&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select");
  65.       return "Error attempting to validate your steam account, please try again.";
  66.   }
  67.   // If you make it past the above link, you've got a validated steam account. $steamid64 is like a $user->id for steam accounts, so it's the main thing you'll want to store.
  68.   return "You have successfully linked your steam account.";
  69. }
  70.  
  71. /*
  72.  * Load steam XML
  73.  * Call this function with the steamid64 obtained from steam_openid to load the XML feed for that user. This contains name, avatar, and lots of other information
  74.  */
  75.  
  76. function steam_load_xml($steamid64) {
  77.   $url = "http://steamcommunity.com/profiles/" . $steamid64 . "/?xml=1";
  78.   $context = stream_context_create(array('http' => array('timeout' => 5)));
  79.   if($fp = @fopen($url, 'r', FALSE, $context))
  80.   {
  81.     $xml = stream_get_contents($fp);
  82.     fclose($fp);
  83.     $xml = new SimpleXMLElement($xml);
  84.     return $xml;
  85.   }
  86.   return FALSE;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement