/tools/api_pull.php

https://bitbucket.org/jimorland/tripwire · PHP · 144 lines · 105 code · 30 blank · 9 comment · 19 complexity · 9ce883d2f8e8be902134464b069144e5 MD5 · raw file

  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['super']) || $_SESSION['super'] != 1) {
  4. echo 'Security Failure!';
  5. exit();
  6. }
  7. ini_set('display_errors', 'On');
  8. require_once('../db.inc.php');
  9. require('../api.class.php');
  10. date_default_timezone_set('UTC');
  11. $startTime = microtime(true);
  12. $time = date('Y-m-d H:i', time());
  13. $output = null;
  14. // Get server status
  15. $url = 'https://api.eveonline.com/server/ServerStatus.xml.aspx';
  16. if ($xml = @simplexml_load_file($url)) {
  17. $output['players'] = (int)$xml->result->onlinePlayers;
  18. $output['status'] = (int)$xml->result->serverOpen == 'True' && (int)$xml->result->onlinePlayers != 0 ? 1 : 0;
  19. $query = 'INSERT INTO eve_api.serverStatus (time, status, players) VALUES (:time, :status, :players)';
  20. $stmt = $mysql->prepare($query);
  21. $stmt->bindValue(':time', $time, PDO::PARAM_STR);
  22. $stmt->bindValue(':status', $output['status'], PDO::PARAM_INT);
  23. $stmt->bindValue(':players', $output['players'], PDO::PARAM_INT);
  24. $stmt->execute();
  25. } else {
  26. $players = 0;
  27. $status = 0;
  28. $query = 'INSERT INTO eve_api.serverStatus (time, status, players) VALUES (:time, :status, :players)';
  29. $stmt = $mysql->prepare($query);
  30. $stmt->bindValue(':time', $time, PDO::PARAM_STR);
  31. $stmt->bindValue(':status', $status, PDO::PARAM_INT);
  32. $stmt->bindValue(':players', $players, PDO::PARAM_INT);
  33. $stmt->execute();
  34. }
  35. // Get activity
  36. $query = "SELECT time FROM eve_api.cacheTime WHERE type = 'activity' AND time IS NOT NULL";
  37. $stmt = $mysql->prepare($query);
  38. $stmt->execute();
  39. $row = $stmt->fetchObject();
  40. if (!$row || ($row && strtotime($row->time) + 3600 <= time())) {
  41. $activity = array();
  42. $query = "UPDATE eve_api.cacheTime SET time = NULL WHERE type = 'activity'";
  43. $stmt = $mysql->prepare($query);
  44. $stmt->execute();
  45. // Gather jump data
  46. $url = 'https://api.eveonline.com/map/Jumps.xml.aspx';
  47. if ($xml = @simplexml_load_file($url)) {
  48. foreach ($xml->result->rowset->row AS $row) {
  49. $activity[(int)$row['solarSystemID']]['shipJumps'] = (int)$row['shipJumps'];
  50. }
  51. $jumpCache = $xml->cachedUntil;
  52. }
  53. // Gather kill data
  54. $url = 'https://api.eveonline.com/map/Kills.xml.aspx';
  55. if ($xml = @simplexml_load_file($url)) {
  56. foreach ($xml->result->rowset->row AS $row) {
  57. $activity[(int)$row['solarSystemID']]['shipKills'] = (int)$row['shipKills'];
  58. $activity[(int)$row['solarSystemID']]['podKills'] = (int)$row['podKills'];
  59. $activity[(int)$row['solarSystemID']]['npcKills'] = (int)$row['factionKills'];
  60. }
  61. $killCache = $xml->cachedUntil;
  62. }
  63. // Output
  64. $csv = fopen(dirname(__FILE__).'/activity.csv', 'w');
  65. foreach ($activity AS $index => $line) {
  66. $data = Array(
  67. $index,
  68. $time,
  69. isset($line['shipJumps']) ? $line['shipJumps'] : 0,
  70. isset($line['shipKills']) ? $line['shipKills'] : 0,
  71. isset($line['podKills']) ? $line['podKills'] : 0,
  72. isset($line['npcKills']) ? $line['npcKills'] : 0
  73. );
  74. fputcsv($csv, $data);
  75. }
  76. fclose($csv);
  77. $query = 'UPDATE eve_api.recentActivity SET shipJumps = 0, shipKills = 0, podKills = 0, npcKills = 0';
  78. $stmt = $mysql->prepare($query);
  79. $stmt->execute();
  80. // Insert data
  81. $query = "LOAD DATA INFILE 'http://10.132.120.172/activity.csv' INTO TABLE eve_api.systemActivity FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n'";
  82. $stmt = $mysql->prepare($query);
  83. $stmt->execute();
  84. $query = 'INSERT INTO eve_api.cacheTime (time, type) VALUES (:time, "activity") ON DUPLICATE KEY UPDATE time = :time';
  85. //$query = 'UPDATE eve_api.cacheTime SET time = :time WHERE type = "activity"';
  86. $stmt = $mysql->prepare($query);
  87. $stmt->bindValue(':time', $time, PDO::PARAM_STR);
  88. $stmt->execute();
  89. }
  90. // Check characters
  91. $query = 'SELECT c.characterID FROM active a INNER JOIN characters c ON a.userID = c.userID';
  92. $stmt = $mysql->prepare($query);
  93. $stmt->execute();
  94. $chars = $stmt->fetchAll(PDO::FETCH_COLUMN);
  95. if ($chars && count($chars) > 0) {
  96. $result = array();
  97. $API = new API();
  98. for ($x = 0, $l = count($chars); $x < $l; $x += 250) {
  99. $apiData = $API->getEveIds(implode(',', array_slice($chars, $x, 250)));
  100. if ($apiData != 0 && count($apiData) > 0)
  101. $result = array_merge($result, $apiData);
  102. }
  103. $query = 'UPDATE characters SET corporationID = :corporationID, corporationName = :corporationName, ban = 0, admin = 0 WHERE characterID = :characterID AND corporationID <> :corporationID';
  104. $stmt = $mysql->prepare($query);
  105. foreach ($result as $char) {
  106. $stmt->bindValue(':characterID', $char->characterID, PDO::PARAM_INT);
  107. $stmt->bindValue(':corporationID', $char->corporationID, PDO::PARAM_INT);
  108. $stmt->bindValue(':corporationName', $char->corporationName, PDO::PARAM_STR);
  109. $stmt->execute();
  110. }
  111. }
  112. $output['proccessTime'] = sprintf('%.4f', microtime(true) - $startTime);
  113. #echo json_encode($output);
  114. ?>