Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. <?php
  2.  
  3. if(isset($_GET["ip"]) && isset($_GET["key"]) && $_GET["key"] == "BlackDemonZyTandLaurenshup") {
  4. $ip = $_GET["ip"];
  5. $port = 25565;
  6. if(strpos($ip, ":")) {
  7. $split = explode(":", $ip);
  8. $ip = $split[0];
  9. $port = $split[1];
  10. }
  11.  
  12. $targetip = $ip;
  13. $targetport = $port;
  14. $records = dns_get_record("_minecraft._tcp.".$ip, DNS_SRV);
  15. if(!empty($records)) {
  16. foreach($records as $record) {
  17. if(isset($record["target"]) && isset($record["port"])) {
  18. $targetip = $record["target"];
  19. $targetport = $record["port"];
  20. }
  21. }
  22. }
  23.  
  24. // Database things starts here
  25. $servername = "localhost";
  26. $username = "BlackDemonZyT";
  27. $password = "BlackDemonZyTandLaurenshup";
  28. $conn = new mysqli($servername, $username, $password, "minecraft_widget");
  29.  
  30. $select = $conn->query("SELECT * FROM `server_queries` WHERE `ip` = '".$targetip."'");
  31.  
  32. if($select->num_rows == 1) { // Already in database
  33. $row = $select->fetch_array(MYSQLI_ASSOC);
  34. if(time() >= ($row["last_updated"] + 5)) { // Update required because the 5 seconds have passed
  35. require_once('minestat.php');
  36. $ms = new MineStat($targetip, $targetport);
  37. if($ms->is_online()) {
  38. $object = new stdClass();
  39. $object->o = str_replace("\0", '', $ms->get_current_players());
  40. $object->m = str_replace("\0", '', $ms->get_max_players());
  41. echo json_encode($object);
  42.  
  43. // Found server data, now update the values and add 1 request and register a new query for that client IP
  44. $conn->query("UPDATE `server_queries` SET `online_players`=".$object->o.",`max_players`=".$object->m.",`last_updated`=".time().",`requests`=".($row["requests"] + 1)." WHERE `ip`='".$targetip."'");
  45.  
  46. // Function to get the IP of the requester
  47. function get_client_ip() {
  48. $ipaddress = '';
  49. if (getenv('HTTP_CLIENT_IP'))
  50. $ipaddress = getenv('HTTP_CLIENT_IP');
  51. else if(getenv('HTTP_X_FORWARDED_FOR'))
  52. $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  53. else if(getenv('HTTP_X_FORWARDED'))
  54. $ipaddress = getenv('HTTP_X_FORWARDED');
  55. else if(getenv('HTTP_FORWARDED_FOR'))
  56. $ipaddress = getenv('HTTP_FORWARDED_FOR');
  57. else if(getenv('HTTP_FORWARDED'))
  58. $ipaddress = getenv('HTTP_FORWARDED');
  59. else if(getenv('REMOTE_ADDR'))
  60. $ipaddress = getenv('REMOTE_ADDR');
  61. else
  62. $ipaddress = 'UNKNOWN';
  63. return $ipaddress;
  64. }
  65.  
  66.  
  67. $ipaddress = get_client_ip();
  68. $now = today();
  69. $selected = $conn->query("SELECT * FROM `user_queries` WHERE `ip` = '".$ipaddress."'");
  70. if($selected->num_rows == 1) { // If requester Already in database
  71. $row = $selected->fetch_array(MYSQLI_ASSOC);
  72. $conn->query("UPDATE `user_queries` SET `requests`=".($row["requests"] + 1).",`last_updated`=".$now." WHERE `ip`='".$ipaddress."'");
  73. echo $conn;
  74. } else {
  75. $conn->query("INSERT INTO `user_queries` (`ip`, `last_updated`, `requests`) VALUES ('".$ipaddress."', '".$now."', '1')");
  76. echo $conn;
  77. }
  78.  
  79. }
  80. } else { // Return old values, 5 seconds have not passed
  81. $object = new stdClass();
  82. $object->o = $row["online_players"];
  83. $object->m = $row["max_players"];
  84. echo json_encode($object);
  85.  
  86. // Add 1 request
  87. $conn->query("UPDATE `server_queries` SET `requests`=".($row["requests"] + 1)." WHERE `ip`='".$targetip."'");
  88. }
  89. } else { // Not in database
  90. require_once('minestat.php');
  91. $ms = new MineStat($targetip, $targetport);
  92. if($ms->is_online()) {
  93. $object = new stdClass();
  94. $object->o = str_replace("\0", '', $ms->get_current_players());
  95. $object->m = str_replace("\0", '', $ms->get_max_players());
  96. echo json_encode($object);
  97.  
  98. // Found server data, now insert it into the database
  99. $conn->query("INSERT INTO `server_queries` (`ip`, `online_players`, `max_players`, `last_updated`, `requests`) VALUES ('".$targetip."', '".$object->o."', '".$object->m."', '".time()."', '1')");
  100. }
  101. }
  102. }
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement