chat.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * Coordinate decimal places to earth resolution
  4. decimal
  5. places degrees distance
  6. ------- ------- --------
  7. 0 1 111 km
  8. 1 0.1 11.1 km
  9. 2 0.01 1.11 km
  10. 3 0.001 111 m
  11. 4 0.0001 11.1 m
  12. 5 0.00001 1.11 m
  13. 6 0.000001 11.1 cm
  14. 7 0.0000001 1.11 cm
  15. 8 0.00000001 1.11 mm
  16. */
  17. require 'required.php';
  18. require 'onlyloggedin.php';
  19. use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;
  20. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) {
  21. sendError("Latitude (lat) is in the wrong format.", true);
  22. }
  23. if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) {
  24. sendError("Longitude (long) is in the wrong format.", true);
  25. }
  26. if (is_empty($VARS['msg'])) {
  27. // Get messages
  28. if (is_empty($VARS['lat']) || is_empty($VARS['long'])) {
  29. sendError("Missing information.", true);
  30. }
  31. $radius = 50;
  32. if (!is_empty($VARS['radius']) && is_numeric($VARS['radius'])) {
  33. $radius = intval($VARS['radius']);
  34. }
  35. $userlocation = GeoLocation::fromDegrees($VARS['lat'], $VARS['long']);
  36. $searchbounds = $userlocation->boundingCoordinates($radius, 'miles');
  37. //echo $searchbounds[0]->getLatitudeInDegrees();
  38. //echo $searchbounds[0]->getLongitudeInDegrees();
  39. //echo $searchbounds[1]->getLatitudeInDegrees();
  40. //echo $searchbounds[1]->getLongitudeInDegrees();
  41. $msgs = $database->select('messages', ["[>]players" => ["uuid" => "uuid"]], ['messages.uuid', 'messages.message', 'messages.time', 'players.nickname'], ['AND' =>
  42. ["OR" =>
  43. [
  44. "AND #regular messages" =>
  45. [
  46. 'lat[>]' => $searchbounds[0]->getLatitudeInDegrees(), 'lat[<]' => $searchbounds[1]->getLatitudeInDegrees(), 'long[>]' => $searchbounds[0]->getLongitudeInDegrees(), 'long[<]' => $searchbounds[1]->getLongitudeInDegrees()
  47. ],
  48. "AND #global announcement messages" =>
  49. [
  50. 'lat' => null, 'long' => null
  51. ]
  52. ]
  53. ],
  54. "ORDER" => "messages.time DESC",
  55. "LIMIT" => 30
  56. ]);
  57. foreach ($msgs as $key => $msg) {
  58. if (is_null($msg['uuid'])) {
  59. $msgs[$key]['uuid'] = "0";
  60. $msgs[$key]['nickname'] = "SERVER MESSAGE";
  61. $msgs[$key]['color'] = CHAT_ADMIN_COLOR;
  62. $msgs[$key]['css'] = CHAT_ADMIN_CSS;
  63. } else if (in_array($msg['nickname'], CHAT_ADMINS)) {
  64. $msgs[$key]['color'] = CHAT_ADMIN_COLOR;
  65. $msgs[$key]['css'] = CHAT_ADMIN_CSS;
  66. }
  67. }
  68. echo json_encode($msgs);
  69. } else {
  70. // Post message
  71. if (is_empty($VARS['lat']) || is_empty($VARS['long']) || is_empty($VARS['msg'])) {
  72. sendError("Missing information.", true);
  73. }
  74. $msg = strip_tags($VARS['msg']);
  75. preg_match_all("/\@\w+/", $msg, $search, PREG_PATTERN_ORDER);
  76. $privmsgto = $search[0];
  77. foreach ($privmsgto as $to) {
  78. $name = str_replace("@", "", $to); // Remove leading @
  79. if ($database->has('players', ['nickname' => $name])) {
  80. $touuid = $database->select('players', ['uuid'], ['nickname' => $name])[0]['uuid'];
  81. $database->insert('private_messages', ['#time' => 'NOW()', 'message' => $msg, 'from_uuid' => $_SESSION['uuid'], 'to_uuid' => $touuid]);
  82. }
  83. }
  84. $database->insert('messages', ['#time' => 'NOW()', 'uuid' => $_SESSION['uuid'], 'message' => $msg, 'lat' => $VARS['lat'], 'long' => $VARS['long']]);
  85. sendOK();
  86. }