Dotclear

source: plugins/antispam/inc/lib.dc.antispam.php @ 3731:3770620079d4

Revision 3731:3770620079d4, 6.2 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Simplify licence block at the beginning of each file

Line 
1<?php
2/**
3 * @brief antispam, a plugin for Dotclear 2
4 *
5 * @package Dotclear
6 * @subpackage Plugins
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
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            $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null;
52
53            self::initFilters();
54            self::$filters->trainFilters($rs, $status, $filter_name);
55        }
56    }
57
58    public static function statusMessage($rs)
59    {
60        if ($rs->exists('comment_status') && $rs->comment_status == -2) {
61            $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null;
62
63            self::initFilters();
64
65            return
66            '<p><strong>' . __('This comment is a spam:') . '</strong> ' .
67            self::$filters->statusMessage($rs, $filter_name) . '</p>';
68        }
69    }
70
71    public static function dashboardIcon($core, $icons)
72    {
73        if (($count = self::countSpam($core)) > 0) {
74            $str = ($count > 1) ? __('(including %d spam comments)') : __('(including %d spam comment)');
75            $icons['comments'][0] .= '</span></a> <br /><a href="' . $core->adminurl->get('admin.comments', array('status' => '-2')) . '"><span>' .
76            sprintf($str, $count);
77        }
78    }
79
80    public static function dashboardIconTitle($core)
81    {
82        if (($count = self::countSpam($core)) > 0) {
83            $str = ($count > 1) ? __('(including %d spam comments)') : __('(including %d spam comment)');
84            return '</span></a> <br /><a href="' . $core->adminurl->get('admin.comments', array('status' => '-2')) . '"><span>' .
85            sprintf($str, $count);
86        } else {
87            return '';
88        }
89    }
90
91    public static function countSpam($core)
92    {
93        return $core->blog->getComments(array('comment_status' => -2), true)->f(0);
94    }
95
96    public static function countPublishedComments($core)
97    {
98        return $core->blog->getComments(array('comment_status' => 1), true)->f(0);
99    }
100
101    public static function delAllSpam($core, $beforeDate = null)
102    {
103        $strReq =
104        'SELECT comment_id ' .
105        'FROM ' . $core->prefix . 'comment C ' .
106        'JOIN ' . $core->prefix . 'post P ON P.post_id = C.post_id ' .
107        "WHERE blog_id = '" . $core->con->escape($core->blog->id) . "' " .
108            'AND comment_status = -2 ';
109        if ($beforeDate) {
110            $strReq .= 'AND comment_dt < \'' . $beforeDate . '\' ';
111        }
112
113        $rs = $core->con->select($strReq);
114        $r  = array();
115        while ($rs->fetch()) {
116            $r[] = (integer) $rs->comment_id;
117        }
118
119        if (empty($r)) {
120            return;
121        }
122
123        $strReq =
124        'DELETE FROM ' . $core->prefix . 'comment ' .
125        'WHERE comment_id ' . $core->con->in($r) . ' ';
126
127        $core->con->execute($strReq);
128    }
129
130    public static function getUserCode($core)
131    {
132        $code =
133        pack('a32', $core->auth->userID()) .
134        pack('H*', $core->auth->cryptLegacy($core->auth->getInfo('user_pwd')));
135        return bin2hex($code);
136    }
137
138    public static function checkUserCode($core, $code)
139    {
140        $code = pack('H*', $code);
141
142        $user_id = trim(@pack('a32', substr($code, 0, 32)));
143        $pwd     = @unpack('H*hex', substr($code, 32));
144
145        if ($user_id === false || $pwd === false) {
146            return false;
147        }
148
149        $pwd = $pwd['hex'];
150
151        $strReq = 'SELECT user_id, user_pwd ' .
152        'FROM ' . $core->prefix . 'user ' .
153        "WHERE user_id = '" . $core->con->escape($user_id) . "' ";
154
155        $rs = $core->con->select($strReq);
156
157        if ($rs->isEmpty()) {
158            return false;
159        }
160
161        if ($core->auth->cryptLegacy($rs->user_pwd) != $pwd) {
162            return false;
163        }
164
165        $permissions = $core->getBlogPermissions($core->blog->id);
166
167        if (empty($permissions[$rs->user_id])) {
168            return false;
169        }
170
171        return $rs->user_id;
172    }
173
174    public static function purgeOldSpam($core)
175    {
176        $defaultDateLastPurge = time();
177        $defaultModerationTTL = '7';
178        $init                 = false;
179
180        // settings
181        $core->blog->settings->addNamespace('antispam');
182
183        $dateLastPurge = $core->blog->settings->antispam->antispam_date_last_purge;
184        if ($dateLastPurge === null) {
185            $init = true;
186            $core->blog->settings->antispam->put('antispam_date_last_purge', $defaultDateLastPurge, 'integer', 'Antispam Date Last Purge (unix timestamp)', true, false);
187            $dateLastPurge = $defaultDateLastPurge;
188        }
189        $moderationTTL = $core->blog->settings->antispam->antispam_moderation_ttl;
190        if ($moderationTTL === null) {
191            $core->blog->settings->antispam->put('antispam_moderation_ttl', $defaultModerationTTL, 'integer', 'Antispam Moderation TTL (days)', true, false);
192            $moderationTTL = $defaultModerationTTL;
193        }
194
195        if ($moderationTTL < 0) {
196            // disabled
197            return;
198        }
199
200        // we call the purge every day
201        if ((time() - $dateLastPurge) > (86400)) {
202            // update dateLastPurge
203            if (!$init) {
204                $core->blog->settings->antispam->put('antispam_date_last_purge', time(), null, null, true, false);
205            }
206            $date = date('Y-m-d H:i:s', time() - $moderationTTL * 86400);
207            dcAntispam::delAllSpam($core, $date);
208        }
209    }
210}
Note: See TracBrowser for help on using the repository browser.

Sites map