<?php
// [安全加固] 已移除原项目的 eval 后门：
//   if(!function_exists('get_php_code')) { function get_php_code(){} }
//   eval("@banquansuoyou;get_php_code();");
header("HTTP/1.1 200 OK");
error_reporting(0);
define( "DIR", dirname(__FILE__) );

function randKey($len) 
{
	$chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" );
	$charsLen = count($chars) - 1;
	shuffle($chars);
	$str = "";
	for ($i=0; $i<$len; $i++) 
	{
		$str .= $chars[mt_rand(0, $charsLen)];
	}
	return $str;
}

function rdomain( $d ) 
{
	$provace = array( );
	return str_replace( "*", dechex( date( "s" ).mt_rand( 1111, 9999 ) ), $d );
}

function rarray_rand( $arr ) 
{
	if(empty($arr)) return 0;
	return mt_rand( 0, count( $arr ) - 1 );
}

function varray_rand( $arr ) 
{
	if(empty($arr)) return '';
	return $arr[rarray_rand($arr)];
}

function get_folder_files($folder) 
{
	$arr_file = array();
	if(!is_dir($folder)) return $arr_file;
	$fp = opendir($folder);
	while(false != $file = readdir($fp) ) 
	{
		if($file!='.' &&$file!='..') 
		{
			$arr_file[]=$file;
		}
	}
	closedir($fp);
	return $arr_file;
}

$timeout = 1;
require DIR . '/config.php';

$template1_list = get_folder_files(DIR . '/templates/map/');
if(empty($template1_list)) exit('无地图模板');

// [差异化] 全部改成按域名哈希固定挑选，同一域名 sitemap 骨架/语料稳定
$kw_file = DIR."/keywords/".hpick($keyword_list, 'kw');
$keywords = file_exists($kw_file) ? file($kw_file) : [];

$ct_file = DIR."/content/".hpick($wenku_list, 'ct');
$content = file_exists($ct_file) ? file($ct_file) : [];

$ct2_file = DIR."/system/content2/".hpick($wenku_list2, 'ct2');
$content2 = file_exists($ct2_file) ? file($ct2_file) : [];

$dm_file = DIR."/system/domains/".hpick($domain_list, 'dm');
$domains = file_exists($dm_file) ? file($dm_file) : [];

$sp_file = DIR."/system/spider/".hpick($spider_link, 'sp');
$spider = file_exists($sp_file) ? file($sp_file) : [];

$tpl_file = DIR."/templates/map/".hpick($template1_list, 'tpl_map');
$moban = file_exists($tpl_file) ? file_get_contents($tpl_file) : '';


$dk_file = DIR."/system/duankou/duankous.txt";
$duankous = file_exists($dk_file) ? file($dk_file) : [];

$sp_file = DIR."/video/shipin.txt";
$shipins = file_exists($sp_file) ? file($sp_file) : [];

// 视频
$shipin = substr_count($moban, '<站群_随机视频>');
for ($sp=0; $sp<$shipin; $sp++) 
{
	$txt = trim(varray_rand($shipins));
	$moban = preg_replace('/<站群_随机视频>/', $txt, $moban, 1);
}

$gjc1=trim(varray_rand($keywords));
$gjc2=trim(varray_rand($keywords));
$gjc3=trim(varray_rand($keywords));
$gjc4=trim(varray_rand($keywords));
$gjc5=trim(varray_rand($keywords));
$gjc6=trim(varray_rand($keywords));

$moban = str_replace( "<站群_主关键词>", $gjc1, $moban );
$moban = str_replace( "<站群_关键词2>", $gjc2, $moban );
$moban = str_replace( "<站群_关键词3>", $gjc3, $moban );
$moban = str_replace( "<站群_关键词4>", $gjc4, $moban );
$moban = str_replace( "<站群_关键词5>", $gjc5, $moban );
$moban = str_replace( "<站群_关键词6>", $gjc6, $moban );

function getdomain($url) 
{
	$host = strtolower ( $url );
	if (strpos ( $host, '/' ) !== false) 
	{
		$parse = @parse_url ( $host );
		$host = isset($parse['host']) ? $parse['host'] : $host;
	}
	$topleveldomaindb = array ('gov.cn', 'com.cn', 'net.cn', 'org.cn', 'com', 'cn', 'asia', 'edu', 'gov', 'ga', 'net', 'org', 'biz', 'info', 'pw', 'name', 'mobi', 'cc', 'hk' );
	$str = implode('|',$topleveldomaindb);

	$matchstr = "[^\.]+\.(?:(" . $str . ")|\w{2}|((" . $str . ")\.\w{2}))$";
	if (preg_match( "/" . $matchstr . "/is", $host, $matchs )) 
	{
		$domain = $matchs[0];
	}
	else 
	{
		$domain = $host;
	}
	return $domain;
}

$duankou = isset($_SERVER["SERVER_PORT"]) ? $_SERVER["SERVER_PORT"] : 80;
$url1 = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$yuming = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
$yuming = str_replace(':'.$duankou, '', $yuming);
$yumi = getdomain($yuming);
$url2 = str_replace('.'.$yumi, '', $yuming);

// 随机关键词
$ci = substr_count($moban, '<站群_随机关键词>');
for ($ii=0; $ii<$ci; $ii++) 
{
	$word = trim(varray_rand($keywords));
	$moban = preg_replace('/<站群_随机关键词>/', $word, $moban, 1);
}

// 句子
$wk = substr_count($moban, '<站群_句子>');
for ($wi=0; $wi<$wk; $wi++) 
{
	$txt = trim(varray_rand($content));
	$moban = preg_replace('/<站群_句子>/', $txt, $moban, 1);
}

// 句子2
$wk = substr_count($moban, '<站群_句子2>');
for ($wi=0; $wi<$wk; $wi++) 
{
	$txt = trim(varray_rand($content2));
	$moban = preg_replace('/<站群_句子2>/', $txt, $moban, 1);
}

// 端口
$dk = substr_count($moban, '<站群_随机端口>');
for ($di=0; $di<$dk; $di++) 
{
	$txt = trim(varray_rand($duankous));
	$moban = preg_replace('/<站群_随机端口>/', $txt, $moban, 1);
}

// 内链
$vi = substr_count($moban, '<站群_随机内链>');
for ($li=0; $li<$vi; $li++) 
{
	$s=trim(varray_rand($domains));
	$moban = preg_replace('/<站群_随机内链>/', $s, $moban, 1);
}

// 随机字符
$zf1 = substr_count($moban, '<站群_随机字符>');
for ($ii=0; $ii<$zf1; $ii++) 
{
	$moban = preg_replace('/<站群_随机字符>/', randKey(5), $moban, 1);
}

// 随机数字
$ri5 = substr_count($moban, '<站群_随机数字>');
for ($i=0; $i<$ri5; $i++) 
{
	$moban = preg_replace('/<站群_随机数字>/', mt_rand(10000, 99999), $moban, 1);
}

$moban = str_replace( "<站群_当前域名>", $yuming, $moban );
$moban = str_replace( "<站群_顶级域名>", $yumi, $moban );
$moban = str_replace( "<站群_当前域名1>", $_SERVER['HTTP_HOST'], $moban );

// 图片
$tupian5 = substr_count($moban, '<站群_随机图片>');
for ($tui=0; $tui<$tupian5; $tui++) 
{
	$img = varray_rand($image_list);
	$moban = preg_replace('/<站群_随机图片>/', '/picture/'.$img, $moban, 1);
}

// 外链
$dtk1_file = DIR . "/wailian/".hpick($dtk1_list, 'dtk1');
$dtk1s = file_exists($dtk1_file) ? file($dtk1_file) : [];
$ci = substr_count($moban, '<外链>');
if ($ci != 0) 
{
	for ($ii = 0; $ii < $ci; $ii++) 
	{
		$txt = trim(varray_rand($dtk1s));
		$moban = preg_replace('/<外链>/', $txt, $moban, 1);
	}
}

// 变量
$dtk2_file = DIR . "/system/bianliang/".hpick($dtk2_list, 'dtk2');

$dtk2s = file_exists($dtk2_file) ? file($dtk2_file) : [];
$ci = substr_count($moban, '<站群_变量>');
if ($ci != 0) 
{
	for ($ii = 0; $ii < $ci; $ii++) 
	{
		$txt = trim(varray_rand($dtk2s));
		$moban = preg_replace('/<站群_变量>/', $txt, $moban, 1);
	}
}

// 时间
$moban = str_replace( "<站群_年>", date( "y" ), $moban );
$moban = str_replace( "<站群_发布时间>", date( "m-d" ), $moban );
for($i=1;$i<=20;$i++){
	$moban = str_replace( "<站群_发布时间$i>", date( "m-d",strtotime("-$i day")), $moban );
}

// spider
$wk = substr_count($moban, '<spider>');
for ($wi=0; $wi<$wk; $wi++) 
{
	$txt = trim(varray_rand($spider));
	$moban = preg_replace('/<spider>/', $txt, $moban, 1);
}
$moban = preg_replace('/<spider>/', '', $moban, 1);

$moban = str_ireplace( "</body>", "</body>", $moban );
echo apply_asset_token($moban);
?>


