/server/predictor/map-ajax.php

https://github.com/kevinjameshunt/Spaceflight-Live-Tracker · PHP · 132 lines · 93 code · 18 blank · 21 comment · 12 complexity · 6c28804bdd9c5639f01bd8409350af0c MD5 · raw file

  1. <?php
  2. /*
  3. * Near Space Balloon Tracker V 1.0
  4. * Kevin James Hunt 2011
  5. * kevinjameshunt@gmail.com
  6. * http://www.kevinjameshunt.com
  7. *
  8. * http://github.com/kevinjameshunt/near-space-balloon-tracker
  9. *
  10. * AJAX functions for retrieving location and prediction data from server
  11. *
  12. * Prediction data adapted from CUSF Landing Prediction Version 2 by Jon Sowman
  13. * http://github.com/jonsowman/cusf-standalone-predictor
  14. *
  15. */
  16. require("map_includes/dbinfo.php");
  17. define("PREDSTORE_PATH", "predstore/");
  18. define("FLIGHT_CSV", "flight_path.csv");
  19. function parseToXML($htmlStr)
  20. {
  21. $xmlStr=str_replace('','&lt;',$htmlStr);
  22. $xmlStr=str_replace(':','&gt;',$xmlStr);
  23. $xmlStr=str_replace('"','&quot;',$xmlStr);
  24. $xmlStr=str_replace("'",'&#39;',$xmlStr);
  25. $xmlStr=str_replace("&",'&amp;',$xmlStr);
  26. return $xmlStr;
  27. }
  28. $action = $_GET['action'];
  29. switch($action) {
  30. case "getTripData":
  31. $lastTimestamp = $_GET['timestamp'];
  32. $latestMessage = $_GET['latestmessage'];
  33. // Opens a connection to a MySQL server
  34. $connection=mysql_connect (localhost, $username, $password);
  35. if (!$connection) {
  36. die('Not connected : ' . mysql_error());
  37. }
  38. // Set the active MySQL database
  39. $db_selected = mysql_select_db($database, $connection);
  40. if (!$db_selected) {
  41. die ('Can\'t use db : ' . mysql_error());
  42. }
  43. // Get Last Updated ID
  44. $query = "SELECT * FROM space_message WHERE 1 ORDER BY id DESC LIMIT 1 ";
  45. $result = mysql_query($query);
  46. if (!$result) {
  47. die('Invalid query: ' . mysql_error());
  48. }
  49. $row = @mysql_fetch_assoc($result);
  50. $tripid = $row['tripid'];
  51. $uuid = $row['uuid'];
  52. $message = $row['message'];
  53. if ($message == "Found") {
  54. $timestamp = $row['timestamp'];
  55. } else {
  56. $timestamp = "";
  57. }
  58. $result = null;
  59. // Select all the rows in the markers table
  60. $querySql = "SELECT * FROM space_coords WHERE tripid=" . $tripid;
  61. if ($lastTimestamp != "") {
  62. $querySql .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d %h:%i:%s' )>= DATE_FORMAT('" . $lastTimestamp . "', '%Y-%m-%d %h:%i:%s' )";
  63. }
  64. $result = mysql_query($querySql);
  65. if (!$result) {
  66. die('Invalid query: ' . mysql_error());
  67. }
  68. $json = array();
  69. $json['tripData'] = array();
  70. $json['tripData']['tripid'] = $tripid;
  71. $json['tripData']['uuid'] = $uuid;
  72. $json['tripData']['message'] = $message;
  73. $json['tripData']['timestamp'] = $timestamp;
  74. $json['tripData']['latestMessage'] = $latestMessage;
  75. // Start markers object, echo parent node
  76. $json['markers'] = array();
  77. // Iterate through the rows, printing XML nodes for each
  78. $count = 0;
  79. while ($row = @mysql_fetch_assoc($result)){
  80. // ADD TO XML DOCUMENT NODE
  81. $json['markers'][$count] = array();
  82. $json['markers'][$count]['lat'] = $row['latitude'];
  83. $json['markers'][$count]['lon'] = $row['longitude'];
  84. $json['markers'][$count]['id'] = $row['id'];
  85. $json['markers'][$count]['alt'] = $row['altitude'];
  86. $json['markers'][$count]['state'] = $row['appstate'];
  87. $json['markers'][$count]['bat'] = $row['batlevel'];
  88. $json['markers'][$count]['timestamp'] = $row['timestamp'];
  89. $count +=1;
  90. }
  91. $encoded = json_encode($json);
  92. die($encoded);
  93. break;
  94. case "getCSVLongTerm":
  95. $uuid = $_GET['uuid'];
  96. $tryfile = PREDSTORE_PATH . $uuid . "/" . FLIGHT_CSV;
  97. if(!file_exists($tryfile)) return false;
  98. $fh = fopen($tryfile, "r");
  99. $data = array();
  100. while (!feof($fh)) {
  101. $line = trim(fgets($fh));
  102. array_push($data, $line);
  103. }
  104. $returned = json_encode($data);
  105. echo $returned;
  106. break;
  107. default:
  108. echo "Couldn't interpret 'action' variable";
  109. break;
  110. }
  111. ?>