servers.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. // Description:
  3. // SoundRTS clients call this script to get the servers list.
  4. // Parameters:
  5. // - header: string to include at the start of the reply
  6. // (optional; used to detect 404 errors; nothing is included by default)
  7. // (must contain only letters, numbers and underscores; max length: 20)
  8. // - include_ports: if 1, include the port numbers in the reply
  9. // (optional; used after 1.1-a4; 0 by default)
  10. // Example: http://jlpo.free.fr/soundrts/metaserver/servers.php
  11. // Example: http://jlpo.free.fr/soundrts/metaserver/servers.php?header=SERVERS_LIST
  12. // Example: http://jlpo.free.fr/soundrts/metaserver/servers.php?header=SERVERS_LIST&include_ports=1
  13. // Returns, for each server, a line containing, separated by the space character:
  14. // - the UNIX time in seconds;
  15. // - the IP address of the server;
  16. // - the compatibility version of the server; for example: 1.0-b10o;
  17. // - the login of the server.
  18. // End of description.
  19. // The code starts here...
  20. require("config.php");
  21. // get the parameters
  22. $rvar_header = "";
  23. import_request_variables("gP", "rvar_");
  24. if (!preg_match("/^[a-zA-Z0-9_]{0,20}$/", $rvar_header)) exit("error: bad header");
  25. if (!preg_match("/^[0-1]$/", $rvar_include_ports)) $rvar_include_ports = "0";
  26. // print the optional header
  27. echo $rvar_header;
  28. // connect to the database
  29. $link = mysql_connect($dbhost, $dbuser, $dbpasswd)
  30. or die("error: connexion to database failed");
  31. mysql_select_db($dbname)
  32. or die("error: could not select database");
  33. // get the servers from the database
  34. $time_limit = time() - 60 * 10;
  35. $query = 'SELECT time, ip, version, login, port FROM soundrts_servers WHERE time > '.$time_limit;
  36. $result = mysql_query($query) or die('error: query failed: '.mysql_error());
  37. // print a line for each server
  38. while ($row = mysql_fetch_assoc($result)) {
  39. if ($rvar_include_ports == "0")
  40. echo $row["time"]." ".$row["ip"]." ".$row["version"]." ".$row["login"]."\n";
  41. else
  42. echo $row["time"]." ".$row["ip"]." ".$row["version"]." ".$row["login"]." ".$row["port"]."\n";
  43. }
  44. // disconnect from the database
  45. mysql_close($link);
  46. ?>