server('HTTP_USER_AGENT', ''), 0, 255);
$ip = $request->ip(0, false);
$auth = Auth::instance();
$content = $params['content'];
if (!$auth->id) {
throw new Exception("请登录后发表评论");
}
if ($auth->score < $config['limitscore']['postcomment']) {
throw new Exception("积分必须大于{$config['limitscore']['postcomment']}才可以发表评论");
}
if (!isset($params['aid']) || !isset($params['content'])) {
throw new Exception("内容不能为空");
}
$params['user_id'] = $auth->id;
$params['type'] = isset($params['type']) ? $params['type'] : 'archives';
$params['content'] = nl2br($params['content']);
$params['content'] = preg_replace("/(@([\s\S]*?))\s+/i", '$1 ', $params['content']);
$archives = $params['type'] == 'archives' ? Archives::get($params['aid']) : ($params['type'] == 'special' ? Special::get($params['aid']) : Page::get($params['aid']));
if (!$archives || $archives['status'] == 'hidden') {
throw new Exception("文档未找到或正在审核");
}
if (!$archives['iscomment']) {
throw new Exception("文档评论功能已关闭");
}
$rule = [
'type' => 'require|in:archives,page,special',
'pid' => 'require|number',
'user_id' => 'require|number',
'content|内容' => 'require|length:3,250',
'__token__' => 'require|token',
];
$message = [
'content.length' => '评论最少输入3个字符'
];
$validate = new Validate($rule, $message);
$result = $validate->check($params);
if (!$result) {
throw new Exception($validate->getError());
}
//查找最后评论
$lastComment = self::where(['type' => $params['type'], 'aid' => $params['aid'], 'ip' => $ip])->order('id', 'desc')->find();
if ($lastComment && time() - $lastComment['createtime'] < 30) {
throw new Exception("对不起!您发表评论的速度过快!");
}
if ($lastComment && $lastComment['content'] == $params['content']) {
throw new Exception("您可能连续了相同的评论,请不要重复提交");
}
//审核状态
$status = 'normal';
if ($config['iscommentaudit'] == 1) {
$status = 'hidden';
} elseif ($config['iscommentaudit'] == 0) {
$status = 'normal';
} elseif ($config['iscommentaudit'] == -1) {
if (!Service::isContentLegal($content)) {
$status = 'hidden';
}
}
$params['ip'] = $ip;
$params['useragent'] = $useragent;
$params['status'] = $status;
Db::startTrans();
try {
$model = new static();
$model->allowField(true)->save($params);
//评论正常则增加积分和统计
if ($status == 'normal') {
$archives->setInc('comments');
//增加积分
$status == 'normal' && User::score($config['score']['postcomment'], $auth->id, '发表评论');
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
throw new Exception("发表评论失败");
}
//发送通知
if ($status === 'hidden') {
Service::notice('CMS收到一条待审核评论', $config['auditnotice'], $config['noticetemplateid']);
throw new CommentException("发表评论成功,但评论需要显示审核后才会展示", 1);
}
if (isset($params['pid'])) {
//查找父评论,是否并发邮件通知
$parentComment = self::get($params['pid'], 'user');
if ($parentComment && $parentComment['subscribe'] && Validate::is($parentComment->user->email, 'email')) {
$domain = $request->domain();
$config = get_addon_config('cms');
$title = "{$parentComment->user->nickname},您发表在《{$archives['title']}》上的评论有了新回复 - {$config['sitename']}";
$archivesurl = $domain . $archives['url'];
$unsubscribe_url = addon_url("cms/comment/unsubscribe", ['id' => $parentComment['id'], 'key' => md5($parentComment['id'] . $parentComment->user->email)], true, true);
$content = "亲爱的{$parentComment->user->nickname}:
您于" . date("Y-m-d H:i:s") .
"在《{$archives['title']}》上发表的评论
{$parentComment['content']}" . "