sendlocation.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. //Get fields
  51. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  52. $pass = mysqli_real_escape_string($con, $_GET[GET_PASS]);
  53. $lat = mysqli_real_escape_string($con, $_GET[GET_LAT]);
  54. $lon = mysqli_real_escape_string($con, $_GET[GET_LON]);
  55. $action = mysqli_real_escape_string($con, $_GET[GET_ACTION]);
  56. //Validate user
  57. $q = mysqli_query($con, "SELECT id FROM user WHERE username = '$user' AND md5(concat(password, md5(salt))) = '$pass';");
  58. if (mysqli_num_rows($q) == 0){
  59. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  60. http_response_code(403); // Forbidden
  61. $error = $error . ERR_TARGET . mysqli_real_escape_string($con, $_GET[GET_TARGET]);
  62. }
  63. // Get id
  64. $r = mysqli_fetch_array($q);
  65. $uid = $r['id'];
  66. //Validate fields
  67. if (!in_array($action, $actions)){
  68. http_response_code(400); // Bad request
  69. $error = $error . ERR_ACTION . $action;
  70. }
  71. if (is_numeric($lat) == false || is_numeric($lon) == false){
  72. http_response_code(400); // Bad request
  73. $error = $error . ERR_LOCATION . '($lat, $lon)';
  74. }
  75. if (strlen($lat) == 0 xor strlen($lon) == 0){
  76. // Only one coordinate.
  77. http_response_code(400); // Bad request
  78. $error = $error . ERR_LOCATION . '($lat, $lon)';
  79. }
  80. if (strlen($lat) != 0 && ($lat < -90.0 || $lat > 90.0)){
  81. // Invalid latitude
  82. http_response_code(400); // Bad request
  83. $error = $error . ERR_LOCATION . '(Lat: $lat)';
  84. }
  85. if (strlen($lon) != 0 && ($lon < -180.0 || $lon > 180.0)){
  86. // Invalid longitude
  87. http_response_code(400); // Bad request
  88. $error = $error . ERR_LOCATION . '(Lon: $lat)';
  89. }
  90. // Discern action
  91. switch ($action){
  92. case ACTION_START:
  93. // Insert
  94. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  95. break;
  96. case ACTION_REFRESH:
  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. // No recent reports. Start anew.
  101. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  102. }
  103. else{
  104. $r = mysqli_fetch_array($q);
  105. if ($r['action'] == 'F'){
  106. // Previous track was stoped. Start anew.
  107. mysqli_query($con, "INSERT INTO location (lat, lon, action, user) VALUES ($lat, $lon, 'S', $uid);");
  108. }
  109. else{
  110. // Continue track.
  111. $s = $r['start'];
  112. mysqli_query($con, "INSERT INTO location (lat, lon, action, user, start) VALUES ($lat, $lon, 'S', $uid, $s);");
  113. }
  114. }
  115. break;
  116. case ACTION_STOP:
  117. // Look for start node.
  118. $q = mysqli_query($con, "SELECT id, start FROM location WHERE user = $uid AND dtime > NOW() - INTERVAL 30 MINUTE ORDER BY dtime DESC LIMIT 1;");
  119. if (mysqli_num_rows($q) > 0){
  120. $r = mysqli_fetch_array($q);
  121. if ($r['action'] != 'F'){
  122. // Finish track.
  123. $s = $r['start'];
  124. if (strlen($lat) > 0 && strlen($lon) > 0){
  125. mysqli_query($con, "INSERT INTO location (lat, lon, action, user, start) VALUES ($lat, $lon, 'F', $uid, $s);");
  126. }
  127. else{
  128. mysqli_query($con, "INSERT INTO location (action, user, start) VALUES ('F', $uid, $s);");
  129. }
  130. }
  131. }
  132. break;
  133. }
  134. http_response_code(204); // No content.
  135. ?>