Example #1
0
 /**
  * TODO 本来之前用setXAttribute方式来做的默认值,但是看源码需要有这三个值传过来才行,SO。。。
  */
 protected static function boot()
 {
     static::creating(function (Comment $comment) {
         $comment->ip = \Request::getClientIp();
         $comment->user_agent = \Request::header('User-Agent');
         $comment->status = static::STATUS_VERIFY;
         if (!\Auth::guest()) {
             $comment->user_id = \Auth::user()->id;
         }
         //TODO 目前没有好的xss解决办法(HtmlPurifier会将<script>标签去掉, 导致评论有可能和原意不一致)
         $pattern = ['/(<\\s*?script\\s*?>.*?<\\s*?\\/\\s*?script\\s*?>)/i', '/(<\\s*?style\\s*?>.*<\\s*?\\/?style\\s*?>)/i'];
         $replacement = ["\n```javascript\n\$1\n```\n", "\n```css\n\$1\n```\n"];
         $comment->text = strip_tags(preg_replace($pattern, $replacement, $comment->text), '<script><style>');
     });
     static::created(function (Comment $comment) {
         //            if ($comment->parent_id > 0 && $replyComment = static::active()->find($comment->parent_id)) {
         //TODO 发送邮件(这儿应该还要根据at的人再发邮件...)
         //            }
     });
     parent::boot();
 }