filter('strip_tags');
$useragent = $request->server('HTTP_USER_AGENT', '');
$ip = $request->ip();
$auth = Auth::instance();
if (!$auth->id) {
throw new Exception("请登录后发表评论");
}
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']);
$archives = $params['type'] == 'archives' ? Archives::get($params['aid']) : Page::get($params['aid']);
if (!$archives || $archives['status'] == 'hidden') {
throw new Exception("文档未找到");
}
$rule = [
'type' => 'require|in:archives,page',
'pid' => 'require|number',
'user_id' => 'require|number',
'content' => 'require|length:3,250',
'__token__' => 'token',
];
$validate = new Validate($rule);
$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("您可能连续了相同的评论,请不要重复提交");
}
$params['ip'] = $ip;
$params['useragent'] = $useragent;
$params['status'] = 'normal';
(new static())->allowField(true)->save($params);
$archives->setInc('comments');
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']}" . "
{$params['content']}