places.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Takes the latitude and longitude and gets nearby places from OSM.
  4. *
  5. * Uses WGS84 in the DD.DD format, because I say so.
  6. */
  7. require 'required.php';
  8. //ini_set('memory_limit','32M');
  9. use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
  10. if (is_empty($VARS['lat'])) {
  11. sendError("Missing required latitude (lat) variable.", true);
  12. }
  13. if (is_empty($VARS['long'])) {
  14. sendError("Missing required longitude (long) variable.", true);
  15. }
  16. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{1,}/', $VARS['lat'])) {
  17. sendError("Latitude (lat) is in the wrong format, or does not have enough precision (DD.DD, at least 2 decimal places.", true);
  18. }
  19. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{1,}/', $VARS['long'])) {
  20. sendError("Longitude (long) is in the wrong format, or does not have enough precision (DD.DD, at least 2 decimal places.", true);
  21. }
  22. $lat = number_format((float) $VARS['lat'], 5, '.', '');
  23. $long = number_format((float) $VARS['long'], 5, '.', '');
  24. $radius = 5;
  25. if (!is_empty($VARS['radius']) && is_numeric($VARS['radius'])) {
  26. $radius = floatval($VARS['radius']);
  27. }
  28. if ($VARS['debug']) {
  29. echo "got to the place data query ";
  30. }
  31. $places = json_decode(file_get_contents("http://gis.terranquest.net/places.php?key=" . GIS_API_KEY . "&lat=" . $VARS['lat'] . "&long=" . $VARS['long'] . "&radius=" . $radius), TRUE)['features'];
  32. $data['status'] = 'OK';
  33. $data['places'] = $places;
  34. header('Content-Type: application/json');
  35. $geo['name'] = "Places";
  36. $geo['type'] = 'FeatureCollection';
  37. $geo['features'] = [];
  38. if ($VARS['debug']) {
  39. echo "got to the game data loop ";
  40. }
  41. foreach ($places as $place) {
  42. $osmid = $place['properties']['osm_id'];
  43. $name = $place['properties']['name'];
  44. $plong = $place['geometry']['coordinates'][0];
  45. $plat = $place['geometry']['coordinates'][1];
  46. if (!$database->has('locations', ['osmid' => $osmid])) {
  47. $database->insert('locations', ['osmid' => $osmid, 'teamid' => 0]);
  48. }
  49. $gameinfo = $database->select('locations', ["[>]players" => ["owneruuid" => "uuid"]], ['locations.locationid', 'players.nickname', 'locations.teamid', 'locations.currentlife', 'locations.maxlife'], ['osmid' => $osmid])[0];
  50. //$gameinfo = $database->select('locations', ['locationid', 'teamid', 'currentlife', 'maxlife'], ['osmid' => $osmid])[0];
  51. // Reset owner info for dead places
  52. if ($gameinfo['currentlife'] <= 0) {
  53. $database->update('locations', ['teamid' => 0, 'owneruuid' => null, 'maxlife' => 0], ['locationid' => $gameinfo['locationid']]);
  54. $gameinfo = $database->select('locations', ['locationid', 'teamid', 'currentlife', 'maxlife'], ['osmid' => $osmid])[0];
  55. $gameinfo['nickname'] = "";
  56. }
  57. $geo['features'][] = array("type" => "Feature",
  58. "geometry" => [
  59. "type" => "Point",
  60. "coordinates" => [
  61. floatval($plong),
  62. floatval($plat)
  63. ]
  64. ],
  65. "properties" => [
  66. "osm_id" => intval($osmid),
  67. "name" => $name,
  68. "gameinfo" => $gameinfo//['teamid' => $gameinfo['teamid'], 'owneruuid' => $gameinfo['owneruuid']]
  69. ]
  70. );
  71. }
  72. if ($VARS['debug']) {
  73. echo "got all the way to the encode ";
  74. }
  75. function utf8ize($d) {
  76. if (is_array($d)) {
  77. foreach ($d as $k => $v) {
  78. $d[$k] = utf8ize($v);
  79. }
  80. } else if (is_object($d)) {
  81. foreach ($d as $k => $v) {
  82. $d->$k = utf8ize($v);
  83. }
  84. } else {
  85. return utf8_encode($d);
  86. }
  87. return $d;
  88. }
  89. echo json_encode(utf8ize($geo));
  90. if ($VARS['debug']) {
  91. switch (json_last_error()) {
  92. case JSON_ERROR_NONE:
  93. echo ' - No errors';
  94. break;
  95. case JSON_ERROR_DEPTH:
  96. echo ' - Maximum stack depth exceeded';
  97. break;
  98. case JSON_ERROR_STATE_MISMATCH:
  99. echo ' - Underflow or the modes mismatch';
  100. break;
  101. case JSON_ERROR_CTRL_CHAR:
  102. echo ' - Unexpected control character found';
  103. break;
  104. case JSON_ERROR_SYNTAX:
  105. echo ' - Syntax error, malformed JSON';
  106. break;
  107. case JSON_ERROR_UTF8:
  108. echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  109. break;
  110. default:
  111. echo ' - Unknown error';
  112. break;
  113. }
  114. }