sendlocation.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // Gasteizko Margolariak API v3 //
  3. //var_dump($_POST);
  4. //exit(0);
  5. // $_GET valid parameters
  6. define('GET_USER', 'user');
  7. define('GET_PASS', 'pass');
  8. define('GET_ACTION', 'action');
  9. define('GET_LAT', 'lat');
  10. define('GET_LON', 'lon');
  11. // Valid values
  12. define('ACTION_START', 'start');
  13. define('ACTION_REFRESH', 'refresh');
  14. define('ACTION_STOP', 'stop');
  15. $actions = [ACTION_START, ACTION_REFRESH, ACTION_STOP];
  16. // Error messages
  17. define('ERR_USER', '-USER:');
  18. define('ERR_ACTION', '-ACTION:');
  19. define('ERR_LOCATION', '-TITLE:');
  20. include('functions.php');
  21. $con = startdb('rw');
  22. $error = "";
  23. //Get fields
  24. $user = mysqli_real_escape_string($con, $_POST[GET_USER]);
  25. $pass = mysqli_real_escape_string($con, $_POST[GET_PASS]);
  26. $lat = mysqli_real_escape_string($con, $_GET[GET_LAT]);
  27. $lon = mysqli_real_escape_string($con, $_GET[GET_LON]);
  28. $action = mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  29. //Validate user
  30. if (!login($con, $user, $pass)){
  31. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  32. $error = $error . ERR_USER . mysqli_real_escape_string($con, $user);
  33. error_log($error);
  34. http_response_code(403); // Forbidden
  35. exit(-1);
  36. }
  37. //Validate fields
  38. if (!in_array($action, $actions)){
  39. http_response_code(400); // Bad request
  40. $error = $error . ERR_ACTION . $action;
  41. error_log($error);
  42. exit(-2);
  43. }
  44. if (is_numeric($lat) == false || is_numeric($lon) == false){
  45. http_response_code(400); // Bad request
  46. $error = $error . ERR_LOCATION . '($lat, $lon)';
  47. error_log($error);
  48. exit(-3);
  49. }
  50. if (strlen($lat) == 0 xor strlen($lon) == 0){
  51. // Only one coordinate.
  52. http_response_code(400); // Bad request
  53. $error = $error . ERR_LOCATION . '($lat, $lon)';
  54. error_log($error);
  55. exit(-4);
  56. }
  57. if (strlen($lat) != 0 && ($lat < -90.0 || $lat > 90.0)){
  58. // Invalid latitude
  59. http_response_code(400); // Bad request
  60. $error = $error . ERR_LOCATION . '(Lat: $lat)';
  61. error_log($error);
  62. exit(-5);
  63. }
  64. if (strlen($lon) != 0 && ($lon < -180.0 || $lon > 180.0)){
  65. // Invalid longitude
  66. http_response_code(400); // Bad request
  67. $error = $error . ERR_LOCATION . '(Lon: $lat)';
  68. error_log($error);
  69. exit(-6);
  70. }
  71. // Discern action
  72. switch ($action){
  73. case ACTION_START:
  74. // Insert
  75. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  76. break;
  77. case ACTION_REFRESH:
  78. // Look for start node.
  79. $q = mysqli_query($con, "SELECT id, start, action FROM location WHERE user = $uid AND dtime > NOW() - INTERVAL 30 MINUTE ORDER BY dtime DESC LIMIT 1;");
  80. if (mysqli_num_rows($q) == 0){
  81. // No recent reports. Start anew.
  82. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  83. }
  84. else{
  85. $r = mysqli_fetch_array($q);
  86. if ($r['action'] == 'F'){
  87. // Previous track was stoped. Start anew.
  88. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  89. }
  90. else{
  91. // Continue track.
  92. $s = $r['id'];
  93. mysqli_query($con, "INSERT INTO location (lat, lon, action, user, start) VALUES ($lat, $lon, 'R', $uid, $s);");
  94. }
  95. }
  96. break;
  97. case ACTION_STOP:
  98. // Look for start node.
  99. $q = mysqli_query($con, "SELECT id, start FROM location WHERE user = $uid AND dtime > NOW() - INTERVAL 30 MINUTE ORDER BY dtime DESC LIMIT 1;");
  100. if (mysqli_num_rows($q) > 0){
  101. $r = mysqli_fetch_array($q);
  102. if ($r['action'] != 'F'){
  103. // Finish track.
  104. $s = $r['start'];
  105. if (strlen($lat) > 0 && strlen($lon) > 0){
  106. mysqli_query($con, "INSERT INTO location (lat, lon, action, user, start) VALUES ($lat, $lon, 'F', $uid, $s);");
  107. }
  108. else{
  109. mysqli_query($con, "INSERT INTO location (action, user, start) VALUES ('F', $uid, $s);");
  110. }
  111. }
  112. }
  113. break;
  114. }
  115. http_response_code(204); // No content.
  116. ?>