Dotclear

source: plugins/antispam/inc/lib.dc.antispam.php @ 0:54703be25dd6

Revision 0:54703be25dd6, 4.9 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

2.3 branch (trunk) first checkin

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Antispam, a plugin for Dotclear 2.
5#
6# Copyright (c) 2003-2010 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class dcAntispam
15{
16     public static $filters;
17     
18     public static function initFilters()
19     {
20          global $core;
21         
22          if (!isset($core->spamfilters)) {
23               return;
24          }
25         
26          self::$filters = new dcSpamFilters($core);
27          self::$filters->init($core->spamfilters);
28     }
29     
30     public static function isSpam($cur)
31     {
32          self::initFilters();
33          self::$filters->isSpam($cur);
34     }
35     
36     public static function trainFilters($blog,$cur,$rs)
37     {
38          $status = null;
39          # From ham to spam
40          if ($rs->comment_status != -2 && $cur->comment_status == -2) {
41               $status = 'spam';
42          }
43         
44          # From spam to ham
45          if ($rs->comment_status == -2 && $cur->comment_status == 1) {
46               $status = 'ham';
47          }
48         
49          # the status of this comment has changed
50          if ($status)
51          {
52               $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null;
53               
54               self::initFilters();
55               self::$filters->trainFilters($rs,$status,$filter_name);
56          }
57     }
58     
59     public static function statusMessage($rs)
60     {
61          if ($rs->exists('comment_status') && $rs->comment_status == -2)
62          {
63               $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null;
64               
65               self::initFilters();
66               
67               return
68               '<p><strong>'.__('This comment is a spam:').'</strong> '.
69               self::$filters->statusMessage($rs,$filter_name).'</p>';
70          }
71     }
72     
73     public static function dashboardIcon($core, $icons)
74     {
75          if (($count = self::countSpam($core)) > 0) {
76               $str = ($count > 1) ? __('(including %d spam comments)') : __('(including %d spam comment)');
77               $icons['comments'][0] .= '</a> <br /><a href="comments.php?status=-2">'.sprintf($str,$count);
78          }
79     }
80     
81     public static function countSpam($core)
82     {
83          return $core->blog->getComments(array('comment_status'=>-2),true)->f(0);
84     }
85     
86     public static function countPublishedComments($core)
87     {
88          return $core->blog->getComments(array('comment_status'=>1),true)->f(0);
89     }
90     
91     public static function delAllSpam($core, $beforeDate = null)
92     {
93          $strReq =
94          'SELECT comment_id '.
95          'FROM '.$core->prefix.'comment C '.
96          'JOIN '.$core->prefix.'post P ON P.post_id = C.post_id '.
97          "WHERE blog_id = '".$core->con->escape($core->blog->id)."' ".
98          'AND comment_status = -2 ';
99          if ($beforeDate) {
100               $strReq .= 'AND comment_dt < \''.$beforeDate.'\' ';
101          }
102         
103          $rs = $core->con->select($strReq);
104          $r = array();
105          while ($rs->fetch()) {
106               $r[] = (integer) $rs->comment_id;
107          }
108         
109          if (empty($r)) {
110               return;
111          }
112         
113          $strReq =
114          'DELETE FROM '.$core->prefix.'comment '.
115          'WHERE comment_id '.$core->con->in($r).' ';
116         
117          $core->con->execute($strReq);
118     }
119     
120     public static function getUserCode($core)
121     {
122          $code =
123          pack('a32',$core->auth->userID()).
124          pack('H*',crypt::hmac(DC_MASTER_KEY,$core->auth->getInfo('user_pwd')));
125          return bin2hex($code);
126     }
127     
128     public static function checkUserCode($core,$code)
129     {
130          $code = pack('H*',$code);
131         
132          $user_id = trim(@pack('a32',substr($code,0,32)));
133          $pwd = @unpack('H40hex',substr($code,32,40));
134         
135          if ($user_id === false || $pwd === false) {
136               return false;
137          }
138         
139          $pwd = $pwd['hex'];
140         
141          $strReq = 'SELECT user_id, user_pwd '.
142                    'FROM '.$core->prefix.'user '.
143                    "WHERE user_id = '".$core->con->escape($user_id)."' ";
144         
145          $rs = $core->con->select($strReq);
146         
147          if ($rs->isEmpty()) {
148               return false;
149          }
150         
151          if (crypt::hmac(DC_MASTER_KEY,$rs->user_pwd) != $pwd) {
152               return false;
153          }
154         
155          return $rs->user_id;
156     }
157     
158     public static function purgeOldSpam($core)
159     {
160          $defaultDateLastPurge = time();
161          $defaultModerationTTL = '7';
162          $init = false;
163         
164          // settings
165          $core->blog->settings->addNamespace('antispam');
166         
167          $dateLastPurge = $core->blog->settings->antispam->antispam_date_last_purge;
168          if ($dateLastPurge === null) {
169               $init = true;
170               $core->blog->settings->antispam->put('antispam_date_last_purge',$defaultDateLastPurge,'integer','Antispam Date Last Purge (unix timestamp)',true,false);
171               $dateLastPurge = $defaultDateLastPurge;
172          }
173          $moderationTTL = $core->blog->settings->antispam->antispam_moderation_ttl;
174          if ($moderationTTL === null) {
175               $core->blog->settings->antispam->put('antispam_moderation_ttl',$defaultModerationTTL,'integer','Antispam Moderation TTL (days)',true,false);
176               $moderationTTL = $defaultModerationTTL;
177          }
178         
179          if ($moderationTTL < 0) {
180               // disabled
181               return;
182          }
183         
184          // we call the purge every day
185          if ((time()-$dateLastPurge) > (86400)) {
186               // update dateLastPurge
187               if (!$init) {
188                    $core->blog->settings->antispam->put('antispam_date_last_purge',time(),null,null,true,false);
189               }   
190               $date = date('Y-m-d H:i:s', time() - $moderationTTL*86400);
191               dcAntispam::delAllSpam($core, $date);
192          }
193     }
194}
195?>
Note: See TracBrowser for help on using the repository browser.

Sites map