Skip to content

Instantly share code, notes, and snippets.

@Bluscream
Last active June 20, 2023 22:47
Show Gist options
  • Save Bluscream/0ddfa2480cc3683ee7507fbda8102c47 to your computer and use it in GitHub Desktop.
Save Bluscream/0ddfa2480cc3683ee7507fbda8102c47 to your computer and use it in GitHub Desktop.
IW4x Map chooser
<?php // session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define("PHP_OS_FAMILY", "Windows");
$out="";
function logg($msg) {
global $out;
if (is_array($msg)) {
$msg = implode("\n", $msg);
}
$out .= strval($msg) . "<br>\n";
}
function run_cmd($cmd) {
logg("Running command: $cmd");
$output=null;
$retval=null;
shell_exec($cmd); // , $output, $retval);
logg("Returned with status $retval and output:");
logg($output);
}
function getClipboard() {
if(PHP_OS_FAMILY==="Windows"){
// works on windows 7 + (PowerShell v2 + )
// TODO: is it -1 or -2 bytes? i think it was -2 on win7 and -1 on win10?
return substr(shell_exec('powershell -sta "add-type -as System.Windows.Forms; [windows.forms.clipboard]::GetText()"'),0,-1);
}elseif(PHP_OS_FAMILY==="Linux"){
// untested! but should work on X.org-based linux GUI's
return substr(shell_exec('xclip -out -selection primary'),0,-1);
}elseif(PHP_OS_FAMILY==="Darwin"){
// untested!
return substr(shell_exec('pbpaste'),0,-1);
}else{
throw new \Exception("running on unsupported OS: ".PHP_OS_FAMILY." - only Windows, Linux, and MacOS supported.");
}
}
function setClipboard($new) {
if(PHP_OS_FAMILY==="Windows"){
// works on windows 7 +
$clip=popen("clip","wb");
}elseif(PHP_OS_FAMILY==="Linux"){
// tested, works on ArchLinux
$clip=popen('xclip -selection clipboard','wb');
}elseif(PHP_OS_FAMILY==="Darwin"){
// untested!
$clip=popen('pbcopy','wb');
}else{
throw new \Exception("running on unsupported OS: ".PHP_OS_FAMILY." - only Windows, Linux, and MacOS supported.");
}
$written=fwrite($clip,$new);
return (pclose($clip)===0 && strlen($new)===$written);
}
$buttons = [];
function make_buttons() {
require('maps.inc.php');
global $buttons;
foreach ($maps as $game => $_maps) {
foreach ($_maps as $name => $map) {
$mapname = $map['zone_name'];
// $cmd = '"S:\\Call of Duty\\CoD 6 (Server)\\iw4x.exe" -dedicated -enable_rcon -enable_b3 -secure_b3 -scriptablehttp +set sv_lanonly 0 +map "'.$mapname.'"';
$cmd = "map $mapname";
// Copy to clipboard
// $cmd = "echo " . escapeshellarg($cmd) . " | clip";
$label = $map['localised_name'] . " (" . $map['zone_name'] . ")";
$img = "img/$game/" . $map['preview_name'] . ".png";
if (!file_exists($img)) {
$img = "https://image.gametracker.com/images/maps/160x120/cod4/$mapname.jpg";
}
$buttons[] = [
'image' => $img,
'label' => $label,
'copy' => "map $mapname",
// 'cmd' => $cmd,
'code' => setClipboard($cmd),
];
}
}
}
make_buttons();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['map'])) {
$map = $_POST['map'];
if (array_key_exists($map, $buttons)) {
if (array_key_exists('cmd', $buttons[$map])) {
run_cmd($buttons[$map]['cmd']);
}
if (array_key_exists('code', $buttons[$map])) {
$code = $buttons[$map]['code'];
logg("Executing code: $code");
eval($code); // Avoid using eval() if possible
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<script type="text/javascript">
function copyToClipboard(e) {
var tempItem = document.createElement('input');
tempItem.setAttribute('type','text');
tempItem.setAttribute('display','none');
let content = e;
if (e instanceof HTMLElement) {
content = e.innerHTML;
}
tempItem.setAttribute('value',content);
document.body.appendChild(tempItem);
tempItem.select();
document.execCommand('Copy');
tempItem.parentElement.removeChild(tempItem);
}
</script>
</head>
<body>
<!-- Your content goes here -->
<?php /*<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">*/ ?>
<div class="container">
<div class="row">
<div class="row my-3">
<?php foreach ($buttons as $index => $button) { ?>
<div class="col-md-4">
<button type="submit" class="btn btn-lg" name="map" onclick='copyToClipboard("<?php echo $button["copy"]; ?>")' value="<?php echo $index; ?>">
<img width="400px" src="<?php echo $button['image']; /* alt="<?php echo $button['label']; ?>" */ ?>">
<?php echo $button['label']; ?>
</button>
</div>
<?php } ?>
</div>
</div>
</div>
<?php /*</form>*/ ?>
<!-- Bootstrap JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
<?php
echo $out;
?>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment