PageRenderTime 68ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/omg2/application/libraries/xfire.php

https://github.com/CMDcentral/CoManD
PHP | 418 lines | 272 code | 59 blank | 87 comment | 35 complexity | 2e234666a2a47cffcf960c25fd09ffdd MD5 | raw file
  1. <?php
  2. class xfire {
  3. var $username; // stores the currently used nickname
  4. var $compatibility; // compatibility mode
  5. var $type; // holds what type of feed is being parsed
  6. var $read_xml; // xml parser object
  7. var $xml_storage = array(); // stored data
  8. var $data = array(); // CXS backwards compatibility
  9. /**
  10. * Sets the username to use for downloading feeds
  11. *
  12. * @param string $username
  13. */
  14. function SetUsername( $username )
  15. {
  16. $this->username = $username;
  17. }
  18. /**
  19. * Enables or disables compatibility mode
  20. *
  21. * @param bool $compat
  22. */
  23. function SetCompatibilityMode( $compat )
  24. {
  25. $this->compatibility = $compat;
  26. }
  27. /**
  28. * Gets the user's basic profile information
  29. *e
  30. * @return array Profile / false if failed
  31. */
  32. function GetProfile( ) // profile
  33. {
  34. return $this->ParseFeed( "profile" );
  35. }
  36. /**
  37. * Gets the user's friends list
  38. *
  39. * @return array Friends / false if failed
  40. */
  41. function GetFriends( ) // friends
  42. {
  43. return $this->ParseFeed( "friends" );
  44. }
  45. /**
  46. * Gets live information about the user
  47. *
  48. * @return array Info / false if failed
  49. */
  50. function GetLive( ) // live
  51. {
  52. return $this->ParseFeed( "live" );
  53. }
  54. /**
  55. * Gets what games the user has been playing
  56. *
  57. * @return array Gamedata / false if failed
  58. */
  59. function GetGamedata( ) // gameplay
  60. {
  61. return $this->ParseFeed( "gameplay" );
  62. }
  63. /**
  64. * Gets a list of servers the user has favourited
  65. *
  66. * @return array Servers / false if failed
  67. */
  68. function GetServers( ) // servers
  69. {
  70. return $this->ParseFeed( "servers" );
  71. }
  72. /**
  73. * Gets the screenshots the user has taken and uploaded.
  74. *
  75. * @return array Screenshots / false if failed
  76. */
  77. function GetScreenshots( ) // screenshots
  78. {
  79. return $this->ParseFeed( "screenshots" );
  80. }
  81. /**
  82. * Gets information about the user's gaming computer
  83. *
  84. * @return array Gamerig / false if failed
  85. */
  86. function GetGamerig( ) // gamerig
  87. {
  88. return $this->ParseFeed( "gamerig" );
  89. }
  90. // CXS backwards compatibility
  91. function set( $username )
  92. {
  93. $this->username = $username;
  94. $this->compatibility = true;
  95. $this->load();
  96. }
  97. function load( )
  98. {
  99. $opt = array();
  100. $opt["profile"] = array_merge( $this->GetLive(), $this->GetProfile() );
  101. $opt["games"] = $this->GetGamedata();
  102. $opt["servers"] = $this->GetServers();
  103. if( $this->compatibility == true ) // use old array indexes
  104. {
  105. $opt["profile"]["rname"] = $opt["profile"]["realname"];
  106. $opt["profile"]["regsince"] = $opt["profile"]["joindate"];
  107. $opt["profile"]["about"] = $opt["profile"]["bio"];
  108. unset( $opt["profile"]["realname"] );
  109. unset( $opt["profile"]["joindate"] );
  110. unset( $opt["profile"]["bio"] );
  111. foreach( $opt["games"] as $k => $v )
  112. {
  113. $opt["games"][ $k ][ "name" ] = $opt["games"][ $k ][ "longname" ];
  114. $opt["games"][ $k ][ "icon" ] = $opt["games"][ $k ][ "shortname" ];
  115. unset( $opt["games"][ $k ][ "longname" ] );
  116. unset( $opt["games"][ $k ][ "shortname" ] );
  117. }
  118. $this->data = $opt;
  119. return;
  120. }
  121. return $opt;
  122. }
  123. // the code below is writting by Madwormer (Jason Reading) for CXS
  124. // IT IS DESIGNED TO BE USED WITH COMPATIBILITY MODE
  125. function parseData($type){
  126. if( $this->compatibility == false )
  127. {
  128. echo "<span style=\"color: #FF0000\">Cannot call parseData without compatibility mode</span>";
  129. return;
  130. }
  131. switch ($type)
  132. {
  133. case 'alltime':
  134. // Obtain a list of columns
  135. foreach ($this->data['games'] as $key => $row)
  136. {
  137. $nm[$key] = $row['name'];
  138. $tw[$key] = $row['thisweek'];
  139. $at[$key] = $row['alltime'];
  140. $ic[$key] = $row['icon'];
  141. }
  142. // Sort the data with volume descending, edition ascending
  143. // Add $data as the last parameter, to sort by the common key
  144. array_multisort($at, SORT_DESC, $this->data['games']);
  145. return $this->data['games'][0];
  146. break;
  147. case 'thisweek':
  148. // Obtain a list of columns
  149. foreach ($this->data['games'] as $key => $row)
  150. {
  151. $nm[$key] = $row['name'];
  152. $tw[$key] = $row['thisweek'];
  153. $at[$key] = $row['alltime'];
  154. $ic[$key] = $row['icon'];
  155. }
  156. // Sort the data with volume descending, edition ascending
  157. // Add $data as the last parameter, to sort by the common key
  158. array_multisort($tw, SORT_DESC, $this->data['games']);
  159. return $this->data['games'][0];
  160. break;
  161. }
  162. }
  163. // the functions below should be left alone, unless you know exactly what you're doing
  164. // these are used to download data and parse it into a usable format
  165. function XML_StartTag( $parser, $tag, $params )
  166. {
  167. if( $tag == "XFIRE" ) { return; }
  168. switch( $this->type )
  169. {
  170. case "screenshots":
  171. case "servers":
  172. case "friends":
  173. if( $tag != "SCREENSHOTS" && $tag != "SERVER" && $tag != "FRIEND" )
  174. {
  175. $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ] = strtolower( $tag );
  176. if( $params )
  177. {
  178. $no = $this->xml_storage[ $this->username ][ $this->type ][ "no" ];
  179. $ctag = $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ];
  180. foreach( $params as $k => $v )
  181. {
  182. $this->xml_storage[ $this->username ][ $this->type ][ $no ][ $ctag . "_" . strtolower( $k ) ] = $v;
  183. }
  184. }
  185. }
  186. break;
  187. case "gameplay":
  188. if( $tag == "GAME" )
  189. {
  190. $this->xml_storage[ $this->username ][ $this->type ][ "current_gid" ] = $params[ "ID" ];
  191. }
  192. else
  193. {
  194. $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ] = strtolower( $tag );
  195. }
  196. break;
  197. case "profile":
  198. case "gamerig":
  199. case "live":
  200. default: // will be for profile
  201. $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ] = strtolower( $tag );
  202. }
  203. }
  204. function XML_EndTag( $parser, $tag )
  205. {
  206. switch( $this->type )
  207. {
  208. case "screenshots":
  209. case "servers":
  210. case "friends":
  211. if( $tag == "SCREENSHOT" || $tag == "SERVER" || $tag == "FRIEND" )
  212. {
  213. $this->xml_storage[ $this->username ][ $this->type ][ "no" ] = $this->xml_storage[ $this->username ][ $this->type ][ "no" ]+1;
  214. }
  215. break;
  216. default: // nothing
  217. }
  218. }
  219. function XML_CharData( $parser, $data )
  220. {
  221. $data = str_replace( array( "\t", "\n" ), "", $data );
  222. if( ( empty( $data ) || $data == "" || strlen( $data ) < 1 ) && !is_numeric( $data ) ) { return; }
  223. switch( $this->type )
  224. {
  225. case "screenshots":
  226. case "servers":
  227. case "friends":
  228. $no = $this->xml_storage[ $this->username ][ $this->type ][ "no" ];
  229. $ctag = $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ];
  230. $this->xml_storage[ $this->username ][ $this->type ][ $no ][ $ctag ] .= $data;
  231. break;
  232. case "gameplay":
  233. $gid = $this->xml_storage[ $this->username ][ $this->type ][ "current_gid" ];
  234. $ctag = $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ];
  235. $this->xml_storage[ $this->username ][ $this->type ][ $gid ][ $ctag ] .= $data;
  236. break;
  237. case "profile":
  238. case "gamerig":
  239. case "live":
  240. default: // will be for profile
  241. $this->xml_storage[ $this->username ][ $this->type ][ $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ] ] .= $data;
  242. }
  243. }
  244. /**
  245. * Downloads a fresh copy of an XML feed from the Xfire servers
  246. *
  247. * @param string $type
  248. */
  249. function DownloadFeed( $type )
  250. {
  251. $fp = curl_init(); // we use curl for this
  252. $download_url = "http://www.xfire.com/xml/" . $this->username . "/". $type . "/"; // construct the download url
  253. curl_setopt( $fp, CURLOPT_URL, $download_url ); // set the url
  254. curl_setopt( $fp, CURLOPT_HEADER, 0 ); // do not include the header in the downloaded content
  255. curl_setopt( $fp, CURLOPT_RETURNTRANSFER, true ); // yes....
  256. curl_setopt( $fp, CURLOPT_USERAGENT, XFIREXML_USERAGENT ); // we need this so we don't get firewalled
  257. $xml = curl_exec( $fp ); // get what we need
  258. curl_close( $fp ); // close the handle
  259. if( $h = fopen( "cache/" . $this->username . "_" . $type . ".xml" , "w" ) ) // can we write?
  260. {
  261. fwrite( $h, $xml ); // yes we can
  262. fclose( $h );
  263. }
  264. else
  265. {
  266. die( "Unable to create file handle. Make sure the file is writable." ); // no we can't
  267. }
  268. }
  269. /**
  270. * Parses a specific feed, you can call this directly, or use some of the "helper" functions above. Feed types: [profile, gameplay, servers, gamerig, friends, screenshots, live]
  271. *
  272. * @param string $type
  273. * @return array page / false if error
  274. */
  275. function ParseFeed( $type )
  276. {
  277. if( empty( $this->xml_storage[ $this->username ][ $type ] ) )
  278. {
  279. // we're using XML Caching
  280. $timenow = date( "U" ); // time now
  281. if( file_exists( "cache/" . $this->username . "_" . $type . ".xml" ) == true ) // check file exists.
  282. {
  283. if( $timenow > ( filemtime( "cache/" . $this->username . "_" . $type . ".xml" ) + ( XFIREXML_KEEPTIME * 60 ) ) ) // new download x minutes
  284. {
  285. $this->DownloadFeed( $type ); // we need to download
  286. }
  287. }
  288. else
  289. {
  290. $this->DownloadFeed( $type ); // yeah we really need to download
  291. }
  292. $this->type = $type;
  293. $this->xml_storage[ $this->username ][ $this->type ][ "no" ] = 0;
  294. $this->read_xml = xml_parser_create();
  295. xml_set_object( $this->read_xml, $this );
  296. xml_set_element_handler( $this->read_xml, "XML_StartTag", "XML_EndTag" );
  297. xml_set_character_data_handler( $this->read_xml, "XML_CharData" );
  298. xml_parse( $this->read_xml, file_get_contents( "cache/" . $this->username . "_" . $type . ".xml" ) );
  299. xml_parser_free( $this->read_xml );
  300. }
  301. unset( $this->xml_storage[ $this->username ][ $this->type ][ "no" ] );
  302. unset( $this->xml_storage[ $this->username ][ $this->type ][ "current_tag" ] );
  303. unset( $this->xml_storage[ $this->username ][ $this->type ][ "current_gid" ] );
  304. return $this->xml_storage[ $this->username ][ $this->type ];
  305. }
  306. }
  307. // LEGACY FUNCTIONS BELOW, FOR USE WITH OLD SCRIPTS (WITH MINOR CHANGES)
  308. /**
  309. * Gets the user's Xfire data
  310. *
  311. * @param string $username
  312. * @return array User's data / false if failed
  313. */
  314. function xfire_getmain( $username )
  315. {
  316. global $x;
  317. if( !$x )
  318. {
  319. $x = new xfire_data;
  320. }
  321. $x->SetUsername( $username );
  322. $x->SetCompatibilityMode( false );
  323. return $x->load();
  324. }
  325. /**
  326. * Gets the user's Xfire friends
  327. *
  328. * @param string $username
  329. * @return array User's data / false if failed
  330. */
  331. function xfire_getfriends( $username )
  332. {
  333. global $x;
  334. if( !$x )
  335. {
  336. $x = new xfire_data;
  337. }
  338. $x->SetUsername( $username );
  339. $x->SetCompatibilityMode( false );
  340. return $x->GetFriends();
  341. }
  342. /**
  343. * Gets the user's Xfire screen shots
  344. *
  345. * @param string $username
  346. * @return array User's data / false if failed
  347. */
  348. function xfire_getscreens( $username )
  349. {
  350. global $x;
  351. if( !$x )
  352. {
  353. $x = new xfire_data;
  354. }
  355. $x->SetUsername( $username );
  356. $x->SetCompatibilityMode( false );
  357. return $x->GetScreenshots();
  358. }
  359. ?>