diff --git a/app/Config/core.php b/app/Config/core.php index 3de63f6..07ec511 100644 --- a/app/Config/core.php +++ b/app/Config/core.php @@ -32,13 +32,13 @@ * * In production mode, flash messages redirect after a time interval. * In development mode, you need to click the flash message to continue. - */ + */ // TODO : generate config.salt.php Configure::write('debug', 0); - Configure::write('mushraider', array('version' => '1.7.0.2', 'date' => '2017-02-19')); + Configure::write('mushraider', array('version' => '1.7.0.3', 'date' => '2019-01-17')); /** * Configure the Error handler used to handle errors for your application. By default diff --git a/app/Model/Datasource/RaidheadSource.php b/app/Model/Datasource/RaidheadSource.php index 42dbab0..18920d8 100644 --- a/app/Model/Datasource/RaidheadSource.php +++ b/app/Model/Datasource/RaidheadSource.php @@ -6,70 +6,80 @@ * @desc Used for reading from RaidHead API. * */ - + App::uses('DataSource', 'Model/Datasource'); App::uses('HttpSocket', 'Network/Http'); +App::uses('Configure', 'Core'); class RaidheadSource extends DataSource { - public $config = array( - 'baseUrl' => 'http://api.raidhead.com/', - 'langs' => array('eng', 'fra') - ); - - private $http; - - public function __construct() { - $this->http = new HttpSocket(); - $lang = strtolower(Configure::read('Settings.language')); - if(!in_array($lang, $this->config['langs'])) { - $lang = $this->config['langs'][0]; - } - - $this->config['baseUrl'] .= $lang; - } - - public function gets($type = 'all') { - $list = array(); - $json = $this->http->get($this->config['baseUrl'].'/games/index.json'); - $games = json_decode($json, true); - if(is_null($games)) { - $error = json_last_error(); - throw new CakeException($error); - } - - if($type == 'list') { - if(!empty($games)) { - foreach($games as $game) { - $list[$game['short']] = $game['title']; - } - } - - return $list; - } - - return $games; - } - - public function get($slug) { - $json = $this->http->get($this->config['baseUrl'].'/games/get/'.$slug.'.json'); - $game = json_decode($json, true); - if(is_null($game)) { - $error = json_last_error(); - throw new CakeException($error); - } - - return $game; - } - - public function serverStatus($slug) { - $json = $this->http->get($this->config['baseUrl'].'/server_status/get/'.$slug.'.json'); - $serverStatus = json_decode($json, true); - if(is_null($serverStatus)) { - $error = json_last_error(); - throw new CakeException($error); - } - - return $serverStatus; - } -} \ No newline at end of file + public $config = array( + 'baseUrl' => 'https://api.raidhead.com/', + 'langs' => array('eng', 'fra') + ); + + public function __construct() { + $lang = strtolower(Configure::read('Settings.language')); + if(!in_array($lang, $this->config['langs'])) { + $lang = $this->config['langs'][0]; + } + + $this->config['baseUrl'] .= $lang; + } + + /** + * query api and return response as array or false + * @param string $uri api uri + * @return array response as array + * @throws CakeException + */ + private function _get($uri) + { + $client = new HttpSocket(array( + 'request' => array( + 'redirect' => 2, + ), + )); + + try { + $response = $client->get($this->config['baseUrl'] . $uri, array(), array( + 'header' => array( + 'User-Agent' => 'Mushraider/' . Configure::read('mushraider.version'), + ), + )); + + $return = json_decode($response->body(), true); + + if ($return === false) { + throw new CakeException(json_last_error()); + } + + return $return; + } catch (Exception $e) { + throw $e; + } + } + + public function gets($type = 'all') { + $games = $this->_get('/games/index.json'); + + if($type == 'list') { + $list = array(); + foreach($games as $game) { + $list[$game['short']] = $game['title']; + } + + return $list; + } + + return $games; + } + + public function get($slug) { + return $this->_get('/games/get/' . $slug . '.json'); + } + + public function serverStatus($slug) { + return $this->_get('/server_status/get/' . $slug . '.json'); + } +}