function minecraftCommand($service_id, $command) { if (strlen($command > 1000)) { return "Error: the entered command is too long!"; } //get the configuration $configuration = minecraftGetConfiguration($service_id, false); if (isset($configuration['rcon.password']) && isset($configuration['rcon.port'])) { $hostname = "localhost"; if (!empty($configuration['server-ip'])) { $hostname = $configuration['server-ip']; } require_once includePath() . "/rcon.php"; try { $rcon = new RCon($hostname, $configuration['rcon.port'], $configuration['rcon.password']); } catch (Exception $e) { return "Error: {$e}"; } if ($rcon->Auth()) { //allow execution of multiple commands if (is_array($command)) { foreach ($command as $str) { $rcon->rconCommand($command); } } else { $rcon->rconCommand($command); } return true; } else { return "Error: failed to connect and authenticate with Minecraft server (is it online?)."; } } else { return "Error: could not find rcon port and password to use."; } }
<?php include_once "../includes/config.php"; if (!isset($_SESSION['server'])) { echo '({"command": "", "responseSuccess": "failure", "responseText": "Connection with server lost. Please log in again"})'; exit; } //concat full command $command = ""; if (!empty($_POST['prefix']) && strlen(trim($_POST['prefix']))) { $command .= trim(addslashes($_POST['prefix'])) . " "; } //check for content, trim and crap-strip fields if (!empty($_POST['command']) && strlen(trim($_POST['command']))) { $command .= trim(addslashes($_POST['command'])); } else { echo '({"command": "", "responseSuccess": "failure", "responseText": "Invalid command"})'; } $rcon = new RCon($_SESSION['server'], $_SESSION['port'], $_SESSION['password']); $success = $rcon->authenticate(); echo '({"command": ' . json_encode($command) . ', "responseSuccess": "","responseText": ' . json_encode($rcon->rconCommand($command)) . '})';