PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/eve/api.php

http://ooe.googlecode.com/
PHP | 274 lines | 227 code | 42 blank | 5 comment | 55 complexity | 8a7558ad1a54449711de46cbc1a11900 MD5 | raw file
  1. <?php
  2. require_once('curl.class.php');
  3. require_once('apiClasses.php');
  4. require_once('apiMailClasses.php');
  5. require_once('apiCorpClasses.php');
  6. require_once('apiChar.php');
  7. require_once('apiCorp.php');
  8. require_once('apidb.php');
  9. require_once('apimarket.php');
  10. require_once('apiConstants.php');
  11. $cacheDelays = array(
  12. 101, 103, 115, 116, 117, 119
  13. );
  14. $eveTime = time() - date('Z');
  15. $GLOBALS['EVEAPI_ERRORS'] = array();
  16. function apiError($method, $error) {
  17. if (!isset($GLOBALS['EVEAPI_NO_ERRORS']) || (isset($GLOBALS['EVEAPI_NO_ERRORS']) && !$GLOBALS['EVEAPI_NO_ERRORS']))
  18. $GLOBALS['EVEAPI_ERRORS'][] = 'API "' . $method . '": [' . (string)$error['code'] . '] ' . (string)$error;
  19. }
  20. function otherError($method, $error) {
  21. if (!isset($GLOBALS['EVEAPI_NO_ERRORS']) || (isset($GLOBALS['EVEAPI_NO_ERRORS']) && !$GLOBALS['EVEAPI_NO_ERRORS']))
  22. $GLOBALS['EVEAPI_ERRORS'][] = $method . ': ' . $error;
  23. }
  24. class apiRequest {
  25. var $cachedResponse = '';
  26. var $response = '';
  27. var $data = null;
  28. /**
  29. * This is probably the most disgusting function you have ever seen in your entire life.
  30. * GLHF.
  31. */
  32. function apiRequest($method, $user = null, $params = null, $printErrors = true) {
  33. $http = new cURL();
  34. $http->setOption('CURLOPT_USERAGENT', 'Out of Eve (dev; shrimp@shrimpworks.za.net)');
  35. $http->setOption('CURLOPT_TIMEOUT', 45);
  36. $apiUrl = $GLOBALS['config']['eve']['api_url'];
  37. $fetchMethod = $GLOBALS['config']['eve']['method'];
  38. if (!$params)
  39. $params = array();
  40. if ($user) {
  41. $params['userID'] = $user[0];
  42. $params['apiKey'] = $user[1];
  43. if (isset($user[2]))
  44. $params['characterID'] = $user[2];
  45. }
  46. $hadError = false;
  47. $cachedError = false;
  48. $cached = $this->checkCache($method, $params);
  49. if (!empty($this->cachedResponse)) {
  50. try {
  51. $tmp = @new SimpleXMLElement($this->cachedResponse);
  52. } catch (Exception $e) {
  53. }
  54. $cachedError = isset($tmp->error);
  55. $cachedDate = $tmp->cachedUntil;
  56. if (isset($tmp) && isset($tmp->cachedUntil) && ((strtotime($tmp->cachedUntil) + date('Z') + 300) > time())) {
  57. $this->response = $this->cachedResponse;
  58. $this->data = $tmp;
  59. }
  60. }
  61. if (!isset($this->data)) {
  62. $logFile = @fopen($GLOBALS['config']['eve']['cache_dir'] . '_api.log', 'a+');
  63. if (!$cached) {
  64. if (strtoupper($fetchMethod == 'GET')) {
  65. $this->response = $http->get($apiUrl . '/' . $method . $this->queryString($params));
  66. echo $apiUrl . '/' . $method . $this->queryString($params);
  67. } else
  68. $this->response = $http->post($apiUrl . '/' . $method, $params);
  69. }
  70. $resonseInfo = $http->getInfo();
  71. if (($resonseInfo['http_code'] >= 200) && ($resonseInfo['http_code'] <= 300) && (!empty($this->response))) {
  72. try {
  73. $this->data = @new SimpleXMLElement($this->response);
  74. } catch (Exception $e) {
  75. $hadError = true;
  76. @fwrite($logFile, date('Y-d-m H:i:s: ') . "\t" . $method . "\t" . $e->getMessage() . "\n");
  77. if ($printErrors)
  78. otherError($method, $e->getMessage() . '. Using local cache.');
  79. if (!empty($this->cachedResponse)) {
  80. try {
  81. $this->data = @new SimpleXMLElement($this->cachedResponse);
  82. } catch (Exception $e) {
  83. @fwrite($logFile, date('Y-d-m H:i:s: ') . "\tCache load failed\t" . $e->getMessage() . "\n");
  84. if ($printErrors)
  85. otherError($method, 'Superhypermegaultrabbqfail: ' . $e->getMessage());
  86. }
  87. }
  88. }
  89. if (isset($this->data->error) && (!empty($this->cachedResponse) && !$cachedError)) {
  90. $hadError = true;
  91. @fwrite($logFile, date('Y-d-m H:i:s: ') . "\t" . $method . "\t" . (string)$this->data->error . "\n");
  92. $errorCode = (int)$this->data->error['code'];
  93. $errorCache = (string)$this->data->cachedUntil;
  94. if ($printErrors) {
  95. apiError($method, $this->data->error);
  96. }
  97. try {
  98. $this->data = @new SimpleXMLElement($this->cachedResponse);
  99. $this->data->gotError = $errorCode;
  100. if (in_array($errorCode, $GLOBALS['cacheDelays'])) {
  101. $this->data->cachedUntil = $errorCache;
  102. $this->response = $this->data->asXML();
  103. $this->saveCache($method, $params, strtotime($this->data->cachedUntil) + date('Z') + 300);
  104. }
  105. } catch (Exception $e) {
  106. if ($printErrors)
  107. otherError($method, 'Superhypermegaultrabbqfail: ' . $e->getMessage());
  108. @fwrite($logFile, date('Y-d-m H:i:s: ') . "\tCache load failed\t" . $e->getMessage() . "\n");
  109. }
  110. }
  111. if (!$hadError && !$cached && isset($this->data->cachedUntil))
  112. $this->saveCache($method, $params, strtotime($this->data->cachedUntil) + date('Z') + 300);
  113. } else {
  114. @fwrite($logFile, date('Y-d-m H:i:s: ') . "\tHTTP error: $method\t" . $this->response . "\n");
  115. if ((!empty($this->cachedResponse) && !$cachedError)) {
  116. if ($printErrors)
  117. otherError($method, 'API error (HTTP ' . $resonseInfo['http_code'] . '); using local cache -- expired ' . $cachedDate);
  118. try {
  119. $this->data = @new SimpleXMLElement($this->cachedResponse);
  120. } catch (Exception $e) {
  121. }
  122. } else {
  123. @fwrite($logFile, date('Y-d-m H:i:s: ') . "\tFailed with no cache: $method\t" . $this->response . "\n");
  124. otherError($method, 'Failed to get API data from '.$method.': ' . $this->response);
  125. }
  126. }
  127. @fclose($logFile);
  128. }
  129. }
  130. function queryString($params) {
  131. $res = '?';
  132. foreach ($params as $key => $value) {
  133. $res .= $key . '=' . urlencode($value) . '&';
  134. }
  135. return substr($res, 0, -1);
  136. }
  137. function checkCache($method, $params)
  138. {
  139. $cacheSum = md5($method . implode('.', $params));
  140. $this->cacheFile = $GLOBALS['config']['eve']['cache_dir'] . $cacheSum;
  141. if (file_exists($this->cacheFile)) {
  142. $this->cachedResponse = file_get_contents($this->cacheFile);
  143. if (time() <= (filemtime($this->cacheFile))) {
  144. $this->response = $this->cachedResponse;
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. function saveCache($method, $params, $cachedUntil) {
  151. $cacheSum = md5($method . implode('.', $params));
  152. $this->cacheFile = $GLOBALS['config']['eve']['cache_dir'] . $cacheSum;
  153. file_put_contents($this->cacheFile, $this->response);
  154. touch($this->cacheFile, $cachedUntil);
  155. }
  156. function clearOldCache() {
  157. // maximum cache age is 7 days - one week
  158. $maxAge = 3600 * 24 * 7;
  159. $files = scandir($GLOBALS['config']['eve']['cache_dir']);
  160. foreach ($files as $file) {
  161. $file = $GLOBALS['config']['eve']['cache_dir'] . $file;
  162. if (is_file($file)) {
  163. echo 'heh: ' . (time() - (filemtime($file)));
  164. break;
  165. if (time() - (filemtime($file)) > $maxAge) {
  166. unlink($file);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. class eveAccount {
  173. var $userId = '';
  174. var $apiKey = '';
  175. var $characters = array();
  176. var $accountStatus = null;
  177. var $error = false;
  178. var $timeOffset = 0;
  179. var $db = null;
  180. function eveAccount($userId, $apiKey, $timeOffset = 0, $autoLoad = true) {
  181. $this->userId = $userId;
  182. $this->apiKey = $apiKey;
  183. $this->timeOffset = $timeOffset * 3600;
  184. $this->db = new eveDB();
  185. if ($autoLoad) {
  186. $this->getCharacters();
  187. }
  188. }
  189. function getCharacters() {
  190. $charData = new apiRequest('account/Characters.xml.aspx', array($this->userId, $this->apiKey));
  191. if ($charData->data) {
  192. if ($charData->data->error)
  193. $this->error = array('code' => (int)$charData->data->error['code'], 'message' => (string)$charData->data->error);
  194. if (!$this->error)
  195. foreach ($charData->data->result->rowset->row as $char)
  196. $this->characters[] = new eveCharacter($this, (int)$char['characterID']);
  197. if (!$this->error && count($this->characters) == 0)
  198. $this->error = array('code' => 1, 'message' => 'No characters (WTF?)!');
  199. }
  200. }
  201. function getAccountStatus() {
  202. $accData = new apiRequest('account/AccountStatus.xml.aspx', array($this->userId,
  203. $this->apiKey));
  204. if (!$accData->data) {
  205. return;
  206. }
  207. if ($accData->data->error) {
  208. apiError('account/AccountStatus.xml.aspx', $accData->data->error);
  209. $this->error = (string)$accData->data->error;
  210. } else {
  211. $this->accountStatus = new eveAccountStatus($this, $accData->data->result);
  212. }
  213. }
  214. function checkFullAccess() {
  215. $balanceTest = new apiRequest('char/AccountBalance.xml.aspx', array($this->userId, $this->apiKey, $this->characters[0]->characterID));
  216. if ($balanceTest->data->error)
  217. $this->error = array('code' => (int)$balanceTest->data->error['code'], 'message' => (string)$balanceTest->data->error);
  218. }
  219. }
  220. function characterName($id) {
  221. $charData = new apiRequest('eve/CharacterName.xml.aspx', array(), array('ids' => $id));
  222. if (!$charData->data) {
  223. return 'Lookup Error';
  224. }
  225. if ($charData->data->error) {
  226. apiError('eve/CharacterName.xml.aspx', $charData->data->error);
  227. return 'Lookup Error';
  228. } else {
  229. return (string)$charData->data->result->rowset->row['name'];
  230. }
  231. }
  232. ?>