sendlocation.php 4.6 KB

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