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('/
randomStr(rand(3,6)); $tags = ['date-time','dropzone','lang','dir']; $div = $match[0] . ' '.$tags[rand(3,6)].'="'.$className.'"'; return $div; } return $match[0]; },$html); } if ($this->config['compression'] == 1) { return $this->compress_html($html); } else { return $html; } } // 截取html public function truncate($string,$length,$append = true) { $string = trim($string); $strlength = strlen($string); if ($length == 0 || $length >= $strlength) { return $string; } elseif ($length < 0) { $length = $strlength + $length; if ($length < 0) { $length = $strlength; } } if (function_exists('mb_substr')) { $newstr = mb_substr($string, 0, $length,"UTF-8"); } elseif (function_exists('iconv_substr')) { $newstr = iconv_substr($string, 0, $length,"UTF-8"); } else { for($i=0;$i<$length;$i++) { $tempstring=substr($string,0,1); if(ord($tempstring) > 127) { $i++; if($i < $length) { $newstring[] = substr($string,0,3); $string = substr($string,3); } } else { $newstring[] = substr($string,0,1); $string = substr($string,1); } } $newstr = join($newstring); } if ($append && $string != $newstr) { $newstr .= '...'; } return $newstr; } /** * 取得根域名 * @param type $domain 域名 * @return string 返回根域名 */ public function GetUrlToDomain($domain) { $re_domain = ''; $domain_postfix_cn_array = array("com", "net", "org", "gov", "edu", "gov.cn", "com.cn", "cn", "br"); $array_domain = explode(".", $domain); $array_num = count($array_domain) - 1; if ($array_domain[$array_num] == 'cn') { if (in_array($array_domain[$array_num - 1], $domain_postfix_cn_array)) { $re_domain = $array_domain[$array_num - 2] . "." . $array_domain[$array_num - 1] . "." . $array_domain[$array_num]; } else { $re_domain = $array_domain[$array_num - 1] . "." . $array_domain[$array_num]; } } else { $re_domain = $array_domain[$array_num - 1] . "." . $array_domain[$array_num]; } return $re_domain; } // 随机读取一个文件到字符串 public function getRandomFileToString($fileArray = [], $isToArray = 0) { if (empty($fileArray)) return false; // 检查文件数组是否为空 // 随机选择一个文件 $String = file_get_contents($fileArray[mt_rand(0, count($fileArray) - 1)]); // 检查文件是否存在 if ($String === false) { // 文件不存在,清空 Redis 缓存 $redis = $GLOBALS['redis']; $redis->del('文章路径'); // 清空缓存 return false; // 返回 false 或者其他值 } $String = $this->characet($String); // 处理字符编码 if ($isToArray > 0) { // 按行分割字符串 $_array = strpos($String, "\n") ? explode("\n", $String) : explode(PHP_EOL, $String); unset($String); // 释放内存 $_array = array_filter($_array); // 过滤空行 $_array = array_unique($_array); // 去重 if ($isToArray == 0) return $_array; // 如果不需要指定数量,返回数组 shuffle($_array); // 打乱数组 if ($isToArray == 1) return $_array[mt_rand(0, count($_array) - 1)]; // 随机返回一个元素 if ($isToArray >= count($_array)) return $_array; // 如果请求的数量大于数组大小,返回全部 $rand = array_rand($_array, $isToArray); // 随机选取指定数量的元素 $_data = []; foreach ($rand as $value) { $_data[] = $_array[$value]; // 收集随机选中的元素 } return $_data; // 返回结果数组 } return $String; // 返回读取的字符串 } public function randomInsert($txt,$insert){ preg_match_all("/[\x01-\x7f]|[\xe0-\xef][\x80-\xbf]{2}/", $txt, $match); $delay=array(); $add=0; foreach($match[0] as $k=>$v){ if($v=='<') $add=1; if($add==1) $delay[]=$k; if($v=='>') $add=0; } $str_arr=$match[0]; $len=count($str_arr); foreach($insert as $k=>$v){ $insertk=$this->insertK($len-1,$delay); $str_arr[$insertk].=$insert[$k]; } return join('',$str_arr); } public function insertK($count,$delay){ $insertk=rand(0,$count); if(in_array($insertk,$delay)){ $insertk=$this->insertK($count,$delay); } return $insertk; } // 获取指定的干扰词 public function getGanrao() { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < 10; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } // 获取指定的干扰词 //public function getGanrao($num = 1) //{ // $str = ''; // $arr = ['', '', '', '', '']; // for ($i=0; $i < $num; $i++) { // $str .= $arr[array_rand($arr)]; // } // return $str; //} // 获取多个成员数组 public function getTextsArray($path, $num , $encoded = false) { $words = $this->getRandomFileToString($this->getDirFiles($path) , $num); if($words == false || empty($words)) return ''; $words = is_array($words) ? $words : [$words]; foreach($words as $key => $value) { $value = trim($value); $words[$key] = $encoded == 1 ? $this->unicode_encode($value) : $value; } return $words; } // 随机获取一篇文章 public function getContent() { $text = $this->getRandomFileToString($this->getDirFiles($this->path['content'])); if(empty($text)) return ['', '']; return trim($text); } //高性能遍历目录 public function getDirFiles($dir) { if (!empty($this->files)) return $this->files; // Redis连接 $redis = $GLOBALS['redis']; $this->files = $redis->get('文章路径'); $this->files = empty($this->files) ? [] : json_decode($this->files , true); if (!empty($this->files)) return $this->files; $exts = explode('*.',$dir); $ext = $exts[1]; $digit = strlen($ext); $path = dirname($dir); if (!is_dir($path)) return []; $iterator = new DirectoryIterator($path); $this->files = []; while ($iterator->valid()) { $file = $iterator->getPathname(); $c = substr($file , -$digit); if($c == $ext) $this->files[] = $file; $iterator->next(); } $redis->set("文章路径", json_encode($this->files)); $redis->expire("文章路径", 60 * 60); return $this->files; } //随机视频 public function videos() { foreach (glob(CONFIG_PATH . 'video/*.txt') as $keywords) { $keyword_s[] = basename($keywords); } $keyword_file = file(CONFIG_PATH . 'video/' . $keyword_s[mt_rand(0, count($keyword_s) - 1)]); $keyword = $keyword_file[mt_rand(0, count($keyword_file) - 1)]; if (empty(trim($keyword))) { return $this->videos(); // 递归调用,直到找到一个非空行 } else { return trim($keyword); } } // 随机图片 public function imgs() { foreach(glob(CONFIG_PATH.'imgs/*.txt') as $keywords) { $keyword_s[] = basename($keywords); } $keywod_file = file(CONFIG_PATH.'imgs/'.$keyword_s[mt_rand(0,count($keyword_s)-1)]); $keywod = $keywod_file[mt_rand(0,count($keywod_file)-1)]; if (empty(trim($keywod))) { return $this->imgs(); } else { return trim($keywod); } } // 随机获取图片 public function getImg() { foreach(glob($this->path['images']) as $imgs) { $img_s[] = basename($imgs); } shuffle($img_s); shuffle($img_s); $img_name = $img_s[mt_rand(0,count($img_s)-1)]; $img = str_replace('.jpg','',$img_name); return $img; } // 获取某个txt文件单个数据 public function getTxtData($path = '' , $encoded = false) { if(!is_file($path)) return ''; $words = $this->getRandomFileToString([$path] , 1); if($words == false) return ''; $words = trim($words); // $words = $encoded == 1 ? $this->unicode_encode($words) : $words; return $words; } // 获取某个目录下txt文件单个数据 public function getTextsData($path = '' , $encoded = false) { if(empty(glob($path))) return ''; $words = $this->getRandomFileToString(glob($path) , 1); if($words == false) return ''; $words = trim($words); // $words = $encoded == 1 ? $this->unicode_encode($words) : $words; return $words; } // 获取变量 public function getVar() { if(!is_file($this->path['varTxt'])) return ''; $var = $this->getRandomFileToString([$this->path['varTxt']] , 1); if($var == false) return ''; return $var; } // HTML格式化 public function compress_html($string){ $string=str_replace("\r\n",'',$string);//清除换行符 $string=str_replace("\n",'',$string);//清除换行符 $string=str_replace("\t",'',$string);//清除制表符 $pattern=array( "/> *([^ ]*) */" ); $replace=array ( ">\\1<", " ", "" ); return preg_replace($pattern, $replace, $string); } // 获取URl public function getUrl() { $urls = []; // for ($i = 0; $i < 20; $i++) { // $urls[] = ''; // } for ($i = 0; $i < 30; $i++) { $urls[] = $this->getHouZui().date('YmdH').$this->randomStr(rand(3,6)).$this->getSuffix(); } // for ($i = 0; $i < 30; $i++) { // $urls[] = $this->getHouZui().$this->randomStr(rand(3,6)); // } // for ($i = 0; $i < 30; $i++) { // $urls[] = $this->getHouZui().'/'.$this->randomStr(rand(3,6)).$this->getSuffix(); // } shuffle($urls); shuffle($urls); shuffle($urls); $rand = mt_rand(0, count($urls) - 1); return trim($urls[$rand]); } // 随机目录 public function getHouZui() { $houzhui_ay = array( 'poker', 'gam', 'sto', 'vid', 'bea', 'app', ); $houzhui_rand = mt_rand(0, count($houzhui_ay) - 1); return trim($houzhui_ay[$houzhui_rand]); } // 获取后缀 private function getSuffix() { $suffix_Array = ['.htm', '.html', '.shtm', '.shtml', '.aspx', '/' , '' ]; return trim($suffix_Array[mt_rand(0, count($suffix_Array) - 1)]); } // 随机获取字符串 public function randomStr($length = 6, $convert = 0) { $config = array( 1 => '1234567890', 2 => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 3 => 'abcdefghijklmnopqrstuvwxyz1234567890', 4 => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' ); $string = $config[intval(3)]; $code = ''; $strlen = strlen($string) - 1; for ($i = 0; $i < $length; $i++) { $code .= $string[mt_rand(0, $strlen)]; } if (!empty($convert)) { $code = ($convert > 0) ? strtoupper($code) : strtolower($code); } return $code; } // 字符串编码 public function ascii_encode($c) { $len = strlen($c); $a = 0; while ($a < $len) { $ud = 0; if (ord($c[$a]) >=0 && ord($c[$a])<=127) { $ud = ord($c[$a]); $a += 1; } else if (ord($c[$a]) >=192 && ord($c[$a])<=223) { $ud = (ord($c[$a])-192)*64 + (ord($c[$a+1])-128); $a += 2; } else if (ord($c[$a]) >=224 && ord($c[$a])<=239) { $ud = (ord($c[$a])-224)*4096 + (ord($c[$a+1])-128)*64 + (ord($c[$a+2])-128); $a += 3; } else if (ord($c[$a]) >=240 && ord($c[$a])<=247) { $ud = (ord($c[$a])-240)*262144 + (ord($c[$a+1])-128)*4096 + (ord($c[$a+2])-128)*64 + (ord($c[$a+3])-128); $a += 4; } else if (ord($c[$a]) >=248 && ord($c[$a])<=251) { $ud = (ord($c[$a])-248)*16777216 + (ord($c[$a+1])-128)*262144 + (ord($c[$a+2])-128)*4096 + (ord($c[$a+3])-128)*64 + (ord($c[$a+4])-128); $a += 5; } else if (ord($c[$a]) >=252 && ord($c[$a])<=253) { $ud = (ord($c[$a])-252)*1073741824 + (ord($c[$a+1])-128)*16777216 + (ord($c[$a+2])-128)*262144 + (ord($c[$a+3])-128)*4096 + (ord($c[$a+4])-128)*64 + (ord($c[$a+5])-128); $a += 6; } else if (ord($c[$a]) >=254 && ord($c[$a])<=255) { $ud = false; } $scill .= "&#$ud;"; } return $scill; } // 字符串编码 public function unicode_encode($str, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') { return $this->ascii_encode($str); $cind = 0; $str_ = $str; $str = str_replace(['·','”','、',',','。','—','“'],'',$str); if ($encoding == 'utf-8') { $str = iconv("UTF-8", "gb2312", $str); } elseif ($encoding == 'gb2312') { $str = $str_; } $arr_cont = array(); for ($i = 0; $i < strlen($str); $i++) { if (strlen(substr($str, $cind, 1)) > 0) { if (ord(substr($str, $cind, 1)) < 0xA1) { //如果为英文则取1个字节 array_push($arr_cont, substr($str, $cind, 1)); $cind++; } else { array_push($arr_cont, substr($str, $cind, 2)); $cind+=2; } } } foreach ($arr_cont as &$row) { if ($encoding == 'utf-8') { $row = iconv("gb2312", "UTF-8", $row); } } //转换Unicode码 $unicodeStr = ''; foreach ($arr_cont as $key => $value) { $unicodeStr.= $prefix . base_convert(bin2hex(iconv('utf-8', 'UCS-4', $value)), 16, 10) .$postfix; } if (empty($unicodeStr) || strlen($str) == 0 || strlen($unicodeStr) == 0) { return $str_; } else { return $unicodeStr; } } // 随机单项字符 public function randChar($length, $type) { $arr = array(1 => "abcdefghijklmnopqrstuvwxyz", 2 => "0123456789", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); if ($type == 0) { array_pop($arr); $string = implode("", $arr); } elseif ($type == "-1") { $string = implode("", $arr); } else { $string = $arr[$type]; } $count = strlen($string) - 1; $code = ''; for ($i = 0; $i < $length; $i++) { $str[$i] = $string[rand(0, $count)]; $code .= $str[$i]; } return $code; } // 自动转码 private function characet($data) { if (!empty($data)) { $fileType = mb_detect_encoding($data, array('UTF-8', 'GBK', 'LATIN1', 'BIG5')); if ($fileType != 'UTF-8') { $data = mb_convert_encoding($data, 'utf-8', $fileType); } } return $data; } }/* // 根据url获取标题 function getUriContent($url = '') { $redis = $GLOBALS['redis']; $cacheTTL = 3600; // 缓存时间:1小时 $hashKey = 'URL标题_hash'; // 模拟 TTL:判断是否需要刷新 $lastUpdate = (int) $redis->hGet($hashKey, 'update_time'); if (time() - $lastUpdate > $cacheTTL) { $files = glob(CONFIG_PATH . 'duan/b2_*'); if (!empty($files)) { foreach ($files as $file) { $cacheField = 'content_' . md5($file); $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (!empty($lines)) { $redis->hSet($hashKey, $cacheField, serialize($lines)); } } $redis->hSet($hashKey, 'files', serialize($files)); $redis->hSet($hashKey, 'update_time', time()); // 更新最后更新时间 } } // 去掉URL结尾的斜杠 $url = rtrim($url, '/'); // 提取标题关键词 $title = ''; if (preg_match('/-(.*?)$/is', $url, $matches)) { $titles = explode('.', $matches[1]); if (count($titles) >= 2) unset($titles[count($titles) - 1]); $titles = explode('-', implode('.', $titles)); while (preg_match('/^\d+$/', $titles[0])) { unset($titles[0]); $titles = array_values($titles); } while (preg_match('/^\d+$/', $titles[count($titles) - 1])) { unset($titles[count($titles) - 1]); $titles = array_values($titles); } $title = implode(' ', $titles); } // 如果没获取到标题,就随机一条内容作为标题 if (empty($title)) { $files = unserialize($redis->hGet($hashKey, 'files')); if (!empty($files)) { for ($i = 0; $i < 10; $i++) { $file = $files[array_rand($files)]; $cacheField = 'content_' . md5($file); $lines = $redis->hGet($hashKey, $cacheField); if ($lines !== false) { $lines = unserialize($lines); $bt2 = trim($lines[array_rand($lines)]); if (!empty($bt2)) { $title = $bt2; break; } } } } } return $title; }*/