sendlocation.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // $_GET valid parameters
  3. define('GET_USER', 'user');
  4. define('GET_PASS', 'pass');
  5. define('GET_ACTION', 'action');
  6. define('GET_LAT', 'lat');
  7. define('GET_LON', 'lon');
  8. // Valid values
  9. define('ACTION_START', 'start');
  10. define('ACTION_REFRESH', 'refresh');
  11. define('ACTION_STOP', 'stop');
  12. $actions = [ACTION_START, ACTION_REFRESH, ACTION_STOP];
  13. // Error messages
  14. define('ERR_USER', '-USER:');
  15. define('ERR_ACTION', '-ACTION:');
  16. define('ERR_LOCATION', '-TITLE:');
  17. /****************************************************
  18. * This function is called from almost everywhere at *
  19. * the beggining of the page. It initializes the *
  20. * session variables, connect to the db, enabling *
  21. * the variable $con for futher use everywhere in *
  22. * the php code, and populates the arrays $user *
  23. * and $permission, with info about the user. *
  24. * *
  25. * @return: (db connection): The connection handler. *
  26. ****************************************************/
  27. function startdb(){
  28. //Include the db configuration file. It's somehow like this
  29. /*
  30. <?php
  31. $host = 'XXXX';
  32. $db_name = 'XXXX';
  33. $username_ro = 'XXXX';
  34. $username_rw = 'XXXX';
  35. $pass_ro = 'XXXX';
  36. $pass_rw = 'XXXX';
  37. ?>
  38. */
  39. include('../../.htpasswd');
  40. //Connect to to database
  41. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  42. //Set encoding options
  43. mysqli_set_charset($con, 'utf-8');
  44. header('Content-Type: text/html; charset=utf8');
  45. mysqli_query($con, 'SET NAMES utf8;');
  46. //Return the db connection
  47. return $con;
  48. }
  49. $con = startdb('rw');
  50. $error = "";
  51. //Get fields
  52. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  53. $pass = mysqli_real_escape_string($con, $_GET[GET_PASS]);
  54. $lat = mysqli_real_escape_string($con, $_GET[GET_LAT]);
  55. $lon = mysqli_real_escape_string($con, $_GET[GET_LON]);
  56. $action = mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  57. //Validate user
  58. $q = mysqli_query($con, "SELECT id FROM user WHERE lower(username) = lower('$user') AND password = '$pass';");
  59. if (mysqli_num_rows($q) == 0){
  60. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  61. http_response_code(403); // Forbidden
  62. $error = $error . ERR_USER . mysqli_real_escape_string($con, $_GET[GET_USER]);
  63. error_log($error);
  64. exit(-1);
  65. }
  66. // Get id
  67. $r = mysqli_fetch_array($q);
  68. $uid = $r['id'];
  69. //Validate fields
  70. if (!in_array($action, $actions)){
  71. http_response_code(400); // Bad request
  72. $error = $error . ERR_ACTION . $action;
  73. error_log($error);
  74. exit(-2);
  75. }
  76. if (is_numeric($lat) == false || is_numeric($lon) == false){
  77. http_response_code(400); // Bad request
  78. $error = $error . ERR_LOCATION . '($lat, $lon)';
  79. error_log($error);
  80. exit(-3);
  81. }
  82. if (strlen($lat) == 0 xor strlen($lon) == 0){
  83. // Only one coordinate.
  84. http_response_code(400); // Bad request
  85. $error = $error . ERR_LOCATION . '($lat, $lon)';
  86. error_log($error);
  87. exit(-4);
  88. }
  89. if (strlen($lat) != 0 && ($lat < -90.0 || $lat > 90.0)){
  90. // Invalid latitude
  91. http_response_code(400); // Bad request
  92. $error = $error . ERR_LOCATION . '(Lat: $lat)';
  93. error_log($error);
  94. exit(-5);
  95. }
  96. if (strlen($lon) != 0 && ($lon < -180.0 || $lon > 180.0)){
  97. // Invalid longitude
  98. http_response_code(400); // Bad request
  99. $error = $error . ERR_LOCATION . '(Lon: $lat)';
  100. error_log($error);
  101. exit(-6);
  102. }
  103. // Discern action
  104. switch ($action){
  105. case ACTION_START:
  106. // Insert
  107. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  108. break;
  109. case ACTION_REFRESH:
  110. // Look for start node.
  111. $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;");
  112. if (mysqli_num_rows($q) == 0){
  113. // No recent reports. Start anew.
  114. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  115. }
  116. else{
  117. $r = mysqli_fetch_array($q);
  118. if ($r['action'] == 'F'){
  119. // Previous track was stoped. Start anew.
  120. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  121. }
  122. else{
  123. // Continue track.
  124. $s = $r['id'];
  125. mysqli_query($con, "INSERT INTO location (lat, lon, action, user, start) VALUES ($lat, $lon, 'R', $uid, $s);");
  126. }
  127. }
  128. break;
  129. case ACTION_STOP:
  130. // Look for start node.
  131. $q = mysqli_query($con, "SELECT id, start FROM location WHERE user = $uid AND dtime > NOW() - INTERVAL 30 MINUTE ORDER BY dtime DESC LIMIT 1;");
  132. if (mysqli_num_rows($q) > 0){
  133. $r = mysqli_fetch_array($q);
  134. if ($r['action'] != 'F'){
  135. // Finish track.
  136. $s = $r['start'];
  137. if (strlen($lat) > 0 && strlen($lon) > 0){
  138. mysqli_query($con, "INSERT INTO location (lat, lon, action, user, start) VALUES ($lat, $lon, 'F', $uid, $s);");
  139. }
  140. else{
  141. mysqli_query($con, "INSERT INTO location (action, user, start) VALUES ('F', $uid, $s);");
  142. }
  143. }
  144. }
  145. break;
  146. }
  147. http_response_code(204); // No content.
  148. ?>