解决AI小编文库详情页显示HTML源码问题详细步骤(1)
2026/05/02 08:33:25
解决文库详情页显示HTML源码问题详细步骤问题现象
function cleanDangerousCSS_7ree($content) { // 移除所有内联样式(包括HTML实体编码的样式) $content = preg_replace('/\s*style\s*=\s*"[^"]*"/i', '', $content); $content = preg_replace('/\s*style\s*=\s*"[^&]*"/i', '', $content); $content = preg_replace('/\s*style\s*=\s*'[^&]*'/i', '', $content); // 移除可能导致布局错乱的CSS属性 $content = preg_replace('/position\s*:\s*[^;"]+[;"]/i', '', $content); $content = preg_replace('/display\s*:\s*flex[^;"]*[;"]/i', '', $content); $content = preg_replace('/width\s*:\s*\d+px[;"]/i', '', $content); $content = preg_replace('/margin-left\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/margin-right\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/margin-top\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/margin-bottom\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/z-index\s*:\s*\d+[;"]/i', '', $content); $content = preg_replace('/float\s*:\s*[^;"]+[;"]/i', '', $content); $content = preg_replace('/<svg[^>]*>.*?<\/svg>/is', '', $content); $content = preg_replace('/<section[^>]*>\s*<\/section>/is', '', $content); $content = preg_replace('/<span[^>]*>\s*<\/span>/is', '', $content); return $content;}
在AI文库详情页(plugin.php?id=aieditor_7ree:library_7ree&option=1&tid=xxx)中,论坛手动发布的帖子内容显示为HTML源代码(如 <p>...),而不是正常渲染的格式化文本。
原因分析- Discuz编辑器存储机制:用户通过论坛编辑器发布的帖子,其中的HTML标签会被实体编码存储(< 变为 <,> 变为 >),以防止XSS攻击。
- 插件未正确解码:文库详情页的PHP代码(page_7ree.php)在读取帖子内容后,直接传递给模板,没有将HTML实体解码还原。
- 模板引擎二次转义:Discuz模板引擎为安全考虑,会自动对变量输出进行转义,导致已解码的HTML再次变成实体。
- 游客与登录状态处理不一致:游客状态下会截取部分内容,截取前的处理流程与登录状态不同,导致游客看到的内容可能仍包含未解码的HTML。
修改文件:source/plugin/aieditor_7ree/include/library_7ree/page_7ree.php
第2步:添加循环解码与反转义函数在文件末尾 ?> 之前添加以下清理危险CSS样式的函数(用于登录状态内容):
phpfunction cleanDangerousCSS_7ree($content) { // 移除所有内联样式(包括HTML实体编码的样式) $content = preg_replace('/\s*style\s*=\s*"[^"]*"/i', '', $content); $content = preg_replace('/\s*style\s*=\s*"[^&]*"/i', '', $content); $content = preg_replace('/\s*style\s*=\s*'[^&]*'/i', '', $content); // 移除可能导致布局错乱的CSS属性 $content = preg_replace('/position\s*:\s*[^;"]+[;"]/i', '', $content); $content = preg_replace('/display\s*:\s*flex[^;"]*[;"]/i', '', $content); $content = preg_replace('/width\s*:\s*\d+px[;"]/i', '', $content); $content = preg_replace('/margin-left\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/margin-right\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/margin-top\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/margin-bottom\s*:\s*-\d+px[;"]/i', '', $content); $content = preg_replace('/z-index\s*:\s*\d+[;"]/i', '', $content); $content = preg_replace('/float\s*:\s*[^;"]+[;"]/i', '', $content); $content = preg_replace('/<svg[^>]*>.*?<\/svg>/is', '', $content); $content = preg_replace('/<section[^>]*>\s*<\/section>/is', '', $content); $content = preg_replace('/<span[^>]*>\s*<\/span>/is', '', $content); return $content;}