Guest User

Untitled

a guest
Mar 27th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 67.62 KB | None | 0 0
  1. <?php
  2. /**
  3. * @file
  4. * Functions related to retrieving and manipulating data from the EVE API.
  5. */
  6.  
  7. /**
  8. * Function to retrieve the XML.
  9. *
  10. * @param string $type
  11. * Indicate what URL to use to retrieve the XML.
  12. * @param array $query
  13. * The URL variables to pass through the URL.
  14. *
  15. * @return mixed
  16. * Returns a valid XML object or ERROR information.
  17. */
  18. function eve_api_get_xml($type, $query = array()) {
  19. $url = 'https://api.eveonline.com';
  20.  
  21. switch ($type) {
  22. case 'ContactList':
  23. $url .= '/corp/ContactList.xml.aspx';
  24. break;
  25.  
  26. case 'AllianceList':
  27. $url .= '/eve/AllianceList.xml.aspx';
  28. break;
  29.  
  30. case 'CorporationSheet':
  31. $url .= '/corp/CorporationSheet.xml.aspx';
  32. break;
  33.  
  34. case 'CharacterSheet':
  35. $url .= '/char/CharacterSheet.xml.aspx';
  36. break;
  37.  
  38. case 'FacWarStats':
  39. $url .= '/char/FacWarStats.xml.aspx';
  40. break;
  41.  
  42. case 'CharacterID':
  43. $url .= '/eve/CharacterID.xml.aspx';
  44. break;
  45.  
  46. case 'CharacterName':
  47. $url .= '/eve/CharacterName.xml.aspx';
  48. break;
  49.  
  50. case 'CharacterInfo':
  51. $url .= '/eve/CharacterInfo.xml.aspx';
  52. break;
  53.  
  54. case 'callList':
  55. $url .= '/api/callList.xml.aspx';
  56. break;
  57.  
  58. case 'SkillTree':
  59. $url .= '/eve/SkillTree.xml.aspx';
  60. break;
  61.  
  62. case 'SkillInTraining':
  63. $url .= '/char/SkillInTraining.xml.aspx';
  64. break;
  65.  
  66. case 'WalletJournal':
  67. $url .= '/char/WalletJournal.xml.aspx';
  68. break;
  69.  
  70. case 'Contracts':
  71. $url .= '/char/Contracts.xml.aspx';
  72. break;
  73.  
  74. case 'MailMessages':
  75. $url .= '/char/MailMessages.xml.aspx';
  76. break;
  77.  
  78. case 'MailBodies':
  79. $url .= '/char/MailBodies.xml.aspx';
  80. break;
  81.  
  82. case 'Notifications':
  83. $url .= '/char/Notifications.xml.aspx';
  84. break;
  85.  
  86. case 'NotificationTexts':
  87. $url .= '/char/NotificationTexts.xml.aspx';
  88. break;
  89.  
  90. case 'ErrorList':
  91. $url .= '/eve/ErrorList.xml.aspx';
  92. break;
  93.  
  94. default:
  95. case 'APIKeyInfo':
  96. $url .= '/account/APIKeyInfo.xml.aspx';
  97. break;
  98. }
  99.  
  100. $cache_name = 'xml' . $type . str_replace('=', '', str_replace('&', '', drupal_http_build_query($query)));
  101. $html = '';
  102. $response = array();
  103.  
  104. if ($cached = cache_get($cache_name, 'cache')) {
  105. $html = $cached->data;
  106. $response['errorCode'] = $html->code;
  107.  
  108. if (isset($html->error)) {
  109. $response['errorText'] = $html->error;
  110. }
  111. else {
  112. $response['errorText'] = 'OK';
  113. }
  114. }
  115.  
  116. if (empty($html) || $type == 'SkillTree') {
  117. if (variable_get('eve_api_cron_error_count', 0) >= variable_get('eve_api_cron_error_count_limit', 5)) {
  118. variable_set('eve_api_cron_error_time', time() + variable_get('eve_api_cron_error_time_limit', 10 * 60));
  119. variable_set('eve_api_cron_error_count', 0);
  120. watchdog('eve_api', 'EVE API has failed 5 times to pull cron data, API cron resumes at ' . date('Y-m-d H:i:s', variable_get('eve_api_cron_error_time', 0)), NULL, WATCHDOG_EMERGENCY);
  121. return array(
  122. 'errorCode' => 2,
  123. 'errorText' => 'EVE API has failed 5 times to pull cron data, API cron resumes at ' . date('Y-m-d H:i:s', variable_get('eve_api_cron_error_time', 0)),
  124. );
  125. }
  126. elseif (variable_get('eve_api_cron_error_time', 0) >= time()) {
  127. return array(
  128. 'errorCode' => 2,
  129. 'errorText' => 'EVE API has failed 5 times to pull cron data, API cron resumes at ' . date('Y-m-d H:i:s', variable_get('eve_api_cron_error_time', 0)),
  130. );
  131. }
  132.  
  133. $has_curl = function_exists('curl_init');
  134. $open_basedir = ini_get('open_basedir');
  135.  
  136.  
  137. $options = array(
  138. 'method' => 'GET',
  139. 'data' => drupal_http_build_query($query),
  140. 'timeout' => 45,
  141. 'headers' => array(
  142. 'Content-Type' => 'application/x-www-form-urlencoded',
  143. 'Accept-Encoding' => 'gzip',
  144. ),
  145. );
  146.  
  147. $html = drupal_http_request($url, $options);
  148.  
  149.  
  150. // $html = eve_api_curl_http_request($url, $options);
  151. // }
  152.  
  153.  
  154. if (isset($html->error) && !empty($html->error)) {
  155. $response['errorText'] = $html->error;
  156.  
  157. if (variable_get('eve_api_debug', FALSE)) {
  158. watchdog('eve_api', 'EVE API returned an error with the HTTP request. (' . $type . '?' . drupal_http_build_query($query) . ') Error: ' . $html->error . ' Code: ' . $html->code, NULL, WATCHDOG_ERROR);
  159. }
  160. }
  161. else {
  162. $response['errorText'] = 'OK';
  163. }
  164.  
  165. $response['errorCode'] = $html->code;
  166. }
  167.  
  168. // Fix for HTTP_REQUEST_TIMEOUT returning as -1
  169. if ($response['errorCode'] <= 0) {
  170. return array(
  171. 'errorCode' => 3,
  172. 'errorText' => $response['errorText'],
  173. );
  174. }
  175.  
  176. if (FALSE !== strpos($html->data, '<!DOCTYPE html')) {
  177. variable_set('eve_api_cron_error_count', 1 + variable_get('eve_api_cron_error_count', 0));
  178. watchdog('eve_api', 'EVE API returned an error with the HTTP request. (' . $type . '?' . drupal_http_build_query($query) . ') Error: ' . $html->error . ' Code: ' . $html->code, NULL, WATCHDOG_ERROR);
  179. $response['errorCode'] = 4;
  180. }
  181. else {
  182. try {
  183. $response['errorCode'] = 0;
  184. $response['xml'] = @new SimpleXMLElement($html->data);
  185. variable_set('eve_api_cron_error_count', 0);
  186. }
  187. catch (Exception $e) {
  188. variable_set('eve_api_cron_error_count', 1 + variable_get('eve_api_cron_error_count', 0));
  189. watchdog('eve_api', 'EVE API returned an error with the HTTP request. (' . $type . '?' . drupal_http_build_query($query) . ') Error: ' . $e->getMessage(), NULL, WATCHDOG_ERROR);
  190. $response['errorCode'] = 5;
  191. $response['xml'] = NULL;
  192. }
  193. }
  194.  
  195. if (isset($response['xml']->error)) {
  196. $error = $response['xml']->error->attributes();
  197. $response['errorCode'] = $error->code;
  198. $response['errorText'] = $response['xml']->error;
  199.  
  200. if (variable_get('eve_api_debug', FALSE)) {
  201. watchdog('eve_api', 'EVE API returned an error with the API data. (' . $type . '?' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_NOTICE);
  202. }
  203.  
  204. if (eve_api_error_code($response['errorCode'])) {
  205. //variable_set('eve_api_cron_error_count', 1 + variable_get('eve_api_cron_error_count', 0));
  206. }
  207. else {
  208. variable_set('eve_api_cron_error_count', 0);
  209. }
  210. }
  211. elseif (isset($response['xml']->cachedUntil)) {
  212. variable_set('eve_api_cron_error_count', 0);
  213. }
  214.  
  215. if (isset($response['xml']->cachedUntil)) {
  216. cache_set($cache_name, $html, 'cache', strtotime($response['xml']->cachedUntil));
  217. }
  218.  
  219. unset($html);
  220.  
  221. return $response;
  222. }
  223.  
  224. /**
  225. * Performs an HTTP request.
  226. *
  227. * This is a flexible and powerful HTTP client implementation. Correctly
  228. * handles GET, POST, PUT or any other HTTP requests. Handles redirects.
  229. * Based on "cURL HTTP Request v1.5" by Vincenzo and minorOffense
  230. * Based on "Yapeal (http://code.google.com/p/yapeal/)" by Kevin Burkholder
  231. * Modified for use with "Omni EVE API"
  232. *
  233. *
  234. * @param string $url
  235. * A string containing a fully qualified URI.
  236. * @param array $options
  237. * (optional) An array composed of various options.
  238. *
  239. * @return object
  240. * An object that can have one or more of the following components:
  241. * - request: A string containing the request body that was sent.
  242. * - code: An integer containing the response status code, or the error code
  243. * if an error occurred.
  244. * - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
  245. * - status_message: The status message from the response, if a response was
  246. * received.
  247. * - redirect_code: If redirected, an integer containing the initial response
  248. * status code.
  249. * - redirect_url: If redirected, a string containing the URL of the redirect
  250. * target.
  251. * - error: If an error occurred, the error message. Otherwise not set.
  252. * - errno: If an error occurred, a cURL error number greater than 0.
  253. * Otherwise set to 0.
  254. * - headers: An array containing the response headers as name/value pairs.
  255. * HTTP header names are case-insensitive (RFC 2616, section 4.2), so for
  256. * easy access the array keys are returned in lower case.
  257. * - data: A string containing the response body that was received.
  258. */
  259. function eve_api_curl_http_request($url, array $options = array()) {
  260. $result = new stdClass();
  261.  
  262. // Parse the URL and make sure we can handle the schema.
  263. $uri = @parse_url($url);
  264.  
  265.  
  266. if ($uri == FALSE) {
  267. $result->error = 'unable to parse URL';
  268. $result->code = -1001;
  269. return $result;
  270. }
  271.  
  272. if (!isset($uri['scheme'])) {
  273. $result->error = 'missing schema';
  274. $result->code = -1002;
  275. return $result;
  276. }
  277.  
  278. timer_start(__FUNCTION__);
  279.  
  280. // Merge the default options.
  281. $options = $options + array(
  282. 'data' => array(),
  283. 'headers' => array(),
  284. 'method' => 'POST',
  285. 'timeout' => 45,
  286. 'http_proxy' => variable_get('http_proxy'),
  287. 'https_proxy' => variable_get('https_proxy'),
  288. );
  289.  
  290. // Select the right proxy for the right protocol.
  291. $proxy = ('https' == $uri['scheme']) ? $options['https_proxy'] : $options['http_proxy'];
  292.  
  293. // Nullify the proxy if the host to send the request to is part of the proxy's
  294. // exceptions.
  295. if ((!empty($proxy['exceptions'])) && (array_key_exists($uri['host'], $proxy['exceptions']))) {
  296. $proxy = NULL;
  297. }
  298.  
  299. $curl = curl_version();
  300. $user_agent = 'Drupal (+http://drupal.org/)';
  301. $user_agent .= ' Omni EVE API/' . eve_api_version();
  302. $user_agent .= ' (' . PHP_OS . ' ' . php_uname('m') . ')';
  303. $user_agent .= ' libcurl/' . $curl['version'];
  304. $user_agent = trim($user_agent);
  305.  
  306. $curl_opt = array(
  307. CURLOPT_HEADER => TRUE,
  308. CURLINFO_HEADER_OUT => TRUE,
  309. CURLOPT_TIMEOUT => $options['timeout'],
  310. CURLOPT_FOLLOWLOCATION => TRUE,
  311. CURLOPT_RETURNTRANSFER => TRUE,
  312. CURLOPT_MAXREDIRS => 5,
  313. CURLOPT_NOBODY => FALSE,
  314. CURLOPT_FORBID_REUSE => FALSE,
  315. CURLOPT_LOW_SPEED_LIMIT => 10,
  316. CURLOPT_LOW_SPEED_TIME => ceil($options['timeout'] / 4),
  317. CURLOPT_MAXCONNECTS => 5,
  318. CURLOPT_CONNECTTIMEOUT => ceil($options['timeout'] / 2),
  319. CURLOPT_REFERER => 'http://danlee.ca/git/',
  320. CURLOPT_UNRESTRICTED_AUTH => TRUE,
  321. CURLOPT_ENCODING => 'gzip',
  322. CURLOPT_BINARYTRANSFER => FALSE,
  323. CURLOPT_USERAGENT => $user_agent,
  324. CURLOPT_SSL_VERIFYPEER => TRUE,
  325. CURLOPT_SSL_VERIFYHOST => 2,
  326. CURLOPT_CAINFO => drupal_get_path('module', 'eve_api') . '/eveonline.crt',
  327. CURLOPT_COOKIEJAR => drupal_get_path('module', 'eve_api') . '/cookies.txt',
  328. );
  329.  
  330. $ssl_version = $curl['ssl_version'];
  331. $has_nss = (strpos($ssl_version, "NSS") > -1);
  332.  
  333. if ($has_nss) {
  334. $curl_opt += array(CURLOPT_SSL_CIPHER_LIST => 'rsa_aes_128_sha,rsa_aes_256_sha,rsa_3des_sha,rsa_rc4_128_sha,rsa_rc4_128_md5');
  335. }
  336. else {
  337. $curl_opt += array(CURLOPT_SSL_CIPHER_LIST => 'AES128-SHA AES256-SHA DES-CBC3-SHA RC4-SHA RC4-MD5');
  338. }
  339.  
  340. if (!empty($proxy)) {
  341. $proxy_options = array(
  342. CURLOPT_PROXY => $proxy['server'],
  343. CURLOPT_PROXYPORT => $proxy['port'],
  344. );
  345. // For the time being let's just support HTTP proxies with basic
  346. // authentication.
  347. if (isset($proxy['username']) && isset($proxy['password'])) {
  348. $proxy_options += array(
  349. CURLOPT_PROXYUSERPWD => implode(':', array(
  350. $proxy['username'],
  351. $proxy['password'],
  352. )),
  353. CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
  354. CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
  355. );
  356. }
  357. $curl_opt += $proxy_options;
  358. }
  359.  
  360. $default_ports = array(
  361. 'http' => 80,
  362. 'feed' => 80,
  363. 'https' => 443,
  364. );
  365.  
  366. if (array_key_exists($uri['scheme'], $default_ports)) {
  367. if (!isset($uri['port'])) {
  368. $uri['port'] = $default_ports[$uri['scheme']];
  369. }
  370. // RFC 2616: "non-standard ports MUST, default ports MAY be included".
  371. // We don't add the standard port to prevent from breaking rewrite rules
  372. // checking the host that do not take into account the port number.
  373. $options['headers']['Host'] = $uri['host'] . ($uri['port'] != 80 ? ':' . $uri['port'] : '');
  374. }
  375. else {
  376. $result->error = 'invalid schema ' . $uri['scheme'];
  377. $result->code = -1003;
  378. return $result;
  379. }
  380.  
  381. // Merge the default headers.
  382. $options['headers'] += array(
  383. 'User-Agent' => 'Drupal (+http://drupal.org/)',
  384. );
  385.  
  386. // Only add Content-Length if we actually have any content or if it is a POST
  387. // or PUT request. Some non-standard servers get confused by Content-Length in
  388. // at least HEAD/GET requests, and Squid always requires Content-Length in
  389. // POST/PUT requests.
  390. $content_length = strlen(str_replace('%2C', ',', drupal_http_build_query($options['data'])));
  391. if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
  392. $options['headers']['Content-Length'] = $content_length;
  393. }
  394.  
  395. // If the server URL has a user then attempt to use basic authentication.
  396. if (isset($uri['user'])) {
  397. $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ''));
  398. }
  399.  
  400. // Set all the headers.
  401. $curl_opt[CURLOPT_HTTPHEADER] = array();
  402. foreach ($options['headers'] as $name => $value) {
  403. $curl_opt[CURLOPT_HTTPHEADER][] = $name . ": " . trim($value);
  404. }
  405.  
  406. // Set the request method.
  407. switch ($options['method']) {
  408. case 'GET':
  409. $curl_opt[CURLOPT_HTTPGET] = TRUE;
  410. break;
  411.  
  412. case 'POST':
  413. $curl_opt[CURLOPT_POST] = TRUE;
  414. if (!empty($options['data'])) {
  415. $curl_opt[CURLOPT_POSTFIELDS] = str_replace('%2C', ',', drupal_http_build_query($options['data']));
  416. $options['headers'] += array(
  417. 'Content-Type' => 'application/x-www-form-urlencoded',
  418. );
  419. }
  420. break;
  421.  
  422. case 'PUT':
  423. $curl_opt[CURLOPT_PUT] = TRUE;
  424. break;
  425.  
  426. default:
  427. $result->error = 'invalid method ' . $options['method'];
  428. $result->code = -1004;
  429. return $result;
  430. }
  431.  
  432. // Make the request.
  433. $ch = curl_init($url);
  434. curl_setopt_array($ch, $curl_opt);
  435.  
  436. // Full response stored. To be parsed later on and split in protocol, code,
  437. // status message, and response headers.
  438. $result->data = trim(curl_exec($ch));
  439. $result->error = curl_error($ch);
  440. $result->errno = curl_errno($ch);
  441.  
  442. // If there's been an error, do not continue.
  443. if ($result->error) {
  444. // Request timed out.
  445. if (CURLE_OPERATION_TIMEOUTED == $result->errno) {
  446. $result->code = HTTP_REQUEST_TIMEOUT;
  447. $result->error = 'request timed out';
  448. return $result;
  449. }
  450. $result->code = $result->errno;
  451. return $result;
  452. }
  453.  
  454. // The last effective URL should correspond to the Redirect URL.
  455. $result->redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  456.  
  457. // Save the request sent into the result object.
  458. $result->request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  459.  
  460. // Parse response headers from the response body.
  461. // Be tolerant of malformed HTTP responses that separate header and body with
  462. // \n\n or \r\r instead of \r\n\r\n.
  463. list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $result->data, 2);
  464.  
  465. // Sometimes when making an HTTP request via proxy using cURL, you end up with
  466. // a multiple set of headers:
  467. // from the web server being the actual target, from the proxy itself, etc.
  468. // The following 'if' statement is to check for such a situation and make sure
  469. // we get a proper split between
  470. // actual response body and actual response headers both coming from the web
  471. // server.
  472. while ('HTTP/' == substr($result->data, 0, 5)) {
  473. list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $result->data, 2);
  474. }
  475.  
  476. $response = preg_split("/\r\n|\n|\r/", $response);
  477.  
  478. // Parse the response status line.
  479. list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
  480. $result->protocol = $protocol;
  481. $result->status_message = $status_message;
  482.  
  483. $result->headers = array();
  484.  
  485. // Parse the response headers.
  486. while ($line = trim(array_shift($response))) {
  487. list($name, $value) = explode(':', $line, 2);
  488. $name = strtolower($name);
  489. if (isset($result->headers[$name]) && $name == 'set-cookie') {
  490. // RFC 2109: the Set-Cookie response header comprises the token Set-
  491. // Cookie:, followed by a comma-separated list of one or more cookies.
  492. $result->headers[$name] .= ',' . trim($value);
  493. }
  494. else {
  495. $result->headers[$name] = trim($value);
  496. }
  497. }
  498.  
  499. $responses = array(
  500. 100 => 'Continue',
  501. 101 => 'Switching Protocols',
  502. 200 => 'OK',
  503. 201 => 'Created',
  504. 202 => 'Accepted',
  505. 203 => 'Non-Authoritative Information',
  506. 204 => 'No Content',
  507. 205 => 'Reset Content',
  508. 206 => 'Partial Content',
  509. 300 => 'Multiple Choices',
  510. 301 => 'Moved Permanently',
  511. 302 => 'Found',
  512. 303 => 'See Other',
  513. 304 => 'Not Modified',
  514. 305 => 'Use Proxy',
  515. 307 => 'Temporary Redirect',
  516. 400 => 'Bad Request',
  517. 401 => 'Unauthorized',
  518. 402 => 'Payment Required',
  519. 403 => 'Forbidden',
  520. 404 => 'Not Found',
  521. 405 => 'Method Not Allowed',
  522. 406 => 'Not Acceptable',
  523. 407 => 'Proxy Authentication Required',
  524. 408 => 'Request Time-out',
  525. 409 => 'Conflict',
  526. 410 => 'Gone',
  527. 411 => 'Length Required',
  528. 412 => 'Precondition Failed',
  529. 413 => 'Request Entity Too Large',
  530. 414 => 'Request-URI Too Large',
  531. 415 => 'Unsupported Media Type',
  532. 416 => 'Requested range not satisfiable',
  533. 417 => 'Expectation Failed',
  534. 500 => 'Internal Server Error',
  535. 501 => 'Not Implemented',
  536. 502 => 'Bad Gateway',
  537. 503 => 'Service Unavailable',
  538. 504 => 'Gateway Time-out',
  539. 505 => 'HTTP Version not supported',
  540. );
  541. // RFC 2616 states that all unknown HTTP codes must be treated the same as the
  542. // base code in their class.
  543. if (!isset($responses[$code])) {
  544. $code = floor($code / 100) * 100;
  545. }
  546. $result->code = $code;
  547.  
  548. switch ($code) {
  549. case 200:
  550. // OK.
  551. case 304:
  552. // Not modified.
  553. break;
  554.  
  555. case 301:
  556. // Moved permanently.
  557. case 302:
  558. // Moved temporarily.
  559. case 307:
  560. // Moved temporarily.
  561. $location = $result->headers['location'];
  562. $options['timeout'] -= timer_read(__FUNCTION__) / 1000;
  563. if ($options['timeout'] <= 0) {
  564. $result->code = HTTP_REQUEST_TIMEOUT;
  565. $result->error = 'request timed out';
  566. }
  567. elseif ($options['max_redirects']) {
  568. // Redirect to the new location.
  569. $options['max_redirects']--;
  570. $result = curl_http_request($location, $options);
  571. $result->redirect_code = $code;
  572. }
  573. if (!isset($result->redirect_url)) {
  574. $result->redirect_url = $location;
  575. }
  576. break;
  577.  
  578. default:
  579. $result->error = $status_message;
  580. }
  581.  
  582. curl_close($ch);
  583. return $result;
  584. }
  585.  
  586. /**
  587. * Function to retrieve individual characters from the database.
  588. *
  589. * @param string $character
  590. * Unique character name used to retrieve information about the character
  591. *
  592. * @return array
  593. * A complex array of a single character information.
  594. */
  595. function eve_api_get_basic_character_info($character) {
  596. $result = db_query('SELECT c.uid, c.deleted, c.errorCode, c.errorText, c.apiID, c.characterName, c.characterID, c.corporationName, c.corporationID, c.corporationTicker, c.allianceID, c.allianceName, c.allianceTicker
  597. FROM {eve_api_characters} c
  598. WHERE c.characterName = :characterName OR c.characterID = :characterID', array(
  599. ':characterName' => (string) $character,
  600. ':characterID' => (int) $character,
  601. ));
  602.  
  603. $api_data = array();
  604.  
  605. if ($result->rowCount()) {
  606. foreach ($result->fetchAssoc() as $name => $data) {
  607. $api_data += array($name => strip_tags(decode_entities($data)));
  608. }
  609. }
  610. else {
  611. $api_data = FALSE;
  612. }
  613.  
  614. return $api_data;
  615. }
  616.  
  617. /**
  618. * Function to retrieve individual characters from the database.
  619. *
  620. * @param string $character
  621. * Unique character name used to retrieve information about the character
  622. *
  623. * @return array
  624. * A complex array of a single character information.
  625. */
  626. function eve_api_get_character_info($character) {
  627. $result = db_query('SELECT c.accessMask, c.uid, st.skillInTraining, c.characterName, c.characterID
  628. FROM {eve_api_characters} c
  629. INNER JOIN {eve_api_skill_in_training} st ON st.characterID = c.characterID
  630. WHERE c.characterName = :characterName OR c.characterID = :characterID', array(
  631. ':characterName' => (string) $character,
  632. ':characterID' => (int) $character,
  633. ));
  634.  
  635. $row = $result->fetchAssoc();
  636.  
  637. if (empty($row)) {
  638. return FALSE;
  639. }
  640.  
  641. $access_mask = $row['accessMask'];
  642. $is_training = $row['skillInTraining'];
  643. $uid = $row['uid'];
  644. $character_id = $row['characterID'];
  645.  
  646. $query = db_select('eve_api_characters', 'c');
  647. $query->join('eve_api_character_sheet', 'cs', 'cs.characterID = c.characterID');
  648. $query->join('eve_api_character_info', 'ci', 'ci.characterID = c.characterID');
  649. $query = $query->fields('c', array(
  650. 'uid',
  651. 'deleted',
  652. 'errorCode',
  653. 'errorText',
  654. 'apiID',
  655. 'characterName',
  656. 'characterID',
  657. 'corporationName',
  658. 'corporationID',
  659. 'corporationTicker',
  660. 'allianceID',
  661. 'allianceName',
  662. 'allianceTicker',
  663. 'accessMask',
  664. ))
  665. ->fields('cs', array(
  666. 'DoB',
  667. 'race',
  668. 'bloodLine',
  669. 'ancestry',
  670. 'gender',
  671. 'cloneName',
  672. 'balance',
  673. 'intelligence',
  674. 'memory',
  675. 'charisma',
  676. 'perception',
  677. 'willpower',
  678. ))
  679. ->fields('ci', array(
  680. 'skillPoints',
  681. 'shipName',
  682. 'shipTypeID',
  683. 'shipTypeName',
  684. 'corporationDate',
  685. 'allianceDate',
  686. 'lastKnownLocation',
  687. 'securityStatus',
  688. 'nextTrainingEnds',
  689. ));
  690.  
  691. if (($access_mask & 131072) == 131072) {
  692. $query->join('eve_api_skill_in_training', 'st', 'st.characterID = c.characterID');
  693.  
  694. if ($is_training != 0) {
  695. $query->join('eve_api_skill_tree', 'sts', 'sts.typeID = st.trainingTypeID');
  696. $query->fields('sts', array('typeName'));
  697. }
  698.  
  699. $query->fields('st', array(
  700. 'trainingEndTime',
  701. 'trainingStartTime',
  702. 'trainingStartSP',
  703. 'trainingDestinationSP',
  704. 'trainingToLevel',
  705. 'skillInTraining',
  706. ));
  707. $query->addField('st', 'trainingTypeID', 'typeID');
  708. }
  709.  
  710. $result = $query->condition('c.characterID', (int) $character_id, '=')->execute();
  711.  
  712. $api_data = array();
  713.  
  714. if ($result->rowCount()) {
  715. foreach ($result->fetchAssoc() as $name => $data) {
  716. $api_data += array($name => strip_tags(decode_entities($data)));
  717. }
  718.  
  719. $result = db_query('SELECT csae.bonusName, csae.augmentatorValue
  720. FROM {eve_api_character_sheet_attribute_enhancers} csae
  721. LEFT JOIN {eve_api_characters} c ON c.characterID = csae.characterID
  722. WHERE c.characterID = :characterID', array(
  723. ':characterID' => (int) $character_id,
  724. ));
  725.  
  726. if ($result->rowCount()) {
  727. foreach ($result->fetchAll() as $row) {
  728. $api_data += array($row->bonusName => strip_tags(decode_entities($row->augmentatorValue)));
  729. }
  730. }
  731.  
  732. $result = db_query('SELECT cich.recordID, cich.corporationID, cich.startDate
  733. FROM {eve_api_character_info_corporation_history} cich
  734. LEFT JOIN {eve_api_characters} c ON c.characterID = cich.characterID
  735. WHERE c.characterID = :characterID', array(
  736. ':characterID' => (int) $character_id,
  737. ));
  738.  
  739. if ($result->rowCount()) {
  740. foreach ($result->fetchAll() as $row) {
  741. $history_corp = array();
  742.  
  743. foreach ($row as $name => $data) {
  744. $history_corp += array($name => strip_tags(decode_entities($data)));
  745. }
  746.  
  747. $api_data['history'][] = $history_corp;
  748. }
  749. }
  750.  
  751. $result = db_query('SELECT deleted, errorCode, errorText, characterName, characterID, characterName, characterID, corporationName, corporationID, corporationTicker, allianceID, allianceName, allianceTicker
  752. FROM {eve_api_characters}
  753. WHERE uid = :uid', array(
  754. ':uid' => (string) $uid,
  755. ));
  756.  
  757. if ($result->rowCount()) {
  758. foreach ($result->fetchAll() as $row) {
  759. $characters = array();
  760.  
  761. foreach ($row as $name => $data) {
  762. $characters += array($name => strip_tags(decode_entities($data)));
  763. }
  764.  
  765. $api_data['characters'][] = $characters;
  766. }
  767. }
  768.  
  769. $result = db_query('SELECT apiID, keyID, vCode, errorCode, errorText
  770. FROM {eve_api_keys}
  771. WHERE uid = :uid', array(
  772. ':uid' => (string) $uid,
  773. ));
  774.  
  775. if ($result->rowCount()) {
  776. foreach ($result->fetchAll() as $row) {
  777. $api_keys = array();
  778.  
  779. foreach ($row as $name => $data) {
  780. $api_keys += array($name => strip_tags(decode_entities($data)));
  781. }
  782.  
  783. $api_data['api_keys'][] = $api_keys;
  784. }
  785. }
  786. }
  787.  
  788. return $api_data;
  789. }
  790.  
  791. /**
  792. * Function to retrieve individual characters skills from the database.
  793. *
  794. * @param int $character_id
  795. * Unique character id used to retrieve information about the character
  796. *
  797. * @return array
  798. * A complex array of a single character skills.
  799. */
  800. function eve_api_get_character_skills($character_id) {
  801. $result = db_query('SELECT css.level, css.skillpoints, st.typeName, sg.groupName, sg.groupID, st.typeID
  802. FROM {eve_api_character_sheet_skills} css
  803. INNER JOIN {eve_api_skill_tree} st ON st.typeID = css.typeID
  804. INNER JOIN {eve_api_skill_tree_groups} sg ON sg.groupID = st.groupID
  805. WHERE css.characterID = :characterID ORDER BY sg.groupName, st.typeName ASC', array(
  806. ':characterID' => (int) $character_id,
  807. ));
  808.  
  809. $api_data = array();
  810.  
  811. if ($result->rowCount()) {
  812. foreach ($result->fetchAll() as $row) {
  813. $api_data[(string) $row->groupName][] = array(
  814. 'level' => check_plain((int) $row->level),
  815. 'groupID' => check_plain((int) $row->groupID),
  816. 'typeID' => check_plain((int) $row->typeID),
  817. 'typeName' => check_plain((string) $row->typeName),
  818. 'skillpoints' => check_plain((int) $row->skillpoints),
  819. );
  820. }
  821. }
  822.  
  823. return $api_data;
  824. }
  825.  
  826. /**
  827. * Function to retrieve individual characters API info.
  828. *
  829. * @param array $query
  830. * The URL variables to pass through the URL.
  831. *
  832. * @return array
  833. * A complex array of multiple character information.
  834. */
  835. function eve_api_get_api_key_info_api($query) {
  836. $response = eve_api_get_xml('APIKeyInfo', $query);
  837.  
  838. if (!isset($response['xml']->result)) {
  839. if (variable_get('eve_api_debug', FALSE)) {
  840. watchdog('eve_api', 'Failed getting API Key Info API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  841. }
  842.  
  843. unset($response['xml']);
  844. return array('error' => TRUE) + $response;
  845. }
  846.  
  847. $api_data = array();
  848. $api_info = $response['xml']->result->key->attributes();
  849. $api_data['accessMask'] = (int) $api_info->accessMask;
  850. $api_data['type'] = (string) $api_info->type;
  851. $api_data['expires'] = ((string) $api_info->expires) ? TRUE : FALSE;
  852. $api_data['errorCode'] = (int) $response['errorCode'];
  853. $api_data['errorText'] = (string) $response['errorText'];
  854.  
  855. foreach ($response['xml']->result->key->rowset->children() as $rows) {
  856. $row = $rows->attributes();
  857.  
  858. $query = array('corporationID' => (int) $row->corporationID);
  859. $corp_info = eve_api_get_corporation_sheet_api($query);
  860.  
  861. if (isset($corp_info['error'])) {
  862. return array('error' => $corp_info['error']);
  863. }
  864. else {
  865. $api_data['characters'][(int) $row->characterID] = array_merge(array(
  866. 'characterName' => (string) $row->characterName,
  867. 'characterID' => (int) $row->characterID,
  868. 'corporationName' => (string) $row->corporationName,
  869. 'corporationID' => (int) $row->corporationID,
  870. ), $corp_info);
  871. }
  872. }
  873.  
  874. return $api_data;
  875. }
  876.  
  877. /**
  878. * Function to retrieve a single characters skills in training.
  879. *
  880. * @param array $query
  881. * The URL variables to pass through the URL.
  882. *
  883. * @return array
  884. * A complex array of character information.
  885. */
  886. function eve_api_get_skill_in_training_api($query) {
  887. $response = eve_api_get_xml('SkillInTraining', $query);
  888.  
  889. if (!isset($response['xml']->result)) {
  890. if (variable_get('eve_api_debug', FALSE)) {
  891. watchdog('eve_api', 'Failed getting Skill in Training API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  892. }
  893.  
  894. unset($response['xml']);
  895. return array('error' => TRUE) + $response;
  896. }
  897.  
  898. $api_data = array();
  899. $api_data['errorCode'] = (int) $response['errorCode'];
  900. $api_data['errorText'] = (string) $response['errorText'];
  901.  
  902. foreach ($response['xml']->result->children() as $result) {
  903. $api_data['info'][(string) $result->getName()] = (string) $result;
  904. }
  905.  
  906. return $api_data;
  907. }
  908.  
  909. /**
  910. * Function to retrieve a single characters wallet journal.
  911. *
  912. * @param array $query
  913. * The URL variables to pass through the URL.
  914. *
  915. * @return array
  916. * A complex array of character information.
  917. */
  918. function eve_api_get_wallet_journal_api($query) {
  919. $response = eve_api_get_xml('WalletJournal', $query);
  920.  
  921. if (!isset($response['xml']->result)) {
  922. if (variable_get('eve_api_debug', FALSE)) {
  923. watchdog('eve_api', 'Failed getting Wallet Journal API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  924. }
  925.  
  926. unset($response['xml']);
  927. return array('error' => TRUE) + $response;
  928. }
  929.  
  930. $api_data = array();
  931. $api_data['errorCode'] = (int) $response['errorCode'];
  932. $api_data['errorText'] = (string) $response['errorText'];
  933.  
  934. $count = 0;
  935.  
  936. foreach ($response['xml']->result->rowset->children() as $rows) {
  937. foreach ($rows->attributes() as $name => $value) {
  938. $api_data['wallet_journal'][(int) $count][(string) $name] = (string) $value;
  939. }
  940.  
  941. $count++;
  942. }
  943.  
  944. return $api_data;
  945. }
  946.  
  947. /**
  948. * Function to retrieve a single characters contracts.
  949. *
  950. * @param array $query
  951. * The URL variables to pass through the URL.
  952. *
  953. * @return array
  954. * A complex array of character information.
  955. */
  956. function eve_api_get_contracts_api($query) {
  957. $response = eve_api_get_xml('Contracts', $query);
  958.  
  959. if (!isset($response['xml']->result)) {
  960. if (variable_get('eve_api_debug', FALSE)) {
  961. watchdog('eve_api', 'Failed getting Contracts API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  962. }
  963.  
  964. unset($response['xml']);
  965. return array('error' => TRUE) + $response;
  966. }
  967.  
  968. $api_data = array();
  969. $api_data['errorCode'] = (int) $response['errorCode'];
  970. $api_data['errorText'] = (string) $response['errorText'];
  971.  
  972. $count = 0;
  973.  
  974. foreach ($response['xml']->result->rowset->children() as $rows) {
  975. foreach ($rows->attributes() as $name => $value) {
  976. $api_data['contracts'][(int) $count][(string) $name] = (string) $value;
  977. }
  978.  
  979. $count++;
  980. }
  981.  
  982. return $api_data;
  983. }
  984.  
  985. /**
  986. * Function to retrieve a single characters mail headers.
  987. *
  988. * @param array $query
  989. * The URL variables to pass through the URL.
  990. *
  991. * @return array
  992. * A complex array of character information.
  993. */
  994. function eve_api_get_mail_messages_api($query) {
  995. $response = eve_api_get_xml('MailMessages', $query);
  996.  
  997. if (!isset($response['xml']->result)) {
  998. if (variable_get('eve_api_debug', FALSE)) {
  999. watchdog('eve_api', 'Failed getting Mail Messages API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1000. }
  1001.  
  1002. unset($response['xml']);
  1003. return array('error' => TRUE) + $response;
  1004. }
  1005.  
  1006. $api_data = array();
  1007. $api_data['errorCode'] = (int) $response['errorCode'];
  1008. $api_data['errorText'] = (string) $response['errorText'];
  1009.  
  1010. foreach ($response['xml']->result->rowset->children() as $rows) {
  1011. $row = $rows->attributes();
  1012.  
  1013. foreach ($rows->attributes() as $name => $value) {
  1014. $api_data['mail'][(int) $row->messageID][(string) $name] = (string) $value;
  1015. }
  1016. }
  1017.  
  1018. return $api_data;
  1019. }
  1020.  
  1021. /**
  1022. * Function to retrieve a single characters mail bodies.
  1023. *
  1024. * @param array $query
  1025. * The URL variables to pass through the URL.
  1026. *
  1027. * @return array
  1028. * A complex array of character information.
  1029. */
  1030. function eve_api_get_mail_bodies_api($query) {
  1031. $response = eve_api_get_xml('MailBodies', $query);
  1032.  
  1033. if (!isset($response['xml']->result) && !isset($response['xml']->result->missingMessageIDs)) {
  1034. if (variable_get('eve_api_debug', FALSE)) {
  1035. watchdog('eve_api', 'Failed getting Mail Bodies API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1036. }
  1037.  
  1038. unset($response['xml']);
  1039. return array('error' => TRUE) + $response;
  1040. }
  1041.  
  1042. $api_data = array();
  1043. $api_data['errorCode'] = (int) $response['errorCode'];
  1044. $api_data['errorText'] = (string) $response['errorText'];
  1045.  
  1046. if (isset($response['xml']->result->missingMessageIDs)) {
  1047. $api_data['missingMessageIDs'] = (string) $response['xml']->result->missingMessageIDs;
  1048. }
  1049.  
  1050. foreach ($response['xml']->result->rowset->children() as $rows) {
  1051. $row = $rows->attributes();
  1052.  
  1053. $api_data['mail_body'][(int) $row->messageID]['message'] = (string) $rows;
  1054.  
  1055. foreach ($rows->attributes() as $name => $value) {
  1056. $api_data['mail_body'][(int) $row->messageID][(string) $name] = (string) $value;
  1057. }
  1058. }
  1059.  
  1060. return $api_data;
  1061. }
  1062.  
  1063. /**
  1064. * Function to retrieve a single characters character info.
  1065. *
  1066. * @param array $query
  1067. * The URL variables to pass through the URL.
  1068. *
  1069. * @return array
  1070. * A complex array of character information.
  1071. */
  1072. function eve_api_get_character_info_api($query) {
  1073. $response = eve_api_get_xml('CharacterInfo', $query);
  1074.  
  1075. if (!isset($response['xml']->result)) {
  1076. if (variable_get('eve_api_debug', FALSE)) {
  1077. watchdog('eve_api', 'Failed getting Character Sheet API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1078. }
  1079.  
  1080. unset($response['xml']);
  1081. return array('error' => TRUE) + $response;
  1082. }
  1083.  
  1084. $api_data = array();
  1085. $api_data['errorCode'] = (int) $response['errorCode'];
  1086. $api_data['errorText'] = (string) $response['errorText'];
  1087.  
  1088. foreach ($response['xml']->result->children() as $results) {
  1089. switch ((string) $results->getName()) {
  1090. case 'rowset':
  1091. foreach ($results->children() as $rows) {
  1092. $row = $rows->attributes();
  1093.  
  1094. $api_data['employmentHistory'][(int) $row->recordID] = array(
  1095. 'recordID' => (int) $row->recordID,
  1096. 'corporationID' => (int) $row->corporationID,
  1097. 'startDate' => (string) $row->startDate,
  1098. );
  1099. }
  1100. break;
  1101.  
  1102. default:
  1103. $api_data['info'][(string) $results->getName()] = (string) $results;
  1104. break;
  1105. }
  1106. }
  1107.  
  1108. return $api_data;
  1109. }
  1110.  
  1111. /**
  1112. * Function to retrieve a single characters character sheet.
  1113. *
  1114. * @param array $query
  1115. * The URL variables to pass through the URL.
  1116. *
  1117. * @return array
  1118. * A complex array of character sheet information.
  1119. */
  1120. function eve_api_get_character_sheet_api($query) {
  1121. $response = eve_api_get_xml('CharacterSheet', $query);
  1122.  
  1123. if (!isset($response['xml']->result)) {
  1124. if (variable_get('eve_api_debug', FALSE)) {
  1125. watchdog('eve_api', 'Failed getting Character Sheet API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1126. }
  1127.  
  1128. unset($response['xml']);
  1129. return array('error' => TRUE) + $response;
  1130. }
  1131.  
  1132. $api_data = array();
  1133. $api_data['errorCode'] = (int) $response['errorCode'];
  1134. $api_data['errorText'] = (string) $response['errorText'];
  1135.  
  1136. foreach ($response['xml']->result->children() as $results) {
  1137. switch ((string) $results->getName()) {
  1138. case 'rowset':
  1139. $rowset = $results->attributes();
  1140.  
  1141. foreach ($results->children() as $rows) {
  1142. $row = $rows->attributes();
  1143.  
  1144. switch ((string) $rowset->name) {
  1145. case 'skills':
  1146. $api_data['skills'][] = array(
  1147. 'typeID' => (int) $row->typeID,
  1148. 'skillpoints' => (int) $row->skillpoints,
  1149. 'level' => (int) $row->level,
  1150. 'published' => (int) $row->published,
  1151. );
  1152. break;
  1153.  
  1154. case 'certificates':
  1155. $api_data['certificates'][]['certificateID'] = (int) $row->certificateID;
  1156. break;
  1157.  
  1158. case 'corporationTitles':
  1159. $api_data['corporationTitles'][] = array(
  1160. 'titleID' => (int) $row->titleID,
  1161. 'titleName' => (string) $row->titleName,
  1162. );
  1163. break;
  1164.  
  1165. default:
  1166. $api_data['roles'][(string) $rowset->name][] = array(
  1167. 'roleID' => (int) $row->roleID,
  1168. 'roleName' => (string) $row->roleName,
  1169. );
  1170. break;
  1171. }
  1172. }
  1173. break;
  1174.  
  1175. case 'attributeEnhancers':
  1176. foreach ($results->children() as $rows) {
  1177. foreach ($rows->children() as $row) {
  1178. $api_data['attributeEnhancers'][(string) $rows->getName()][(string) $row->getName()] = (string) $row;
  1179. }
  1180. }
  1181. break;
  1182.  
  1183. case 'attributes':
  1184. foreach ($results->children() as $rows) {
  1185. $api_data['info'][(string) $rows->getName()] = (int) $rows;
  1186. }
  1187. break;
  1188.  
  1189. default:
  1190. $api_data['info'][(string) $results->getName()] = (string) $results;
  1191. break;
  1192. }
  1193. }
  1194.  
  1195. return $api_data;
  1196. }
  1197.  
  1198. /**
  1199. * Function to retrieve skill definitions.
  1200. *
  1201. * @return array
  1202. * A complex array of skill definitions.
  1203. */
  1204. function eve_api_get_skill_tree_api() {
  1205. $response = eve_api_get_xml('SkillTree');
  1206.  
  1207. if (!isset($response['xml']->result)) {
  1208. if (variable_get('eve_api_debug', FALSE)) {
  1209. watchdog('eve_api', 'Failed getting Skill Tree API Data. Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1210. }
  1211.  
  1212. unset($response['xml']);
  1213. return array('error' => TRUE) + $response;
  1214. }
  1215.  
  1216. $api_data = array();
  1217. $api_data['errorCode'] = (int) $response['errorCode'];
  1218. $api_data['errorText'] = (string) $response['errorText'];
  1219.  
  1220. foreach ($response['xml']->result->rowset->children() as $rowsets) {
  1221. $rowset = $rowsets->attributes();
  1222.  
  1223. $api_data['group'][(int) $rowset->groupID] = array(
  1224. 'groupID' => (int) $rowset->groupID,
  1225. 'groupName' => (string) $rowset->groupName,
  1226. );
  1227.  
  1228. foreach ($rowsets->rowset->children() as $rows) {
  1229. $row = $rows->attributes();
  1230.  
  1231. $api_data['type'][(int) $row->typeID] = array(
  1232. 'typeID' => (int) $row->typeID,
  1233. 'typeName' => (string) $row->typeName,
  1234. 'groupID' => (int) $row->groupID,
  1235. );
  1236. }
  1237. }
  1238.  
  1239. return $api_data;
  1240. }
  1241.  
  1242. /**
  1243. * Function to retrieve the alliance tags.
  1244. *
  1245. * @param int $alliance_id
  1246. * The Unique ID for an alliance.
  1247. *
  1248. * @return string
  1249. * The Alliance ticker for the $allianceID.
  1250. */
  1251. function eve_api_get_alliance_ticker($alliance_id) {
  1252. $result = db_query('SELECT allianceTicker FROM {eve_api_alliance_list_tickers} WHERE allianceID = :id', array(
  1253. ':id' => (int) $alliance_id,
  1254. ));
  1255. $row = $result->fetchAssoc();
  1256.  
  1257. return $row['allianceTicker'];
  1258. }
  1259.  
  1260. /**
  1261. * Function to retrieve corporation API Info.
  1262. *
  1263. * @param array $query
  1264. * The URL variables to pass through the URL.
  1265. *
  1266. * @return array
  1267. * A complex array of corporate information.
  1268. */
  1269. function eve_api_get_corporation_sheet_api($query) {
  1270. $response = eve_api_get_xml('CorporationSheet', $query);
  1271.  
  1272. if (!isset($response['xml']->result)) {
  1273. if (variable_get('eve_api_debug', FALSE)) {
  1274. watchdog('eve_api', 'Failed getting Corporation Sheet API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1275. }
  1276.  
  1277. unset($response['xml']);
  1278. return array('error' => TRUE) + $response;
  1279. }
  1280.  
  1281. $api_data = array(
  1282. 'corporationID' => (int) $response['xml']->result->corporationID,
  1283. 'corporationName' => (string) $response['xml']->result->corporationName,
  1284. 'corporationTicker' => (string) $response['xml']->result->ticker,
  1285. 'allianceID' => (int) $response['xml']->result->allianceID,
  1286. 'allianceName' => (string) $response['xml']->result->allianceName,
  1287. 'allianceTicker' => (string) eve_api_get_alliance_ticker((int) $response['xml']->result->allianceID),
  1288. 'ceoName' => (string) $response['xml']->result->ceoName,
  1289. 'ceoID' => (int) $response['xml']->result->ceoID,
  1290. );
  1291.  
  1292. $api_data['errorCode'] = (int) $response['errorCode'];
  1293. $api_data['errorText'] = (string) $response['errorText'];
  1294.  
  1295. return $api_data;
  1296. }
  1297.  
  1298. /**
  1299. * Function to retrieve Character/Alliance/Corporation ID's from a name.
  1300. *
  1301. * @param array $query
  1302. * The URL variables to pass through the URL.
  1303. *
  1304. * @return array
  1305. * A simple array containing unique name and ID information.
  1306. */
  1307. function eve_api_get_character_id_api($query) {
  1308. $response = eve_api_get_xml('CharacterID', $query);
  1309.  
  1310. if (!isset($response['xml']->result)) {
  1311. if (variable_get('eve_api_debug', FALSE)) {
  1312. watchdog('eve_api', 'Failed getting Character ID API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1313. }
  1314.  
  1315. unset($response['xml']);
  1316. return array('error' => TRUE) + $response;
  1317. }
  1318.  
  1319. $api_data = array();
  1320. $api_data['errorCode'] = (int) $response['errorCode'];
  1321. $api_data['errorText'] = (string) $response['errorText'];
  1322.  
  1323. foreach ($response['xml']->result->rowset->children() as $rows) {
  1324. $row = $rows->attributes();
  1325. $name = strtoupper((string) $row->name);
  1326.  
  1327. $api_data[$name] = array(
  1328. 'name' => (string) $row->name,
  1329. 'characterID' => (int) $row->characterID,
  1330. );
  1331. }
  1332.  
  1333. return $api_data;
  1334. }
  1335.  
  1336. /**
  1337. * Function to retrieve character names from character ids.
  1338. *
  1339. * @param array $query
  1340. * The URL variables to pass through the URL.
  1341. *
  1342. * @return array
  1343. * A simple array containing unique name and ID information.
  1344. */
  1345. function eve_api_get_character_name_api($query) {
  1346. $response = eve_api_get_xml('CharacterName', $query);
  1347.  
  1348. if (!isset($response['xml']->result)) {
  1349. if (variable_get('eve_api_debug', FALSE)) {
  1350. watchdog('eve_api', 'Failed getting Character Name API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1351. }
  1352.  
  1353. unset($response['xml']);
  1354. return array('error' => TRUE) + $response;
  1355. }
  1356.  
  1357. $api_data = array();
  1358. $api_data['errorCode'] = (int) $response['errorCode'];
  1359. $api_data['errorText'] = (string) $response['errorText'];
  1360.  
  1361. foreach ($response['xml']->result->rowset->children() as $rows) {
  1362. $row = $rows->attributes();
  1363.  
  1364. $api_data[(int) $row->characterID] = (string) $row->name;
  1365. }
  1366.  
  1367. return $api_data;
  1368. }
  1369.  
  1370. /**
  1371. * Function to retrieve updated list of EVE API Error Codes.
  1372. *
  1373. * @param array $query
  1374. * The URL variables to pass through the URL.
  1375. *
  1376. * @return array
  1377. * A simple array containing unique error code and text messages.
  1378. */
  1379. function eve_api_get_error_list_api() {
  1380. $response = eve_api_get_xml('ErrorList');
  1381.  
  1382. if (!isset($response['xml']->result)) {
  1383. if (variable_get('eve_api_debug', FALSE)) {
  1384. watchdog('eve_api', 'Failed getting Error List API Data. (' . drupal_http_build_query($query) . ') Error Code: ' . $response['errorCode'] . ' Error Message: ' . $response['errorText'], NULL, WATCHDOG_WARNING);
  1385. }
  1386.  
  1387. unset($response['xml']);
  1388. return array('error' => TRUE) + $response;
  1389. }
  1390.  
  1391. $api_data = array();
  1392. $api_data['errorCode'] = (int) $response['errorCode'];
  1393. $api_data['errorText'] = (string) $response['errorText'];
  1394. $api_data['errors'] = array();
  1395.  
  1396. foreach ($response['xml']->result->rowset->children() as $rows) {
  1397. $row = $rows->attributes();
  1398.  
  1399. $api_data['errors'][(int) $row->errorCode] = (string) $row->errorText;
  1400. }
  1401.  
  1402. return $api_data;
  1403. }
  1404.  
  1405. /**
  1406. * Function to compare the API against the Alliance Blue List.
  1407. *
  1408. * @param array $characters
  1409. * A complex array full of character information.
  1410. *
  1411. * @return bool
  1412. * Indicates a character is blue to the alliance.
  1413. */
  1414. function eve_api_verify_blue($characters) {
  1415. if (isset($characters['characterID'])) {
  1416. if (($characters['allianceID'] == variable_get('eve_api_allianceID', 0) && variable_get('eve_api_allianceID', 0) != 0) || $characters['corporationID'] == variable_get('eve_api_corporationID', 0)) {
  1417. return TRUE;
  1418. }
  1419.  
  1420. $result = db_query('SELECT corporationID FROM {eve_api_alliance_list_corporations} WHERE corporationID = :corporationID', array(
  1421. ':corporationID' => (int) $characters['corporationID'],
  1422. ));
  1423.  
  1424. if ($result->rowCount()) {
  1425. return TRUE;
  1426. }
  1427.  
  1428. $result = db_query('SELECT contactID FROM {eve_api_contact_list} WHERE standing >= :standing AND contactID IN (:contactIDs)', array(
  1429. ':standing' => (float) variable_get('eve_api_required_standing', 0.1),
  1430. ':contactIDs' => array(
  1431. (int) $characters['characterID'],
  1432. (int) $characters['corporationID'],
  1433. (int) $characters['allianceID'],
  1434. ),
  1435. ));
  1436.  
  1437. if ($result->rowCount()) {
  1438. return TRUE;
  1439. }
  1440. }
  1441. elseif (isset($characters['characters'])) {
  1442. foreach ($characters['characters'] as $character) {
  1443. if (($character['allianceID'] == variable_get('eve_api_allianceID', 0) && variable_get('eve_api_allianceID', 0) != 0) || $character['corporationID'] == variable_get('eve_api_corporationID', 0)) {
  1444. return TRUE;
  1445. }
  1446.  
  1447. $result = db_query('SELECT corporationID FROM {eve_api_alliance_list_corporations} WHERE corporationID = :corporationID', array(
  1448. ':corporationID' => (int) $character['corporationID'],
  1449. ));
  1450.  
  1451. if ($result->rowCount()) {
  1452. return TRUE;
  1453. }
  1454.  
  1455. $result = db_query('SELECT contactID FROM {eve_api_contact_list} WHERE standing >= :standing AND contactID IN (:contactIDs)', array(
  1456. ':standing' => (float) variable_get('eve_api_required_standing', 0.1),
  1457. ':contactIDs' => array(
  1458. (int) $character['characterID'],
  1459. (int) $character['corporationID'],
  1460. (int) $character['allianceID'],
  1461. ),
  1462. ));
  1463.  
  1464. if ($result->rowCount()) {
  1465. return TRUE;
  1466. }
  1467. }
  1468. }
  1469.  
  1470. return FALSE;
  1471. }
  1472.  
  1473. /**
  1474. * Function to generate an array of Blue Characters to the Alliance.
  1475. *
  1476. * @param array $characters
  1477. * A complex array full of character information.
  1478. *
  1479. * @return array
  1480. * A simple array of characters blue to the alliance.
  1481. */
  1482. function eve_api_list_valid_characters($characters, $blue_only = TRUE) {
  1483. $chars = array();
  1484.  
  1485. if (!empty($characters)) {
  1486. foreach ($characters['characters'] as $character) {
  1487. if (!$blue_only) {
  1488. $chars[] = $character['characterName'];
  1489. continue;
  1490. }
  1491.  
  1492. $result = db_query('SELECT contactID FROM {eve_api_contact_list} WHERE standing > 0 AND contactID IN (:contactIDs)', array(
  1493. ':contactIDs' => array(
  1494. (int) $character['characterID'],
  1495. (int) $character['corporationID'],
  1496. (int) $character['allianceID'],
  1497. ),
  1498. ));
  1499.  
  1500. if ($result->rowCount()) {
  1501. $chars[] = $character['characterName'];
  1502. continue;
  1503. }
  1504.  
  1505. $result = db_query('SELECT corporationID FROM {eve_api_alliance_list_corporations} WHERE corporationID = :corporationID', array(
  1506. ':corporationID' => (int) $character['corporationID'],
  1507. ));
  1508.  
  1509. if ($result->rowCount()) {
  1510. $chars[] = $character['characterName'];
  1511. }
  1512. }
  1513. }
  1514.  
  1515. return $chars;
  1516. }
  1517.  
  1518. /**
  1519. * Function to check if the Characters already exist in the DB.
  1520. *
  1521. * @param array $characters
  1522. * A complex array full of character information.
  1523. *
  1524. * @return mixed
  1525. * A simple array of characters found or a bool FALSE.
  1526. */
  1527. function eve_api_characters_exist($characters) {
  1528. $chars = array();
  1529.  
  1530. if (!empty($characters)) {
  1531. foreach ($characters['characters'] as $character) {
  1532. $result = db_query('SELECT characterID FROM {eve_api_characters} WHERE deleted = 0 AND characterID = :characterID', array(
  1533. ':characterID' => (int) $character['characterID'],
  1534. ));
  1535.  
  1536. if ($result->rowCount()) {
  1537. $chars[] = check_plain($character['characterName']);
  1538. }
  1539. }
  1540. }
  1541.  
  1542. if (empty($chars)) {
  1543. $chars = FALSE;
  1544. }
  1545.  
  1546. return $chars;
  1547. }
  1548.  
  1549. /**
  1550. * Function to check if the Characters is in a corp.
  1551. *
  1552. * @param int $character_id
  1553. * A unique character id.
  1554. * @param int $corporation_id
  1555. * A unique corporation id.
  1556. *
  1557. * @return bool
  1558. * TRUE or FALSE.
  1559. */
  1560. function eve_api_character_is_in_corp($character_id, $corporation_id) {
  1561. $query = db_select('eve_api_characters', 'c');
  1562.  
  1563. $result = $query->fields('c', array(
  1564. 'characterID',
  1565. ))
  1566. ->condition('c.corporationID', $corporation_id, '=')
  1567. ->condition('c.characterID', (int) $character_id, '=')
  1568. ->execute();
  1569.  
  1570. if ($result->rowCount()) {
  1571. return TRUE;
  1572. }
  1573.  
  1574. return FALSE;
  1575. }
  1576.  
  1577. /**
  1578. * Function to check if the Characters is a Director.
  1579. *
  1580. * @param int $character_id
  1581. * A unique character id.
  1582. * @param int $corporation_id
  1583. * A unique corporation id.
  1584. *
  1585. * @return bool
  1586. * TRUE or FALSE.
  1587. */
  1588. function eve_api_character_is_director($character_id, $corporation_id = NULL) {
  1589. $query = db_select('eve_api_character_sheet_roles', 'cr');
  1590.  
  1591. if (isset($corporation_id)) {
  1592. $query->join('eve_api_characters', 'c', 'cr.characterID = c.characterID');
  1593. $query->condition('c.corporationID', $corporation_id, '=');
  1594. }
  1595.  
  1596. $result = $query->fields('cr', array(
  1597. 'characterID',
  1598. ))
  1599. ->condition('cr.roleID', 1, '=')
  1600. ->condition('cr.characterID', (int) $character_id, '=')
  1601. ->execute();
  1602.  
  1603. if ($result->rowCount()) {
  1604. return TRUE;
  1605. }
  1606.  
  1607. $query = db_select('eve_api_role_ceo_director', 'crw');
  1608.  
  1609. if (isset($corporation_id)) {
  1610. $query->join('eve_api_characters', 'c', 'crw.characterID = c.characterID');
  1611. $query->condition('c.corporationID', $corporation_id, '=');
  1612. }
  1613.  
  1614. $result = $query->fields('crw', array(
  1615. 'characterID',
  1616. ))
  1617. ->condition('crw.isDirector', 1, '=')
  1618. ->condition('crw.characterID', (int) $character_id, '=')
  1619. ->execute();
  1620.  
  1621. if ($result->rowCount()) {
  1622. return TRUE;
  1623. }
  1624.  
  1625. return FALSE;
  1626. }
  1627.  
  1628. /**
  1629. * Function to check if the Characters is a CEO.
  1630. *
  1631. * @param int $character_id
  1632. * A unique character id.
  1633. * @param int $corporation_id
  1634. * A unique corporation id.
  1635. *
  1636. * @return bool
  1637. * TRUE or FALSE.
  1638. */
  1639. function eve_api_character_is_ceo($character_id, $corporation_id = NULL) {
  1640. $query = db_select('eve_api_alliance_list_corporations', 'ac');
  1641.  
  1642. if (isset($corporation_id)) {
  1643. $query->condition('ac.corporationID', $corporation_id, '=');
  1644. }
  1645.  
  1646. $result = $query->fields('ac', array(
  1647. 'ceoID',
  1648. ))
  1649. ->condition('ac.ceoID', (int) $character_id, '=')
  1650. ->execute();
  1651.  
  1652. if ($result->rowCount()) {
  1653. return TRUE;
  1654. }
  1655.  
  1656. $query = db_select('eve_api_role_ceo_director', 'crw');
  1657.  
  1658. if (isset($corporation_id)) {
  1659. $query->join('eve_api_characters', 'c', 'crw.characterID = c.characterID');
  1660. $query->condition('c.corporationID', $corporation_id, '=');
  1661. }
  1662.  
  1663. $result = $query->fields('crw', array(
  1664. 'characterID',
  1665. ))
  1666. ->condition('crw.isCEO', 1, '=')
  1667. ->condition('crw.characterID', (int) $character_id, '=')
  1668. ->execute();
  1669.  
  1670. if ($result->rowCount()) {
  1671. return TRUE;
  1672. }
  1673.  
  1674. return FALSE;
  1675. }
  1676.  
  1677. /**
  1678. * Function to check if the Characters is in a corp.
  1679. *
  1680. * @param int $uid
  1681. * A unique drupal id.
  1682. * @param int $corporation_id
  1683. * A unique corporation id.
  1684. *
  1685. * @return bool
  1686. * TRUE or FALSE.
  1687. */
  1688. function eve_api_user_is_in_corp($uid, $corporation_id) {
  1689. $query = db_select('eve_api_characters', 'c');
  1690. $query->join('users', 'u', 'c.characterID = u.characterID');
  1691.  
  1692. $result = $query->fields('c', array(
  1693. 'characterID',
  1694. ))
  1695. ->condition('c.corporationID', $corporation_id, '=')
  1696. ->condition('u.uid', (int) $uid, '=')
  1697. ->execute();
  1698.  
  1699. if ($result->rowCount()) {
  1700. return TRUE;
  1701. }
  1702.  
  1703. return FALSE;
  1704. }
  1705.  
  1706. /**
  1707. * Function to check if the Characters is a Director.
  1708. *
  1709. * @param int $uid
  1710. * A unique drupal id.
  1711. * @param int $corporation_id
  1712. * A unique corporation id.
  1713. *
  1714. * @return bool
  1715. * TRUE or FALSE.
  1716. */
  1717. function eve_api_user_is_director($uid, $corporation_id = NULL) {
  1718. $query = db_select('eve_api_character_sheet_roles', 'cr');
  1719. $query->join('users', 'u', 'cr.characterID = u.characterID');
  1720.  
  1721. if (isset($corporation_id)) {
  1722. $query->join('eve_api_characters', 'c', 'cr.characterID = c.characterID');
  1723. $query->condition('c.corporationID', $corporation_id, '=');
  1724. }
  1725.  
  1726. $result = $query->fields('cr', array(
  1727. 'characterID',
  1728. ))
  1729. ->condition('cr.roleID', 1, '=')
  1730. ->condition('u.uid', (int) $uid, '=')
  1731. ->execute();
  1732.  
  1733. if ($result->rowCount()) {
  1734. return TRUE;
  1735. }
  1736.  
  1737. $query = db_select('eve_api_role_ceo_director', 'crw');
  1738. $query->join('users', 'u', 'crw.characterID = u.characterID');
  1739.  
  1740. if (isset($corporation_id)) {
  1741. $query->join('eve_api_characters', 'c', 'crw.characterID = c.characterID');
  1742. $query->condition('c.corporationID', $corporation_id, '=');
  1743. }
  1744.  
  1745. $result = $query->fields('crw', array(
  1746. 'characterID',
  1747. ))
  1748. ->condition('crw.isDirector', 1, '=')
  1749. ->condition('u.uid', (int) $uid, '=')
  1750. ->execute();
  1751.  
  1752. if ($result->rowCount()) {
  1753. return TRUE;
  1754. }
  1755.  
  1756. return FALSE;
  1757. }
  1758.  
  1759. /**
  1760. * Function to check if the Characters is a CEO.
  1761. *
  1762. * @param int $uid
  1763. * A unique drupal id.
  1764. * @param int $corporation_id
  1765. * A unique corporation id.
  1766. *
  1767. * @return bool
  1768. * TRUE or FALSE.
  1769. */
  1770. function eve_api_user_is_ceo($uid, $corporation_id = NULL) {
  1771. $query = db_select('eve_api_alliance_list_corporations', 'ac');
  1772. $query->join('users', 'u', 'ac.ceoID = u.characterID');
  1773.  
  1774. if (isset($corporation_id)) {
  1775. $query->condition('ac.corporationID', $corporation_id, '=');
  1776. }
  1777.  
  1778. $result = $query->fields('ac', array(
  1779. 'ceoID',
  1780. ))
  1781. ->condition('u.uid', (int) $uid, '=')
  1782. ->execute();
  1783.  
  1784. if ($result->rowCount()) {
  1785. return TRUE;
  1786. }
  1787.  
  1788. $query = db_select('eve_api_role_ceo_director', 'crw');
  1789. $query->join('users', 'u', 'crw.characterID = u.characterID');
  1790.  
  1791. if (isset($corporation_id)) {
  1792. $query->join('eve_api_characters', 'c', 'crw.characterID = c.characterID');
  1793. $query->condition('c.corporationID', $corporation_id, '=');
  1794. }
  1795.  
  1796. $result = $query->fields('crw', array(
  1797. 'characterID',
  1798. ))
  1799. ->condition('crw.isCEO', 1, '=')
  1800. ->condition('u.uid', (int) $uid, '=')
  1801. ->execute();
  1802.  
  1803. if ($result->rowCount()) {
  1804. return TRUE;
  1805. }
  1806.  
  1807. return FALSE;
  1808. }
  1809.  
  1810. /**
  1811. * Function that converts an Error ID to a readable message.
  1812. *
  1813. * @param int $error_id
  1814. * An error ID number.
  1815. *
  1816. * @return string
  1817. * An error message.
  1818. */
  1819. function eve_api_api_error_msg($error_id, $error_text) {
  1820. switch ($error_id) {
  1821. case 0:
  1822. $message = t('OK');
  1823. break;
  1824.  
  1825. case 1:
  1826. $message = t('Invalid API Mask/Type/Expiry');
  1827. break;
  1828.  
  1829. case 2:
  1830. // Too Many Failed API Requests, we will wait 10 minutes to try again.
  1831. $message = t('API Temporarily Down');
  1832. break;
  1833.  
  1834. case 3:
  1835. // API Request Timed Out.
  1836. $message = t('API Temporarily Down');
  1837. break;
  1838.  
  1839. case 4:
  1840. // Invalid response, response received as a HTML document.
  1841. $message = t('API Temporarily Down');
  1842. break;
  1843.  
  1844. case 5:
  1845. // Invalid response cannot parse as XML.
  1846. $message = t('API Temporarily Down');
  1847. break;
  1848.  
  1849. case 403:
  1850. $message = t('API Key Does Not Exist');
  1851. break;
  1852.  
  1853. default:
  1854. $result = db_query('SELECT errorCode, errorText
  1855. FROM {eve_api_error_list}
  1856. WHERE errorCode = :errorCode', array(
  1857. ':errorCode' => (int) $error_id,
  1858. ));
  1859.  
  1860. $row = $result->fetchAssoc();
  1861.  
  1862. if (empty($row)) {
  1863. $message = t('API Temporarily Down');
  1864. }
  1865. else {
  1866. $message = check_plain($error_text);
  1867. }
  1868. break;
  1869. }
  1870.  
  1871. return $message;
  1872. }
  1873.  
  1874. /**
  1875. * Function that retrieves a simple list of API's associated with a user id.
  1876. *
  1877. * @param int $uid
  1878. * A unique Drupal User ID.
  1879. *
  1880. * @return mixed
  1881. * A simple array of character and api key information.
  1882. */
  1883. function eve_api_list_api_simple($uid) {
  1884. $uid = (int) $uid;
  1885. $api_list = array();
  1886.  
  1887. $result_a = db_query('SELECT apiID, keyID, vCode, errorCode, updated, created FROM {eve_api_keys} WHERE uid = :uid', array(
  1888. ':uid' => $uid,
  1889. ));
  1890.  
  1891. if ($result_a->rowCount()) {
  1892. foreach ($result_a->fetchAll() as $row_a) {
  1893. if (eve_api_error_code_deleted($row_a->errorCode)) {
  1894. continue;
  1895. }
  1896.  
  1897. $result_b = db_query('SELECT characterID, characterName, corporationID, corporationName, corporationTicker, allianceID, allianceName, allianceTicker FROM {eve_api_characters} WHERE deleted = 0 AND uid = :uid AND apiID = :apiID', array(
  1898. ':uid' => $uid,
  1899. ':apiID' => (int) $row_a->apiID,
  1900. ));
  1901.  
  1902. if ($result_b->rowCount()) {
  1903. foreach ($result_b->fetchAll() as $row_b) {
  1904. $api_list['characters'][$row_b->characterID] = array(
  1905. 'characterID' => check_plain($row_b->characterID),
  1906. 'characterName' => strip_tags(decode_entities($row_b->characterName)),
  1907. 'corporationID' => check_plain($row_b->corporationID),
  1908. 'corporationName' => strip_tags(decode_entities($row_b->corporationName)),
  1909. 'corporationTicker' => strip_tags(decode_entities($row_b->corporationTicker)),
  1910. 'allianceID' => check_plain($row_b->allianceID),
  1911. 'allianceName' => strip_tags(decode_entities($row_b->allianceName)),
  1912. 'allianceTicker' => strip_tags(decode_entities($row_b->allianceTicker)),
  1913. );
  1914. }
  1915. }
  1916. }
  1917. }
  1918.  
  1919. return $api_list;
  1920. }
  1921.  
  1922. /**
  1923. * Function that inserts a new API Key.
  1924. *
  1925. * @param object $account
  1926. * A Drupal user object containing.
  1927. * @param int $key_id
  1928. * A unique EVE API Key ID.
  1929. * @param string $v_code
  1930. * A unique EVE API Verification Code.
  1931. *
  1932. * @return mixed
  1933. * A mixed array of data regarding the success/failure.
  1934. */
  1935. function eve_api_create_key($account, $key_id, $v_code) {
  1936. $created = date('Y-m-d H:i:s', time());
  1937. $uid = (int) $account->uid;
  1938.  
  1939. $query = array(
  1940. 'keyID' => $key_id,
  1941. 'vCode' => $v_code,
  1942. );
  1943.  
  1944. $characters = eve_api_get_api_key_info_api($query);
  1945.  
  1946. if (isset($characters['error'])) {
  1947. watchdog('eve_api', 'Error occured during registration. (' . drupal_http_build_query($query) . ')<br /><pre>' . print_r($characters, TRUE) . '</pre>', NULL, WATCHDOG_WARNING);
  1948.  
  1949. return FALSE;
  1950. }
  1951.  
  1952. if (!isset($characters['characters'])) {
  1953. watchdog('eve_api', 'Error occured, should never happen. (' . drupal_http_build_query($query) . ')<br /><pre>' . print_r($characters, TRUE) . '</pre>', NULL, WATCHDOG_DEBUG);
  1954.  
  1955. return FALSE;
  1956. }
  1957.  
  1958. $whitelist = array();
  1959.  
  1960. if (!empty($characters)) {
  1961. foreach ($characters['characters'] as $character) {
  1962. $whitelist[] = (int) $character['characterID'];
  1963. }
  1964. }
  1965.  
  1966. $result = db_query('SELECT characterID FROM {eve_api_whitelist} WHERE characterID IN (:characterIDs)', array(
  1967. ':characterIDs' => $whitelist,
  1968. ));
  1969.  
  1970. $allow_expires = variable_get('eve_api_require_expires', FALSE) ? FALSE : !empty($characters['expires']);
  1971. $allow_type = variable_get('eve_api_require_type', TRUE) ? $characters['type'] != 'Account' : FALSE;
  1972.  
  1973. if ($result->rowCount()) {
  1974. if ($allow_expires || ($characters['accessMask'] & 8388680) != 8388680) {
  1975. $characters['errorCode'] = 1;
  1976. $characters['errorText'] = 'Invalid API Mask/Type/Expiry';
  1977. }
  1978. }
  1979. else {
  1980. if ($allow_expires || $allow_type || ($characters['accessMask'] & variable_get('eve_api_access_mask', 268435455)) != variable_get('eve_api_access_mask', 268435455)) {
  1981. $characters['errorCode'] = 1;
  1982. $characters['errorText'] = 'Invalid API Mask/Type/Expiry';
  1983. }
  1984. }
  1985.  
  1986. $api_id = db_insert('eve_api_keys')->fields(array(
  1987. 'uid' => (int) $uid,
  1988. 'keyID' => (int) $key_id,
  1989. 'vCode' => (string) $v_code,
  1990. 'updated' => (string) $created,
  1991. 'created' => (string) $created,
  1992. 'errorCode' => $characters['errorCode'],
  1993. 'errorText' => $characters['errorText'],
  1994. ))->execute();
  1995.  
  1996. if ($characters['errorCode'] == 1) {
  1997. return FALSE;
  1998. }
  1999.  
  2000. foreach ($characters['characters'] as $character) {
  2001. db_merge('eve_api_characters')->key(array('characterID' => (int) $character['characterID']))->fields(array(
  2002. 'characterID' => (int) $character['characterID'],
  2003. 'uid' => (int) $uid,
  2004. 'apiID' => (int) $api_id,
  2005. 'characterName' => (string) $character['characterName'],
  2006. 'corporationID' => (int) $character['corporationID'],
  2007. 'corporationName' => (string) $character['corporationName'],
  2008. 'corporationTicker' => (string) $character['corporationTicker'],
  2009. 'allianceID' => (int) $character['allianceID'],
  2010. 'allianceName' => (string) $character['allianceName'],
  2011. 'allianceTicker' => (string) $character['allianceTicker'],
  2012. 'accessMask' => (int) $characters['accessMask'],
  2013. 'errorCode' => $characters['errorCode'],
  2014. 'errorText' => $characters['errorText'],
  2015. 'deleted' => 0,
  2016. ))->execute();
  2017.  
  2018. if ($account->name == (string) $character['characterName']) {
  2019. $character_data = $character;
  2020. }
  2021. }
  2022.  
  2023. return isset($character_data) ? $character_data : array('not_found' => TRUE);
  2024. }
  2025.  
  2026. /**
  2027. * Function that modifies an existing API Key.
  2028. *
  2029. * @param object $account
  2030. * A Drupal user object containing.
  2031. * @param int $key_id
  2032. * A unique EVE API Key ID.
  2033. * @param string $v_code
  2034. * A unique EVE API Verification Code.
  2035. * @param int $api_id
  2036. * A unique API ID.
  2037. *
  2038. * @return mixed
  2039. * A mixed array of data regarding the success/failure.
  2040. */
  2041. function eve_api_modify_key($account, $key_id, $v_code, $api_id) {
  2042. $updated = date('Y-m-d H:i:s', time());
  2043. $uid = (int) $account->uid;
  2044.  
  2045. $query = array(
  2046. 'keyID' => $key_id,
  2047. 'vCode' => $v_code,
  2048. );
  2049.  
  2050. $characters = eve_api_get_api_key_info_api($query);
  2051.  
  2052. if ($api_id == 0) {
  2053. return TRUE;
  2054. }
  2055.  
  2056. if (isset($characters['error'])) {
  2057. db_merge('eve_api_keys')->key(array('apiID' => (int) $api_id))->fields(array(
  2058. 'apiID' => (int) $api_id,
  2059. 'errorCode' => $characters['errorCode'],
  2060. 'errorText' => $characters['errorText'],
  2061. 'updated' => (string) $updated,
  2062. ))->execute();
  2063.  
  2064. if (eve_api_error_code_deleted($characters['errorCode'])) {
  2065. db_update('eve_api_characters')->fields(array(
  2066. 'errorCode' => $characters['errorCode'],
  2067. 'errorText' => $characters['errorText'],
  2068. 'deleted' => 1,
  2069. ))->condition('apiID', (int) $api_id, '=')->execute();
  2070. return TRUE;
  2071. }
  2072. else {
  2073. db_update('eve_api_characters')->fields(array(
  2074. 'errorCode' => $characters['errorCode'],
  2075. 'errorText' => $characters['errorText'],
  2076. ))->condition('apiID', (int) $api_id, '=')->execute();
  2077. return FALSE;
  2078. }
  2079. }
  2080.  
  2081. if (!isset($characters['characters'])) {
  2082. watchdog('eve_api', 'Error occured, should never happen. (' . drupal_http_build_query($query) . ')<br /><pre>' . print_r($characters, TRUE) . '</pre>', NULL, WATCHDOG_WARNING);
  2083. return TRUE;
  2084. }
  2085.  
  2086. $whitelist = array();
  2087.  
  2088. if (!empty($characters)) {
  2089. foreach ($characters['characters'] as $character) {
  2090. $whitelist[] = (int) $character['characterID'];
  2091. }
  2092. }
  2093.  
  2094. $result = db_query('SELECT characterID FROM {eve_api_whitelist} WHERE characterID IN (:characterIDs)', array(
  2095. ':characterIDs' => $whitelist,
  2096. ));
  2097.  
  2098. $allow_expires = variable_get('eve_api_require_expires', FALSE) ? FALSE : !empty($characters['expires']);
  2099. $allow_type = variable_get('eve_api_require_type', TRUE) ? $characters['type'] != 'Account' : FALSE;
  2100.  
  2101. if ($result->rowCount()) {
  2102. if ($allow_expires || ($characters['accessMask'] & 8388680) != 8388680) {
  2103. $characters['errorCode'] = 1;
  2104. $characters['errorText'] = 'Invalid API Mask/Type/Expiry';
  2105. }
  2106. }
  2107. else {
  2108. if ($allow_expires || $allow_type || ($characters['accessMask'] & variable_get('eve_api_access_mask', 268435455)) != variable_get('eve_api_access_mask', 268435455)) {
  2109. $characters['errorCode'] = 1;
  2110. $characters['errorText'] = 'Invalid API Mask/Type/Expiry';
  2111. }
  2112. }
  2113.  
  2114. db_update('eve_api_characters')->fields(array(
  2115. 'errorCode' => $characters['errorCode'],
  2116. 'errorText' => $characters['errorText'],
  2117. 'deleted' => 1,
  2118. ))->condition('apiID', (int) $api_id)->execute();
  2119. db_merge('eve_api_keys')->key(array('apiID' => (int) $api_id))->fields(array(
  2120. 'apiID' => (int) $api_id,
  2121. 'uid' => (int) $uid,
  2122. 'keyID' => (int) $key_id,
  2123. 'vCode' => (string) $v_code,
  2124. 'updated' => (string) $updated,
  2125. 'errorCode' => $characters['errorCode'],
  2126. 'errorText' => $characters['errorText'],
  2127. ))->execute();
  2128.  
  2129. if ($characters['errorCode'] == 1) {
  2130. return TRUE;
  2131. }
  2132.  
  2133. foreach ($characters['characters'] as $character) {
  2134. db_merge('eve_api_characters')->key(array('characterID' => (int) $character['characterID']))->fields(array(
  2135. 'characterID' => (int) $character['characterID'],
  2136. 'uid' => (int) $uid,
  2137. 'apiID' => (int) $api_id,
  2138. 'characterName' => (string) $character['characterName'],
  2139. 'corporationID' => (int) $character['corporationID'],
  2140. 'corporationName' => (string) $character['corporationName'],
  2141. 'corporationTicker' => (string) $character['corporationTicker'],
  2142. 'allianceID' => (int) $character['allianceID'],
  2143. 'allianceName' => (string) $character['allianceName'],
  2144. 'allianceTicker' => (string) $character['allianceTicker'],
  2145. 'accessMask' => (int) $characters['accessMask'],
  2146. 'errorCode' => $characters['errorCode'],
  2147. 'errorText' => $characters['errorText'],
  2148. 'deleted' => 0,
  2149. ))->execute();
  2150. }
  2151.  
  2152. return FALSE;
  2153. }
  2154.  
  2155. /**
  2156. * Function that checks if a specific Error Code means we need to stop.
  2157. *
  2158. * @param int $code
  2159. * An EVE API Error Code.
  2160. *
  2161. * @return bool
  2162. * TRUE or FALSE if the code means to stop.
  2163. */
  2164. function eve_api_error_code($code) {
  2165. $error_codes = array(
  2166. 200,
  2167. 201,
  2168. 202,
  2169. 203,
  2170. 204,
  2171. 205,
  2172. 206,
  2173. 207,
  2174. 208,
  2175. 209,
  2176. 210,
  2177. 211,
  2178. 212,
  2179. 213,
  2180. 220,
  2181. 221,
  2182. 222,
  2183. 223,
  2184. 901,
  2185. 902,
  2186. 903,
  2187. 904,
  2188. 999,
  2189. 1001,
  2190. );
  2191.  
  2192. if (in_array($code, $error_codes)) {
  2193. return TRUE;
  2194. }
  2195. else {
  2196. return FALSE;
  2197. }
  2198. }
  2199.  
  2200. /**
  2201. * Function that checks if a specific Error Code is Deleted/Invalid.
  2202. *
  2203. * @param int $code
  2204. * An EVE API Error Code.
  2205. *
  2206. * @return bool
  2207. * TRUE or FALSE if the code means to stop.
  2208. */
  2209. function eve_api_error_code_deleted($code) {
  2210. $error_codes = array(
  2211. 202,
  2212. 203,
  2213. 204,
  2214. 205,
  2215. 210,
  2216. 211,
  2217. 212,
  2218. 222,
  2219. 521,
  2220. );
  2221.  
  2222. if (in_array($code, $error_codes)) {
  2223. return TRUE;
  2224. }
  2225. else {
  2226. return FALSE;
  2227. }
  2228. }
Add Comment
Please, Sign In to add comment