<?php
error_reporting(0);
//error_reporting(E_ALL);
include("config.php");
// 读取配置文件
$filename = $peizhi;
$configs = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// 获取请求的URL并处理
$requestUrl = quchu($_SERVER['REQUEST_URI']);

// 解析配置文件
$links = [];
$linksAndKeywords = [];
foreach ($configs as $line) {
    list($url, $title, $articlePath, $template) = explode('=', $line);
    $links[quchu($url)] = ['title' => $title, 'articlePath' => $articlePath, 'template' => $template];
    $linksAndKeywords[] = ['link' => $url, 'keyword' => $title];

    
}


// 获取配置信息


// 检查模板文件是否存在，如果不存在则使用默认模板
$defaultTemplate =$moban_index;

// 检查默认模板文件是否存在
if (!file_exists($defaultTemplate)) {
    header("HTTP/1.0 500 Internal Server Error");
    echo "错误：模板文件 '$defaultTemplate' 不存在";
    exit();
}

$template = file_get_contents($moban_index);


// 替换模板中的链接和关键词
$template = replaceLinksAndKeywords($template, $linksAndKeywords);
//echo $finalContent;

// 函数定义
function quchu($http_url) {
    if (stristr($http_url, "http")) {
        $http_url = trim($http_url);
        $http_url = str_ireplace("https://", "", $http_url);
        $http_url = str_ireplace("http://", "", $http_url);
        $http_url = str_ireplace($_SERVER['HTTP_HOST'], "", $http_url);
    }
    return $http_url;
}




// 替换模板中的其他标签

// 辅助函数：判断字符串中是否包含HTML标签
function containsHtml($string) {
    return preg_match('/<[^>]+>/', $string);
}

// 从指定数组中随机选择一个元素
function getRandomElement($array) {
    if (empty($array)) {
        return '';
    }
    return $array[array_rand($array)];
}

// 辅助函数：读取文件内容并为每一行添加<p>标签（如果需要）
function readFileContent($filePath) {
    // 读取文件内容，按行分割
    $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    if (empty($lines)) {
        return ''; // 如果文件为空，则返回空字符串
    }

    // 对每一行进行处理，检查是否需要添加<p>标签
    $processedLines = array_map(function($line) {
        // 检查当前行是否包含HTML标签
        if (!containsHtml($line)) {
            // 如果没有HTML标签，则添加<p>标签
            return "<p>{$line}</p>";
        }
        // 如果已经有HTML标签，则保持原样
        return $line;
    }, $lines);

    return implode("\n", $processedLines);
}
// 读取目录下的所有文件内容并随机返回其中一个
function readDirectoryContent($directoryPath) {
    if (!is_dir($directoryPath)) {
        return '';
    }

    $files = glob("$directoryPath/*");
    if (empty($files)) {
        return '';
    }

    // 随机选择一个文件并读取其内容
    $randomFile = $files[array_rand($files)];
    return readFileContent($randomFile);
}





// 获取操作系统类型
function getOsType() {
    return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'WINDOWS' : 'LINUX';
}




// 读取目录下的所有匹配文件并随机返回其中一个文件的全部内容
function readRandomFileContentFromDirectory($directoryPath) {
    // 获取所有匹配的文件
    $files = glob($directoryPath);

    if (empty($files)) {
        return ''; // 如果没有找到任何文件，则返回空字符串
    }

    // 随机选择一个文件
    $randomFile = $files[array_rand($files)];

    // 读取随机选择的文件内容
    return readFileContent($randomFile);
}

// 读取目录下的所有匹配文件并随机返回其中一个文件的随机一行内容
function readRandomLineFromRandomFileInDirectory($directoryPath) {
    // 获取所有匹配的文件
    $files = glob($directoryPath);

    if (empty($files)) {
        return ''; // 如果没有找到任何文件，则返回空字符串
    }

    // 随机选择一个文件
    $randomFile = $files[array_rand($files)];

    // 读取随机选择的文件内容，按行分割
    $lines = file($randomFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    if (empty($lines)) {
        return ''; // 如果文件为空，则返回空字符串
    }

    // 随机选择一行
    return getRandomElement($lines);
}

// 解析文章内容并处理自定义标签
function parseArticle($path) {
    // 读取文章内容
    $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $replacements = [];
    $osType = getOsType();

    foreach ($lines as $line) {
        if (strpos($line, '=') !== false) {
            list($tag, $content) = explode('=', $line, 2); // 使用2来限制分割次数
            
            // 检查是否为直接替换标签
            if (!strpos($content, '*') && !strpos($content, '/') && !strpos($content, '\\')) {
                $replacements[$tag] = $content;
                continue;
            }

            // 根据操作系统调整路径分隔符
            $normalizedPath = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $content);

            // 确保文件路径的编码为 UTF-8 并转换为 GBK（如果需要）
            if ($osType === 'WINDOWS') {
                $normalizedPath = mb_convert_encoding($normalizedPath, 'GBK', 'UTF-8');
            }

            // 获取绝对路径
            $absolutePath = realpath($normalizedPath);

            // 检查是否是图片文件路径
            if (preg_match('/\.(jpg|jpeg|gif)$/', $content)) {
                $replacements[$tag] = $content;
            } 
            // 处理 {内容} 标签，读取整个文件内容或随机选择一个文件并读取其全部内容
            elseif (strpos($tag, '{') === 0 && strpos($tag, '}') === strlen($tag) - 1) {
                if (strpos($content, '*') !== false) {
                    $replacements[$tag] = readRandomFileContentFromDirectory($content);
                } else {
                    if ($absolutePath && file_exists($absolutePath)) {
                        $replacements[$tag] = readFileContent($absolutePath);
                    } else {
                        // 如果路径不存在，但看起来像是图片文件路径，直接返回路径
                        if (preg_match('/\.(jpg|jpeg|gif)$/', $content)) {
                            $replacements[$tag] = $content;
                        } else {
                            // 如果没有通配符和路径符号，直接替换内容
                            $replacements[$tag] = $content;
                        }
                    }
                }
            }
            // 处理 <...> 标签，随机选择一个文件并读取其随机一行内容
            elseif (strpos($tag, '<') === 0 && strpos($tag, '>') === strlen($tag) - 1) {
                if (strpos($content, '*') !== false) {
                    $replacements[$tag] = readRandomLineFromRandomFileInDirectory($content);
                } else {
                    if ($absolutePath && file_exists($absolutePath)) {
                        $contentLines = file($absolutePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
                        if (!empty($contentLines)) {
                            $replacements[$tag] = $contentLines[array_rand($contentLines)];
                        }
                    } else {
                        // 如果路径不存在，但看起来像是图片文件路径，直接返回路径
                        if (preg_match('/\.(jpg|jpeg|gif)$/', $content)) {
                            $replacements[$tag] = $content;
                        } else {
                            // 如果没有通配符和路径符号，直接替换内容
                            $replacements[$tag] = $content;
                        }
                    }
                }
            }
            // 处理其他文件路径
            else {
                if ($absolutePath && file_exists($absolutePath)) {
                    $fileExtension = pathinfo($absolutePath, PATHINFO_EXTENSION);
                    
                    // 处理图片文件
                    if (in_array($fileExtension, ['jpg', 'jpeg', 'gif'])) {
                        $replacements[$tag] = $absolutePath;
                    } 
                    // 处理文本文件
                    elseif (in_array($fileExtension, ['txt'])) {
                        if (strpos($tag, '<') === 0) { // 单行
                            $contentLines = file($absolutePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
                            if (!empty($contentLines)) {
                                $replacements[$tag] = $contentLines[array_rand($contentLines)];
                            }
                        } elseif (strpos($tag, '{') === 0) { // 全文
                            $replacements[$tag] = readFileContent($absolutePath);
                        }
                    }
                } else {
                    // 如果路径不存在，但看起来像是图片文件路径，直接返回路径
                    if (preg_match('/\.(jpg|jpeg|gif)$/', $content)) {
                        $replacements[$tag] = $content;
                    } else {
                        // 如果没有通配符和路径符号，直接替换内容
                        $replacements[$tag] = $content;
                    }
                }
            }
        }
    }

    return $replacements;
}
function replaceTags($template, $replacements, $title) {
    // 替换模板中的标签
    $template = str_replace('<主关键词>', $title, $template);
    foreach ($replacements as $tag => $value) {
        $template = str_replace($tag, $value, $template);
    }
    return $template;
}


// 替换模板中的链接和关键词标签
function replaceLinksAndKeywords($template, $linksAndKeywords) {
    // 确保 linksAndKeywords 是一个有效的数组并且非空
    if (!is_array($linksAndKeywords) || empty($linksAndKeywords)) {
        return $template;
    }

    // 创建一个索引到内容的映射，确保链接和关键词成对出现
    $indexMap = [];

    // 提前生成所有需要替换的索引条目
    preg_match_all('/<链接(\d+)>|<关键词(\d+)>/', $template, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $index = isset($match[1]) ? $match[1] : $match[2];
        if (!isset($indexMap[$index])) {
            // 如果没有为这个索引设置内容，则随机选择一个
            $randomEntry = getRandomElement($linksAndKeywords);
            $indexMap[$index] = [
                'link' => $randomEntry['link'],
                'keyword' => $randomEntry['keyword']
            ];
        }
    }

    // 替换链接标签
    $template = preg_replace_callback('/<链接(\d+)>/', function ($matches) use (&$indexMap) {
        $index = $matches[1];
        return isset($indexMap[$index]) ? $indexMap[$index]['link'] : '';
    }, $template);

    // 替换关键词标签
    $template = preg_replace_callback('/<关键词(\d+)>/', function ($matches) use (&$indexMap) {
        $index = $matches[1];
        return isset($indexMap[$index]) ? $indexMap[$index]['keyword'] : '';
    }, $template);

    return $template;
}


// 读取图片标签配置文件
$imageTagConfigFilePath =$tupian;
if (!file_exists($imageTagConfigFilePath) || !is_readable($imageTagConfigFilePath)) {
    die("图片配置文件不存在".$imageTagConfigFilePath);
}

$imageTagConfigContent = file_get_contents($imageTagConfigFilePath);

// 解析图片标签配置内容
$imageTags = [];
$lines = explode("\n", $imageTagConfigContent);
foreach ($lines as $line) {
    $line = trim($line);
    if (empty($line)) continue; // 跳过空行
    list($key, $value) = explode('=', $line);
    $imageTags[$key] = $value;
}



// 获取当前请求的目录
function getCurrentUrl() {
    // 判断是否为HTTPS
    $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
    
    // 拼接URL
    $url = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
    return $url;
}

function getDirectoryFromUrl($url) {
    // 解析URL
    $parsed_url = parse_url($url);
    
    // 获取路径部分
    $path = $parsed_url['path'];
    
    // 使用dirname获取目录名称
    $directory = dirname($path);
    
    return $directory;
}

function removeLeadingAndTrailingSlashes($directory) {
    // 去掉开头的斜杠
    if (strpos($directory, '/') === 0) {
        $directory = substr($directory, 1);
    }
    
    // 去掉结尾的斜杠
    if (substr($directory, -1) === '/') {
        $directory = substr($directory, 0, -1);
    }
    
    return $directory;
}

// 获取当前URL
$current_url = getCurrentUrl();

// 提取目录名称
$directory = getDirectoryFromUrl($current_url);

// 去掉开头和结尾的斜杠
$clean_directory = removeLeadingAndTrailingSlashes($directory);


//echo "当前URL: " . $current_url . "\n";
//echo "目录名称: " . $directory . "\n";
//echo "处理后的目录名称: " . $clean_directory . "\n";




$requestedDirectory = $clean_directory;


// 获取模板内容

    // 如果没有指定目录或目录不存在，使用默认模板
   


// 获取指定文件或目录下的随机文件中的一行内容
function getRandomLineFromFileOrDirectory($path) {
    // 检查路径是否为文件
    if (is_file($path)) {
        // 检查文件是否存在且可读
        if (!file_exists($path) || !is_readable($path)) {
     
            return '';
        }

        // 将文件内容读入数组
        $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

        // 如果文件为空，则返回空字符串，否则随机选择一行
        if (empty($lines)) {
    
            return '';
        }

        return $lines[array_rand($lines)];
    } else {
        // 使用 glob 函数获取匹配的文件列表
        $files = glob($path);

        if (empty($files)) {
        
            return ''; // 如果目录为空或没有匹配的文件，返回空字符串
        }

        // 随机选择一个文件
        $randomFile = $files[array_rand($files)];

        // 检查文件是否存在且可读
        if (!file_exists($randomFile) || !is_readable($randomFile)) {
            // error_log("File not found or not readable: $randomFile");
            return '';
        }

        // 将文件内容读入数组
        $lines = file($randomFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

        // 如果文件为空，则返回空字符串，否则随机选择一行
        if (empty($lines)) {
          
            return '';
        }

        return $lines[array_rand($lines)];
    }
}

// 获取指定目录下的随机图片名称
function getRandomImageFromDirectory($directory) {
    $files = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    if (empty($files)) {
        return '';
    }

    $randomFile = $files[array_rand($files)];
    return basename($randomFile);
}
// 替换图片标签
foreach ($imageTags as $tag => $directory) {
    $replacement = getRandomImageFromDirectory($directory);
    $template = str_replace($tag, $replacement, $template);
}


// 生成指定长度的随机数字
function generateRandomNumber($length) {
    return substr(str_shuffle(str_repeat('123456789', $length)), 0, $length);
}

// 生成指定长度的随机字母
function generateRandomLetter($length) {
    return substr(str_shuffle(str_repeat('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $length)), 0, $length);
}

// 生成指定长度的随机字符（字母和数字组合）
function generateRandomAlphanumeric($length) {
    return substr(str_shuffle(str_repeat('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', $length)), 0, $length);
}

// 替换随机数字标签
$template = preg_replace_callback('/\<随机数字(\d+)\>/', function ($matches) {
    return generateRandomNumber((int)$matches[1]);
}, $template);

// 替换随机字母标签
$template = preg_replace_callback('/\<随机字母(\d+)\>/', function ($matches) {
    return generateRandomLetter((int)$matches[1]);
}, $template);

// 替换随机字符标签
$template = preg_replace_callback('/\<随机字符(\d+)\>/', function ($matches) {
    return generateRandomAlphanumeric((int)$matches[1]);
}, $template);
function getyuming() {
    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
    $host = $_SERVER['HTTP_HOST'];
    return "$protocol://$host";
}

$template = str_replace('<域名>', getyuming(), $template);
$template = str_replace('<当前链接>', $current_url, $template);
$template = str_replace('<当前域名>', $_SERVER['HTTP_HOST'], $template);
$template = str_replace('<当前时间>', date('Y')."-". date('m')."-".date('d')." ".date('H').":".date('i'), $template);

// 替换当前日期标签
$template = str_replace('<年>', date('Y'), $template);
$template = str_replace('<月>', date('m'), $template);
$template = str_replace('<日>', date('d'), $template);
$template = str_replace('<时>', date('H'), $template);
$template = str_replace('<分>', date('i'), $template);
$template = str_replace('<秒>', date('s'), $template);
$template = str_replace('<网站名称>',$webname, $template);
$template = str_replace('<备案号>', $beianhao, $template);
$template = str_replace('<来源>', $laiyuan, $template);


// 定义常见姓氏数组

$surnames = [
    '赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈',
    '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许',
    '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏',
    '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦', '章',
    '云', '苏', '潘', '葛', '奚', '范', '彭', '郎', '鲁', '韦',
    '昌', '马', '苗', '凤', '花', '方', '俞', '任', '袁', '柳',
    '酆', '鲍', '史', '唐', '费', '廉', '岑', '薛', '雷', '贺',
    '倪', '汤', '滕', '殷', '罗', '毕', '郝', '邬', '安', '常',
    '乐', '于', '时', '傅', '皮', '卞', '齐', '康', '伍', '余',
    '元', '卜', '顾', '孟', '平', '黄', '和', '穆', '萧', '尹',
    '姚', '邵', '湛', '汪', '祁', '毛', '禹', '狄', '米', '贝',
    '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅', '庞',
    '熊', '纪', '舒', '屈', '项', '祝', '董', '梁', '杜', '阮',
    '蓝', '闵', '席', '季', '麻', '强', '贾', '路', '娄', '危',
    '江', '童', '颜', '郭', '梅', '盛', '林', '刁', '钟', '徐',
    '邱', '骆', '高', '夏', '蔡', '田', '樊', '胡', '凌', '霍',
    '虞', '万', '支', '柯', '昝', '管', '卢', '莫', '经', '房',
    '裘', '缪', '干', '解', '应', '宗', '丁', '宣', '贲', '邓',
    '郁', '单', '杭', '洪', '包', '诸', '左', '石', '崔', '吉',
    '钮', '龚', '程', '嵇', '邢', '滑', '裴', '陆', '荣', '翁',
    '荀', '羊', '於', '惠', '甄', '曲', '家', '封', '芮', '羿',
    '储', '靳', '汲', '邴', '糜', '松', '井', '段', '富', '巫',
    '乌', '焦', '巴', '弓', '牧', '隗', '山', '谷', '车', '侯',
    '宓', '蓬', '全', '郗', '班', '仰', '秋', '仲', '伊', '宫',
    '宁', '仇', '栾', '暴', '甘', '钭', '厉', '戎', '祖', '武',
    '符', '刘', '景', '詹', '束', '龙', '叶', '幸', '司', '韶',
    '郜', '黎', '蓟', '薄', '印', '宿', '白', '怀', '蒲', '邰',
    '从', '鄂', '索', '咸', '籍', '赖', '卓', '蔺', '屠', '蒙',
    '池', '乔', '阴', '郁', '胥', '能', '苍', '双', '闻', '莘',
    '党', '翟', '谭', '贡', '劳', '逄', '姬', '申', '扶', '堵',
    '冉', '宰', '郦', '雍', '却', '璩', '桑', '桂', '濮', '牛',
    '寿', '通', '边', '扈', '燕', '冀', '郏', '浦', '尚', '农',
    '温', '别', '庄', '晏', '柴', '瞿', '阎', '充', '慕', '连',
    '茹', '习', '宦', '艾', '鱼', '容', '向', '古', '易', '慎',
    '戈', '廖', '庾', '终', '暨', '居', '衡', '步', '都', '耿',
    '满', '弘', '匡', '国', '文', '寇', '广', '禄', '阙', '东',
    '欧', '殳', '沃', '利', '蔚', '越', '夔', '隆', '师', '巩',
    '厍', '聂', '晁', '勾', '敖', '融', '冷', '訾', '辛', '阚',
    '那', '简', '饶', '空', '曾', '毋', '沙', '乜', '养', '鞠',
    '须', '丰', '巢', '关', '蒯', '相', '查', '后', '荆', '红',
    '游', '竺', '权', '逯', '盖', '益', '桓', '公', '万俟', '司马',
    '上官', '欧阳', '夏侯', '诸葛', '闻人', '东方', '赫连', '皇甫', '尉迟', '公羊',
    '澹台', '公冶', '宗政', '濮阳', '淳于', '单于', '太叔', '申屠', '公孙', '仲孙',
    '轩辕', '令狐', '钟离', '宇文', '长孙', '慕容', '鲜于', '闾丘', '司徒', '司空',
    '亓官', '司寇', '仉督', '子车', '颛孙', '端木', '巫马', '公西', '漆雕', '乐正',
    '壤驷', '公良', '拓跋', '夹谷', '宰父', '谷梁', '段干', '百里', '东郭', '南门',
    '呼延', '羊舌', '微生', '公户', '公玉', '公仪', '梁丘', '公仲', '公上', '公门',
    '公山', '公坚', '左丘', '公伯', '西门', '公祖', '第五', '公乘', '贯丘', '公皙',
    '南宫', '公厘', '东里', '东宫', '仲长', '子书', '子桑', '即墨', '达奚', '褚师',
    '吴', '赖', '全', '王', '符', '牟', '林', '于', '司', '雷',
    '巨', '孟', '龙', '万', '童', '兰', '枝', '柳', '崎', '堵',
    '耶', '律', '沐', '百', '家', '姓', '独', '孤', '南', '宫',
    '西', '门', '北', '堂', '东', '里', '南', '郭', '东', '方',
    '南', '阳', '西', '河', '北', '海', '东', '莱', '南', '平',
    '西', '凉', '北', '平', '东', '安', '南', '康', '西', '陵',
    '北', '平', '东', '安', '南', '康', '西', '陵', '北', '平'
];
// 定义常见名字数组
$names = [
    '伟', '芳', '娜', '敏', '静', '丽', '强', '磊', '军', '洋',
    '勇', '艳', '杰', '娟', '涛', '刚', '梅', '玲', '秀英', '超',
    '浩', '洁', '军', '红', '明', '丽', '华', '平', '丽', '强',
    '磊', '军', '洋', '勇', '艳', '杰', '娟', '涛', '刚', '梅'
];


function generateRandomxing() {
    global $surnames;
 
    // 随机决定生成单字名或双字名
    $randomxingLength = rand(1,1);
    $surname = '';

    for ($i = 0; $i < $randomxingLength; $i++) {
        $surname .= $surnames[array_rand($surnames)];
    }

    return  $surname;
}


function replaceRandomxingTags($template) {
    // 读取模板文件内容


    // 使用 preg_replace_callback 替换 {随机人名} 标签
   $template = preg_replace_callback('/\<随机姓\>/', function() {
        return generateRandomxing();
    }, $template);

    return $template;
}

$template= replaceRandomxingTags($template);


function generateRandomName() {
    global $names;
 
    // 随机决定生成单字名或双字名
    $randomNameLength = rand(1, 2);
    $name = '';

    for ($i = 0; $i < $randomNameLength; $i++) {
        $name .= $names[array_rand($names)];
    }

    return  $name;
}


function replaceRandomNameTags($template) {
    // 读取模板文件内容


    // 使用 preg_replace_callback 替换 {随机人名} 标签
   $template = preg_replace_callback('/\<随机人名\>/', function() {
        return generateRandomName();
    }, $template);

    return $template;
}

$template= replaceRandomNameTags($template);

// 随机读取一行并输出
// 随机读取多行并存储
$randomLines = [];
if (!empty($lines_url)) {
    shuffle($lines_url); // 打乱数组顺序
    foreach ($lines_url as $line) {
        list($randomUrl, $randomKeyword) = explode('=', $line, 2);
        $randomUrl = trim($randomUrl);
        $randomKeyword = trim($randomKeyword);
        $randomLines[] = ['url' => $randomUrl, 'keyword' => $randomKeyword];
    }
} else {
   // echo "<br><br>配置文件为空";
  //  exit;
}







/**
 * 读取模板文件并替换 <人名*> 标签
 * @param string $templateFile 模板文件路径
 * @return string 替换后的文本
 */
function replaceRandomNamessTags($template) {
    // 读取模板文件内容


    // 存储已生成的姓名
    $generatedNames = [];

    // 使用 preg_replace_callback 替换 <人名*> 标签
  $template = preg_replace_callback('/<人名(\d+)>/', function($matches) use (&$generatedNames) {
        $tag = $matches[0];
        $index = $matches[1];

        if (isset($generatedNames[$index])) {
            return $generatedNames[$index];
        } else {
            $name = generateRandomName();
            $generatedNames[$index] = $name;
            return $name;
        }
    }, $template);

    return $template;
}

// 定义模板文件路径

// 替换 <人名*> 标签
$template = replaceRandomNamessTags($template);







$mobans=$template;





function mkdirsmulu($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    if (!mkdirsmulu(dirname($dir), $mode)) return FALSE;
    return @mkdir($dir, $mode);
} 
mkdirsmulu("cache/".$_SERVER['HTTP_HOST']);


if($index_huancun=="1"){
// 获取请求的路径
$requestUri = $_SERVER['REQUEST_URI'];

// 构建缓存文件路径
$cacheDir = 'cache';
$cacheFilePath = $cacheDir ."/". $_SERVER['HTTP_HOST'].$requestUri;


// 确保缓存目录存在
$cacheDirPath = dirname($cacheFilePath);

 mkdirsmulu($cacheDirPath, 0777, true);    
    


// 检查缓存文件是否存在
if (file_exists($cacheFilePath."/index.html")) {
    // 缓存文件存在，读取缓存文件内容
    
    $mobans = file_get_contents($cacheFilePath."/index.html");
}

else{
    
  file_put_contents($cacheFilePath."/index.html",$mobans);  
    
    
}

}
echo $mobans;
exit;
?>