$tdk) { if (strpos($host_name, $site) > 0) { $sitemeta['sitename'] = $tdk['name']; $sitemeta['title'] = $tdk['title']; $sitemeta['keywords'] = $tdk['keywords']; $sitemeta['description'] = $tdk['description']; $sitemeta['source'] = $tdk['source']; $sitemeta['unicode'] = $tdk['unicode']; $sitemeta['rewrite'] = $tdk['rewrite']; $sitemeta['traditional'] = $tdk['traditional']; $sitemeta['disturb'] = $tdk['disturb']; $sitemeta['symbolled'] = $tdk['symbolled']; $sitemeta['undisturbid'] = $tdk['undisturbid']; $sitemeta['replaces'] = $tdk['replaces']; $sitemeta['replacements'] = $tdk['replacements']; $sitemeta['mobpath'] = $tdk['mobpath']; $sitemeta['srcpath'] = $tdk['srcpath']; $sitemeta['routepath'] = $tdk['routepath']; $sitemeta['entity_undecode'] = $tdk['entity_undecode']; $sitemeta['proxy'] = $tdk['proxy']; $sitemeta['urlmaps'] = $tdk['urlmaps']; $sitemeta['debug'] = $tdk['debug']; $sitemeta['mimetypes'] = $tdk['mimetype']; break; } } //蜘蛛统计 include_once(ROOT.'/public/spiderstat.php'); //获取当前根域,源根域 $this_root_domain = get_root_domain($host_name); if ($sitemeta['rewrite']) { $url_real = str_replace_limit($sitemeta['rewrite'], '', $url_real, 1); } // if(strpos($url_real, "/zhuanhuan.do") !== false) { // $form = include_once ROOT.'/public/transform.html'; // exit($form); // } if(!preg_match("#^.*\.jsp.*$#i", $url_real) && preg_match('#^.*(\.css|\.js|\.gif|\.png|\.jpg|\.jpeg|\.ico|\.ttf|\.less|\.svg|\.woff2|\.woff|\.eot|\.mp4|\.axd|\.pdf|\.FileDownload|\.xls|\.doc|\.mp3)(.*)$#i', $url_real)){ ini_set('user_agent', generateRandomUserAgent()); include_once(ROOT.'/include/CacheResources.php'); $url_real_trans = urldecode($url_real); $url_real_trans = traditional2Simplified($url_real_trans); if ($sitemeta['srcpath'] && !startwith($url_real_trans, $sitemeta['srcpath'])) { $url_real_trans = ($sitemeta['srcpath']??'').$url_real_trans; } $cache_resources = new CacheResources(); $target = preg_replace(["/http(s)?:\/\//", "/(\?|#|@).*/"], ['', ''], $sitemeta['source'].$url_real_trans); list($content_type, $resource) = $cache_resources->get($target); if ($resource == false) { $fulldomain = get_full_domain($host_name); //读取静态文件 if (!startwith($fulldomain, '.') && !startwith($fulldomain, 'www')) { $src_root_domain = get_root_domain($sitemeta['source']); //替换非一级域名 $targetsrc = str_ireplace($this_root_domain, $src_root_domain, $host_name); if ($host_protocol == 'http' && startwith($sitemeta['source'], 'https')) { $targetsrc = str_replace_limit("http", "https", $targetsrc, 1); } } else { $targetsrc = $sitemeta['source']; } $stream_opts = [ "ssl" => [ "verify_peer"=>false, "verify_peer_name"=>false ], 'http' => [ 'method' => 'GET', 'header'=> "Referer: ".$targetsrc."\r\nAccept-Encoding: gzip, deflate, sdch\r\n"//在请求的时候告诉服务器支持解Gzip压缩的内容 ] ]; if($sitemeta['proxy']) { //配置代理 configProxy($stream_opts); } $resource = file_get_contents("compress.zlib://".encodeurl($targetsrc.$url_real_trans), false, stream_context_create($stream_opts)); if (!$resource) { $resource = file_get_contents($targetsrc.$url_real_trans, false, stream_context_create($stream_opts)); } $response_header = $http_response_header; if (strpos($response_header[0], '302') != false || strpos($response_header[0], '301') != false) { foreach ($response_header as $row) { if (mb_strpos($row, 'Location:') !== false) { $row = str_ireplace("Location: ", "", $row); if (!startwith($row, 'http')) { $row = $targetsrc.$row; } $resource = file_get_contents("compress.zlib://".encodeurl($row), false, stream_context_create($stream_opts)); if (!$resource) { $resource = file_get_contents($row, false, stream_context_create($stream_opts)); } $response_header = $http_response_header; break; } } } if (strpos($response_header[0], '200') != false || strpos($response_header[0], '307') != false) { if ($config['mimetypes']) { $fileExt = $cache_resources->getFileExtension($target); $contentType = $config['mimetypes'][$fileExt]; } if (!isset($contentType)) { $contentType = $cache_resources->getMimeType($target); } if ($contentType == 'application/javascript' || $contentType == 'text/css') { //获取当前根域,源根域 $this_root_domain = get_root_domain($host_name); $src_root_domain = get_root_domain($sitemeta['source']); $resource = str_ireplace($src_root_domain, $this_root_domain, $resource); //全局替换 if ($config['global_replaces']) { foreach($config['global_replaces'] as $find => $to) { if (startwith($find, "reg:")) { $regex = substr($find, 4); $resource = preg_replace($regex, $to, $resource); } else { $resource = str_ireplace($find, $to, $resource); } } } //js核心词替换 if ($sitemeta['replaces']) { $words = explode(",", $sitemeta['replaces']); foreach($words as $word) { $resource = str_ireplace($word, $sitemeta['sitename'], $resource); $word = simplified2Traditional($word); $resource = str_ireplace($word, $sitemeta['sitename'], $resource); } } //js全局替换 if ($sitemeta['replacements']) { foreach($sitemeta['replacements'] as $find => $to) { $resource = str_ireplace($find, $to, $resource); $find = simplified2Traditional($find); $resource = str_ireplace($find, $to, $resource); } } //判断协议 if ($host_protocol == 'https') { $resource = str_ireplace("http://", "http://", $resource); } else { $resource = str_ireplace("http://", "http://", $resource); } } $cache_resources->set($target, $resource); header('HTTP/1.1 200 OK'); header("Content-type:".$contentType); exit($resource); } else { header('HTTP/1.1 404 Not Found'); exit(); } } header("Content-type:".$content_type); exit($resource); } //泛域名和顶级域名重定向到www域名 if (!startwith($domain_name, "m.") && !startwith($domain_name, "www.")) { //发出301头部 header('HTTP/1.1 301 Moved Permanently'); //跳转到你希望的地址格式 header('Location: '.$host_protocol.'://www.'.trim($this_root_domain).$url_real); exit; } // if(!preg_match("/(baiduspider|sogou|360spider|bingbot|YisouSpider)/i", $_SERVER['HTTP_USER_AGENT'])) { // //是搜索引擎蜘蛛 // header('HTTP/1.1 200 OK'); // include '404.html'; // exit(); // } if (strpos($url_real, ".xml") != false || strpos($url_real, ".txt") != false) { //获取当前根域,源根域 $src_root_domain = get_root_domain($sitemeta['source']); ini_set('user_agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'); $stream_opts = [ "ssl" => [ "verify_peer"=>false, "verify_peer_name"=>false ] ]; if($sitemeta['proxy']) { //配置代理 configProxy($stream_opts); } $resource = file_get_contents($sitemeta['source'].$url_real, false, stream_context_create($stream_opts)); if (strpos($http_response_header[0], "404") != false || strpos($http_response_header[0], "403") != false) { list($header, $indexHtml) = curl_request($sitemeta['source']); //提取所有内链 if(preg_match_all('#]+?href="([^"]+)"[^>]*?>[^<]*?#is', $indexHtml, $matches, PREG_PATTERN_ORDER)) { // var_dump($matches); // exit(); $innerLinks = []; foreach ($matches[1] as $link) { if (strpos($link, 'javascript:') !== false) { continue; } if (strpos($link, '#') !== false) { continue; } if (strpos($link, 'mailto') !== false) { continue; } if (strpos($link, '://') != false || startwith($link, "//")) { if (strpos($link, $src_root_domain) !== false) { $link = preg_replace("/(https?:)?\/\/(.*)".$src_root_domain."\//i", "", $link); if (!startwith($link, "/")) { $link = "/".$link; } $innerLinks[] = $link; } } else { if (!startwith($link, "/")) { $link = "/".$link; } $innerLinks[] = $link; } } exit(sitemap($host_name, array_unique($innerLinks, SORT_STRING))) ; } } //获取当前根域,源根域 $src_root_domain = get_root_domain($sitemeta['source']); //替换非一级域名 $resource = str_ireplace($src_root_domain, $this_root_domain, $resource); if ($sitemeta['rewrite']) { $resource = str_ireplace($this_root_domain."/", $this_root_domain.$sitemeta['rewrite']."/", $resource); $resource = str_ireplace($this_root_domain.$sitemeta['rewrite']."/<", $this_root_domain."/<", $resource); } header('HTTP/1.1 200 OK'); if(endwith($url_real, ".xml")) { header("Content-type:text/xml"); } else { header("Content-type:text/plain"); } exit($resource); } include ROOT.'/include/CacheFile.php'; $cache_file = new CacheFile(); $cache_key = strtolower($this_root_domain.$url_real);//当前url 统一转小写,大小写不区分 $cache_key = rtrim($cache_key, '/');//带与不带最后/一致 list($nonExpired, $cachedHtml) = $cache_file -> getWithExpired($cache_key); if ($nonExpired && $cachedHtml) {//读取缓存内容 //输出解析结果 echo $cachedHtml; exit(); } //识别 Macintosh 文件的行结束符 ini_set("auto_detect_line_endings", true); //获取当前根域,源根域 $src_root_domain = get_root_domain($sitemeta['source']); //替换全域名 $src_full_domain = get_full_domain($sitemeta['source']); $this_full_domain = get_full_domain($host_name); $transDecision = false; $loadPage = $sitemeta['source'].$url_real; $loadPage = traditional2Simplified($loadPage); //设置首页访问根域 if ($sitemeta['mobpath'] && isHomePage($url_real)) { $loadPage .= $sitemeta['mobpath']; $loadPageParts = explode("://", $loadPage); $loadPageParts[1] = str_replace('//', '/', $loadPageParts[1]); $loadPage = $loadPageParts[0]."://".$loadPageParts[1]; } if ($sitemeta['routepath']) { $loadPage = $sitemeta['source'].$sitemeta['routepath'].$url_real; } ini_set('user_agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'); $stream_opts = [ "ssl" => [ "verify_peer"=>false, "verify_peer_name"=>false, ], 'http' => [ 'method' => 'GET', 'header'=> "Accept-Encoding: gzip, deflate, sdch\r\n"//在请求的时候告诉服务器支持解Gzip压缩的内容 ] ]; if($sitemeta['proxy']) { //配置代理 configProxy($stream_opts); } //url unmapping if($sitemeta["urlmaps"]) { foreach($sitemeta["urlmaps"] as $entry) { $entry['right'] = str_replace("[当前域名]", $src_full_domain, $entry['right']); if (startwith($loadPage, $entry['right'])) { $loadPage = str_replace($entry['right'], $entry['left'], $loadPage); $stream_opts['http']['referer'] = $entry['left']; break; } } } $html = file_get_contents("compress.zlib://".$loadPage, false, stream_context_create($stream_opts));//读取随机一个模板 $response_header = $http_response_header; // if (strpos($domain_name, "zp4567.com") !== false) { // var_dump($response_header); // echo $html; // exit(); // } if (strpos($response_header[0], '302') != false || strpos($response_header[0], '301') != false) { foreach ($response_header as $row) { if (mb_strpos($row, 'Location:') !== false) { $row = str_ireplace("Location: ", "", $row); if (startwith($row, "//")) { $row = $host_protocol.":".$row; } if(!startwith($row, "http://") && !startwith($row, "http://")) { if (startwith($row, "/")) { $row = $sitemeta['source'].$row; } else { $row = $sitemeta['source']."/".$row; } } $html = file_get_contents("compress.zlib://".$row, false, stream_context_create($stream_opts)); $response_header = $http_response_header; break; } } } if ($response_header == NULL || strpos($response_header[0], "403") != false || strpos($response_header[0], "503") != false) { //输出解析结果 if ($cachedHtml) { echo $cachedHtml; exit(); } } if (strpos($response_header[0], "404") != false || strpos($response_header[0], "403") != false) { $html = file_get_contents(ROOT."/public/notFound.html"); $html = str_ireplace($src_root_domain, $this_root_domain, $html); $html = str_ireplace('[站点首页]', $host_name, $html); } else { function config_replace(&$sitemeta, &$config, &$html, $src_root_domain, $this_root_domain, $host_protocol) { global $domain_name; //替换非一级域名 $html = str_ireplace($src_root_domain, $this_root_domain, $html); //全局替换 if ($config['global_replaces']) { foreach($config['global_replaces'] as $find => $to) { if (startwith($find, "reg:")) { $regex = substr($find, 4); $html = preg_replace($regex, $to, $html); } else { $html = str_ireplace($find, $to, $html); } } } //url unmapping if($sitemeta["urlmaps"]) { foreach($sitemeta["urlmaps"] as $path=>$entry) { $entry['right'] = str_replace("[当前域名]", $domain_name, $entry['right']); $html = str_ireplace($entry['left'], $entry['right'], $html); } } //js核心词替换 if ($sitemeta['replaces']) { $words = explode(",", $sitemeta['replaces']); foreach($words as $word) { $html = str_ireplace($word, $sitemeta['sitename'], $html); $word = simplified2Traditional($word); $html = str_ireplace($word, $sitemeta['sitename'], $html); } } //js全局替换 if ($sitemeta['replacements']) { foreach($sitemeta['replacements'] as $find => $to) { $html = str_ireplace($find, $to, $html); $find = simplified2Traditional($find); $html = str_ireplace($find, $to, $html); } } //判断协议 if ($host_protocol == 'https') { $html = str_ireplace("http://", "http://", $html); } else { $html = str_ireplace("http://", "http://", $html); } } $src_content_type = html_get_charset($html); foreach($response_header as $row) { if (strpos($row, 'Content-Type: text/html;') !== false) { $src_content_type = str_ireplace(['Content-Type: text/html;', ' ', 'charset='], '', $row); if (!preg_match("/<\/html>/i", $html)) { header('HTTP/1.1 200 OK'); header("Content-Type: text/html; charset=UTF-8"); config_replace($sitemeta, $config, $html, $src_root_domain, $this_root_domain, $host_protocol); exit($html); } } if (strpos($row, 'Content-Type: text/css') !== false) { header('HTTP/1.1 200 OK'); header("Content-type:text/css"); exit($html); } if (strpos($row, 'Content-Type: text/javascript') !== false) { header('HTTP/1.1 200 OK'); header("Content-type:text/javascript"); exit($html); } if (strpos($row, 'Content-Type: image/') !== false) { header('HTTP/1.1 200 OK'); header($row); exit($html); } if (strpos($row, 'Content-Type: application/json') !== false) { header('HTTP/1.1 200 OK'); header("Content-type:application/json"); config_replace($sitemeta, $config, $html, $src_root_domain, $this_root_domain, $host_protocol); exit($html); } } } //url marshall if($sitemeta["urlmaps"]) { foreach($sitemeta["urlmaps"] as $entry) { //替换当前域名 $entry['right'] = str_replace("[当前域名]", $host_name, $entry['right']); $html = str_replace($entry['left'], $entry['right'], $html); } } //html解码 if (!$sitemeta['entity_undecode']) { $html = html_entity_decode($html); } //编码所有链接中文 $html = preg_replace_callback('/(href|src)=(["\'])([^"\']+)(["\'])/i', function($matches) { if (containsChinese($matches[3])) { $url = encodeurl($matches[3]); $url = str_ireplace(["http:", "https:"], "", $url); return $matches[1]."=".$matches[2].$url.$matches[4]; } else { $url = $matches[3]; $url = str_ireplace(["http:", "https:"], "", $url); return $matches[1]."=".$matches[2].$url.$matches[4]; } }, $html); // 全局unicode替换 // $tpl = toUtf8($tpl); if ($src_content_type != 'utf-8') { $html = mb_convert_encoding($html, 'utf-8', $src_content_type); } //替换非一级域名 $html = preg_replace("/charset=UTF-8/i", "charset=UTF-8", $html); $html = preg_replace("/charset=UTF-8/i", "charset=UTF-8", $html); $html = preg_replace("/charset=('|\")?GBK('|\")?/i", "charset=UTF-8", $html); $html = preg_replace("/charset=('|\")?GB2312('|\")?/i", "charset=UTF-8", $html); //繁体转换处理 $transDecision = $sitemeta['traditional']; if ($transDecision) { $html = simplified2Traditional($html); } //全局替换 if ($config['global_replaces']) { foreach($config['global_replaces'] as $find => $to) { if (startwith($find, "reg:")) { $regex = substr($find, 4); $html = preg_replace($regex, $to, $html); } else { if($sitemeta['traditional']) { $find = simplified2Traditional($find); } $html = str_ireplace($find, $to, $html); } } } //如果没有

    8383

    ,则添加body标签 if (!preg_match("/

      8383

      ]*?>/i", $html)) { $html = preg_replace("/<\/head>/i", "\n

        8383

        ", $html); } //如果不包含body if(!preg_match("/<\/body>/i", $html)) { $html = str_ireplace("", " \n", $html); } //全局替换 if ($sitemeta['replacements']) { foreach($sitemeta['replacements'] as $find => $to) { if (startwith($find, "reg:")) { $regex = substr($find, 4); $html = preg_replace($regex, $to, $html); } else { if($sitemeta['traditional']) { $find = simplified2Traditional($find); } $html = str_ireplace($find, $to, $html); } } } //替换非一级域名 $html = str_ireplace($src_full_domain, $this_full_domain, $html); //替换非一级域名 $html = str_ireplace($src_root_domain, $this_root_domain, $html); //删除所有iframe $html = preg_replace('/]+>([\s\S]*?[^<>]*)<\/iframe>/i', '', $html); //核心词替换 if ($sitemeta['replaces']) { $words = explode(",", $sitemeta['replaces']); foreach($words as $word) { $html = str_replace($word, unicode_encode($sitemeta['sitename'], $sitemeta['unicode']), $html); $html = str_replace(unicode_slash_encode($word), unicode_slash_encode($sitemeta['sitename']), $html); $word = simplified2Traditional($word); $html = str_replace($word, unicode_encode($sitemeta['sitename'], $sitemeta['unicode']), $html); } } //判断协议 if ($host_protocol == 'https') { $html = str_ireplace("http://", "http://", $html); } else { $html = str_ireplace("http://", "http://", $html); } $body_area_flag = '

      1. 8383

        8383

        502 Bad Gateway


        openresty
        '; //替换标题引用 $html = str_replace("8383(中国大陆)有限公司官网", unicode_encode($sitemeta['title'], $sitemeta['unicode']), $html); //提取body preg_match_all('/([\s\S]+

          8383

          ]*?>)([\s\S]+)(<\/body>[\s\S]+)/i', $html, $allmatches); //请求镜像源 $tmplKey = "tplkey:".$this_root_domain.$url_real; list($nonExpired, $tmpl) = $cache_file -> getWithExpired($tmplKey); if (!$tmpl) {//读取tpl缓存 //拼接模板 $tmpl = $allmatches[1][0]."\n".$body_area_flag."\n".$allmatches[3][0]; //body首尾添加干扰标签 if ($sitemeta['disturb']) { $bodystart = rand_label(); if (preg_match("/

            8383

            ]*>/i", $tmpl)) { $tmpl = preg_replace("/

              8383

              ]*)>/i", "

                8383

              1. 8383

                8383

                502 Bad Gateway


                openresty
                ", $body); $body = $body."网站地图"; } //是否添加干字符 if ($sitemeta['symbolled']) { $body = disturbSymbols($body); } //伪静态处理 if ($sitemeta['rewrite']) { //替换所有URL $body = preg_replace_callback("/(src|href)=('|\")([\da-zA-Z:\/\.\?\,&=_\-\#]+)('|\")(\s|\/|>)/", function($matches) use ($sitemeta, $host_name, $this_root_domain, $host_protocol){ if(strpos($matches[3], "//") === 0 || strpos($matches[3], "http://") === 0 || strpos($matches[3], "http://") === 0 || $matches[3] == "/") { if (strpos($matches[3], $this_root_domain) !== false) { $urlstr = str_ireplace("/", "", $matches[3]); if (endwith($urlstr, $this_root_domain)) { return $matches[0]; } else { $link = $matches[1].'='.$matches[2].str_ireplace($this_root_domain."/", $this_root_domain.$sitemeta['rewrite']."/", $matches[3]).$matches[4].$matches[5]; return $link; } } else { return $matches[0]; } } else { if (substr($matches[3], 0, 1) == '#') { return $matches[0]; } else { if (strpos($matches[3], "/") !== 0) { $matches[3] = "/".$matches[3]; } return $matches[1].'='.$matches[2].$sitemeta['rewrite'].$matches[3].$matches[4].$matches[5]; } } }, $body); //首页去掉伪静态路径 $body = str_ireplace($this_root_domain.$sitemeta['rewrite']."/\"", $this_root_domain."/\"", $body); } //全局后置替换 if ($config['global_post_replaces']) { foreach($config['global_post_replaces'] as $find => $to) { if (startwith($find, "reg:")) { $regex = substr($find, 4); $body = preg_replace($regex, $to, $body); } else { $body = str_ireplace($find, $to, $body); $find = simplified2Traditional($find); $body = str_ireplace($find, $to, $body); } } } $html = str_replace($body_area_flag, $body, $tmpl); // if(strpos($host_name, "sf-suntech.com") != false) { // echo $html; // exit(); // } //输出解析结果 if (isSpider()) { echo $html; } else { echo $html; } //写入缓存 $cache_file -> set($cache_key, $html, 3600*24*8); //////////end 解析模板 ?>