From 3f02750cfb5051373181d1a0ec84018ffb6d5113 Mon Sep 17 00:00:00 2001 From: 9iky6 Date: Sat, 7 Apr 2018 23:50:56 +0500 Subject: [PATCH 1/6] Update project to composer use. --- composer.json | 24 + .../gameme_api_currentplayers.php | 102 +- .../gameme_api_currentservers.php | 82 +- sdk/gameme_api_sdk.php | 1564 ----------------- sdk/gameme_api_test.php | 319 ---- src/Exception/gameMEAPI_Exception.php | 13 + src/gameMEAPI.php | 309 ++++ src/gameMECache.php | 109 ++ src/gameMEClientAPI.php | 229 +++ src/gameMEGlobalAPI.php | 169 ++ src/gameMEXMLParser.php | 521 ++++++ 11 files changed, 1466 insertions(+), 1975 deletions(-) create mode 100644 composer.json rename {sdk/examples => examples}/gameme_api_currentplayers.php (97%) rename {sdk/examples => examples}/gameme_api_currentservers.php (97%) delete mode 100644 sdk/gameme_api_sdk.php delete mode 100644 sdk/gameme_api_test.php create mode 100644 src/Exception/gameMEAPI_Exception.php create mode 100644 src/gameMEAPI.php create mode 100644 src/gameMECache.php create mode 100644 src/gameMEClientAPI.php create mode 100644 src/gameMEGlobalAPI.php create mode 100644 src/gameMEXMLParser.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..935e45f --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "gamemedev/gamemeapi", + "version": "dev-master", + "description": "", + "license": "GPL-2.0", + "authors": [ + { + "name": "TTS Oetzel", + "email": "dev@gameme.com" + }, + { + "name": "Goerz GmbH", + "email": "dev@ameme.com" + } + ], + "require":{ + "php": ">=5.3" + }, + "autoload": { + "psr-4": { + "GameMEAPI\\": "src/" + } + } +} diff --git a/sdk/examples/gameme_api_currentplayers.php b/examples/gameme_api_currentplayers.php similarity index 97% rename from sdk/examples/gameme_api_currentplayers.php rename to examples/gameme_api_currentplayers.php index a32ca52..9516af2 100644 --- a/sdk/examples/gameme_api_currentplayers.php +++ b/examples/gameme_api_currentplayers.php @@ -1,52 +1,52 @@ -client_api_serverlist(GAMEME_FILTER_NONE); - foreach($server_list['serverlist'] as $server) { - $current_slots += $server['act']; - $current_bots += $server['bots']; - $maximum_slots += $server['max']; - $total_servers++; - } - - } catch (Exception $e) { - die ("Client API Serverlist Error: ".$e->getMessage()."\n"); - } - - print "Currently ".$current_slots."/".$maximum_slots." players (".$current_bots." bots) on ".$total_servers." gameservers\n"; - - +client_api_serverlist(GAMEME_FILTER_NONE); + foreach($server_list['serverlist'] as $server) { + $current_slots += $server['act']; + $current_bots += $server['bots']; + $maximum_slots += $server['max']; + $total_servers++; + } + + } catch (Exception $e) { + die ("Client API Serverlist Error: ".$e->getMessage()."\n"); + } + + print "Currently ".$current_slots."/".$maximum_slots." players (".$current_bots." bots) on ".$total_servers." gameservers\n"; + + ?> \ No newline at end of file diff --git a/sdk/examples/gameme_api_currentservers.php b/examples/gameme_api_currentservers.php similarity index 97% rename from sdk/examples/gameme_api_currentservers.php rename to examples/gameme_api_currentservers.php index 676aebf..d388989 100644 --- a/sdk/examples/gameme_api_currentservers.php +++ b/examples/gameme_api_currentservers.php @@ -1,42 +1,42 @@ -client_api_serverlist(GAMEME_FILTER_NONE); - foreach($server_list['serverlist'] as $server) { - echo $server['name']." ".$server['act']."/".$server['max']." - ".$server['map']."\n"; - } - - } catch (Exception $e) { - die ("Client API Serverlist Error: ".$e->getMessage()."\n"); - } - +client_api_serverlist(GAMEME_FILTER_NONE); + foreach($server_list['serverlist'] as $server) { + echo $server['name']." ".$server['act']."/".$server['max']." - ".$server['map']."\n"; + } + + } catch (Exception $e) { + die ("Client API Serverlist Error: ".$e->getMessage()."\n"); + } + ?> \ No newline at end of file diff --git a/sdk/gameme_api_sdk.php b/sdk/gameme_api_sdk.php deleted file mode 100644 index f9b51ea..0000000 --- a/sdk/gameme_api_sdk.php +++ /dev/null @@ -1,1564 +0,0 @@ -client_api_url = $client_api_url; - } - $this->global_api_url = $global_api_url; - } - - - /** - * Client API wrapper - */ - - private function check_client_api_object() { - if (!$this->client_api_object) { - $this->client_api_object = new gameMEClientAPI($this->client_api_url); - } - } - - - public function client_api_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_client_api_object(); - return $this->client_api_object->get_serverlist($filter, $filter_value, $limit, $sort_column, $hash_key); - } - - - public function client_api_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_client_api_object(); - return $this->client_api_object->get_serverinfo($address, $data, $hash_key); - } - - - public function client_api_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_client_api_object(); - return $this->client_api_object->get_playerlist($ranking, $filter, $filter_value, $limit, $sort_column, $hash_key); - } - - - public function client_api_full_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_client_api_object(); - return $this->client_api_object->get_full_playerlist($ranking, $filter, $filter_value, $sort_column, $hash_key); - } - - - public function client_api_playerinfo($ranking, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_client_api_object(); - return $this->client_api_object->get_playerinfo($ranking, $uniqueid, $data, $hash_key); - } - - - public function client_api_voiceserver_status() { - $this->check_client_api_object(); - return $this->client_api_object->get_voiceserver_status(); - } - - - public function client_api_awards($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { - $this->check_client_api_object(); - return $this->client_api_object->get_awards($ranking, $filter, $filter_value); - } - - - public function client_api_ribbons($ranking) { - $this->check_client_api_object(); - return $this->client_api_object->get_ribbons($ranking); - } - - - public function client_api_ribboninfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { - $this->check_client_api_object(); - return $this->client_api_object->get_ribboninfo($ranking, $filter, $filter_value); - } - - - public function client_api_ranks($ranking) { - $this->check_client_api_object(); - return $this->client_api_object->get_ranks($ranking); - } - - - public function client_api_rankinfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { - $this->check_client_api_object(); - return $this->client_api_object->get_rankinfo($ranking, $filter, $filter_value); - } - - - /** - * Global API wrapper - */ - - private function check_global_api_object() { - if (!$this->global_api_object) { - $this->global_api_object = new gameMEGlobalAPI($this->global_api_url); - } - } - - - public function global_api_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_global_api_object(); - return $this->global_api_object->get_serverlist($filter, $filter_value, $limit, $sort_column, $hash_key); - } - - - public function global_api_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_global_api_object(); - return $this->global_api_object->get_serverinfo($address, $data, $hash_key); - } - - - public function global_api_playerlist($game, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_global_api_object(); - return $this->global_api_object->get_playerlist($game, $filter, $filter_value, $limit, $sort_column, $hash_key); - } - - - public function global_api_playerinfo($game, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - $this->check_global_api_object(); - return $this->global_api_object->get_playerinfo($game, $uniqueid, $data, $hash_key); - } - - } - - - /** - * Class to access global gameME Stats API - * Docs: http://www.gameme.com/docs/api/globalapi - */ - class gameMEGlobalAPI { - - /** - * Global API url - */ - private $url; - - /** - * Allowed global API games - */ - private $game_filters = array( - GAMEME_GAME_CSGO => "csgo", - GAMEME_GAME_CSS => "css", - GAMEME_GAME_TF2 => "tf2", - GAMEME_GAME_DODS => "dods", - GAMEME_GAME_MOH => "moh", - GAMEME_GAME_BFBC2 => "bcii", - GAMEME_GAME_L4D2 => "l4dii", - GAMEME_GAME_CS16 => "cs16", - GAMEME_GAME_DOD13 => "dod13", - GAMEME_GAME_HL2MP => "hl2mp", - GAMEME_GAME_CZERO => "czero", - GAMEME_GAME_INSMOD => "insmod", - GAMEME_GAME_L4D => "l4d", - GAMEME_GAME_TFC => "tfc", - GAMEME_GAME_HL2CTF => "hl2ctf" - ); - - /** - * Allowed serverlist filters - */ - private $serverlist_filters = array( - GAMEME_FILTER_NAME => "name", - GAMEME_FILTER_MAP => "map", - GAMEME_FILTER_ADDRESS => "address", - GAMEME_FILTER_GAME => "game", - GAMEME_FILTER_COUNTRY => "country", - GAMEME_FILTER_CITY => "city" - ); - - /** - * Allowed serverinfo filters - */ - private $serverinfo_filters = array( - ); - - /** - * Allowed serverinfo packet data - */ - private $serverinfo_packet_data = array( - GAMEME_DATA_STATUS => "status", - GAMEME_DATA_PLAYERS => "players" - ); - - /** - * Allowed playerlist filters - */ - private $playerlist_filters = array( - GAMEME_FILTER_UNIQUEID => "uniqueid", - GAMEME_FILTER_NAME => "name", - GAMEME_FILTER_COUNTRY => "country", - GAMEME_FILTER_ONLINE => "online" - ); - - /** - * Allowed playerinfo filters - */ - private $playerinfo_filters = array( - ); - - /** - * Allowed playerinfo packet data - */ - private $playerinfo_packet_data = array( - GAMEME_DATA_STATUS => "status", - ); - - - /** - * Default cache times - */ - private $cache_times = array( - "serverlist" => 60, - "serverinfo" => 60, - "playerlist" => 300, - "playerinfo" => 300 - ); - - - - function gameMEGlobalAPI($url = GAMEME_GLOBALAPI_URL) { - $this->url = $url; - } - - - public function get_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - return $this->get_result("serverlist", $this->serverlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['serverlist']); - } - - - public function get_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - if ($address == "") { - throw new gameMEAPI_Exception("No valid address given: ".$address); - } - if (is_array($address)) { - $address = join("/", $address); - } - if (($data > GAMEME_DATA_DEFAULT) && (isset($this->serverinfo_packet_data[$data]))) { - return $this->get_result("serverinfo/".$address."/".$this->serverinfo_packet_data[$data], $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); - } else { - return $this->get_result("serverinfo/".$address, $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); - } - } - - - public function get_playerlist($game, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - if (!isset($this->game_filters[$game])) { - throw new gameMEAPI_Exception("No valid game given: ".$game); - } - return $this->get_result("playerlist/".$this->game_filters[$game], $this->playerlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['playerlist']); - } - - - public function get_playerinfo($game, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - if (!isset($this->game_filters[$game])) { - throw new gameMEAPI_Exception("No valid game given: ".$game); - } - if (is_array($uniqueid)) { - $uniqueid = join("/", $uniqueid); - } - if ($uniqueid == "") { - throw new gameMEAPI_Exception("No unique-id given"); - } - if (($data > GAMEME_DATA_DEFAULT) && (isset($this->playerinfo_packet_data[$data]))) { - return $this->get_result("playerinfo/".$this->game_filters[$game]."/".$uniqueid."/".$this->playerinfo_packet_data[$data], $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); - } else { - return $this->get_result("playerinfo/".$this->game_filters[$game]."/".$uniqueid, $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); - } - } - - - private function get_result($command, $filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $cache_time = 0, $pagination = array()) { - $content_url = ""; - if (($filter > GAMEME_FILTER_NONE) && (isset($filters[$filter])) && ($filter_value != "")) { - if ($filters[$filter] == "online") { - $filter_value = 1; - } - $content_url = $this->url."/".$command."/".$filters[$filter]."/".$filter_value."/?limit=".$limit; - } else { - $content_url = $this->url."/".$command."/?limit=".$limit; - } - - if ((GAMEME_APICACHE_ENABLED == 1) && ($cache_time > 0)) { - $cache_identifier = md5($command.$filters.$filter.$filter_value.$limit.$sort_column.$hash_key); - $content_cache = new gameMECache(); - if ($content_cache->is_hit($cache_identifier, $cache_time)) { - $xml_parser_result = $content_cache->get($cache_identifier); - return $xml_parser_result; - } else { - $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key); - $content_cache->add($cache_identifier, $xml_parser->result); - } - } else { - $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key); - } - return $xml_parser->result; - } - - } - - - - /** - * Class to access client gameME Stats API - * Docs: http://www.gameme.com/docs/api/clientapi - */ - - class gameMEClientAPI { - - /** - * Client API url - */ - private $url; - - /** - * Allowed serverlist filters - */ - private $serverlist_filters = array( - GAMEME_FILTER_NAME => "name", - GAMEME_FILTER_MAP => "map", - GAMEME_FILTER_ADDRESS => "address", - GAMEME_FILTER_GAME => "game", - GAMEME_FILTER_COUNTRY => "country" - ); - - /** - * Allowed serverinfo filters - */ - private $serverinfo_filters = array( - ); - - /** - * Allowed serverinfo packet data - */ - private $serverinfo_packet_data = array( - GAMEME_DATA_STATUS => "status", - GAMEME_DATA_PLAYERS => "players" - ); - - /** - * Allowed playerlist filters - */ - private $playerlist_filters = array( - GAMEME_FILTER_UNIQUEID => "uniqueid", - GAMEME_FILTER_NAME => "name", - GAMEME_FILTER_COUNTRY => "country", - GAMEME_FILTER_ONLINE => "online" - ); - - /** - * Allowed playerinfo filters - */ - private $playerinfo_filters = array( - ); - - - /** - * Allowed awards filters - */ - private $awards_filters = array( - GAMEME_FILTER_DATE => "date" - ); - - /** - * Allowed ribboninfo filters - */ - private $ribboninfo_filters = array( - GAMEME_FILTER_RIBBONCODE => "code" - ); - - /** - * Allowed rankinfo filters - */ - private $rankinfo_filters = array( - GAMEME_FILTER_RANKID => "id" - ); - - - /** - * Allowed playerinfo packet data - */ - private $playerinfo_packet_data = array( - GAMEME_DATA_STATUS => "status", - GAMEME_DATA_ALIASES => "aliases", - GAMEME_DATA_AWARDS => "awards", - GAMEME_DATA_MAPS => "maps", - GAMEME_DATA_ROLES => "roles", - GAMEME_DATA_TARGETS => "targets", - GAMEME_DATA_TEAMS => "teams", - GAMEME_DATA_WEAPONS => "weapons", - GAMEME_DATA_EXTENDED => "extended", - ); - - - /** - * Default cache times - */ - private $cache_times = array( - "serverlist" => 60, - "serverinfo" => 60, - "playerlist" => 300, - "playerinfo" => 300, - "voiceserver" => 300, - "awards" => 600, - "ribbons" => 600, - "ribboninfo" => 600, - "ranks" => 600, - "rankinfo" => 600 - ); - - - function gameMEClientAPI($client_api_url) { - $this->url = $client_api_url; - } - - - public function get_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - return $this->get_result("serverlist", $this->serverlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['serverlist']); - } - - - public function get_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - if ($address == "") { - throw new gameMEAPI_Exception("No valid address given: ".$address); - } - if (is_array($address)) { - $address = join("/", $address); - } - if (($data > GAMEME_DATA_DEFAULT) && (isset($this->serverinfo_packet_data[$data]))) { - return $this->get_result("serverinfo/".$address."/".$this->serverinfo_packet_data[$data], $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); - } else { - return $this->get_result("serverinfo/".$address, $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); - } - } - - - public function get_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - return $this->get_result("playerlist/".$ranking, $this->playerlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['playerinfo']); - } - - - public function get_full_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - return $this->get_result("playerlist/".$ranking, $this->playerlist_filters, $filter, $filter_value, GAMEME_CLIENTAPI_MAXIMUM_LIMIT, $sort_column, $hash_key, $this->cache_times['playerlist'], array("playerlist", "player")); - } - - - public function get_playerinfo($ranking, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - if (is_array($uniqueid)) { - $uniqueid = join("/", $uniqueid); - } - if ($uniqueid == "") { - throw new gameMEAPI_Exception("No unique-id given"); - } - if (($data > GAMEME_DATA_DEFAULT) && (isset($this->playerinfo_packet_data[$data]))) { - return $this->get_result("playerinfo/".$ranking."/".$uniqueid."/".$this->playerinfo_packet_data[$data], $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); - } else { - return $this->get_result("playerinfo/".$ranking."/".$uniqueid, $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); - } - } - - - public function get_voiceserver_status() { - return $this->get_result("voiceserver", "", GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['voiceserver']); - } - - - public function get_awards($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - return $this->get_result("awards/".$ranking, $this->awards_filters, $filter, $filter_value, GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['awards']); - } - - - public function get_ribbons($ranking) { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - return $this->get_result("ribbons/".$ranking, "", GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['ribbons']); - } - - - public function get_ribboninfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - return $this->get_result("ribboninfo/".$ranking, $this->ribboninfo_filters, $filter, $filter_value, GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['ribboninfo']); - } - - - public function get_ranks($ranking) { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - return $this->get_result("ranks/".$ranking, "", GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['ranks']); - } - - - public function get_rankinfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { - if ($ranking == "") { - throw new gameMEAPI_Exception("No valid ranking given"); - } - return $this->get_result("rankinfo/".$ranking, $this->rankinfo_filters, $filter, $filter_value, GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['rankinfo']); - } - - - private function get_result($command, $filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $cache_time = 0, $pagination = array()) { - $content_url = ""; - if (($filter > GAMEME_FILTER_NONE) && (isset($filters[$filter])) && ($filter_value != "")) { - if ($filters[$filter] == "online") { - $filter_value = 1; - } - $content_url = $this->url."/".$command."/".$filters[$filter]."/".$filter_value."/?limit=".$limit; - } else { - $content_url = $this->url."/".$command."/?limit=".$limit; - } - - if ((GAMEME_APICACHE_ENABLED == 1) && ($cache_time > 0)) { - $cache_identifier = md5($command.$filters.$filter.$filter_value.$limit.$sort_column.$hash_key.$pagination); - $content_cache = new gameMECache(); - if ($content_cache->is_hit($cache_identifier, $cache_time)) { - $xml_parser_result = $content_cache->get($cache_identifier); - return $xml_parser_result; - } else { - $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key, $pagination); - $content_cache->add($cache_identifier, $xml_parser->result); - } - } else { - $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key, $pagination); - } - return $xml_parser->result; - } - - } - - - - /** - * Class to parse xml - */ - - class gameMEXMLParser { - - /** - * Private xml content - */ - private $xml; - - /** - * Private parsed xml content - */ - private $parsed_xml; - - /** - * Parser result - */ - public $result = array(); - - /** - * Available sort columns - */ - private $sort_columns = array( - GAMEME_SORT_NAME_ASC => array( - "data" => "name", - "type" => "asc" - ), - GAMEME_SORT_NAME_DESC => array( - "data" => "name", - "type" => "desc" - ), - GAMEME_SORT_ADDRESS_ASC => array( - "data" => "address", - "type" => "asc" - ), - GAMEME_SORT_ADDRESS_DESC => array( - "data" => "address", - "type" => "desc" - ), - GAMEME_SORT_GAME_ASC => array( - "data" => "game", - "type" => "asc" - ), - GAMEME_SORT_GAME_DESC => array( - "data" => "game", - "type" => "desc" - ), - GAMEME_SORT_MAP_ASC => array( - "data" => "map", - "type" => "asc" - ), - GAMEME_SORT_MAP_DESC => array( - "data" => "map", - "type" => "desc" - ), - GAMEME_SORT_SLOTS_ASC => array( - "data" => "act", - "type" => "asc" - ), - GAMEME_SORT_SLOTS_DESC => array( - "data" => "act", - "type" => "desc" - ), - GAMEME_SORT_POS_ASC => array( - "data" => "rank", - "type" => "asc" - ), - GAMEME_SORT_POS_DESC => array( - "data" => "rank", - "type" => "desc" - ), - GAMEME_SORT_SKILL_ASC => array( - "data" => "skill", - "type" => "asc" - ), - GAMEME_SORT_SKILL_DESC => array( - "data" => "skill", - "type" => "desc" - ), - GAMEME_SORT_KILLS_ASC => array( - "data" => "kills", - "type" => "asc" - ), - GAMEME_SORT_KILLS_DESC => array( - "data" => "kills", - "type" => "desc" - ), - GAMEME_SORT_DEATHS_ASC => array( - "data" => "deaths", - "type" => "asc" - ), - GAMEME_SORT_DEATHS_DESC => array( - "data" => "deaths", - "type" => "desc" - ), - GAMEME_SORT_HEADSHOTS_ASC => array( - "data" => "hs", - "type" => "asc" - ), - GAMEME_SORT_HEADSHOTS_DESC => array( - "data" => "hs", - "type" => "desc" - ), - GAMEME_SORT_ASSISTS_ASC => array( - "data" => "assists", - "type" => "asc" - ), - GAMEME_SORT_ASSISTS_DESC => array( - "data" => "assists", - "type" => "desc" - ), - GAMEME_SORT_WEAPON_ASC => array( - "data" => "weapon", - "type" => "asc" - ), - GAMEME_SORT_WEAPON_DESC => array( - "data" => "weapon", - "type" => "desc" - ), - GAMEME_SORT_ACTIVITY_ASC => array( - "data" => "activity", - "type" => "asc" - ), - GAMEME_SORT_ACTIVITY_DESC => array( - "data" => "activity", - "type" => "desc" - ), - GAMEME_SORT_TIME_ASC => array( - "data" => "time", - "type" => "asc" - ), - GAMEME_SORT_TIME_DESC => array( - "data" => "time", - "type" => "desc" - ), - GAMEME_SORT_SUICIDES_ASC => array( - "data" => "suicides", - "type" => "asc" - ), - GAMEME_SORT_SUICIDES_DESC => array( - "data" => "suicides", - "type" => "desc" - ), - GAMEME_SORT_SHOTS_ASC => array( - "data" => "shots", - "type" => "asc" - ), - GAMEME_SORT_SHOTS_DESC => array( - "data" => "shots", - "type" => "desc" - ), - GAMEME_SORT_HITS_ASC => array( - "data" => "hits", - "type" => "asc" - ), - GAMEME_SORT_HITS_DESC => array( - "data" => "hits", - "type" => "desc" - ), - GAMEME_SORT_KILLSTREAK_ASC => array( - "data" => "killstreak", - "type" => "asc" - ), - GAMEME_SORT_KILLSTREAK_DESC => array( - "data" => "killstreak", - "type" => "desc" - ), - GAMEME_SORT_DEATHSTREAK_ASC => array( - "data" => "deathstreak", - "type" => "asc" - ), - GAMEME_SORT_DEATHSTREAK_DESC => array( - "data" => "deathstreak", - "type" => "desc" - ), - GAMEME_SORT_ASSISTED_ASC => array( - "data" => "assisted", - "type" => "asc" - ), - GAMEME_SORT_ASSISTED_DESC => array( - "data" => "assisted", - "type" => "desc" - ), - GAMEME_SORT_TEAMKILLS_ASC => array( - "data" => "teamkills", - "type" => "asc" - ), - GAMEME_SORT_TEAMKILLS_DESC => array( - "data" => "teamkills", - "type" => "desc" - ), - GAMEME_SORT_TEAMKILLED_ASC => array( - "data" => "teamkilled", - "type" => "asc" - ), - GAMEME_SORT_TEAMKILLED_DESC => array( - "data" => "teamkilled", - "type" => "desc" - ), - GAMEME_SORT_HEALEDPOINTS_ASC => array( - "data" => "healedpoints", - "type" => "asc" - ), - GAMEME_SORT_HEALEDPOINTS_DESC => array( - "data" => "healedpoints", - "type" => "desc" - ), - GAMEME_SORT_FLAGSCAPTURED_ASC => array( - "data" => "flagscaptured", - "type" => "asc" - ), - GAMEME_SORT_FLAGSCAPTURED_DESC => array( - "data" => "flagscaptured", - "type" => "desc" - ), - GAMEME_SORT_CUSTOMWINS_ASC => array( - "data" => "customwins", - "type" => "asc" - ), - GAMEME_SORT_CUSTOMWINS_DESC => array( - "data" => "customwins", - "type" => "desc" - ), - GAMEME_SORT_ROUNDS_ASC => array( - "data" => "rounds", - "type" => "asc" - ), - GAMEME_SORT_ROUNDS_DESC => array( - "data" => "rounds", - "type" => "desc" - ), - GAMEME_SORT_WINS_ASC => array( - "data" => "wins", - "type" => "asc" - ), - GAMEME_SORT_WINS_DESC => array( - "data" => "wins", - "type" => "desc" - ), - GAMEME_SORT_LOSSES_ASC => array( - "data" => "losses", - "type" => "asc" - ), - GAMEME_SORT_LOSSES_DESC => array( - "data" => "losses", - "type" => "desc" - ), - GAMEME_SORT_SURVIVED_ASC => array( - "data" => "survived", - "type" => "asc" - ), - GAMEME_SORT_SURVIVED_DESC => array( - "data" => "survived", - "type" => "desc" - ), - ); - - - /** - * Current sort column - */ - private $sort_column; - - - /** - * Available hash keys - */ - private $hash_keys = array( - GAMEME_HASH_ADDRESS => array( - "delimiter" => ":", - "data" => array( - "addr", - "port" - ) - ), - GAMEME_HASH_UNIQUEID => "uniqueid" - ); - - /** - * Private stream context for compressed transmission - */ - private $stream_context; - - - function gameMEXMLParser($url, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE, $pagination = array()) { - try { - $this->xml = $this->fetch_content($url); - if ($this->xml != "") { - - $this->parsed_xml = new SimpleXMLElement($this->xml, LIBXML_NOCDATA); - $this->result = $this->prepare_result($this->parsed_xml, $sort_column, $hash_key); - - // check errors - if (isset($this->result['error'])) { - if ((isset($this->result['error']['type'])) && (isset($this->result['error']['description']))) { - throw new gameMEAPI_Exception("An error occurs: ".$this->result['error']['description']." [".$this->result['error']['type']."]"); - } else { - throw new gameMEAPI_Exception("Unknown error occurs!"); - } - } - - // check pagination - if ((count($pagination) == 2) && ($pagination[0] != "") && ($pagination[1] != "")) { - $pagination_definition = $pagination[0]; - $pagination_result = $pagination[1]; - - if (isset($this->parsed_xml->$pagination_definition->pagination)) { - $next_page_link = $this->parsed_xml->$pagination_definition->pagination->nextpagelink; - while ($next_page_link != "") { - $next_page_xml = $this->fetch_content($next_page_link); - - if ($next_page_xml != "") { - $next_page_result = new SimpleXMLElement($next_page_xml, LIBXML_NOCDATA); - if (isset($next_page_result->$pagination_definition->pagination)) { - $next_page_link = $next_page_result->$pagination_definition->pagination->nextpagelink; - } else { - $next_page_link = ""; - } - $next_page_result = $this->prepare_result($next_page_result, $sort_column, $hash_key); - if ((isset($next_page_result[$pagination_definition])) && (is_array($next_page_result[$pagination_definition])) && (count($next_page_result[$pagination_definition]) > 0)) { - $this->result[$pagination_definition] = array_merge($this->result[$pagination_definition], $next_page_result[$pagination_definition]); - } - } else { - $next_page_link = ""; - throw new gameMEAPI_Exception("Cannot retrieve next page xml at url ".$next_page_link); - } - } - - // sort results - if (($sort_column > GAMEME_SORT_DEFAULT) && (isset($this->sort_columns[$sort_column]))) { - $this->sort_column = $this->sort_columns[$sort_column]['data']; - if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - uasort($this->result[$pagination_definition], array($this, 'compare_asc')); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - uasort($this->result[$pagination_definition], array($this, 'compare_desc')); - } - } else { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - usort($this->result[$pagination_definition], array($this, 'compare_asc')); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - usort($this->result[$pagination_definition], array($this, 'compare_desc')); - } - } - } - - } - } - } - } catch (Exception $e) { - throw new gameMEAPI_Exception("Cannot retrieve xml at url ".$url." [".$e->getMessage()."]"); - } - } - - private function fetch_content($url) { - if (GAMEME_FETCHDATA_COMPRESSED == 1) { - if (!$this->stream_context) { - $context_options = array( - 'http'=>array( - 'method' => "GET", - 'header' => "Accept-Encoding: gzip;\r\n" - ) - ); - $this->stream_context = stream_context_create($context_options); - if(!function_exists("gzdecode")) { - return $this->gzdecode(file_get_contents($url, false, $this->stream_context)); - - } else { - return gzdecode(file_get_contents($url, false, $this->stream_context)); - } - } - - } - return file_get_contents($url); - } - - private function compare_asc($a, $b) { - if ($a[$this->sort_column] == $b[$this->sort_column]) { - return 0; - } - return ($a[$this->sort_column] < $b[$this->sort_column]) ? -1 : 1; - } - - private function compare_desc($a, $b) { - if ($a[$this->sort_column] == $b[$this->sort_column]) { - return 0; - } - return ($a[$this->sort_column] > $b[$this->sort_column]) ? -1 : 1; - } - - - private function prepare_result($object, $sort_column, $hash_key, $level = 0) { - $result_items = array(); - - if (!is_object($object)) { - return $result_items; - } - - $child = (array)$object; - - if (sizeof($child) > 0) { - foreach($child as $key => $entry) { - if (is_array($entry)) { - foreach($entry as $entry_key => $entry_entry) { - if (!is_object($entry_entry)) { - $result_items[$entry_key] = $entry_entry; - } else { - if (get_class($entry_entry) == 'SimpleXMLElement') { - if (($level == 0) && (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key])))) { - $array_hash_key = ""; - if (is_array($this->hash_keys[$hash_key])) { - $array_hash_keys = array(); - foreach($this->hash_keys[$hash_key]['data'] as $data) { - if ((string)$entry_entry->$data != "") { - $array_hash_keys[] = (string)$entry_entry->$data; - } - } - $array_hash_key = join($this->hash_keys[$hash_key]['delimiter'], $array_hash_keys); - } else { - $array_hash_key = (string)$entry_entry->{$this->hash_keys[$hash_key]}; - } - if ($array_hash_key == "") { - $result_items[$entry_key] = $this->prepare_result($entry_entry, $sort_column, $hash_key, $level + 1); - } else { - $result_items[$array_hash_key] = $this->prepare_result($entry_entry, $sort_column, $hash_key, $level + 1); - } - } else { - $result_items[$entry_key] = $this->prepare_result($entry_entry, $sort_column, $hash_key, $level + 1); - } - } - } - } - - if (($sort_column > GAMEME_SORT_DEFAULT) && (isset($this->sort_columns[$sort_column]))) { - $this->sort_column = $this->sort_columns[$sort_column]['data']; - if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - uasort($result_items, array($this, 'compare_asc')); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - uasort($result_items, array($this, 'compare_desc')); - } - } else { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - usort($result_items, array($this, 'compare_asc')); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - usort($result_items, array($this, 'compare_desc')); - } - } - } - } else { - if (!is_object($entry)) { - $result_items[$key] = $entry; - } else { - if (get_class($entry) == 'SimpleXMLElement') { - if ($key == "pagination") { - continue; - } - $allowed_new_columns = array( - "vendor" => 1, - "software" => 1, - "account" => 1, - "error" => 1, - "globalinfo" => 1, - "rankinginfo" => 1, - "serverinfo" => 1, - "serverlist" => 1, - "playerlist" => 1, - "playerinfo" => 1, - "voiceserver" => 1, - "awards" => 1, - "ribbons" => 1, - "ribboninfo" => 1, - "ranks" => 1, - "rankinfo" => 1 - ); - if ((isset($allowed_new_columns[$key])) || ($level > 0)) { - $result_items[$key] = $this->prepare_result($entry, $sort_column, $hash_key); - } else { - if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { - $array_hash_key = ""; - if (is_array($this->hash_keys[$hash_key])) { - $array_hash_keys = array(); - foreach($this->hash_keys[$hash_key]['data'] as $data) { - if ((string)$entry->$data != "") { - $array_hash_keys[] = (string)$entry->$data; - } - } - $array_hash_key = join($this->hash_keys[$hash_key]['delimiter'], $array_hash_keys); - } else { - $array_hash_key = (string)$entry->{$this->hash_keys[$hash_key]}; - } - if ($array_hash_key == "") { - $result_items[] = $this->prepare_result($entry, $sort_column, $hash_key, $level + 1); - } else { - $result_items[$array_hash_key] = $this->prepare_result($entry, $sort_column, $hash_key, $level + 1); - } - } else { - $result_items[] = $this->prepare_result($entry, $sort_column, $hash_key, $level + 1); - } - } - } - } - } - } - } - - if (count($result_items) == 0) { - return ""; - } else { - return $result_items; - } - } - - /** - * gzdecode function by katzlbtjunk - * http://de.php.net/manual/en/function.gzdecode.php - */ - private function gzdecode($data,&$filename='',&$error='',$maxlength = null) { - - $len = strlen($data); - if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) { - $error = "Not in GZIP format."; - return null; // Not GZIP format (See RFC 1952) - } - $method = ord(substr($data,2,1)); // Compression method - $flags = ord(substr($data,3,1)); // Flags - if ($flags & 31 != $flags) { - $error = "Reserved bits not allowed."; - return null; - } - // NOTE: $mtime may be negative (PHP integer limitations) - $mtime = unpack("V", substr($data,4,4)); - $mtime = $mtime[1]; - $xfl = substr($data,8,1); - $os = substr($data,8,1); - $headerlen = 10; - $extralen = 0; - $extra = ""; - if ($flags & 4) { - // 2-byte length prefixed EXTRA data in header - if ($len - $headerlen - 2 < 8) { - return false; // invalid - } - $extralen = unpack("v",substr($data,8,2)); - $extralen = $extralen[1]; - if ($len - $headerlen - 2 - $extralen < 8) { - return false; // invalid - } - $extra = substr($data,10,$extralen); - $headerlen += 2 + $extralen; - } - $filenamelen = 0; - $filename = ""; - if ($flags & 8) { - // C-style string - if ($len - $headerlen - 1 < 8) { - return false; // invalid - } - $filenamelen = strpos(substr($data,$headerlen),chr(0)); - if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) { - return false; // invalid - } - $filename = substr($data,$headerlen,$filenamelen); - $headerlen += $filenamelen + 1; - } - $commentlen = 0; - $comment = ""; - if ($flags & 16) { - // C-style string COMMENT data in header - if ($len - $headerlen - 1 < 8) { - return false; // invalid - } - $commentlen = strpos(substr($data,$headerlen),chr(0)); - if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) { - return false; // Invalid header format - } - $comment = substr($data,$headerlen,$commentlen); - $headerlen += $commentlen + 1; - } - $headercrc = ""; - if ($flags & 2) { - // 2-bytes (lowest order) of CRC32 on header present - if ($len - $headerlen - 2 < 8) { - return false; // invalid - } - $calccrc = crc32(substr($data,0,$headerlen)) & 0xffff; - $headercrc = unpack("v", substr($data,$headerlen,2)); - $headercrc = $headercrc[1]; - if ($headercrc != $calccrc) { - $error = "Header checksum failed."; - return false; // Bad header CRC - } - $headerlen += 2; - } - // GZIP FOOTER - $datacrc = unpack("V",substr($data,-8,4)); - $datacrc = sprintf('%u',$datacrc[1] & 0xFFFFFFFF); - $isize = unpack("V",substr($data,-4)); - $isize = $isize[1]; - // decompression: - $bodylen = $len-$headerlen-8; - if ($bodylen < 1) { - // IMPLEMENTATION BUG! - return null; - } - $body = substr($data,$headerlen,$bodylen); - $data = ""; - if ($bodylen > 0) { - switch ($method) { - case 8: - // Currently the only supported compression method: - $data = gzinflate($body,$maxlength); - break; - default: - $error = "Unknown compression method."; - return false; - } - } // zero-byte body content is allowed - // Verifiy CRC32 - $crc = sprintf("%u",crc32($data)); - $crcOK = $crc == $datacrc; - $lenOK = $isize == strlen($data); - if (!$lenOK || !$crcOK) { - $error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.'); - return false; - } - return $data; - } - - } - - - /** - * Class handling caching - */ - - class gameMECache { - - /** - * Private cache directory - */ - private $prefix = "gameme_cache"; - - /** - * Private cache directory - */ - private $suffix = ".cache"; - - /** - * Private cache directory - */ - private $cache_dir = ""; - - - function gameMECache() { - - if (GAMEME_APICACHE_ENABLED == 1) { - if (GAMEME_APICACHE_DIRECTORY != "") { - $this->cache_dir = GAMEME_APICACHE_DIRECTORY; - } else { - if (!function_exists('sys_get_temp_dir')) { - function sys_get_temp_dir() { - if ($temp = getenv('TMP')) { - return $temp; - } - if ($temp = getenv('TEMP')) { - return $temp; - } - if ($temp = getenv('TMPDIR')) { - return $temp; - } - $temp = tempnam(__FILE__, ''); - if (file_exists($temp)) { - unlink($temp); - return dirname($temp); - } - return null; - } - } - $this->cache_dir = realpath(sys_get_temp_dir()); - } - - if ($this->cache_dir == "") { - throw new gameMEAPI_Exception("Cannot setup cache directory"); - } - } - } - - - function is_hit($identifier, $cache_time) { - if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "")) { - $cache_file = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; - if (file_exists($cache_file)) { - $file_timestamp = filemtime($cache_file); - if (($file_timestamp + $cache_time) >= time()) { - return true; - } else { - $this->remove($identifier); - return false; - } - } else { - return false; - } - } - return false; - } - - - function exists($identifier) { - $data = ""; - if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "")) { - $cache_file = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; - if (file_exists($cache_file)) { - $data = $this->get($identifier); - } - } - $this->cleanup(); - return $data; - } - - - function get($identifier) { - $data = ""; - if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "")) { - $cache_file_name = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; - $cache_file = fopen($cache_file_name, "r"); - $data = unserialize(fread($cache_file, GAMEME_APICACHE_MAX_SIZE_LIMIT)); - fclose($cache_file); - } - $this->cleanup(); - return $data; - } - - - function add($identifier, $data) { - if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "") && ($data != "")) { - $cache_file_name = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; - $cache_file = fopen($cache_file_name, "w+"); - fwrite($cache_file, serialize($data)); - fclose($cache_file); - return true; - } - return false; - } - - - function remove($identifier) { - if ((GAMEME_APICACHE_ENABLED == 1) && (GAMEME_APICACHE_AUTO_PRUNE == 1)) { - $cache_file = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; - if (file_exists($cache_file)) { - unlink($cache_file); - } - } - $this->cleanup(); - } - - - function cleanup($force = 0) { - if ((GAMEME_APICACHE_ENABLED == 1) && (GAMEME_APICACHE_AUTO_PRUNE == 1)) { - if ((rand(1, 400) == 100) && ($force == 0)) { - if (is_dir($this->cache_dir)) { - if ($directory_handle = opendir($this->cache_dir)) { - while (($file = readdir($directory_handle)) !== false) { - if (($file != ".") && ($file != "..") && (strpos($file, $this->prefix) !== false)) { - unlink($this->cache_dir."/".$file); - } - } - closedir($directory_handle); - } - } - } - } - } - - } - - - /** - * Init SDK - */ - - if ((!extension_loaded('SimpleXML')) && (!extension_loaded('simplexml'))) { - throw new gameMEAPI_Exception("Extension SimpleXML is not loaded."); - } -?> \ No newline at end of file diff --git a/sdk/gameme_api_test.php b/sdk/gameme_api_test.php deleted file mode 100644 index c008112..0000000 --- a/sdk/gameme_api_test.php +++ /dev/null @@ -1,319 +0,0 @@ -api_object = new gameMEAPI(""); - try { - $test_api = $this->api_object->global_api_serverlist(GAMEME_FILTER_NONE, "", 1); - $this->api_object->client_api_url = $test_api['serverlist'][0]['url']."/api"; - echo "Test-API: ".$this->api_object->client_api_url."\n"; - } catch (Exception $e) { - die ("Client API Serverlist Error: ".$e->getMessage()."\n"); - } - - - $this->check_client_serverlist(); - $this->check_client_serverinfo(); - $this->check_client_playerlist(); - // $this->check_client_full_playerlist(); - $this->check_client_playerinfo(); - - /* - * Disable advanced checks by default - * - $this->check_client_voiceserver_status(); - $this->check_client_awards(); - $this->check_client_ribbons(); - $this->check_client_ribboninfo(); - $this->check_client_ranks(); - $this->check_client_rankinfo(); - */ - - $this->check_global_serverlist(); - $this->check_global_serverinfo(); - $this->check_global_playerlist(); - $this->check_global_playerinfo(); - - print "[#TOT: ".stop_timing(3)."s] Tests completed\n"; - - } - - - /** - * Client API testing functions - * - */ - - function check_client_serverlist() { - try { - start_timing(); - $serverlist = $this->api_object->client_api_serverlist(GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_MULTIPLE_SERVERINFO_LIMIT, GAMEME_SORT_NAME_ASC, GAMEME_HASH_ADDRESS); - foreach ($serverlist['serverlist'] as $server_address => $server_data) { - $this->test_client_servers[] = $server_address; - if ($this->test_client_game == "") { - $this->test_client_game = $server_data['game']; - } - } - print "[#C1: ".stop_timing(3)."s] Test Client Serverlist: Found ".count($this->test_client_servers)." servers [".join(",", $this->test_client_servers)."]\n"; - } catch (Exception $e) { - die ("Client API Serverlist Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_serverinfo() { - try { - start_timing(); - $serverinfo = $this->api_object->client_api_serverinfo($this->test_client_servers[0]); - $serverinfo = $this->api_object->client_api_serverinfo($this->test_client_servers); - print "[#C2: ".stop_timing(3)."s] Test Client Serverinfo: Found ".count($serverinfo['serverinfo'])." servers\n"; - } catch (Exception $e) { - die ("Client API Serverinfo Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_playerlist() { - try { - start_timing(); - $playerlist = $this->api_object->client_api_playerlist($this->test_client_game, GAMEME_FILTER_NONE, "", 5, GAMEME_SORT_DEFAULT, GAMEME_HASH_UNIQUEID); - foreach ($playerlist['playerlist'] as $player_uniqueid => $player_data) { - $this->test_client_players[] = $player_uniqueid; - } - print "[#C3: ".stop_timing(3)."s] Test Client Playerlist: Found ".count($this->test_client_players)." players [".join(",", $this->test_client_players)."]\n"; - } catch (Exception $e) { - die ("Client API Playerlist Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_full_playerlist() { - try { - start_timing(); - $full_playerlist = $this->api_object->client_api_full_playerlist($this->test_client_game, GAMEME_FILTER_NONE, "", GAMEME_SORT_DEFAULT, GAMEME_HASH_UNIQUEID); - print "[#C4: ".stop_timing(3)."s] Test Client Full Playerlist: Found ".count($full_playerlist['playerlist'])." players\n"; - } catch (Exception $e) { - die ("Client API Playerlist Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_playerinfo() { - try { - start_timing(); - $playerinfo = $this->api_object->client_api_playerinfo($this->test_client_game, $this->test_client_players[0], GAMEME_DATA_DEFAULT, GAMEME_HASH_UNIQUEID); - $playerinfo = $this->api_object->client_api_playerinfo($this->test_client_game, $this->test_client_players, GAMEME_DATA_DEFAULT, GAMEME_HASH_UNIQUEID); - print "[#C5: ".stop_timing(3)."s] Test Client Serverinfo: Found ".count($playerinfo['playerinfo'])."/".count($this->test_client_players)." players\n"; - } catch (Exception $e) { - die ("Client API Playerinfo Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_voiceserver_status() { - try { - start_timing(); - $voiceserver_status = $this->api_object->client_api_voiceserver_status(); - if ((isset($voiceserver_status['voiceserver'])) && (is_array($voiceserver_status['voiceserver']))) { - print "[#C6: ".stop_timing(3)."s] Test Client Voiceserver Status: Found ".$voiceserver_status['voiceserver'][0]['clientscount']."/".$voiceserver_status['voiceserver'][0]['maxclients']." clients\n"; - } else { - print "[#C6: ".stop_timing(3)."s] Test Client Voiceserver Status: No voiceserver available\n"; - } - } catch (Exception $e) { - die ("Client API Vocieserver Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_awards() { - try { - start_timing(); - $awards = $this->api_object->client_api_awards($this->test_client_game); - $live_awards = $this->api_object->client_api_awards($this->test_client_game, GAMEME_FILTER_DATE, "live"); - print "[#C7: ".stop_timing(3)."s] Test Client Awards: Found ".count($awards['awards'])."x awards, ".count($live_awards['awards'])."x live-awards\n"; - } catch (Exception $e) { - die ("Client API Awards Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_ribbons() { - try { - start_timing(); - $ribbons = $this->api_object->client_api_ribbons($this->test_client_game); - print "[#C8: ".stop_timing(3)."s] Test Client Ribbons: Found ".count($ribbons['ribbons'])."x ribbons\n"; - } catch (Exception $e) { - die ("Client API Ribbons Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_ribboninfo() { - try { - start_timing(); - $ribboninfo = $this->api_object->client_api_ribboninfo($this->test_client_game, GAMEME_FILTER_RIBBONCODE, "longest_playtime"); - print "[#C9: ".stop_timing(3)."s] Test Client Ribboninfo: Found ".count($ribboninfo['ribboninfo'])."x players\n"; - } catch (Exception $e) { - die ("Client API Rankinfo Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_ranks() { - try { - start_timing(); - $ranks = $this->api_object->client_api_ranks($this->test_client_game); - print "[#C10: ".stop_timing(3)."s] Test Client Ranks: Found ".count($ranks['ranks'])."x ranks\n"; - } catch (Exception $e) { - die ("Client API Ranks Error: ".$e->getMessage()."\n"); - } - } - - - function check_client_rankinfo() { - try { - start_timing(); - $rankinfo = $this->api_object->client_api_rankinfo($this->test_client_game, GAMEME_FILTER_RANKID, 1); - print "[#C11: ".stop_timing(3)."s] Test Client Rankinfo: Found ".count($rankinfo['rankinfo'])."x players\n"; - } catch (Exception $e) { - die ("Client API Rankinfo Error: ".$e->getMessage()."\n"); - } - } - - - /** - * Global API testing functions - * - */ - - function check_global_serverlist() { - try { - start_timing(); - $serverlist = $this->api_object->global_api_serverlist(GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_MULTIPLE_SERVERINFO_LIMIT, GAMEME_SORT_DEFAULT, GAMEME_HASH_ADDRESS); - foreach ($serverlist['serverlist'] as $server_address => $server_data) { - $this->test_global_servers[] = $server_address; - } - print "[#G1: ".stop_timing(3)."s] Test Global Serverlist: Found ".count($this->test_global_servers)." servers [".join(",", $this->test_global_servers)."]\n"; - } catch (Exception $e) { - die ("Global API Serverlist Error: ".$e->getMessage()."\n"); - } - } - - - function check_global_serverinfo() { - try { - start_timing(); - $serverinfo = $this->api_object->global_api_serverinfo($this->test_global_servers[0]); - $serverinfo = $this->api_object->global_api_serverinfo($this->test_global_servers); - print "[#G2: ".stop_timing(3)."s] Test Global Serverinfo: Found ".count($serverinfo['serverinfo'])." servers\n"; - } catch (Exception $e) { - die ("Global API Serverinfo Error: ".$e->getMessage()."\n"); - } - } - - - function check_global_playerlist() { - try { - start_timing(); - $playerlist = $this->api_object->global_api_playerlist(GAMEME_GAME_CSS, GAMEME_FILTER_NONE, "", 5, GAMEME_SORT_DEFAULT, GAMEME_HASH_UNIQUEID); - foreach ($playerlist['playerlist'] as $player_uniqueid => $player_data) { - $this->test_global_players[] = $player_uniqueid; - } - print "[#G3: ".stop_timing(3)."s] Test Global Playerlist: Found ".count($this->test_global_players)." players [".join(",", $this->test_global_players)."]\n"; - - } catch (Exception $e) { - die ("Global API Playerlist Error: ".$e->getMessage()."\n"); - } - } - - - function check_global_playerinfo() { - try { - start_timing(); - $playerinfo = $this->api_object->global_api_playerinfo(GAMEME_GAME_CSS, $this->test_global_players[0], GAMEME_DATA_DEFAULT, GAMEME_HASH_UNIQUEID); - $playerinfo = $this->api_object->global_api_playerinfo(GAMEME_GAME_CSS, $this->test_global_players, GAMEME_DATA_DEFAULT, GAMEME_HASH_UNIQUEID); - print "[#G4: ".stop_timing(3)."s] Test Global Serverinfo: Found ".count($playerinfo['playerinfo'])."/".count($this->test_global_players)." players\n"; - } catch (Exception $e) { - die ("Global API Playerinfo Error: ".$e->getMessage()."\n"); - } - } - } - - /** - * Auto-Run test script (disabled by default) - */ - - // new TestgameMEAPI(); -?> \ No newline at end of file diff --git a/src/Exception/gameMEAPI_Exception.php b/src/Exception/gameMEAPI_Exception.php new file mode 100644 index 0000000..0961a91 --- /dev/null +++ b/src/Exception/gameMEAPI_Exception.php @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/src/gameMEAPI.php b/src/gameMEAPI.php new file mode 100644 index 0000000..23a0719 --- /dev/null +++ b/src/gameMEAPI.php @@ -0,0 +1,309 @@ +client_api_url = $client_api_url; + } + $this->global_api_url = $global_api_url; + } + + /** + * Client API wrapper + */ + private function check_client_api_object() { + if (!$this->client_api_object) { + $this->client_api_object = new gameMEClientAPI($this->client_api_url); + } + } + + public function client_api_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_client_api_object(); + return $this->client_api_object->get_serverlist($filter, $filter_value, $limit, $sort_column, $hash_key); + } + + public function client_api_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_client_api_object(); + return $this->client_api_object->get_serverinfo($address, $data, $hash_key); + } + + public function client_api_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_client_api_object(); + return $this->client_api_object->get_playerlist($ranking, $filter, $filter_value, $limit, $sort_column, $hash_key); + } + + public function client_api_full_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_client_api_object(); + return $this->client_api_object->get_full_playerlist($ranking, $filter, $filter_value, $sort_column, $hash_key); + } + + public function client_api_playerinfo($ranking, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_client_api_object(); + return $this->client_api_object->get_playerinfo($ranking, $uniqueid, $data, $hash_key); + } + + public function client_api_voiceserver_status() { + $this->check_client_api_object(); + return $this->client_api_object->get_voiceserver_status(); + } + + public function client_api_awards($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { + $this->check_client_api_object(); + return $this->client_api_object->get_awards($ranking, $filter, $filter_value); + } + + public function client_api_ribbons($ranking) { + $this->check_client_api_object(); + return $this->client_api_object->get_ribbons($ranking); + } + + public function client_api_ribboninfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { + $this->check_client_api_object(); + return $this->client_api_object->get_ribboninfo($ranking, $filter, $filter_value); + } + + public function client_api_ranks($ranking) { + $this->check_client_api_object(); + return $this->client_api_object->get_ranks($ranking); + } + + public function client_api_rankinfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { + $this->check_client_api_object(); + return $this->client_api_object->get_rankinfo($ranking, $filter, $filter_value); + } + + /** + * Global API wrapper + */ + private function check_global_api_object() { + if (!$this->global_api_object) { + $this->global_api_object = new gameMEGlobalAPI($this->global_api_url); + } + } + + public function global_api_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_global_api_object(); + return $this->global_api_object->get_serverlist($filter, $filter_value, $limit, $sort_column, $hash_key); + } + + public function global_api_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_global_api_object(); + return $this->global_api_object->get_serverinfo($address, $data, $hash_key); + } + + public function global_api_playerlist($game, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_global_api_object(); + return $this->global_api_object->get_playerlist($game, $filter, $filter_value, $limit, $sort_column, $hash_key); + } + + public function global_api_playerinfo($game, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + $this->check_global_api_object(); + return $this->global_api_object->get_playerinfo($game, $uniqueid, $data, $hash_key); + } +} +?> \ No newline at end of file diff --git a/src/gameMECache.php b/src/gameMECache.php new file mode 100644 index 0000000..37af386 --- /dev/null +++ b/src/gameMECache.php @@ -0,0 +1,109 @@ +cache_dir = GAMEME_APICACHE_DIRECTORY; + } else { + $this->cache_dir = realpath(sys_get_temp_dir()); + } + + if ($this->cache_dir == "") { + throw new gameMEAPI_Exception("Cannot setup cache directory"); + } + } + } + + function is_hit($identifier, $cache_time) { + if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "")) { + $cache_file = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; + if (file_exists($cache_file)) { + $file_timestamp = filemtime($cache_file); + if (($file_timestamp + $cache_time) >= time()) { + return true; + } else { + $this->remove($identifier); + return false; + } + } else { + return false; + } + } + return false; + } + + function exists($identifier) { + $data = ""; + if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "")) { + $cache_file = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; + if (file_exists($cache_file)) { + $data = $this->get($identifier); + } + } + $this->cleanup(); + return $data; + } + + function get($identifier) { + $data = ""; + if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "")) { + $cache_file_name = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; + $cache_file = fopen($cache_file_name, "r"); + $data = unserialize(fread($cache_file, GAMEME_APICACHE_MAX_SIZE_LIMIT)); + fclose($cache_file); + } + $this->cleanup(); + return $data; + } + + function add($identifier, $data) { + if ((GAMEME_APICACHE_ENABLED == 1) && ($identifier != "") && ($data != "")) { + $cache_file_name = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; + $cache_file = fopen($cache_file_name, "w+"); + fwrite($cache_file, serialize($data)); + fclose($cache_file); + return true; + } + return false; + } + + function remove($identifier) { + if ((GAMEME_APICACHE_ENABLED == 1) && (GAMEME_APICACHE_AUTO_PRUNE == 1)) { + $cache_file = $this->cache_dir."/".$this->prefix."_".$identifier.$this->suffix; + if (file_exists($cache_file)) { + unlink($cache_file); + } + } + $this->cleanup(); + } + + function cleanup($force = 0) { + if ((GAMEME_APICACHE_ENABLED == 1) && (GAMEME_APICACHE_AUTO_PRUNE == 1)) { + if ((rand(1, 400) == 100) && ($force == 0)) { + if (is_dir($this->cache_dir)) { + if ($directory_handle = opendir($this->cache_dir)) { + while (($file = readdir($directory_handle)) !== false) { + if (($file != ".") && ($file != "..") && (strpos($file, $this->prefix) !== false)) { + unlink($this->cache_dir."/".$file); + } + } + closedir($directory_handle); + } + } + } + } + } +} +?> \ No newline at end of file diff --git a/src/gameMEClientAPI.php b/src/gameMEClientAPI.php new file mode 100644 index 0000000..c8b6251 --- /dev/null +++ b/src/gameMEClientAPI.php @@ -0,0 +1,229 @@ + "name", + GAMEME_FILTER_MAP => "map", + GAMEME_FILTER_ADDRESS => "address", + GAMEME_FILTER_GAME => "game", + GAMEME_FILTER_COUNTRY => "country" + ]; + + /** + * Allowed serverinfo filters + */ + private $serverinfo_filters = []; + + /** + * Allowed serverinfo packet data + */ + private $serverinfo_packet_data = [ + GAMEME_DATA_STATUS => "status", + GAMEME_DATA_PLAYERS => "players" + ]; + + /** + * Allowed playerlist filters + */ + private $playerlist_filters = [ + GAMEME_FILTER_UNIQUEID => "uniqueid", + GAMEME_FILTER_NAME => "name", + GAMEME_FILTER_COUNTRY => "country", + GAMEME_FILTER_ONLINE => "online" + ]; + + /** + * Allowed playerinfo filters + */ + private $playerinfo_filters = []; + + + /** + * Allowed awards filters + */ + private $awards_filters = [ + GAMEME_FILTER_DATE => "date" + ]; + + /** + * Allowed ribboninfo filters + */ + private $ribboninfo_filters = [ + GAMEME_FILTER_RIBBONCODE => "code" + ]; + + /** + * Allowed rankinfo filters + */ + private $rankinfo_filters = [ + GAMEME_FILTER_RANKID => "id" + ]; + + /** + * Allowed playerinfo packet data + */ + private $playerinfo_packet_data = [ + GAMEME_DATA_STATUS => "status", + GAMEME_DATA_ALIASES => "aliases", + GAMEME_DATA_AWARDS => "awards", + GAMEME_DATA_MAPS => "maps", + GAMEME_DATA_ROLES => "roles", + GAMEME_DATA_TARGETS => "targets", + GAMEME_DATA_TEAMS => "teams", + GAMEME_DATA_WEAPONS => "weapons", + GAMEME_DATA_EXTENDED => "extended", + ]; + + /** + * Default cache times + */ + private $cache_times = [ + "serverlist" => 60, + "serverinfo" => 60, + "playerlist" => 300, + "playerinfo" => 300, + "voiceserver" => 300, + "awards" => 600, + "ribbons" => 600, + "ribboninfo" => 600, + "ranks" => 600, + "rankinfo" => 600 + ]; + + function __construct($client_api_url) { + $this->url = $client_api_url; + } + + public function get_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + return $this->get_result("serverlist", $this->serverlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['serverlist']); + } + + public function get_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + if ($address == "") { + throw new gameMEAPI_Exception("No valid address given: ".$address); + } + if (is_array($address)) { + $address = join("/", $address); + } + if (($data > GAMEME_DATA_DEFAULT) && (isset($this->serverinfo_packet_data[$data]))) { + return $this->get_result("serverinfo/".$address."/".$this->serverinfo_packet_data[$data], $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); + } else { + return $this->get_result("serverinfo/".$address, $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); + } + } + + public function get_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_CLIENTAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + return $this->get_result("playerlist/".$ranking, $this->playerlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['playerinfo']); + } + + public function get_full_playerlist($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "", $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + return $this->get_result("playerlist/".$ranking, $this->playerlist_filters, $filter, $filter_value, GAMEME_CLIENTAPI_MAXIMUM_LIMIT, $sort_column, $hash_key, $this->cache_times['playerlist'], array("playerlist", "player")); + } + + public function get_playerinfo($ranking, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + if (is_array($uniqueid)) { + $uniqueid = join("/", $uniqueid); + } + if ($uniqueid == "") { + throw new gameMEAPI_Exception("No unique-id given"); + } + if (($data > GAMEME_DATA_DEFAULT) && (isset($this->playerinfo_packet_data[$data]))) { + return $this->get_result("playerinfo/".$ranking."/".$uniqueid."/".$this->playerinfo_packet_data[$data], $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); + } else { + return $this->get_result("playerinfo/".$ranking."/".$uniqueid, $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); + } + } + + public function get_voiceserver_status() { + return $this->get_result("voiceserver", "", GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['voiceserver']); + } + + public function get_awards($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + return $this->get_result("awards/".$ranking, $this->awards_filters, $filter, $filter_value, GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['awards']); + } + + public function get_ribbons($ranking) { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + return $this->get_result("ribbons/".$ranking, "", GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['ribbons']); + } + + public function get_ribboninfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + return $this->get_result("ribboninfo/".$ranking, $this->ribboninfo_filters, $filter, $filter_value, GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['ribboninfo']); + } + + public function get_ranks($ranking) { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + return $this->get_result("ranks/".$ranking, "", GAMEME_FILTER_NONE, "", GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['ranks']); + } + + public function get_rankinfo($ranking, $filter = GAMEME_FILTER_NONE, $filter_value = "") { + if ($ranking == "") { + throw new gameMEAPI_Exception("No valid ranking given"); + } + return $this->get_result("rankinfo/".$ranking, $this->rankinfo_filters, $filter, $filter_value, GAMEME_CLIENTAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, "", $this->cache_times['rankinfo']); + } + + private function get_result($command, $filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $cache_time = 0, $pagination = array()) { + $content_url = ""; + if (($filter > GAMEME_FILTER_NONE) && (isset($filters[$filter])) && ($filter_value != "")) { + if ($filters[$filter] == "online") { + $filter_value = 1; + } + $content_url = $this->url."/".$command."/".$filters[$filter]."/".$filter_value."/?limit=".$limit; + } else { + $content_url = $this->url."/".$command."/?limit=".$limit; + } + + if ((GAMEME_APICACHE_ENABLED == 1) && ($cache_time > 0)) { + $cache_identifier = md5($command.$filters.$filter.$filter_value.$limit.$sort_column.$hash_key.$pagination); + $content_cache = new gameMECache(); + if ($content_cache->is_hit($cache_identifier, $cache_time)) { + $xml_parser_result = $content_cache->get($cache_identifier); + return $xml_parser_result; + } else { + $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key, $pagination); + $content_cache->add($cache_identifier, $xml_parser->result); + } + } else { + $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key, $pagination); + } + + return $xml_parser->result; + } +} +?> \ No newline at end of file diff --git a/src/gameMEGlobalAPI.php b/src/gameMEGlobalAPI.php new file mode 100644 index 0000000..b44a1fa --- /dev/null +++ b/src/gameMEGlobalAPI.php @@ -0,0 +1,169 @@ + "csgo", + GAMEME_GAME_CSS => "css", + GAMEME_GAME_TF2 => "tf2", + GAMEME_GAME_DODS => "dods", + GAMEME_GAME_MOH => "moh", + GAMEME_GAME_BFBC2 => "bcii", + GAMEME_GAME_L4D2 => "l4dii", + GAMEME_GAME_CS16 => "cs16", + GAMEME_GAME_DOD13 => "dod13", + GAMEME_GAME_HL2MP => "hl2mp", + GAMEME_GAME_CZERO => "czero", + GAMEME_GAME_INSMOD => "insmod", + GAMEME_GAME_L4D => "l4d", + GAMEME_GAME_TFC => "tfc", + GAMEME_GAME_HL2CTF => "hl2ctf" + ]; + + /** + * Allowed serverlist filters + */ + private $serverlist_filters = [ + GAMEME_FILTER_NAME => "name", + GAMEME_FILTER_MAP => "map", + GAMEME_FILTER_ADDRESS => "address", + GAMEME_FILTER_GAME => "game", + GAMEME_FILTER_COUNTRY => "country", + GAMEME_FILTER_CITY => "city" + ]; + + /** + * Allowed serverinfo filters + */ + private $serverinfo_filters = []; + + /** + * Allowed serverinfo packet data + */ + private $serverinfo_packet_data = [ + GAMEME_DATA_STATUS => "status", + GAMEME_DATA_PLAYERS => "players" + ]; + + /** + * Allowed playerlist filters + */ + private $playerlist_filters = [ + GAMEME_FILTER_UNIQUEID => "uniqueid", + GAMEME_FILTER_NAME => "name", + GAMEME_FILTER_COUNTRY => "country", + GAMEME_FILTER_ONLINE => "online" + ]; + + /** + * Allowed playerinfo filters + */ + private $playerinfo_filters = []; + + /** + * Allowed playerinfo packet data + */ + private $playerinfo_packet_data = [ + GAMEME_DATA_STATUS => "status", + ]; + + /** + * Default cache times + */ + private $cache_times = [ + "serverlist" => 60, + "serverinfo" => 60, + "playerlist" => 300, + "playerinfo" => 300 + ]; + + function __construct($url = GAMEME_GLOBALAPI_URL) { + $this->url = $url; + } + + public function get_serverlist($filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + return $this->get_result("serverlist", $this->serverlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['serverlist']); + } + + public function get_serverinfo($address, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + if ($address == "") { + throw new gameMEAPI_Exception("No valid address given: ".$address); + } + if (is_array($address)) { + $address = join("/", $address); + } + if (($data > GAMEME_DATA_DEFAULT) && (isset($this->serverinfo_packet_data[$data]))) { + return $this->get_result("serverinfo/".$address."/".$this->serverinfo_packet_data[$data], $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); + } else { + return $this->get_result("serverinfo/".$address, $this->serverinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['serverinfo']); + } + } + + public function get_playerlist($game, $filter = GAMEME_FILTER_NONE, $filter_value = "", $limit = GAMEME_GLOBALAPI_DEFAULT_LIMIT, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + if (!isset($this->game_filters[$game])) { + throw new gameMEAPI_Exception("No valid game given: ".$game); + } + return $this->get_result("playerlist/".$this->game_filters[$game], $this->playerlist_filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $this->cache_times['playerlist']); + } + + public function get_playerinfo($game, $uniqueid, $data = GAMEME_DATA_DEFAULT, $hash_key = GAMEME_HASH_NONE) { + if (!isset($this->game_filters[$game])) { + throw new gameMEAPI_Exception("No valid game given: ".$game); + } + if (is_array($uniqueid)) { + $uniqueid = join("/", $uniqueid); + } + if ($uniqueid == "") { + throw new gameMEAPI_Exception("No unique-id given"); + } + if (($data > GAMEME_DATA_DEFAULT) && (isset($this->playerinfo_packet_data[$data]))) { + return $this->get_result("playerinfo/".$this->game_filters[$game]."/".$uniqueid."/".$this->playerinfo_packet_data[$data], $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); + } else { + return $this->get_result("playerinfo/".$this->game_filters[$game]."/".$uniqueid, $this->playerinfo_filters, GAMEME_FILTER_NONE, "", GAMEME_GLOBALAPI_DEFAULT_LIMIT, GAMEME_SORT_DEFAULT, $hash_key, $this->cache_times['playerinfo']); + } + } + + private function get_result($command, $filters, $filter, $filter_value, $limit, $sort_column, $hash_key, $cache_time = 0, $pagination = array()) { + $content_url = ""; + if (($filter > GAMEME_FILTER_NONE) && (isset($filters[$filter])) && ($filter_value != "")) { + if ($filters[$filter] == "online") { + $filter_value = 1; + } + $content_url = $this->url."/".$command."/".$filters[$filter]."/".$filter_value."/?limit=".$limit; + } else { + $content_url = $this->url."/".$command."/?limit=".$limit; + } + + if ((GAMEME_APICACHE_ENABLED == 1) && ($cache_time > 0)) { + $cache_identifier = md5($command.$filters.$filter.$filter_value.$limit.$sort_column.$hash_key); + $content_cache = new gameMECache(); + if ($content_cache->is_hit($cache_identifier, $cache_time)) { + $xml_parser_result = $content_cache->get($cache_identifier); + return $xml_parser_result; + } else { + $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key); + $content_cache->add($cache_identifier, $xml_parser->result); + } + } else { + $xml_parser = new gameMEXMLParser($content_url, $sort_column, $hash_key); + } + return $xml_parser->result; + } +} +?> \ No newline at end of file diff --git a/src/gameMEXMLParser.php b/src/gameMEXMLParser.php new file mode 100644 index 0000000..5c5315c --- /dev/null +++ b/src/gameMEXMLParser.php @@ -0,0 +1,521 @@ + [ + "data" => "name", + "type" => "asc" + ], + GAMEME_SORT_NAME_DESC => [ + "data" => "name", + "type" => "desc" + ], + GAMEME_SORT_ADDRESS_ASC => [ + "data" => "address", + "type" => "asc" + ], + GAMEME_SORT_ADDRESS_DESC => [ + "data" => "address", + "type" => "desc" + ], + GAMEME_SORT_GAME_ASC => [ + "data" => "game", + "type" => "asc" + ], + GAMEME_SORT_GAME_DESC => [ + "data" => "game", + "type" => "desc" + ], + GAMEME_SORT_MAP_ASC => [ + "data" => "map", + "type" => "asc" + ], + GAMEME_SORT_MAP_DESC => [ + "data" => "map", + "type" => "desc" + ], + GAMEME_SORT_SLOTS_ASC => [ + "data" => "act", + "type" => "asc" + ], + GAMEME_SORT_SLOTS_DESC => [ + "data" => "act", + "type" => "desc" + ], + GAMEME_SORT_POS_ASC => [ + "data" => "rank", + "type" => "asc" + ], + GAMEME_SORT_POS_DESC => [ + "data" => "rank", + "type" => "desc" + ], + GAMEME_SORT_SKILL_ASC => [ + "data" => "skill", + "type" => "asc" + ], + GAMEME_SORT_SKILL_DESC => [ + "data" => "skill", + "type" => "desc" + ], + GAMEME_SORT_KILLS_ASC => [ + "data" => "kills", + "type" => "asc" + ], + GAMEME_SORT_KILLS_DESC => [ + "data" => "kills", + "type" => "desc" + ], + GAMEME_SORT_DEATHS_ASC => [ + "data" => "deaths", + "type" => "asc" + ], + GAMEME_SORT_DEATHS_DESC => [ + "data" => "deaths", + "type" => "desc" + ], + GAMEME_SORT_HEADSHOTS_ASC => [ + "data" => "hs", + "type" => "asc" + ], + GAMEME_SORT_HEADSHOTS_DESC => [ + "data" => "hs", + "type" => "desc" + ], + GAMEME_SORT_ASSISTS_ASC => [ + "data" => "assists", + "type" => "asc" + ], + GAMEME_SORT_ASSISTS_DESC => [ + "data" => "assists", + "type" => "desc" + ], + GAMEME_SORT_WEAPON_ASC => [ + "data" => "weapon", + "type" => "asc" + ], + GAMEME_SORT_WEAPON_DESC => [ + "data" => "weapon", + "type" => "desc" + ], + GAMEME_SORT_ACTIVITY_ASC => [ + "data" => "activity", + "type" => "asc" + ], + GAMEME_SORT_ACTIVITY_DESC => [ + "data" => "activity", + "type" => "desc" + ], + GAMEME_SORT_TIME_ASC => [ + "data" => "time", + "type" => "asc" + ], + GAMEME_SORT_TIME_DESC => [ + "data" => "time", + "type" => "desc" + ], + GAMEME_SORT_SUICIDES_ASC => [ + "data" => "suicides", + "type" => "asc" + ], + GAMEME_SORT_SUICIDES_DESC => [ + "data" => "suicides", + "type" => "desc" + ], + GAMEME_SORT_SHOTS_ASC => [ + "data" => "shots", + "type" => "asc" + ], + GAMEME_SORT_SHOTS_DESC => [ + "data" => "shots", + "type" => "desc" + ], + GAMEME_SORT_HITS_ASC => [ + "data" => "hits", + "type" => "asc" + ], + GAMEME_SORT_HITS_DESC => [ + "data" => "hits", + "type" => "desc" + ], + GAMEME_SORT_KILLSTREAK_ASC => [ + "data" => "killstreak", + "type" => "asc" + ], + GAMEME_SORT_KILLSTREAK_DESC => [ + "data" => "killstreak", + "type" => "desc" + ], + GAMEME_SORT_DEATHSTREAK_ASC => [ + "data" => "deathstreak", + "type" => "asc" + ], + GAMEME_SORT_DEATHSTREAK_DESC => [ + "data" => "deathstreak", + "type" => "desc" + ], + GAMEME_SORT_ASSISTED_ASC => [ + "data" => "assisted", + "type" => "asc" + ], + GAMEME_SORT_ASSISTED_DESC => [ + "data" => "assisted", + "type" => "desc" + ], + GAMEME_SORT_TEAMKILLS_ASC => [ + "data" => "teamkills", + "type" => "asc" + ], + GAMEME_SORT_TEAMKILLS_DESC => [ + "data" => "teamkills", + "type" => "desc" + ], + GAMEME_SORT_TEAMKILLED_ASC => [ + "data" => "teamkilled", + "type" => "asc" + ], + GAMEME_SORT_TEAMKILLED_DESC => [ + "data" => "teamkilled", + "type" => "desc" + ], + GAMEME_SORT_HEALEDPOINTS_ASC => [ + "data" => "healedpoints", + "type" => "asc" + ], + GAMEME_SORT_HEALEDPOINTS_DESC => [ + "data" => "healedpoints", + "type" => "desc" + ], + GAMEME_SORT_FLAGSCAPTURED_ASC => [ + "data" => "flagscaptured", + "type" => "asc" + ], + GAMEME_SORT_FLAGSCAPTURED_DESC => [ + "data" => "flagscaptured", + "type" => "desc" + ], + GAMEME_SORT_CUSTOMWINS_ASC => [ + "data" => "customwins", + "type" => "asc" + ], + GAMEME_SORT_CUSTOMWINS_DESC => [ + "data" => "customwins", + "type" => "desc" + ], + GAMEME_SORT_ROUNDS_ASC => [ + "data" => "rounds", + "type" => "asc" + ], + GAMEME_SORT_ROUNDS_DESC => [ + "data" => "rounds", + "type" => "desc" + ], + GAMEME_SORT_WINS_ASC => [ + "data" => "wins", + "type" => "asc" + ], + GAMEME_SORT_WINS_DESC => [ + "data" => "wins", + "type" => "desc" + ], + GAMEME_SORT_LOSSES_ASC => [ + "data" => "losses", + "type" => "asc" + ], + GAMEME_SORT_LOSSES_DESC => [ + "data" => "losses", + "type" => "desc" + ], + GAMEME_SORT_SURVIVED_ASC => [ + "data" => "survived", + "type" => "asc" + ], + GAMEME_SORT_SURVIVED_DESC => [ + "data" => "survived", + "type" => "desc" + ], + ]; + + + /** + * Current sort column + */ + private $sort_column; + + + /** + * Available hash keys + */ + private $hash_keys = [ + GAMEME_HASH_ADDRESS => [ + "delimiter" => ":", + "data" => [ + "addr", + "port" + ] + ], + GAMEME_HASH_UNIQUEID => "uniqueid" + ]; + + /** + * Private stream context for compressed transmission + */ + private $stream_context; + + function __construct($url, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEME_HASH_NONE, $pagination = []) { + try { + $this->xml = $this->fetch_content($url); + if (!$this->xml) { + return false; + } + + $this->parsed_xml = new \SimpleXMLElement($this->xml, LIBXML_NOCDATA); + $this->result = $this->prepare_result($this->parsed_xml, $sort_column, $hash_key); + + // check errors + if (isset($this->result['error'])) { + if ((isset($this->result['error']['type'])) && (isset($this->result['error']['description']))) { + throw new gameMEAPI_Exception("An error occurs: ".$this->result['error']['description']." [".$this->result['error']['type']."]"); + } else { + throw new gameMEAPI_Exception("Unknown error occurs!"); + } + } + + // check pagination + if ((count($pagination) == 2) && ($pagination[0] != "") && ($pagination[1] != "")) { + $pagination_definition = $pagination[0]; + $pagination_result = $pagination[1]; + + if (isset($this->parsed_xml->$pagination_definition->pagination)) { + $next_page_link = $this->parsed_xml->$pagination_definition->pagination->nextpagelink; + while ($next_page_link != "") { + $next_page_xml = $this->fetch_content($next_page_link); + + if ($next_page_xml != "") { + $next_page_result = new \SimpleXMLElement($next_page_xml, LIBXML_NOCDATA); + if (isset($next_page_result->$pagination_definition->pagination)) { + $next_page_link = $next_page_result->$pagination_definition->pagination->nextpagelink; + } else { + $next_page_link = ""; + } + $next_page_result = $this->prepare_result($next_page_result, $sort_column, $hash_key); + if (isset($next_page_result[$pagination_definition]) && is_array($next_page_result[$pagination_definition]) && count($next_page_result[$pagination_definition]) > 0) { + $this->result[$pagination_definition] = array_merge($this->result[$pagination_definition], $next_page_result[$pagination_definition]); + } + } else { + $next_page_link = ""; + throw new gameMEAPI_Exception("Cannot retrieve next page xml at url ".$next_page_link); + } + } + $this->sort_result($sort_column); + } + } + } catch (Exception $e) { + throw new gameMEAPI_Exception("Cannot retrieve xml at url ".$url." [".$e->getMessage()."]"); + } + } + + // sort results + private function sort_result($sort_column) { + if (($sort_column > GAMEME_SORT_DEFAULT) && (isset($this->sort_columns[$sort_column]))) { + $this->sort_column = $this->sort_columns[$sort_column]['data']; + if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { + if ($this->sort_columns[$sort_column]['type'] == "asc") { + uasort($this->result[$pagination_definition], [$this, 'compare_asc']); + } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { + uasort($this->result[$pagination_definition], [$this, 'compare_desc']); + } + } else { + if ($this->sort_columns[$sort_column]['type'] == "asc") { + usort($this->result[$pagination_definition], [$this, 'compare_asc']); + } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { + usort($this->result[$pagination_definition], [$this, 'compare_desc']); + } + } + } + } + + private function fetch_content($url) { + if (GAMEME_FETCHDATA_COMPRESSED == 1) { + if (!$this->stream_context) { + $context_options = [ + 'http' => [ + 'method' => "GET", + 'header' => "Accept-Encoding: gzip;\r\n" + ] + ]; + $this->stream_context = stream_context_create($context_options); + return file_get_contents("compress.zlib://" . $url, false, $this->stream_context); + /* + if($open_uri !== false && $open_uri !== FALSE) { + return gzdecode($open_uri); + } + */ + } + } + return file_get_contents($url); + } + + private function compare_asc($a, $b) { + if ($a[$this->sort_column] == $b[$this->sort_column]) { + return 0; + } + return ($a[$this->sort_column] < $b[$this->sort_column]) ? -1 : 1; + } + + private function compare_desc($a, $b) { + if ($a[$this->sort_column] == $b[$this->sort_column]) { + return 0; + } + return ($a[$this->sort_column] > $b[$this->sort_column]) ? -1 : 1; + } + + private function prepare_result($object, $sort_column, $hash_key, $level = 0) { + $result_items = []; + + if (!is_object($object)) { + return $result_items; + } + + $child = (array)$object; + + if (sizeof($child) > 0) { + foreach($child as $key => $entry) { + if (is_array($entry)) { + foreach($entry as $entry_key => $entry_entry) { + if (!is_object($entry_entry)) { + $result_items[$entry_key] = $entry_entry; + } else { + if (get_class($entry_entry) == 'SimpleXMLElement') { + if (($level == 0) && (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key])))) { + $array_hash_key = ""; + if (is_array($this->hash_keys[$hash_key])) { + $array_hash_keys = []; + foreach($this->hash_keys[$hash_key]['data'] as $data) { + if ((string)$entry_entry->$data != "") { + $array_hash_keys[] = (string)$entry_entry->$data; + } + } + $array_hash_key = join($this->hash_keys[$hash_key]['delimiter'], $array_hash_keys); + } else { + $array_hash_key = (string)$entry_entry->{$this->hash_keys[$hash_key]}; + } + if ($array_hash_key == "") { + $result_items[$entry_key] = $this->prepare_result($entry_entry, $sort_column, $hash_key, $level + 1); + } else { + $result_items[$array_hash_key] = $this->prepare_result($entry_entry, $sort_column, $hash_key, $level + 1); + } + } else { + $result_items[$entry_key] = $this->prepare_result($entry_entry, $sort_column, $hash_key, $level + 1); + } + } + } + } + + if (($sort_column > GAMEME_SORT_DEFAULT) && (isset($this->sort_columns[$sort_column]))) { + $this->sort_column = $this->sort_columns[$sort_column]['data']; + if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { + if ($this->sort_columns[$sort_column]['type'] == "asc") { + uasort($result_items, [$this, 'compare_asc']); + } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { + uasort($result_items, [$this, 'compare_desc']); + } + } else { + if ($this->sort_columns[$sort_column]['type'] == "asc") { + usort($result_items, [$this, 'compare_asc']); + } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { + usort($result_items, [$this, 'compare_desc']); + } + } + } + } else { + if (!is_object($entry)) { + $result_items[$key] = $entry; + } else { + if (get_class($entry) == 'SimpleXMLElement') { + if ($key == "pagination") { + continue; + } + $allowed_new_columns = [ + "vendor" => 1, + "software" => 1, + "account" => 1, + "error" => 1, + "globalinfo" => 1, + "rankinginfo" => 1, + "serverinfo" => 1, + "serverlist" => 1, + "playerlist" => 1, + "playerinfo" => 1, + "voiceserver" => 1, + "awards" => 1, + "ribbons" => 1, + "ribboninfo" => 1, + "ranks" => 1, + "rankinfo" => 1 + ]; + if ((isset($allowed_new_columns[$key])) || ($level > 0)) { + $result_items[$key] = $this->prepare_result($entry, $sort_column, $hash_key); + } else { + if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { + $array_hash_key = ""; + if (is_array($this->hash_keys[$hash_key])) { + $array_hash_keys = []; + foreach($this->hash_keys[$hash_key]['data'] as $data) { + if ((string)$entry->$data != "") { + $array_hash_keys[] = (string)$entry->$data; + } + } + $array_hash_key = join($this->hash_keys[$hash_key]['delimiter'], $array_hash_keys); + } else { + $array_hash_key = (string)$entry->{$this->hash_keys[$hash_key]}; + } + if ($array_hash_key == "") { + $result_items[] = $this->prepare_result($entry, $sort_column, $hash_key, $level + 1); + } else { + $result_items[$array_hash_key] = $this->prepare_result($entry, $sort_column, $hash_key, $level + 1); + } + } else { + $result_items[] = $this->prepare_result($entry, $sort_column, $hash_key, $level + 1); + } + } + } + } + } + } + } + + if (count($result_items) == 0) { + return ""; + } else { + return $result_items; + } + } +} +?> \ No newline at end of file From c7c690abce797df95005b2c10a93248db1d97359 Mon Sep 17 00:00:00 2001 From: 9iky6 Date: Sun, 8 Apr 2018 00:05:03 +0500 Subject: [PATCH 2/6] minor bug --- README | 2 +- composer.json | 12 ++++-------- examples/gameme_api_currentplayers.php | 4 ++-- examples/gameme_api_currentservers.php | 6 ++---- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/README b/README index c528d5c..8c022f9 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ gameME SDK Webpage: http://www.gameme.com Docs: http://www.gameme.com/docs/api/sdk -Copyright (C) 2011-2015 TTS Oetzel & Goerz GmbH +Copyright (C) 2011-2018 TTS Oetzel & Goerz GmbH This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License diff --git a/composer.json b/composer.json index 935e45f..4a2602a 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,16 @@ { "name": "gamemedev/gamemeapi", - "version": "dev-master", + "version": "0.2", "description": "", "license": "GPL-2.0", "authors": [ { - "name": "TTS Oetzel", - "email": "dev@gameme.com" - }, - { - "name": "Goerz GmbH", - "email": "dev@ameme.com" + "name": "TTS Oetzel & Goerz GmbH", + "email": "info@gameme.com" } ], "require":{ - "php": ">=5.3" + "php": ">=5.5" }, "autoload": { "psr-4": { diff --git a/examples/gameme_api_currentplayers.php b/examples/gameme_api_currentplayers.php index 9516af2..46a4c2c 100644 --- a/examples/gameme_api_currentplayers.php +++ b/examples/gameme_api_currentplayers.php @@ -25,8 +25,8 @@ * Copyright(C) gameME 2011-2013 TTS Oetzel & Goerz GmbH. All rights reserved. */ - require("gameme_api_sdk.php"); - $gameME_sdk_object = new gameMEAPI(YOUR_URL_HERE); + require __DIR__ .'vendor/autoload.php'; + $gameME_sdk_object = new \GameMEAPI\gameMEAPI(YOUR_URL_HERE); $current_slots = 0; $maximum_slots = 0; diff --git a/examples/gameme_api_currentservers.php b/examples/gameme_api_currentservers.php index d388989..6299490 100644 --- a/examples/gameme_api_currentservers.php +++ b/examples/gameme_api_currentservers.php @@ -25,16 +25,14 @@ * Copyright(C) gameME 2011-2013 TTS Oetzel & Goerz GmbH. All rights reserved. */ - require("gameme_api_sdk.php"); - $gameME_sdk_object = new gameMEAPI(YOUR_URL_HERE); + require __DIR__ .'vendor/autoload.php'; + $gameME_sdk_object = new \GameMEAPI\gameMEAPI(YOUR_URL_HERE); try { - $server_list = $gameME_sdk_object->client_api_serverlist(GAMEME_FILTER_NONE); foreach($server_list['serverlist'] as $server) { echo $server['name']." ".$server['act']."/".$server['max']." - ".$server['map']."\n"; } - } catch (Exception $e) { die ("Client API Serverlist Error: ".$e->getMessage()."\n"); } From 0598289e9fb54645396a989e1120c13847f6fee9 Mon Sep 17 00:00:00 2001 From: 9iky6 Date: Sun, 8 Apr 2018 00:06:20 +0500 Subject: [PATCH 3/6] change protocol --- README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README b/README index 8c022f9..309abbb 100644 --- a/README +++ b/README @@ -1,6 +1,6 @@ gameME SDK -Webpage: http://www.gameme.com -Docs: http://www.gameme.com/docs/api/sdk +Webpage: https://www.gameme.com +Docs: https://www.gameme.com/docs/api/sdk Copyright (C) 2011-2018 TTS Oetzel & Goerz GmbH This program is free software; you can redistribute it and/or From 56bc75c699c9dd6e6a721b45e97269fac04f1751 Mon Sep 17 00:00:00 2001 From: 9iky6 Date: Sun, 8 Apr 2018 01:00:01 +0500 Subject: [PATCH 4/6] remove old code --- src/gameMEXMLParser.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/gameMEXMLParser.php b/src/gameMEXMLParser.php index 5c5315c..7c1e99b 100644 --- a/src/gameMEXMLParser.php +++ b/src/gameMEXMLParser.php @@ -372,11 +372,6 @@ private function fetch_content($url) { ]; $this->stream_context = stream_context_create($context_options); return file_get_contents("compress.zlib://" . $url, false, $this->stream_context); - /* - if($open_uri !== false && $open_uri !== FALSE) { - return gzdecode($open_uri); - } - */ } } return file_get_contents($url); From 41544ea1dbf198f4d64aaf34c1ea424b97a4ff5d Mon Sep 17 00:00:00 2001 From: 9iky6 Date: Sun, 8 Apr 2018 22:17:09 +0500 Subject: [PATCH 5/6] remove double code --- src/gameMEXMLParser.php | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/gameMEXMLParser.php b/src/gameMEXMLParser.php index 7c1e99b..3c3b607 100644 --- a/src/gameMEXMLParser.php +++ b/src/gameMEXMLParser.php @@ -431,24 +431,8 @@ private function prepare_result($object, $sort_column, $hash_key, $level = 0) { } } } - } - - if (($sort_column > GAMEME_SORT_DEFAULT) && (isset($this->sort_columns[$sort_column]))) { - $this->sort_column = $this->sort_columns[$sort_column]['data']; - if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - uasort($result_items, [$this, 'compare_asc']); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - uasort($result_items, [$this, 'compare_desc']); - } - } else { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - usort($result_items, [$this, 'compare_asc']); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - usort($result_items, [$this, 'compare_desc']); - } - } - } + } + $this->sort_result($sort_column); } else { if (!is_object($entry)) { $result_items[$key] = $entry; From 949bf4bb73e0b14dc76430c2688f92f887f1b6cd Mon Sep 17 00:00:00 2001 From: 9iky6 Date: Mon, 9 Apr 2018 15:01:41 +0500 Subject: [PATCH 6/6] fix sorting by addres, by game. --- src/gameMEXMLParser.php | 49 ++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/gameMEXMLParser.php b/src/gameMEXMLParser.php index 3c3b607..efa3d63 100644 --- a/src/gameMEXMLParser.php +++ b/src/gameMEXMLParser.php @@ -37,11 +37,11 @@ class gameMEXMLParser { "type" => "desc" ], GAMEME_SORT_ADDRESS_ASC => [ - "data" => "address", + "data" => "addr", "type" => "asc" ], GAMEME_SORT_ADDRESS_DESC => [ - "data" => "address", + "data" => "addr", "type" => "desc" ], GAMEME_SORT_GAME_ASC => [ @@ -333,33 +333,13 @@ function __construct($url, $sort_column = GAMEME_SORT_DEFAULT, $hash_key = GAMEM throw new gameMEAPI_Exception("Cannot retrieve next page xml at url ".$next_page_link); } } - $this->sort_result($sort_column); + $this->result[$pagination_definition] = $this->sort_result($sort_column, $this->result[$pagination_definition]); } } } catch (Exception $e) { throw new gameMEAPI_Exception("Cannot retrieve xml at url ".$url." [".$e->getMessage()."]"); } } - - // sort results - private function sort_result($sort_column) { - if (($sort_column > GAMEME_SORT_DEFAULT) && (isset($this->sort_columns[$sort_column]))) { - $this->sort_column = $this->sort_columns[$sort_column]['data']; - if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - uasort($this->result[$pagination_definition], [$this, 'compare_asc']); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - uasort($this->result[$pagination_definition], [$this, 'compare_desc']); - } - } else { - if ($this->sort_columns[$sort_column]['type'] == "asc") { - usort($this->result[$pagination_definition], [$this, 'compare_asc']); - } elseif ($this->sort_columns[$sort_column]['type'] == "desc") { - usort($this->result[$pagination_definition], [$this, 'compare_desc']); - } - } - } - } private function fetch_content($url) { if (GAMEME_FETCHDATA_COMPRESSED == 1) { @@ -375,17 +355,30 @@ private function fetch_content($url) { } } return file_get_contents($url); - } - + } + + // sort results + private function sort_result($sort_column, $sort_array) { + if (($sort_column > GAMEME_SORT_DEFAULT) && (isset($this->sort_columns[$sort_column]))) { + $this->sort_column = $this->sort_columns[$sort_column]['data']; + if (($hash_key > GAMEME_HASH_NONE) && (isset($this->hash_keys[$hash_key]))) { + uasort($sort_array, [$this, 'compare_'.$this->sort_columns[$sort_column]['type']]); + } else { + usort($sort_array, [$this, 'compare_'.$this->sort_columns[$sort_column]['type']]); + } + } + return $sort_array; + } + private function compare_asc($a, $b) { - if ($a[$this->sort_column] == $b[$this->sort_column]) { + if (!is_array($a) || !is_array($b) || $a[$this->sort_column] == $b[$this->sort_column]) { return 0; } return ($a[$this->sort_column] < $b[$this->sort_column]) ? -1 : 1; } private function compare_desc($a, $b) { - if ($a[$this->sort_column] == $b[$this->sort_column]) { + if (!is_array($a) || !is_array($b) || $a[$this->sort_column] == $b[$this->sort_column]) { return 0; } return ($a[$this->sort_column] > $b[$this->sort_column]) ? -1 : 1; @@ -432,7 +425,7 @@ private function prepare_result($object, $sort_column, $hash_key, $level = 0) { } } } - $this->sort_result($sort_column); + $result_items = $this->sort_result($sort_column, $result_items); } else { if (!is_object($entry)) { $result_items[$key] = $entry;