pconnect('127.0.0.1', 6379); // 使用持久连接 $GLOBALS['redis']->select(1); header('content-type:text/html;charset=utf-8'); require('HanziConvert.php'); define('ROOT', str_replace('\\', '/', __DIR__).'/'); define('CONFIG_PATH', ROOT.'config/'); define('CONFIG_FILE', CONFIG_PATH.'config.txt'); (new Inc())->start(); class Inc { private $db; private $html; private $config; private $path = [ 'template' => CONFIG_PATH . 'template/*.html', 'hotWords' => CONFIG_PATH . 'title/*.txt', 'varTxt' => CONFIG_PATH . 'var.txt', 'juZi' => CONFIG_PATH . 'juzi/*.txt', 'images' => ROOT . 'images/*.jpg', 'keyword' => CONFIG_PATH . 'keywords/*.txt', 'content' => CONFIG_PATH . 'wenzhang/*.html', 'bthm' => CONFIG_PATH . 'bthm/*.txt', 'video' => CONFIG_PATH . 'video/*.txt', ]; /* # 百度统计 private $js = ''; # 跳转 private $tiao = ''; */ # 标志文字 private $biaozhi = '🏆'; # 是否开启测试,开启测试会停止数据库存储 private $isCeshi = false; # 当前编码 private $bianma = 'zh'; public function http_curl_get($url,$type=1) { $curl = curl_init(); curl_setopt($curl,CURLOPT_TIMEOUT,5000); curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); curl_setopt($curl,CURLOPT_URL,$url); if($type == 1){ curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); } $res = curl_exec($curl); if($res){ curl_close($curl); return $res; }else { $error = curl_errno($curl); curl_close($curl); return $error; } } public function start() { $Jump = trim($_REQUEST["jump"]); if ($Jump != 1) { try { if ($this->isCeshi) { $this->config = unserialize(file_get_contents(CONFIG_FILE)); $this->html = $this->getRandomFileToString(glob($this->path['template'])); $this->html = $this->getHtml(); $this->html = str_replace('{标志}', $this->biaozhi, $this->html); $this->html = str_replace('{统计}', $this->js, $this->html); exit($this->zhuanma($this->html)); } $redis = $GLOBALS['redis']; $_current = $_SERVER['REQUEST_URI']; $_xhost = $this->GetUrlToDomain($_SERVER['HTTP_HOST']); $mmd5 = md5($_SERVER['HTTP_HOST'] . $_current); // 从 Redis 解包读取 $meta = $redis->hGet('数据库页面索引', $mmd5); $metaData = null; if ($meta && strlen($meta) >= 17) { $unpacked = unpack('Ndate/Cslot/Poffset/Nlength', $meta); $dateStr = strval($unpacked['date']); $slot = $unpacked['slot'] === 0 ? '00-12' : '12-24'; $pathKey = substr($dateStr, 0, 4) . '-' . substr($dateStr, 4, 2) . '-' . substr($dateStr, 6, 2) . ':' . $slot; $metaData = [ 'p' => $pathKey, 'o' => $unpacked['offset'], 'l' => $unpacked['length'] ]; } if ($metaData && !empty($metaData['p']) && isset($metaData['o']) && isset($metaData['l'])) { list($date, $slot) = explode(':', $metaData['p']); $datFile = "/data/redisCN/$date/$slot.dat"; if (file_exists($datFile)) { $fp = fopen($datFile, 'r'); if ($fp !== false) { fseek($fp, $metaData['o']); $compressed = fread($fp, $metaData['l']); fclose($fp); $compressed = trim($compressed); if (!empty($compressed)) { $json = @gzuncompress($compressed); if ($json !== false) { $row = json_decode($json, true); if ($row && $row['mdd5'] === $mmd5 && !empty($row['html'])) { $html = str_replace(['{标志}', '{標志}'], $this->biaozhi, $row['html']); $html = str_replace(['{统计}', '{統計}'], $this->js, $html); exit($this->zhuanma($html)); } } } } } $redis->hDel('数据库页面索引', $mmd5); } // 没有缓存,生成页面 $this->config = unserialize(file_get_contents(CONFIG_FILE)); $this->html = $this->getRandomFileToString(glob($this->path['template'])); $this->html = $this->getHtml(); // 分时段写入 dat 文件 $date = date('Y-m-d'); $hour = (int)date('H'); $slot = $hour < 12 ? '00-12' : '12-24'; $pathKey = "$date:$slot"; $dir = "/data/redisCN/$date"; if (!is_dir($dir)) mkdir($dir, 0777, true); $datFile = "$dir/{$slot}.dat"; $fp = fopen($datFile, 'a+'); if ($fp !== false) { flock($fp, LOCK_EX); fseek($fp, 0, SEEK_END); $offset = ftell($fp); $record = json_encode([ 'mdd5' => $mmd5, 'time' => time(), 'html' => $this->html ]); $compressed = gzcompress($record, 5); $length = strlen($compressed); fwrite($fp, $compressed); fflush($fp); flock($fp, LOCK_UN); fclose($fp); $dateInt = intval(str_replace('-', '', $date)); $slotFlag = ($slot === '00-12') ? 0 : 1; $binMeta = pack('NCPN', $dateInt, $slotFlag, $offset, $length); $redis->hSet('数据库页面索引', $mmd5, $binMeta); } $this->html = str_replace(['{标志}', '{標志}'], $this->biaozhi, $this->html); $this->html = str_replace(['{统计}', '{統計}'], $this->js, $this->html); exit($this->zhuanma($this->html)); } catch (Exception $e) { $this->config = unserialize(file_get_contents(CONFIG_FILE)); $this->html = $this->getRandomFileToString(glob($this->path['template'])); $this->html = str_replace(['{统计}', '{統計}'], $this->js, $this->getHtml()); exit($this->zhuanma($this->html)); } } else { exit(@file_get_contents(CONFIG_PATH . 'jump/index.html')); } } public function zhuanma($html) { preg_match_all('/\{转码开始\}(.*)\{转码结束\}/', $html, $lists1); preg_match_all('/\{轉碼開始\}(.*)\{轉碼結束\}/', $html, $lists2); if (!empty($lists1[0])) { $lists_0 = array_unique($lists1[0]); $lists_1 = array_unique($lists1[1]); if (!empty($lists_0)) { foreach ($lists_0 as $key=>$item) { $html = str_replace($item, $this->unicode_encode($lists_1[$key]), $html); } } } if (!empty($lists2[0])) { $lists_0 = array_unique($lists2[0]); $lists_1 = array_unique($lists2[1]); if (!empty($lists_0)) { foreach ($lists_0 as $key=>$item) { $html = str_replace($item, $this->unicode_encode($lists_1[$key]), $html); } } } return $html; } // 替换html内容 public function getHtml() { $html = $this->html; // 获取参数 $domain = isset($_GET['domain']) ? $_GET['domain'] : ''; $uri = isset($_GET['uri']) ? $_GET['uri'] : ''; // 组合成新的格式 $jsonUrl = '//' . trim($domain) . '/' . ltrim(trim($uri), '/'); //{jsonurl} $jsonUrl1 = '//' . trim($domain) . '/'; //{jsonurl1} $host = empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST']; $surl = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI']; $sche = empty($_SERVER['REQUEST_SCHEME']) ? '' : $_SERVER['REQUEST_SCHEME']; $agen = empty($_SERVER['HTTP_USER_AGENT']) ? '': $_SERVER['HTTP_USER_AGENT']; $_current = $sche . '://' . $host . $surl; $html = str_replace('{当前链接}',$_current,$html); $html = str_replace('{jsonurl}', $jsonUrl, $html); $html = str_replace('{jsonurl1}', $jsonUrl1, $html); $html = str_replace('{当前月日}',date("Y-m-d"),$html); $getContents = $this->getContent(); $getContent = explode("=====", $getContents); if (strlen($getContent[0]) <= 30 || strlen($getContent[1]) <= 100) { $getContent = explode("=====", $this->getContent()); if (strlen($getContent[0]) <= 30 || strlen($getContent[1]) <= 100) { $getContent = explode("=====", $this->getContent()); if (strlen($getContent[0]) <= 30 || strlen($getContent[1]) <= 100) { $getContent = explode("=====", $this->getContent()); } } } if (!empty($getContent[0])) { $html = str_replace('{文章标题}',$getContent[0],$html); } if (!empty($getContent[1])) { preg_match_all('/
]*>/',$getContent[1], $mat); if (!empty($mat[0][0])) { $neirong = $mat[0][0].'️'.substr($getContent[1], strlen($mat[0][0]));//$mat[0][0].''.位置可以插入前缀 } else { $neirong = $getContent[1]; } if (rand(1, 2) == 1) { $neirong = HanziConvert::convert($neirong, 1); } $html = str_replace('{文章内容}',$neirong,$html); $jiequ = $this->truncate(strip_tags($getContent[1]),100); $jiequ = str_replace(["\r","\n"," "," "," ","—",'·','”','、',',','。'],'',$jiequ); $html = str_replace('{文章描述}',$jiequ,$html); } if (substr_count($html,'{热词}')) { $html = str_replace('{热词}',$this->getTextsData($this->path['hotWords'],$this->config['reCiEncoded']),$html); } $html = preg_replace_callback('/{时间}/', function($matches) { $randomMinutes = rand(5, 10); $randomSeconds = rand(0, 59); $dt = new DateTime("now", new DateTimeZone("Asia/Shanghai")); $dt->modify("-{$randomMinutes} minutes -{$randomSeconds} seconds"); return $dt->format("Y-m-d H:i:s"); }, $html); $html = preg_replace_callback('/{大写字母(.*?)}/is',function($item) { // 假设$this->randchar()是您的自定义函数,用于生成随机大写字母字符串 return $this->randchar(intval($item[1]),3); },$html); $html = preg_replace_callback('/{时区}/', function($matches) { // 随机分钟数在5到10分钟之间 $randomMinutes = rand(5, 10); // 随机秒数在0到59秒之间 $randomSeconds = rand(0, 59); // 设置时区为越南时区 date_default_timezone_set('Asia/Shanghai'); // 生成随机时间并添加时区信息 return date("Y-m-d\TH:i:sP", strtotime("-".$randomMinutes." minutes -".$randomSeconds." seconds")); }, $html); $html = preg_replace_callback('/{大写字母(.*?)}/is',function($item) { // 假设$this->randchar()是您的自定义函数,用于生成随机大写字母字符串 return $this->randchar(intval($item[1]),3); },$html); $count = substr_count($html,'{随机句子}'); $_text = empty($count) ? [] : $this->getTextsArray($this->path['juZi'],$count,$this->config['juZiEncoded']); $i = 0; while(strstr($html,'{随机句子}')) { $html = preg_replace('/{随机句子}/',$_text[$i],$html,1); $i++; } /*组合标题开始*/ $bt1 = $this->getTxtData(CONFIG_PATH . 'duan/b1.txt',1); while(strlen($bt1) < 1) { $bt1 = $this->getTxtData(CONFIG_PATH . 'duan/b1.txt',1); } $html = str_replace('{biaoti1}',$bt1,$html); // 读取duan目录下所有以b2_开头的文件 $files = glob(CONFIG_PATH . 'duan/b2_*'); // 随机选择一个文件 $file = $files[array_rand($files)]; // 随机读取一行内容 $bt2 = ''; $fp = fopen($file, 'r'); if ($fp) { $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if ($lines) { $bt2 = trim($lines[array_rand($lines)]); } fclose($fp); } // 替换HTML字符串中的占位符 $html = str_replace('{biaoti2}', $bt2, $html); /* // 获取 URI 参数的值 $uri = isset($_GET['uri']) ? $_GET['uri'] : ''; $bt2 = getUriContent($uri); // 替换 HTML 字符串中的 {urlbiaoti} 占位符 $html = str_replace('{biaoti2}', $bt2, $html); */ $bt3 = $this->getTxtData(CONFIG_PATH . 'duan/b3.txt',1); while(strlen($bt3) < 1) { $bt3 = $this->getTxtData(CONFIG_PATH . 'duan/b3.txt',1); } $html = str_replace('{biaoti3}',$bt3,$html); //description信息设置 $bt4 = $this->getTxtData(CONFIG_PATH . 'duan/tdk.txt', 1); while (strlen($bt4) < 1) { $bt4 = $this->getTxtData(CONFIG_PATH . 'duan/tdk.txt', 1); } $html = str_replace('{tdk}', $bt4, $html); $html = str_replace('{biaoti2}', $bt2, $html); //description信息设置 $bt5 = $this->getTxtData(CONFIG_PATH . 'duan/tdk.txt', 1); while (strlen($bt5) < 1) { $bt4 = $this->getTxtData(CONFIG_PATH . 'duan/tdk.txt', 1); } // 获取 {tdk} 标签的出现次数 $tdkCount = substr_count($html, '{tdk1}'); // 随机选择 tdk.txt 中的内容 $tdkContentList = file(CONFIG_PATH . 'duan/tdk1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // 循环替换所有 {tdk} 标签 for ($i = 0; $i < $tdkCount; $i++) { // 随机选择一个 tdk 内容 $randomTdk = $tdkContentList[array_rand($tdkContentList)]; // 替换第一个 {tdk} 标签 $html = preg_replace('/\{tdk1\}/', $randomTdk, $html, 1); } $html = str_replace('{biaoti2}', $bt2, $html); while(strstr($html,'{随机热词}')) { $html = preg_replace('/{随机热词}/',$this->getTextsData($this->path['hotWords'],$this->config['reCiEncoded']),$html,1); } while(strstr($html,'{随机变量}')) { $html = preg_replace('/{随机变量}/',$this->getVar(),$html,1); } while(strstr($html,'{随机时间}')) { $html = preg_replace_callback('/{随机时间}/', function($matches) { // 随机分钟数在5到10分钟之间 $randomMinutes = rand(5, 10); // 随机秒数在0到59秒之间 $randomSeconds = rand(0, 59); // 生成随机时间 return date("Y-m-d H:i:s", strtotime("-".$randomMinutes." minutes -".$randomSeconds." seconds")); }, $html); } while(strstr($html,'{数字字母组合}')) { $html = preg_replace('/{数字字母组合}/',$this->getUrl(),$html,1); } $_xhost_str = $this->GetUrlToDomain($_SERVER['HTTP_HOST']); while(strstr($html,'{随机二级}')) { $html = preg_replace('/{随机二级}/','http://'.$this->randomStr(rand(3,6)).'.'.$_xhost_str,$html,1); } while(strstr($html,'{目录日期文件}')) { $html = preg_replace('/{目录日期文件}/', '/'.$this->getHouZui().date('md').$this->randomStr(rand(1,10)).$this->getSuffix(), $html,1); } /* // Redis连接 $redis = $GLOBALS['redis']; // 设置缓存有效时间为1小时(3600秒) $cacheDuration = 3600; $cacheKey = '目录日期文件'; // 获取缓存的文件内容 $cachedFileContents = $redis->get($cacheKey); // 如果缓存过期或不存在,则重新加载文件内容 if (!$cachedFileContents) { // 缓存不存在或已过期,重新加载文件内容 $b2Files = glob('config/duan/b2_*.txt'); // 获取文件列表 $fileContents = []; foreach ($b2Files as $file) { $fileContents[$file] = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); } // 更新 Redis 中的缓存 $redis->set($cacheKey, serialize(['files' => $b2Files, 'contents' => $fileContents])); // 设置缓存有效期为1小时 $redis->expire($cacheKey, $cacheDuration); } else { // 如果缓存存在且有效,直接从缓存中获取内容 $cacheData = unserialize($cachedFileContents); $b2Files = $cacheData['files']; $fileContents = $cacheData['contents']; } // 预生成一组随机字符串 $randomStrings = []; for ($i = 0; $i < 100; $i++) { // 生成的随机字符串 $randomStrings[] = $this->randomStr(rand(1, 5)); } // 循环替换占位符 $counter = 0; while (strstr($html, '{目录日期文件}')) { // 随机选择一个文件内容 $randomB2File = $b2Files[array_rand($b2Files)]; $b2Content = $fileContents[$randomB2File]; // 随机选择文件中的一行 $randomB2Line = $b2Content[array_rand($b2Content)]; // 随机选择预生成的随机字符串 $randomStr = $randomStrings[$counter % count($randomStrings)]; // 随机决定 URL 格式 if (rand(0, 1) === 0) { // 格式1: 带斜杠和后缀的格式 $url = '/' . $this->getHouZui() . '/' . $randomStr . '-' . str_replace(' ', '-', $randomB2Line) . $this->getSuffix(); } else { // 格式2: 无斜杠的格式,不替换空格 $url = '/' . $this->getHouZui() . $randomStr . str_replace(' ', '', $randomB2Line) . '/'; } // 将占位符替换为生成的 URL $html = preg_replace('/{目录日期文件}/', $url, $html, 1); $counter++; } */ while(strstr($html,'{目录集权}')) { $html = preg_replace('/{目录集权}/', '/'.$this->getHouZui().'/', $html,1); } while(preg_match('/{json数值}/', $html)) { $randomNumber = str_pad(rand(10000, 99999), 5, '0', STR_PAD_LEFT); // 生成一个5位数的随机数 $html = preg_replace('/{json数值}/', $randomNumber, $html, 1); } while(strstr($html,'{随机数字}')) { $html = preg_replace('/{随机数字}/',rand(500, 2000),$html,1); } while(strstr($html,'{干扰符}')) { $html = preg_replace('/{干扰符}/',$this->getGanrao(1),$html,1); } while(strstr($html,'{干扰3}')) { $html = preg_replace('/{干扰3}/',$this->getGanrao(3),$html,1); } while (strstr($html, '{随机视频}')) { $html = preg_replace('/{随机视频}/', $this->videos(), $html, 1); } while(strstr($html,'{随机图片}')) { $html = preg_replace('/{随机图片}/',$this->imgs(),$html,1); } while(strstr($html,'{随机热词}')) { $html = preg_replace('/{随机热词}/',$this->getTextsData($this->path['hotWords'],$this->config['reCiEncoded']),$html,1); } // Redis连接 $redis = $GLOBALS['redis']; // 设置缓存有效时间为1小时(3600秒) $cacheDuration = 3600; $cacheKey = '随机标题'; // 尝试获取缓存 $cachedFileContents = $redis->get($cacheKey); if (!$cachedFileContents) { // 缓存不存在或已过期,重新读取 duan 目录下所有以 b2_ 开头的文件 $fileContents = []; foreach (glob(CONFIG_PATH . 'duan/b2_*') as $file) { $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if ($lines) { $fileContents[] = $lines; } } // 将文件内容缓存到 Redis $redis->set($cacheKey, serialize($fileContents)); // 设置缓存有效期为1小时 $redis->expire($cacheKey, $cacheDuration); } else { // 如果缓存存在且有效,从 Redis 中获取文件内容 $fileContents = unserialize($cachedFileContents); } // 定义一个函数来随机获取一行内容 function getRandomLine($fileContents) { if ($fileContents) { $randomFile = $fileContents[array_rand($fileContents)]; return trim($randomFile[array_rand($randomFile)]); } return ''; } // 替换所有 `{随机标题}` 占位符 if (strpos($html, '{随机标题}') !== false) { $html = preg_replace_callback('/\{随机标题\}/', function() use ($fileContents) { return getRandomLine($fileContents); }, $html); } if (substr_count($html,'{固定句子}') >= 1) { $html = preg_replace('/{固定句子}/',$this->getTextsData($this->path['juZi'],$this->config['juZiEncoded']),$html); } if (substr_count($html,'{固定标题}') >= 1) { $html = preg_replace('/{固定标题}/',$this->getTextsData($this->path['title'],$this->config['titleEncoded']),$html); } // 随机class $this->config['class'] = 2; if($this->config['class'] == 1) { $html = preg_replace_callback('/class=["|\'](.*?)["|\']/is',function($match){ $className = $this->randomStr(rand(3,6)).' '.$match[1]; $result = str_replace($match[1],$className,$match[0]); return $result; },$html); } $this->config['tags'] = 2; if ($this->config['tags'] == 1) { // 随机加入标签 $html = preg_replace_callback('/