Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎉 Added get-server-details #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.log
/.idea/
1 change: 1 addition & 0 deletions publish/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ RewriteEngine On
# Allow pretty paths without file extensions
RewriteRule ^server-list$ server-list.php
RewriteRule ^get-public-ip$ get-public-ip.php
RewriteRule ^get-server-details$ get-server-details.php
55 changes: 55 additions & 0 deletions publish/get-server-details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

require 'globals.include.php';
require 'config.include.php';

$mysqli = connect_mysqli_or_die($config);

$sql_where = "";

if (isset($_GET['name'])) {
$name = $mysqli->real_escape_string($_GET['name']);
$sql_where .= "`name` = '$name'";
} else if (isset($_GET['ip'])) {
$ip = $mysqli->real_escape_string($_GET['ip']);
$sql_where .= "`ip` = '$ip'";
} else {
die_json(400, "name or ip not specified");
}

$sql = "
SELECT
`name`,
`description`,
`terrain-name`,
`version`
`is-official`,
`current-users`,
`max-clients`,
`country`,
`has-password`,
`json-userlist`
FROM
servers
WHERE
$sql_where";

$result = $mysqli->query($sql);
if ($result === false || $result->num_rows == 0) {
die_json(404, "not found");
}

$data = array();

header('content-type: application/json; charset: utf-8');
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
if ($key == "json-userlist")
$data[$key] = json_decode($value);
else if (is_numeric($value))
$data[$key] = intval($value);
else
$data[$key] = $value;
}
}
print json_encode($data);
95 changes: 94 additions & 1 deletion publish/globals.include.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,97 @@
define('LOG_LEVEL_WARNING', 1);
define('LOG_LEVEL_INFO', 2);
define('LOG_LEVEL_DETAIL', 3);
define('LOG_LEVEL_DEBUG', 4);
define('LOG_LEVEL_DEBUG', 4);

function log_msg($level, $msg)
{
global $config;

if ($level > $config['logging']['verbosity']) {
return;
}
$d = new DateTime();
$line = $d->format('[Y-m-d H:i:s]') . " " . $msg . "\n";
file_put_contents($config['logging']['filename'], $line, FILE_APPEND);
}

function log_error($msg)
{
log_msg(LOG_LEVEL_ERROR, $msg);
}

function log_warn($msg)
{
log_msg(LOG_LEVEL_WARNING, $msg);
}

function log_info($msg)
{
log_msg(LOG_LEVEL_INFO, $msg);
}

function log_detail($msg)
{
log_msg(LOG_LEVEL_DETAIL, $msg);
}

function log_debug($msg)
{
log_msg(LOG_LEVEL_DEBUG, $msg);
}

function die_json($http_code, $message)
{
header('content-type: application/json; charset: utf-8');
http_response_code($http_code);
$answer = array(
'result' => ($http_code == 200),
'message' => $message
);
log_info("Response: HTTP {$http_code}, message: {$message}");
log_detail("JSON:\n" . json_encode($answer, JSON_PRETTY_PRINT));
die(json_encode($answer));
}

function check_args_or_die($_args, $req_args)
{
$missing = array();
foreach ($req_args as $key) {
if (!isset($_args[$key])) {
$missing[] = $key;
}
}

if (count($missing) > 0) {
die_json(400, 'Missing required argument(s): ' . implode(", ", $missing));
}
}

function connect_mysqli_or_die($config)
{
$db_config = $config['database'];
$mysqli = new MySQLi($db_config['host'], $db_config['user'], $db_config['password'], $db_config['name']);
if ($mysqli->connect_errno != 0) {
log_error("Cannot connect to DB: {$mysqli->error}");
die_json(500, 'Server error, cannot connect to database.');
}
return $mysqli;
}

function get_json_input()
{
$in_json = trim(file_get_contents('php://input'));
log_detail("JSON input:" . ($in_json != "" ? "\n{$in_json}" : " ~EMPTY~"));
return json_decode($in_json, true);
}

function check_and_purge_outdated()
{
global $config, $mysqli;

$t = time() - $config['heartbeat']['purge-timeout-sec'];
$result = $mysqli->query("DELETE FROM `servers` WHERE `last-heartbeat` < $t;");
if ($result === false) {
log_error("Failed to purge outdated, message: " . $mysqli->error);
}
}
163 changes: 85 additions & 78 deletions publish/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
$db_config = $config['database'];
$mysqli = new MySQLi($db_config['host'], $db_config['user'], $db_config['password'], $db_config['name']);

if ($mysqli->connect_errno != 0)
{
if ($mysqli->connect_errno != 0) {
$data["serverlist-html"] = "<p><strong>Sorry</strong>, a database error occurred.</p>";
}
else
{
} else {
$sql_and_heart = ' AND `last-heartbeat` >= ' . (time() - $config['heartbeat']['hide-timeout-sec']);
// Available servers
$sql = "
Expand All @@ -36,18 +33,13 @@
`is-official`,
(`current-users`/`max-clients`) DESC,
name";

$result = $mysqli->query($sql);
if ($result === false)
{
if ($result === false) {
$data["serverlist-html"] = "<p><strong>Sorry</strong>, a database error occurred.</p>";
}
else if ($result->num_rows == 0)
{
} else if ($result->num_rows == 0) {
$data["serverlist-html"] = "<p>No servers online at the moment.</p>";
}
else
{
} else {
$num_players_total = 0;
$servlist_html = ["<table class='w3-table w3-striped w3-bordered w3-border'>
<tr class='w3-orange'>
Expand All @@ -56,39 +48,35 @@
<th><span class='fa fa-gamepad w3-margin-right'></span>Name</th>
<th><span class='fa fa-road w3-margin-right'></span>Terrain</th>
</tr>"];

while ($row = $result->fetch_assoc())
{

while ($row = $result->fetch_assoc()) {
$type = array();
$name = $row['name'];
if ($row['verified'] == 2)
{
if ($row['verified'] == 2) {
array_push($type, "ranked");
}
if ($row['is-official'] == 1)
{
if ($row['is-official'] == 1) {
array_push($type, "official");
$name = "Official: {$row['name']}";
}
if ($row['has-password'])
{
if ($row['has-password']) {
array_push($type, "password");
}
$types = implode($type, ', ');
$types = implode(', ', $type);
$name_html = htmlspecialchars($name);
$terrn_html = htmlspecialchars($row['terrain-name']);

$servlist_html[] =
"<tr>
"<tr>
<td>{$row['current-users']} / {$row['max-clients']}</td>
<td>$types</td>
<td>$name_html</td>
<td>$terrn_html</td>
</tr>";

$num_players_total += $row['current-users'];
}

$servlist_html[] = "</table>";
$data["serverlist-html"] = implode($servlist_html);
$data["num-online-players"] = $num_players_total;
Expand All @@ -107,62 +95,81 @@
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
#ror-mp-dev-info-box { max-width: 800px; margin-left: auto; margin-right: auto; }
#ror-logo { height: 38px; }
</style>
#ror-mp-dev-info-box {
max-width: 800px;
margin-left: auto;
margin-right: auto;
}

#ror-logo {
height: 38px;
}
</style>
</head>
<body>
<!-- Navbar -->
<div class="w3-display-container">
<ul class="w3-navbar w3-left-align w3-orange">
<li class="w3-hide-medium w3-hide-large w3-opennav w3-right">
<a class="w3-hover-khaki w3-theme-d2" href="javascript:void(0);" onclick="ror_toggle_mobile_nav()"><i class="fa fa-bars"></i></a>
<!-- Navbar -->
<div class="w3-display-container">
<ul class="w3-navbar w3-left-align w3-orange">
<li class="w3-hide-medium w3-hide-large w3-opennav w3-right">
<a class="w3-hover-khaki w3-theme-d2" href="javascript:void(0);" onclick="ror_toggle_mobile_nav()"><i
class="fa fa-bars"></i></a>
</li>
<li><a href="#" class="w3-orange w3-padding-0"><img id="ror-logo"
src="http://rigsofrods.org/images/logos/rorlogo.png"
alt="Logo"></a></li>
<li class="w3-hide-small w3-right"><a href="https://github.com/RigsOfRods" class="w3-hover-khaki"><span
class="fa fa-wrench w3-margin-right"></span>Development</a></li>
<li class="w3-hide-small w3-right"><a href="http://docs.rigsofrods.org/" class="w3-hover-khaki"><span
class="fa fa-book w3-margin-right"></span>Docs</a></li>
<li class="w3-hide-small w3-right"><a href="http://forum.rigsofrods.org/" class="w3-hover-khaki"><span
class="fa fa-comments w3-margin-right"></span>Forum</a></li>
<li class="w3-hide-small w3-right"><a href="http://www.rigsofrods.org/" class="w3-hover-khaki"><span
class="fa fa-home w3-margin-right"></span>Home</a></li>
</ul>

<!-- Navbar on small screens -->
<div id="ror_mobile_nav" class="w3-hide w3-hide-large w3-hide-medium">
<ul class="w3-navbar w3-left-align w3-gray">
<li><a href="http://www.rigsofrods.org/"> <span class="fa fa-home w3-margin-right"></span>Home</a></li>
<li><a href="http://forum.rigsofrods.org/"> <span class="fa fa-comments w3-margin-right"></span>Forum</a>
</li>
<li><a href="http://docs.rigsofrods.org/"> <span class="fa fa-book w3-margin-right"></span>Docs</a></li>
<li><a href="https://github.com/RigsOfRods"><span class="fa fa-wrench w3-margin-right"></span>Development</a>
</li>
<li><a href="#" class="w3-orange w3-padding-0"><img id="ror-logo" src="http://rigsofrods.org/images/logos/rorlogo.png" alt="Logo"></a></li>
<li class="w3-hide-small w3-right"><a href="https://github.com/RigsOfRods" class="w3-hover-khaki"><span class="fa fa-wrench w3-margin-right"></span>Development</a></li>
<li class="w3-hide-small w3-right"><a href="http://docs.rigsofrods.org/" class="w3-hover-khaki"><span class="fa fa-book w3-margin-right"></span>Docs</a></li>
<li class="w3-hide-small w3-right"><a href="http://forum.rigsofrods.org/" class="w3-hover-khaki"><span class="fa fa-comments w3-margin-right"></span>Forum</a></li>
<li class="w3-hide-small w3-right"><a href="http://www.rigsofrods.org/" class="w3-hover-khaki"><span class="fa fa-home w3-margin-right"></span>Home</a></li>
</ul>

<!-- Navbar on small screens -->
<div id="ror_mobile_nav" class="w3-hide w3-hide-large w3-hide-medium">
<ul class="w3-navbar w3-left-align w3-gray">
<li><a href="http://www.rigsofrods.org/"> <span class="fa fa-home w3-margin-right"></span>Home</a></li>
<li><a href="http://forum.rigsofrods.org/"> <span class="fa fa-comments w3-margin-right"></span>Forum</a></li>
<li><a href="http://docs.rigsofrods.org/"> <span class="fa fa-book w3-margin-right"></span>Docs</a></li>
<li><a href="https://github.com/RigsOfRods"><span class="fa fa-wrench w3-margin-right"></span>Development</a></li>
</ul>
</div>
</div>
</div>

<header class="w3-container w3-center">
<h1>Rigs of Rods - Multiplayer portal</h1>

<p><?php echo (int) $data["num-online-servers"] ?> servers / <?php echo $data["num-online-players"] ?> players online.</p>

<div id="ror-mp-dev-info-box" class="w3-panel w3-teal w3-round">
<h2>Under development</h2>
<p>For info and discussion on multiplayer features, see <a href="http://forum.rigsofrods.org/thread-503.html">this forum topic</a>.</p>
<p>Contributors welcome! Find the development repository at <a href="https://github.com/RigsOfRods/multiplayer.rigsofrods.org">GitHub</a>.</p>
</div>
</header>

<section class="w3-container w3-center">
<h1>Server list</h1>

<?php echo $data["serverlist-html"] ?>
</section>

<script>
function ror_toggle_mobile_nav() {
var x = document.getElementById("ror_mobile_nav");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
<header class="w3-container w3-center">
<h1>Rigs of Rods - Multiplayer portal</h1>

<p><?php echo (int)$data["num-online-servers"] ?> servers / <?php echo $data["num-online-players"] ?> players
online.</p>

<div id="ror-mp-dev-info-box" class="w3-panel w3-teal w3-round">
<h2>Under development</h2>
<p>For info and discussion on multiplayer features, see <a href="http://forum.rigsofrods.org/thread-503.html">this
forum topic</a>.</p>
<p>Contributors welcome! Find the development repository at <a
href="https://github.com/RigsOfRods/multiplayer.rigsofrods.org">GitHub</a>.</p>
</div>
</header>

<section class="w3-container w3-center">
<h1>Server list</h1>

<?php echo $data["serverlist-html"] ?>
</section>

<script>
function ror_toggle_mobile_nav() {
var x = document.getElementById("ror_mobile_nav");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
</script>
}
</script>
</body>
</html>
Loading