location.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // Gasteizko Margolariak API v3 //
  3. /*****************************************************
  4. * This function is called from almost everywhere at *
  5. * the beggining of the page. It initializes the *
  6. * session variables and connects to the db. *
  7. * *
  8. * @return: (MySQL server connection): The *
  9. * connection handler. *
  10. *****************************************************/
  11. function startdb(){
  12. //Include the db configuration file. It's somehow like this
  13. /*
  14. <?php
  15. $host = 'XXXX';
  16. $db_name = 'XXXX';
  17. $username_ro = 'XXXX';
  18. $username_rw = 'XXXX';
  19. $pass_ro = 'XXXX';
  20. $pass_rw = 'XXXX';
  21. ?>
  22. */
  23. include('../../.htpasswd');
  24. //Connect to to database
  25. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  26. //Set encoding options
  27. mysqli_set_charset($con, 'utf-8');
  28. header('Content-Type: text/html; charset=utf8');
  29. mysqli_query($con, 'SET NAMES utf8;');
  30. //Return the db connection
  31. return $con;
  32. }
  33. /*****************************************************
  34. * Retrieves the last reported location in the db, *
  35. * only if it was reported in the last 30 mins. *
  36. * *
  37. * @params: *
  38. * con: (MySQL server connection) Db connector. *
  39. * @return: (Double array): array with the 'lat', *
  40. * 'lon' and 'dtime' keys. null if nothing *
  41. * to report. *
  42. *****************************************************/
  43. function get_location($con){
  44. $q = mysqli_query($con, "SELECT lat, lon, dtime FROM location WHERE action <> 'F' AND lat IS NOT null AND lon IS NOT null AND dtime > NOW() - INTERVAL 30 MINUTE ORDER BY dtime DESC LIMIT 1;");
  45. if (mysqli_num_rows($q) > 0){
  46. $r = mysqli_fetch_array($q);
  47. $loc = array();
  48. $loc["lat"] = $r["lat"];
  49. $loc["lon"] = $r["lon"];
  50. $loc["dtime"] = $r["dtime"];
  51. return $loc;
  52. }
  53. return null;
  54. }
  55. /*****************************************************
  56. * Prints info about the last location report, in *
  57. * the format: *
  58. * [{"lat":"<LAT>","lon":"<LON>","dtime":"<DTIME>"}] *
  59. * where <LAT> and <LON> are doubles and <dtime> is *
  60. * the datetime of the report in format: *
  61. * 'YYYY-MM-DD hh:mm:ss'. *
  62. * *
  63. * @params: *
  64. * con: (MySQL server connection) Db connector. *
  65. * @return: (Double array): array with the 'lat' and *
  66. * 'lon' keys. null if nothing to report. *
  67. *****************************************************/
  68. function print_location($location){
  69. echo("[{\"lat\":\"$location[lat]\",\"lon\":\"$location[lon]\",\"dtime\":\"$location[dtime]\"}]");
  70. }
  71. //Connect to the database
  72. $con = startdb('rw');
  73. //Get location
  74. $location = get_location($con);
  75. if($location == null){
  76. //No content
  77. http_response_code(204);
  78. exit(0);
  79. }
  80. else{
  81. // Print the location
  82. print_location($location);
  83. }
  84. ?>