location.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Gasteizko Margolariak API v3 - Location
  4. *
  5. * Used to get location reports.
  6. * This file is to be called directly from a URL request.
  7. *
  8. * https://margolariak.com/API/v3/help/
  9. *
  10. * @since 1.0.0
  11. */
  12. /**
  13. * Initializes the MySQL database connection.
  14. *
  15. * Called at the beggining of the script. It connects to the database using the
  16. * parameters in the .htpasswd file. It also sets database and page encodings.
  17. *
  18. * @since 1.0.0
  19. * @return object Database connection.
  20. */
  21. function startdb(){
  22. //Include the db configuration file. It's somehow like this
  23. /*
  24. <?php
  25. $host = 'XXXX';
  26. $db_name = 'XXXX';
  27. $username_ro = 'XXXX';
  28. $username_rw = 'XXXX';
  29. $pass_ro = 'XXXX';
  30. $pass_rw = 'XXXX';
  31. ?>
  32. */
  33. include('../../.htpasswd');
  34. //Connect to to database
  35. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  36. //Set encoding options
  37. mysqli_set_charset($con, 'utf-8');
  38. header('Content-Type: text/html; charset=utf8');
  39. mysqli_query($con, 'SET NAMES utf8;');
  40. //Return the db connection
  41. return $con;
  42. }
  43. /**
  44. * Retrieves the last location report.
  45. *
  46. * Retrieves the last reported location in the db, only if it was reported
  47. * in the last 30 mins.
  48. *
  49. * @since 3.0.0
  50. * @param object $con Open database connection.
  51. * @return null if there was no location report in the last 30 minutes or
  52. * array{
  53. * @type double lat Last location report latitude component.
  54. * @type double lon Last location report longitude component.
  55. * @type datetime dtime Last location report timestamp.
  56. * }
  57. */
  58. function get_location($con){
  59. $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;");
  60. if (mysqli_num_rows($q) > 0){
  61. $r = mysqli_fetch_array($q);
  62. $loc = array();
  63. $loc["lat"] = $r["lat"];
  64. $loc["lon"] = $r["lon"];
  65. $loc["dtime"] = $r["dtime"];
  66. return $loc;
  67. }
  68. return null;
  69. }
  70. /**
  71. * Prints the last location report.
  72. *
  73. * Prints info about the last location report, in the format:
  74. * [{"lat":"<LAT>","lon":"<LON>","dtime":"<DTIME>"}]
  75. * where <LAT> and <LON> are doubles and <dtime> is the datetime of the
  76. * report in format: 'YYYY-MM-DD hh:mm:ss'
  77. *
  78. * @since 3.0.0
  79. * @params array $location As generated by {@see get_location($con)}.
  80. */
  81. function print_location($location){
  82. echo("[{\"lat\":\"$location[lat]\",\"lon\":\"$location[lon]\",\"dtime\":\"$location[dtime]\"}]");
  83. }
  84. // SCRIPT START
  85. //Connect to the database
  86. $con = startdb('r');
  87. //Get location
  88. $location = get_location($con);
  89. if($location == null){
  90. //No content
  91. http_response_code(204);
  92. exit(0);
  93. }
  94. else{
  95. // Print the location
  96. print_location($location);
  97. }
  98. ?>