oaparse.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /*
  3. * Copyright (C) 2012 Leah Rowe <info@minifree.org>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /* Show server information */
  25. include "oaprotected/common.php";
  26. function getStats()
  27. {
  28. $rxEmail = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(".
  29. "\.[a-z]{2,3})$/";
  30. $rxHostname = "/^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([".
  31. "A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/";
  32. $rxIPv4Addr = "/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.".
  33. "){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/";
  34. // Output text (or error text)
  35. $output = "";
  36. $strError = "";
  37. // IP:Port variables
  38. $getIPAddr = "";
  39. $getPortNum = "";
  40. // check for existence of GET variable
  41. if (isset($_GET["ipaddr"])) $getIPAddr = clean($_GET["ipaddr"]);
  42. if (isset($_GET["portnum"])) $getPortNum = clean($_GET["portnum"]);
  43. // check for empty string
  44. if (strlen($getIPAddr) == 0) $strError .=
  45. "<li>IP Address not set in GET</li>";
  46. if (strlen($getPortNum) == 0) $strError .=
  47. "<li>Portnum not set in GET</li>";
  48. // if no errors so far, check the regular expression
  49. if (strlen($strError) == 0)
  50. {
  51. if (!preg_match($rxIPv4Addr,$getIPAddr))
  52. $strError .= "<li>IPv4 Address does not conform to ".
  53. "regular expression.</li>";
  54. if (!is_numeric($getPortNum))
  55. $strError .= "<li>Portnum is not numerical.</li>";
  56. else if ((int)$getPortNum < 1 || (int)$getPortNum > 0xFFFE)
  57. $strError .= "<li>Not a valid port number.</li>";
  58. }
  59. // if no errors no far, check that this server exists in the database
  60. if (strlen($strError) == 0)
  61. {
  62. $sqlServers = inject("SELECT * FROM servers WHERE (ipaddr = ".
  63. "'$getIPAddr' AND portnum = '$getPortNum')");
  64. $sqlServersNrows = mysql_numrows($sqlServers);
  65. // if server does not exist, return with error
  66. if ($sqlServersNrows == 0)
  67. $strError .= "<li>Server $getIPAddr:$getPortNum does ".
  68. "not exist in the database.</li>";
  69. // if server exists, check whether it is listed as online
  70. else
  71. {
  72. $sqlServersServerDown = mysql_result($sqlServers, 0,
  73. "serverdown");
  74. if ($sqlServersServerDown == "1")
  75. $strError .= "<li>That server is down at the ".
  76. "moment, according to the database.</li>";
  77. }
  78. }
  79. // if there are input errors, return with an error list
  80. if (strlen($strError) > 0)
  81. return "<h1>There were errors</h1><ul>$strError</ul>";
  82. /* --- Error checking done, input was valid. ---- */
  83. // find that server on dpmaster
  84. $sqlQStat = inject("SELECT * FROM qstat");
  85. $sqlQStatNrows = mysql_numrows($sqlQStat);
  86. for ($i = 0; $i < $sqlQStatNrows; $i++) {
  87. $sqlQStatListURL = mysql_result($sqlQStat, $i, "listurl");
  88. $content = curlGetPage($sqlQStatListURL .
  89. (strpos($sqlQStatListURL,"?")!==false?"&":"?") .
  90. "server=$getIPAddr:$getPortNum&xml=1");
  91. $qstat = new SimpleXMLElement($content);
  92. $server_status = $qstat->server[0]['status'];
  93. if ($server_status != "UP") {
  94. continue;
  95. }
  96. // get name of server, set as page title <h1>
  97. foreach($qstat->server[0]->rules->rule as $rule) {
  98. if ($rule['name'] == "sv_hostname")
  99. $output .= "<h1>$rule</h1>";
  100. }
  101. // get player information
  102. $output .= "<table class='playertable'>";
  103. $output .= "<tr><th>Player</th><th>Score</th><th>Ping</th>".
  104. "</tr>";
  105. foreach($qstat->server[0]->players->player as $player) {
  106. $output .= "<tr><td>".$player->name."</td><td>".
  107. $player->score."</td><td>".$player->ping.
  108. "</td></tr>";
  109. }
  110. $output .= "</table>";
  111. // get other server statistics
  112. $output .= "<h1>Server Statistics</h1>";
  113. $output .= "<table class='ruletable'>";
  114. $output .= "<tr><th>Hostname</th><td>".
  115. $qstat->server[0]->hostname."</td></tr>";
  116. $output .= "<tr><th>Name</th><td>".
  117. $qstat->server[0]->name."</td></tr>";
  118. $output .= "<tr><th>Gametype</th><td>".
  119. $qstat->server[0]->gametype."</td></tr>";
  120. $output .= "<tr><th>Map</th><td>".
  121. $qstat->server[0]->map."</td></tr>";
  122. $output .= "<tr><th>Players</th><td>".
  123. $qstat->server[0]->numplayers."</td></tr>";
  124. $output .= "<tr><th>Maxplayers</th><td>".
  125. $qstat->server[0]->maxplayers."</td></tr>";
  126. $output .= "<tr><th>Ping</th><td>".
  127. $qstat->server[0]->ping."</td></tr>";
  128. $output .= "<tr><th>Retries</th><td>".
  129. $qstat->server[0]->retries."</td></tr>";
  130. $output .= "</table>";
  131. // get output for server rules
  132. $output .= "<h1>Server Rules</h1>";
  133. $output .= "<table class='ruletable'>";
  134. foreach($qstat->server[0]->rules->rule as $rule) {
  135. $output .= "<tr><th>".$rule['name']."</th>".
  136. "<td>".$rule."</td></tr>";
  137. }
  138. $output .= "</table>";
  139. }
  140. return "$output";
  141. }
  142. ?>