Advertisement
Guest User

Untitled

a guest
Aug 20th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. <?php
  2. /**
  3. * This file is part of Steam Authentication for XenForo
  4. *
  5. * Written by Morgan Humes <morgan@lanaddict.com>
  6. * Copyright 2012 Morgan Humes
  7. *
  8. * Steam Authentication for XenForo is free software: you can redistribute
  9. * it and/or modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * Steam Authentication for XenForo is distributed in the hope that it
  14. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  15. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. * See the GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with SteamProfile. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21.  
  22. class Steam_Helper_Steam {
  23.  
  24. // cURL Variable
  25. private $ch = null;
  26.  
  27. public function __construct() {
  28. // Setup cURL
  29. $this->ch = curl_init();
  30. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
  31. curl_setopt($this->ch, CURLOPT_TIMEOUT, 4);
  32. }
  33.  
  34. public function getUserInfo($steam_id) {
  35. // cURL
  36. curl_setopt($this->ch, CURLOPT_URL, "http://steamcommunity.com/profiles/{$steam_id}/?xml=1");
  37. $result = curl_exec($this->ch);
  38. $xml = simplexml_load_string($result);
  39.  
  40. if(!empty($xml)) {
  41. return array(
  42. 'username' => $xml->steamID,
  43. 'avatar' => $xml->avatarFull,
  44. 'icon' => $xml->avatarIcon,
  45. 'state' => $xml->onlineState
  46.  
  47. );
  48. } else {
  49. return array(
  50. 'username' => "Unknown Steam Account",
  51. 'avatar' => "Ruh Roh!"
  52. );
  53. }
  54. }
  55.  
  56. public function getUserGames($steam_id) {
  57. $games = array();
  58.  
  59. // cURL
  60. curl_setopt($this->ch, CURLOPT_URL, "http://steamcommunity.com/profiles/$steam_id/games/?xml=1");
  61. $result = curl_exec($this->ch);
  62. $xml = simplexml_load_string($result);
  63.  
  64. if(!empty($xml)) {
  65. if(isset($xml->games)) {
  66. foreach($xml->games->children() as $game) {
  67. $appId = isset($game->appID) ? $game->appID : 0;
  68. $appName = isset($game->name) ? addslashes($game->name) : "";
  69. $appLogo = isset($game->logo) ? addslashes($game->logo) : "";
  70. $appLink = isset($game->storeLink) ? addslashes($game->storeLink) : "";
  71. $hours = isset($game->hoursOnRecord) ? $game->hoursOnRecord : 0;
  72. $hoursRecent = isset($game->hoursLast2Weeks) ? $game->hoursLast2Weeks : 0;
  73.  
  74. if($appId == 0 || $appName == "") {
  75. continue;
  76. }
  77.  
  78. $games["$appId"] = array (
  79. 'name' => $appName,
  80. 'logo' => $appLogo,
  81. 'link' => $appLink,
  82. 'hours' => $hours,
  83. 'hours_recent' => $hoursRecent
  84. );
  85. }
  86. }
  87. }
  88.  
  89. return $games;
  90. }
  91.  
  92. public function deleteSteamData($user_id) {
  93. $db = XenForo_Application::get('db');
  94. $db->query("DELETE FROM xf_user_steam_games WHERE user_id = $user_id");
  95. }
  96.  
  97. public function getGameInfo($id) {
  98. $db = XenForo_Application::get('db');
  99. $row = $db->fetchRow("SELECT game_id, game_name, game_logo, game_link FROM xf_steam_games WHERE game_id = $id");
  100. $rVal = array(
  101. 'id' => $row['game_id'],
  102. 'name' => $row['game_name'],
  103. 'logo' => $row['game_logo'],
  104. 'link' => $row['game_link']
  105. );
  106.  
  107. return $rVal;
  108. }
  109.  
  110. public function getGameOwners($id) {
  111. $rVal = array();
  112. $db = XenForo_Application::get('db');
  113. $results = $db->fetchAll("SELECT u.user_id, u.username, g.game_hours, g.game_hours_recent FROM xf_user_steam_games g, xf_user u WHERE g.user_id = u.user_id AND g.game_id = $id");
  114. foreach($results as $row) {
  115. $rVal[] = array(
  116. 'user_id' => $row['user_id'],
  117. 'username' => $row['username'],
  118. 'hours' => $row['game_hours'],
  119. 'hours_recent' => $row['game_hours_recent']
  120. );
  121. }
  122.  
  123. return $rVal;
  124. }
  125.  
  126. public function getGameStatistics($limit=25) {
  127. $rVal = array();
  128. $db = XenForo_Application::get('db');
  129. $results = $db->fetchAll("SELECT g.game_id, g.game_name, g.game_logo, g.game_link, COUNT(*) AS count FROM xf_user_steam_games u, xf_steam_games g WHERE u.game_id = g.game_id GROUP BY u.game_id ORDER BY count DESC, g.game_id ASC LIMIT $limit;");
  130. foreach($results as $row) {
  131. $rVal[$row['game_id']] = array(
  132. 'name' => $row['game_name'],
  133. 'count' => $row['count'],
  134. 'logo' => $row['game_logo'],
  135. 'link' => $row['game_link']
  136. );
  137. }
  138.  
  139. return $rVal;
  140. }
  141.  
  142. public function getGamePlayedStatistics($limit=25) {
  143. $rVal = array();
  144. $db = XenForo_Application::get('db');
  145. $results = $db->fetchAll("SELECT g.game_id, g.game_name, g.game_logo, g.game_link, SUM(u.game_hours) AS hours FROM xf_user_steam_games u, xf_steam_games g WHERE u.game_id = g.game_id GROUP BY u.game_id ORDER BY hours DESC, g.game_id ASC LIMIT $limit;");
  146. foreach($results as $row) {
  147. $rVal[$row['game_id']] = array(
  148. 'name' => $row['game_name'],
  149. 'hours' => $row['hours'],
  150. 'logo' => $row['game_logo'],
  151. 'link' => $row['game_link']
  152. );
  153. }
  154.  
  155. return $rVal;
  156. }
  157.  
  158. public function getGamePlayedRecentStatistics($limit=25) {
  159. $rVal = array();
  160. $db = XenForo_Application::get('db');
  161. $results = $db->fetchAll("SELECT g.game_id, g.game_name, g.game_logo, g.game_link, SUM(u.game_hours_recent) AS hours FROM xf_user_steam_games u, xf_steam_games g WHERE u.game_id = g.game_id GROUP BY u.game_id ORDER BY hours DESC, g.game_id ASC LIMIT $limit;");
  162. foreach($results as $row) {
  163. $rVal[$row['game_id']] = array(
  164. 'name' => $row['game_name'],
  165. 'hours' => $row['hours'],
  166. 'logo' => $row['game_logo'],
  167. 'link' => $row['game_link']
  168. );
  169. }
  170.  
  171. return $rVal;
  172. }
  173.  
  174. public function getAvailableGames() {
  175. $rVal = array();
  176. $db = XenForo_Application::get('db');
  177. $results = $db->fetchAll("SELECT game_id, game_name, game_link, game_logo FROM xf_steam_games ORDER BY game_name;");
  178. foreach($results as $row) {
  179. $rVal[] = array(
  180. 'id' => $row['game_id'],
  181. 'name' => $row['game_name'],
  182. 'link' => $row['game_link'],
  183. 'logo' => $row['game_logo']
  184. );
  185. }
  186. return $rVal;
  187. }
  188.  
  189. public function getSteamUsers() {
  190. $rVal = array();
  191. $db = XenForo_Application::get('db');
  192. $results = $db->fetchAll("SELECT u.user_id, u.username, p.steam_auth_id FROM xf_user u, xf_user_profile p WHERE u.user_id = p.user_id AND p.steam_auth_id > 0 ORDER BY u.username;");
  193. foreach($results as $row) {
  194. $rVal[] = array(
  195. 'id' => Steam_Helper_Steam::convertIdToString($row['steam_auth_id']),
  196. 'id64' => $row['steam_auth_id'],
  197. 'username' => $row['username'],
  198. 'user_id' => $row['user_id']
  199. );
  200. }
  201. return $rVal;
  202. }
  203.  
  204. public static function convertIdToString($id) {
  205. $steamId1 = substr($id, -1) % 2;
  206. $steamId2a = intval(substr($id, 0, 4)) - 7656;
  207. $steamId2b = substr($id, 4) - 1197960265728;
  208. $steamId2b = $steamId2b - $steamId1;
  209.  
  210. if($steamId2a <= 0 && $steamId2b <= 0) {
  211. throw new SteamCondenserException("SteamID $id is too small.");
  212. }
  213.  
  214. return "STEAM_0:$steamId1:" . (($steamId2a + $steamId2b) / 2);
  215. }
  216. }
  217.  
  218. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement