<?php
require __DIR__.'/s3helper.php';
$cfg=s3_load_config();
$prefix = s3_norm_prefix($_GET['prefix'] ?? '');
$max = (int)($_GET['max'] ?? 1000);
$r = s3_list($cfg, $cfg['bucket'], $prefix, '/', $max);
$folders=[]; $files=[]; $err='';
if($r['ok']){
$xml=@simplexml_load_string($r['body']);
if($xml){
foreach($xml->CommonPrefixes as $p){ $folders[] = (string)$p->Prefix; }
foreach($xml->Contents as $c){
$key=(string)$c->Key;
if ($key === $prefix) continue; // bỏ object “thư mục” rỗng chính nó
$files[]=[
'Key'=>$key,
'Size'=>(int)$c->Size,
'Time'=>date('Y-m-d H:i:s', strtotime((string)$c->LastModified)),
];
}
} else $err='Không đọc được XML trả về.';
}else $err="HTTP {$r['http_code']} — ".strip_tags($r['body']);
function parent_prefix(string $p): string {
if($p==='') return '';
$t=rtrim($p,'/'); $pos=strrpos($t,'/');
return $pos===false? '' : substr($t,0,$pos+1);
}
$up = parent_prefix($prefix);
?>
<!doctype html><html><head><meta charset="utf-8"><title>View Bucket</title>
<style>
body{font-family:Inter,Arial;background:#f9fafb;margin:0;padding:20px}
.panel{max-width:1000px;margin:auto;background:#fff;padding:20px;border-radius:12px;box-shadow:0 2px 10px rgba(0,0,0,.08)}
h2{margin:0 0 6px}
a.btn{display:inline-block;background:#2563eb;color:#fff;padding:6px 10px;border-radius:6px;text-decoration:none}
a.btn:hover{background:#1e40af}
.list{margin:10px 0} .item{padding:8px 10px;border:1px solid #e5e7eb;border-radius:8px;margin:6px 0;background:#fafafa;display:flex;justify-content:space-between;align-items:center}
.meta{color:#6b7280;font-size:13px} .err{padding:10px;border-radius:8px;background:#fee2e2;color:#991b1b}
</style>
</head><body>
<div class="panel">
<h2>📂 Duyệt bucket: <?=htmlspecialchars($cfg['bucket'])?></h2>
<p class="meta">Prefix hiện tại: <code><?=htmlspecialchars($prefix?:'(root)')?></code></p>
<p>
<a class="btn" href="view.php?prefix=">🏠 Root</a>
<?php if($prefix!==''): ?>
<a class="btn" href="view.php?prefix=<?=urlencode($up)?>">⬆️ Lùi lên</a>
<?php endif; ?>
<a class="btn" href="index.php">Trang chính</a>
<a class="btn" href="view_source.php?file=view.php" style="background:#6b7280">Source</a>
</p>
<?php if($err): ?><div class="err"><?=htmlspecialchars($err)?></div><?php endif; ?>
<h3>📁 Thư mục</h3>
<div class="list">
<?php if(!$folders): ?><div class="meta">(Không có thư mục)</div><?php endif; ?>
<?php foreach($folders as $fo): ?>
<div class="item">
<div>📁 <a href="view.php?prefix=<?=urlencode($fo)?>"><?=htmlspecialchars(basename(rtrim($fo,'/')))?></a>
<span class="meta"> (<?=htmlspecialchars($fo)?>)</span>
</div>
<form method="post" action="delete.php" onsubmit="return confirm('Xoá cả thư mục này?');">
<input type="hidden" name="target" value="<?=htmlspecialchars($fo)?>">
<input type="hidden" name="is_folder" value="1">
<button class="btn" style="background:#b91c1c">Xoá thư mục</button>
</form>
</div>
<?php endforeach; ?>
</div>
<h3>🗃️ File</h3>
<div class="list">
<?php if(!$files): ?><div class="meta">(Không có file)</div><?php endif; ?>
<?php foreach($files as $f): ?>
<div class="item">
<div>
📄 <strong><?=htmlspecialchars(basename($f['Key']))?></strong>
<div class="meta"><?=number_format($f['Size']/1024,2)?> KB · <?=$f['Time']?></div>
</div>
<div>
<a class="btn" href="get.php?key=<?=urlencode($f['Key'])?>" target="_blank">Tải</a>
<a class="btn" href="head.php?key=<?=urlencode($f['Key'])?>" target="_blank" style="background:#0ea5a4">Info</a>
<form method="post" action="delete.php" style="display:inline" onsubmit="return confirm('Xoá file này?');">
<input type="hidden" name="target" value="<?=htmlspecialchars($f['Key'])?>">
<button class="btn" style="background:#b91c1c">Xoá</button>
</form>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</body></html>