sendlocation.php 4.8 KB

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