<?php
/**
* Plugin Name: 登录&回复可见隐藏内容-隐藏摘要修复版
* Version: 1.7.0
* Author: 甜橙酱
* Description: 彻底修复已评论内容登录后不显示问题,新增登录注册弹窗功能
*/
if(!defined('EMLOG_ROOT')) {exit('error!');}
// 核心:检查用户是否评论过该文章(增加兼容性处理)
function content_lv_cv_user($logid){
// 1. 确保用户已登录且会话有效
if (empty($_SESSION['uid']) && empty($_SESSION['user']['uid'])) {
return 0; // 会话中无用户ID,直接返回未评论
}
// 兼容不同环境的用户ID存储方式(可能是$_SESSION['uid']或$_SESSION['user']['uid'])
$uid = intval($_SESSION['uid'] ?? $_SESSION['user']['uid'] ?? 0);
if ($uid <= 0) {
return 0;
}
$db = Database::getInstance();
$logid = intval($logid);
// 2. 兼容评论表可能的字段差异(尝试两种常见字段名:uid/userid)
$sql = "SELECT cid FROM " . DB_PREFIX . "comment
WHERE gid = {$logid}
AND (uid = {$uid} OR userid = {$uid}) -- 兼容不同字段名
AND status = 1 -- 仅有效评论
LIMIT 1";
// 3. 执行查询并验证结果
$row = $db->once_fetch_array($sql);
return $row ? 1 : 0;
}
// 清理摘要中的隐藏内容标签
function content_lv_cv_x(){
$content = ob_get_clean();
$content = preg_replace("/\[[c|l]v\](.*?)\[\/[c|l]v\]/is", '', $content);
ob_start();
echo $content;
}
// 处理内容显示逻辑(重构判断逻辑)
function content_lv_cv($logData){
if(preg_match('/\[[c|l]v\](.*?)\[\/[c|l]v\]/is', $logData['log_content'])){
$logid = $logData['logid'];
$content = ob_get_clean();
// 调用主要处理函数
$content = content_lv_cv_log($logData, $content);
ob_start();
echo $content;
}
}
// 主要内容处理函数(核心修改:移除提示框内链接下划线)
function content_lv_cv_log($logData, $content = null){
$logid = $logData['logid'];
if($content === null) {
$content = $logData['log_content'];
}
// CSS样式(关键:增加 .cl_content a { text-decoration: none; } 移除链接下划线)
$css = '<style type="text/css">
.cl_content {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 15px 20px;
border-radius: 8px;
margin: 15px 0;
text-align: center;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
transition: all 0.3s ease;
cursor: pointer;
}
.cl_content:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}
/* 关键修改:移除未登录提示框内所有链接的下划线 */
.cl_content a,
.cl_content span {
color: #FFFFFF;
font-weight: bold;
text-decoration: none !important; /* !important 确保覆盖其他样式 */
}
/* 可选:hover时也不显示下划线(避免鼠标悬浮时出现) */
.cl_content a:hover,
.cl_content span:hover {
text-decoration: none !important;
}
.unlocked-cv, .unlocked-lv {
background: linear-gradient(135deg, #28a745, #20c997 100%);
border: 1px solid #e9ecef;
border-radius: 8px;
padding: 15px;
margin: 15px 0;
}
.login-status-bar {
background: linear-gradient(135deg, #28a745, #20c997);
color: white;
padding: 10px 15px;
border-radius: 6px;
margin: 10px 0;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 14px;
}
.cl_content img {
display: inline-block;
vertical-align: middle;
height: 20px;
width: 20px;
}
/* 保留登录状态栏的链接下划线(不影响其他区域) */
.login-status-bar a {
color: #059669;
text-decoration: underline;
}
</style>';
// 登录状态提示
$login提示 = '';
if(ISLOGIN){
$login提示 = '<div class="login-status-bar">
<div class="user-info">您已登录,可以查看所有登录可见内容</div>
<a href="'.BLOG_URL.'admin/account.php?action=logout">退出登录</a>
</div>';
}
// 先添加CSS和登录提示(移除自定义弹窗HTML)
$content = $css . $login提示 . $content;
// 处理评论可见内容(点击登录调用Ixc_login_but_login())
if(preg_match('/\[cv\](.*?)\[\/cv\]/is', $content,$temp)){
$isCommented = false;
$cookieName = "comment_cv_{$logid}";
// 仅登录用户验证评论权限
if(ISLOGIN){
// 1. 先查数据库(最权威,兼容不同存储方式)
$dbCheck = content_lv_cv_user($logid);
// 2. 再查Cookie(解决数据库查询延迟)
$cookieCheck = isset($_COOKIE[$cookieName]) ? 1 : 0;
// 3. 管理员直接通过
$adminCheck = ROLE == "admin" ? 1 : 0;
// 任一条件满足即视为已评论(强制逻辑:不短路)
$isCommented = ($dbCheck == 1) || ($cookieCheck == 1) || ($adminCheck == 1);
}
// 显示处理(确保无多余换行)
if($isCommented){
$content = preg_replace("/\[cv\](.*?)\[\/cv\]/is", '<div class="unlocked-cv" data-type="cv">\1</div>', $content);
}else{
if(ISLOGIN){
// 已登录但未评论(滚动到评论区)
$content = preg_replace("/\[cv\](.*?)\[\/cv\]/is", '
<div class="cl_content" data-type="cv" data-logid="'.$logid.'" onclick="scrollToComment()">
<img src="https://xianyida.ysxdo.com/content/uploadfile/202508/d0741756030971.png">
此处内容已隐藏,<span>评论</span>后即可查看!
</div>', $content);
}else{
// 未登录(调用Ixc_login_but_login()弹窗)
$content = preg_replace("/\[cv\](.*?)\[\/cv\]/is", '
<div class="cl_content" data-type="cv" onclick="javascript:Ixc_login_but_login();" data-logid="'.$logid.'">
<img src="https://xianyida.ysxdo.com/content/uploadfile/202508/d0741756030971.png">
此处内容已隐藏,<span>登录</span>评论即可查看
</div>', $content);
}
}
}
// 处理登录可见内容(点击直接调用Ixc_login_but_login())
if(preg_match("/\[lv\](.*?)\[\/lv\]/is", $content,$temp)){
if(ISLOGIN){
$content = preg_replace("/\[lv\](.*?)\[\/lv\]/is", '<div class="unlocked-lv" data-type="lv">\1</div>', $content);
}else{
$content = preg_replace("/\[lv\](.*?)\[\/lv\]/is", '
<div class="cl_content" data-type="lv" onclick="javascript:Ixc_login_but_login();" style="cursor: pointer;">
<img src="https://xianyida.ysxdo.com/content/uploadfile/202508/d0741756030971.png">
此处内容登录后查看,<span>点击此处登录</span>
</div>', $content);
}
}
// 前端逻辑(保留Ixc_login_but_login()调用)
$content .= '<script>
// 全局变量声明
var EMLOG_ISLOGIN = '.( ISLOGIN ? 'true' : 'false' ).';
var EMLOG_LOGID = "'.$logid.'";
// 滚动到评论区(修复评论可见逻辑)
function scrollToComment(){
try {
var commentSection = document.querySelector("#comment") || document.querySelector(".comment") || document.querySelector("[id*=comment]") || document.querySelector("#commentform");
if(commentSection){
commentSection.scrollIntoView({behavior: "smooth"});
// 聚焦评论框
var commentInput = commentSection.querySelector("textarea") || commentSection.querySelector("input[type=text]");
if(commentInput) commentInput.focus();
} else {
alert("请向下滚动到评论区发表评论");
}
} catch (e) {
alert("请向下滚动到评论区发表评论");
}
}
// 页面加载完成后执行(自动弹窗:调用Ixc_login_but_login())
document.addEventListener("DOMContentLoaded", function(){
// 检查用户登录状态和隐藏内容
var loginVisibleContent = document.querySelectorAll(".cl_content[data-type=lv]");
var commentVisibleContent = document.querySelectorAll(".cl_content[data-type=cv]");
// 未登录且有隐藏内容时,自动调用Ixc_login_but_login()弹窗
if(!EMLOG_ISLOGIN && (loginVisibleContent.length > 0 || commentVisibleContent.length > 0)){
console.log("检测到隐藏内容,自动唤起登录弹窗");
// 延迟1.5秒弹窗,避免影响页面加载
setTimeout(function(){
try {
// 优先调用Ixc_login_but_login()原生弹窗
if(typeof Ixc_login_but_login === "function"){
Ixc_login_but_login();
} else {
console.warn("Ixc_login_but_login()函数未找到,使用默认登录跳转");
window.location.href = "'.BLOG_URL.'admin/login.php?redirect="+encodeURIComponent(window.location.href);
}
} catch (e) {
console.error("唤起登录弹窗失败:", e);
window.location.href = "'.BLOG_URL.'admin/login.php?redirect="+encodeURIComponent(window.location.href);
}
}, 1500);
}
// 评论成功后刷新页面(确保Cookie生效)
if(typeof window.IxcComment !== "undefined" && typeof window.IxcComment.afterSubmit === "function"){
var originalAfterSubmit = window.IxcComment.afterSubmit;
window.IxcComment.afterSubmit = function(data){
originalAfterSubmit(data);
// 评论成功后1秒刷新,显示隐藏内容
setTimeout(function(){
window.location.reload();
}, 1000);
};
}
});
</script>';
return $content;
}
// 评论成功后强制同步Cookie到服务器(核心修复)
function comment_saved_cv(){
if(ISLOGIN && !empty($_POST['gid'])){
$logid = intval($_POST['gid']);
$cookieName = "comment_cv_{$logid}";
// 1. 设置Cookie(3天有效期)
setcookie($cookieName, 1, time() + 259200, "/");
// 2. 强制当前请求中$_COOKIE生效(解决刷新不读取新Cookie)
$_COOKIE[$cookieName] = 1;
// 3. 主动更新数据库查询缓存(如果有)
content_lv_cv_user($logid);
}
}
// 编辑器按钮(保持不变)
function content_lv_cv_Button(){
echo ' <a id="Button_lv" style="margin-right:10px;padding:3px 8px;border:1px solid #ccc;border-radius:3px;">登录可见</a>
<a id="Button_cv" style="padding:3px 8px;border:1px solid #ccc;border-radius:3px;">评论可见</a>
<script>
$(document).ready(function(){
$("#Button_lv").click(function(){
Editor.insertValue("[lv]这里替换成需要登录可见的内容[/lv]");
});
$("#Button_cv").click(function(){
Editor.insertValue("[cv]这里替换成需要评论可见的内容[/cv]");
});
});
</script>';
}
// 注册钩子(保持不变)
addAction('adm_writelog_head', 'content_lv_cv_Button');
addAction('index_head','content_lv_cv_x');
addAction('log_related','content_lv_cv');
addAction('comment_saved', 'comment_saved_cv');
?>