我之前写过《Typecho不运用插件完成Ajax批评功用》这篇文章,文章中对绛木子的函数完美了批评触发的接口,然则批评过滤部份的接口没有完美,只是纯真的给出了替换计划,实在很早之前就处理了,只是一向没有写出来,本日就把完美好的函数放出来。
完美好的函数
/** * ajaxComment * 完成Ajax批评的要领(完成feedback中的comment功用) * @param Widget_Archive $archive * @return void */function ajaxComment($archive){ $options = Helper::options(); $user = Typecho_Widget::widget('Widget_User'); $db = Typecho_Db::get(); // Security 考证不通过时会直接跳转,所以须要本身举行推断 // 须要开启反垃圾庇护,此时将不考证泉源 if($archive->request->get('_') != Helper::security()->getToken($archive->request->getReferer())){ $archive->response->throwJson(array('status'=>0,'msg'=>_t('不法要求'))); } /** 批评封闭 */ if(!$archive->allow('comment')){ $archive->response->throwJson(array('status'=>0,'msg'=>_t('批评已封闭'))); } /** 搜检ip批评距离 */ if (!$user->pass('editor', true) && $archive->authorId != $user->uid && $options->commentsPostIntervalEnable){ $latestComment = $db->fetchRow($db->select('created')->from('table.comments') ->where('cid = ?', $archive->cid) ->where('ip = ?', $archive->request->getIp()) ->order('created', Typecho_Db::SORT_DESC) ->limit(1)); if ($latestComment && ($options->gmtTime - $latestComment['created'] > 0 && $options->gmtTime - $latestComment['created'] < $options->commentsPostInterval)) { $archive->response->throwJson(array('status'=>0,'msg'=>_t('对不起, 您的谈话过于频仍, 请稍侯再次宣布'))); } } $comment = array( 'cid' => $archive->cid, 'created' => $options->gmtTime, 'agent' => $archive->request->getAgent(), 'ip' => $archive->request->getIp(), 'ownerId' => $archive->author->uid, 'type' => 'comment', 'status' => !$archive->allow('edit') && $options->commentsRequireModeration ? 'waiting' : 'approved' ); /** 推断父节点 */ if ($parentId = $archive->request->filter('int')->get('parent')) { if ($options->commentsThreaded && ($parent = $db->fetchRow($db->select('coid', 'cid')->from('table.comments') ->where('coid = ?', $parentId))) && $archive->cid == $parent['cid']) { $comment['parent'] = $parentId; } else { $archive->response->throwJson(array('status'=>0,'msg'=>_t('父级批评不存在'))); } } $feedback = Typecho_Widget::widget('Widget_Feedback'); //磨练花样 $validator = new Typecho_Validate(); $validator->addRule('author', 'required', _t('必需填写用户名')); $validator->addRule('author', 'xssCheck', _t('请不要在用户名中运用特别字符')); $validator->addRule('author', array($feedback, 'requireUserLogin'), _t('您所运用的用户名已被注册,请登录后再次提交')); $validator->addRule('author', 'maxLength', _t('用户名最多包括200个字符'), 200); if ($options->commentsRequireMail && !$user->hasLogin()) { $validator->addRule('mail', 'required', _t('必需填写电子邮箱地点')); } $validator->addRule('mail', 'email', _t('邮箱地点不合法')); $validator->addRule('mail', 'maxLength', _t('电子邮箱最多包括200个字符'), 200); if ($options->commentsRequireUrl && !$user->hasLogin()) { $validator->addRule('url', 'required', _t('必需填写个人主页')); } $validator->addRule('url', 'url', _t('个人主页地点花样毛病')); $validator->addRule('url', 'maxLength', _t('个人主页地点最多包括200个字符'), 200); $validator->addRule('text', 'required', _t('必需填写批评内容')); $comment['text'] = $archive->request->text; /** 对平常匿名访问者,将用户数据保留一个月 */ if (!$user->hasLogin()) { /** Anti-XSS */ $comment['author'] = $archive->request->filter('trim')->author; $comment['mail'] = $archive->request->filter('trim')->mail; $comment['url'] = $archive->request->filter('trim')->url; /** 修改用户提交的url */ if (!empty($comment['url'])) { $urlParams = parse_url($comment['url']); if (!isset($urlParams['scheme'])) { $comment['url'] = 'http://' . $comment['url']; } } $expire = $options->gmtTime + $options->timezone + 30*24*3600; Typecho_Cookie::set('__typecho_remember_author', $comment['author'], $expire); Typecho_Cookie::set('__typecho_remember_mail', $comment['mail'], $expire); Typecho_Cookie::set('__typecho_remember_url', $comment['url'], $expire); } else { $comment['author'] = $user->screenName; $comment['mail'] = $user->mail; $comment['url'] = $user->url; /** 纪录登录用户的id */ $comment['authorId'] = $user->uid; } /** 批评者之前须有批评通过了考核 */ if (!$options->commentsRequireModeration && $options->commentsWhitelist) { if ($feedback->size($feedback->select()->where('author = ? AND mail = ? AND status = ?', $comment['author'], $comment['mail'], 'approved'))) { $comment['status'] = 'approved'; } else { $comment['status'] = 'waiting'; } } if ($error = $validator->run($comment)) { $archive->response->throwJson(array('status'=>0,'msg'=> implode(';',$error))); }//批评历程的插件接口,平常用于过滤垃圾批评的插件try { $comment = $feedback->pluginHandle()->comment($comment, $feedback->_content);} catch (Typecho_Exception $e) { Typecho_Cookie::set('__typecho_remember_text', $comment['text']); $archive->response->throwJson(array('status'=>0,'msg'=>_t($e->getMessage()))); throw $e;} /** 增加批评 */ $commentId = $feedback->insert($comment); if(!$commentId){ $archive->response->throwJson(array('status'=>0,'msg'=>_t('批评失利'))); } Typecho_Cookie::delete('__typecho_remember_text'); $db->fetchRow($feedback->select()->where('coid = ?', $commentId) ->limit(1), array($feedback, 'push'));//批评完成后的接口,平常用于批评提示插件$feedback->pluginHandle()->finishComment($feedback); // 返回批评数据 $data = array( 'cid' => $feedback->cid, 'coid' => $feedback->coid, 'parent' => $feedback->parent, 'mail' => $feedback->mail, 'url' => $feedback->url, 'ip' => $feedback->ip, 'agent' => $feedback->agent, 'author' => $feedback->author, 'authorId' => $feedback->authorId, 'permalink' => $feedback->permalink, 'created' => $feedback->created, 'datetime' => $feedback->date->format('Y-m-d H:i:s'), 'status' => $feedback->status, ); // 批评内容 ob_start(); $feedback->content(); $data['content'] = ob_get_clean(); $data['avatar'] = Typecho_Common::gravatarUrl($data['mail'], 48, Helper::options()->commentsAvatarRating, NULL, $archive->request->isSecure()); $archive->response->throwJson(array('status'=>1,'comment'=>$data));}
评论列表