<?php
$base_dir = __DIR__;

$current = isset($_GET['path']) ? $_GET['path'] : '';
$current_path = realpath($base_dir . '/' . $current);

if(strpos($current_path,$base_dir)!==0){
$current_path=$base_dir;
}

$files=scandir($current_path);

function formatSize($bytes){
$units=['B','KB','MB','GB','TB'];
for($i=0;$bytes>=1024;$i++){
$bytes/=1024;
}
return round($bytes,2).' '.$units[$i];
}
?>

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>File List</title>

<style>

body{
font-family:tahoma;
background:#0f172a;
color:#fff;
margin:0;
padding:15px;
}

.container{
max-width:1100px;
margin:auto;
}

h1{
text-align:center;
margin-bottom:25px;
font-size:22px;
}

.path{
margin-bottom:15px;
color:#94a3b8;
word-break:break-all;
}

table{
width:100%;
border-collapse:collapse;
background:#1e293b;
border-radius:10px;
overflow:hidden;
}

th,td{
padding:12px;
text-align:right;
}

th{
background:#334155;
}

tr{
border-bottom:1px solid #475569;
}

tr:hover{
background:#334155;
}

.btn{
padding:7px 12px;
border:none;
border-radius:6px;
cursor:pointer;
font-size:13px;
margin-left:5px;
}

.download{
background:#22c55e;
color:white;
}

.copy{
background:#3b82f6;
color:white;
}

.folder{
color:#38bdf8;
font-weight:bold;
text-decoration:none;
}

.file{
color:#e2e8f0;
}

/* &#1605;&#1608;&#1576;&#1575;&#1740;&#1604; */

@media(max-width:700px){

table,thead,tbody,tr,td{
display:block;
width:100%;
}

thead{
display:none;
}

tr{
background:#1e293b;
border-radius:10px;
margin-bottom:12px;
padding:10px;
}

td{
border:none;
padding:6px 0;
}

.actions{
margin-top:6px;
}

.btn{
display:inline-block;
margin-top:6px;
}

}

</style>
</head>

<body>

<div class="container">

<h1>&#128193; &#1604;&#1740;&#1587;&#1578; &#1601;&#1575;&#1740;&#1604;&#8204;&#1607;&#1575;</h1>

<div class="path">
&#1605;&#1587;&#1740;&#1585;: /<?php echo htmlspecialchars($current); ?>
</div>

<table>

<thead>
<tr>
<th>&#1606;&#1575;&#1605;</th>
<th>&#1581;&#1580;&#1605;</th>
<th>&#1593;&#1605;&#1604;&#1740;&#1575;&#1578;</th>
</tr>
</thead>

<tbody>

<?php

foreach($files as $file){

if($file=='.'||$file=='..') continue;

$full=$current_path.'/'.$file;
$relative=trim($current.'/'.$file,'/');

echo "<tr>";

if(is_dir($full)){

echo "<td><a class='folder' href='?path=$relative'>&#128193; $file</a></td>";
echo "<td>-</td>";
echo "<td></td>";

}else{

$size=formatSize(filesize($full));
$link="/$relative";

echo "<td class='file'>&#128196; $file</td>";
echo "<td>$size</td>";

echo "<td class='actions'>
<a class='btn download' href='$link' download>&#1583;&#1575;&#1606;&#1604;&#1608;&#1583;</a>
<button class='btn copy' onclick=\"copyLink('$link')\">&#1705;&#1662;&#1740; &#1604;&#1740;&#1606;&#1705;</button>
</td>";

}

echo "</tr>";

}

?>

</tbody>
</table>

</div>

<script>

function copyLink(link){
let full=window.location.origin+link;
navigator.clipboard.writeText(full);
alert("&#9989; &#1604;&#1740;&#1606;&#1705; &#1705;&#1662;&#1740; &#1588;&#1583;");
}

</script>

</body>
</html>
