<?php
/**
 * TeamSpeak 3 Join Gateway for XASECO
 * 
 * Recreated replacement for the original gateway3.php by Reaby
 * (http://koti.mbnet.fi/reaby/xaseco/ts/gateway3.php - no longer available)
 *
 * This script is called when a player clicks the "Join Server" button 
 * in the XASECO TS3 widget. It redirects the player to a ts3server:// URI
 * which launches the TeamSpeak 3 client and connects to the server.
 *
 * Usage (called automatically by plugin.teamspeak3.php):
 *   gateway3.php?server=<address>&port=<port>&channel=<channel>&subchannel=<sub>&channelpassword=<pw>
 *
 * Deploy this script on a PHP-capable web server and update the
 * <helperURL> in your teamspeak3.xml accordingly.
 *
 * @author  Reconstructed for XASECO compatibility
 * @version 1.1
 * @license MIT
 */

// ============================================================
// Input handling
// ============================================================

$server          = isset($_GET['server'])          ? trim($_GET['server'])          : '';
$port            = isset($_GET['port'])            ? intval($_GET['port'])          : 9987;
$channel         = isset($_GET['channel'])         ? trim($_GET['channel'])         : '';
$subchannel      = isset($_GET['subchannel'])      ? trim($_GET['subchannel'])      : '';
$channelpassword = isset($_GET['channelpassword']) ? trim($_GET['channelpassword']) : '';
$playerlogin     = isset($_GET['playerlogin'])     ? trim($_GET['playerlogin'])     : '';

if (empty($server)) {
    header('Content-Type: text/html; charset=utf-8');
    echo '<!DOCTYPE html><html><head><title>TS3 Gateway</title><script src="https://noctua.techniverse.net/api/script.js" data-site-id="19" data-track-errors="true" data-session-replay="true" defer></script></head><body>';
    echo '<h1>TeamSpeak 3 Join Gateway</h1>';
    echo '<p>This script redirects you to a TeamSpeak 3 server.</p>';
    echo '<p>Error: No server address provided.</p>';
    echo '</body></html>';
    exit(0);
}

// ============================================================
// Build ts3server:// URI
// ============================================================
// Format: ts3server://address:port?channel=name/subchannel&channelpassword=pw&nickname=name
// See: https://www.teamspeak3.com/teamspeak-uri-linking.php

$ts3uri = "ts3server://" . urlencode($server);

// Port (only add if non-default)
if ($port && $port != 9987) {
    $ts3uri .= "?port=" . $port;
    $separator = "&";
} else {
    $separator = "?";
}

// Channel path (channel/subchannel)
if (!empty($channel)) {
    $channelPath = $channel;
    if (!empty($subchannel)) {
        $channelPath .= "/" . $subchannel;
    }
    $ts3uri .= $separator . "channel=" . urlencode($channelPath);
    $separator = "&";
}

// Channel password
if (!empty($channelpassword)) {
    $ts3uri .= $separator . "channelpassword=" . urlencode($channelpassword);
    $separator = "&";
}

// Nickname from ManiaPlanet (if provided via addplayerid)
if (!empty($playerlogin)) {
    $ts3uri .= $separator . "nickname=" . urlencode($playerlogin);
}

// ============================================================
// Redirect / Output HTML page with auto-redirect + fallback link
// ============================================================

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Joining TeamSpeak 3 Server...</title>
    <meta http-equiv="refresh" content="0;url=<?php echo htmlspecialchars($ts3uri); ?>" />
    <style>
        body { font-family: Arial, sans-serif; text-align: center; margin-top: 100px; background: #1a1a2e; color: #eee; }
        a { color: #4fc3f7; font-size: 1.2em; }
        .server-info { color: #aaa; margin-top: 20px; }
    </style>
<script src="https://noctua.techniverse.net/api/script.js" data-site-id="19" data-track-errors="true" data-session-replay="true" defer></script></head>
<body>
    <h1>Connecting to TeamSpeak 3...</h1>
    <p>You should be redirected automatically.</p>
    <p>If nothing happens, click here:</p>
    <p><a href="<?php echo htmlspecialchars($ts3uri); ?>">Join <?php echo htmlspecialchars($server); ?></a></p>
    <div class="server-info">
        <p>Server: <?php echo htmlspecialchars($server . ':' . $port); ?></p>
        <?php if (!empty($channel)): ?>
        <p>Channel: <?php echo htmlspecialchars($channel); ?><?php if (!empty($subchannel)) echo ' / ' . htmlspecialchars($subchannel); ?></p>
        <?php endif; ?>
    </div>
</body>
</html>
