[0] | 1 | <?php |
---|
[3731] | 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 | |
---|
[3730] | 12 | if (!defined('DC_RC_PATH')) {return;} |
---|
[0] | 13 | |
---|
| 14 | class dcAntispam |
---|
| 15 | { |
---|
[3730] | 16 | public static $filters; |
---|
[2566] | 17 | |
---|
[3730] | 18 | public static function initFilters() |
---|
| 19 | { |
---|
| 20 | global $core; |
---|
[2566] | 21 | |
---|
[3730] | 22 | if (!isset($core->spamfilters)) { |
---|
| 23 | return; |
---|
| 24 | } |
---|
[2566] | 25 | |
---|
[3730] | 26 | self::$filters = new dcSpamFilters($core); |
---|
| 27 | self::$filters->init($core->spamfilters); |
---|
| 28 | } |
---|
[2566] | 29 | |
---|
[3730] | 30 | public static function isSpam($cur) |
---|
| 31 | { |
---|
| 32 | self::initFilters(); |
---|
| 33 | self::$filters->isSpam($cur); |
---|
| 34 | } |
---|
[2566] | 35 | |
---|
[3730] | 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 | } |
---|
[2566] | 43 | |
---|
[3730] | 44 | # From spam to ham |
---|
| 45 | if ($rs->comment_status == -2 && $cur->comment_status == 1) { |
---|
| 46 | $status = 'ham'; |
---|
| 47 | } |
---|
[2566] | 48 | |
---|
[3730] | 49 | # the status of this comment has changed |
---|
| 50 | if ($status) { |
---|
| 51 | $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null; |
---|
[2566] | 52 | |
---|
[3730] | 53 | self::initFilters(); |
---|
| 54 | self::$filters->trainFilters($rs, $status, $filter_name); |
---|
| 55 | } |
---|
| 56 | } |
---|
[2566] | 57 | |
---|
[3730] | 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; |
---|
[2566] | 62 | |
---|
[3730] | 63 | self::initFilters(); |
---|
[2566] | 64 | |
---|
[3730] | 65 | return |
---|
| 66 | '<p><strong>' . __('This comment is a spam:') . '</strong> ' . |
---|
| 67 | self::$filters->statusMessage($rs, $filter_name) . '</p>'; |
---|
| 68 | } |
---|
| 69 | } |
---|
[2566] | 70 | |
---|
[3730] | 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)'); |
---|
[3781] | 75 | $icons['comments'][0] .= '</span></a> <br /><a href="' . $core->adminurl->get('admin.comments', array('status' => '-2')) . '"><span class="db-icon-title-spam">' . |
---|
[3730] | 76 | sprintf($str, $count); |
---|
| 77 | } |
---|
| 78 | } |
---|
[2566] | 79 | |
---|
[3730] | 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)'); |
---|
[3781] | 84 | return '</span></a> <br /><a href="' . $core->adminurl->get('admin.comments', array('status' => '-2')) . '"><span class="db-icon-title-spam">' . |
---|
[3730] | 85 | sprintf($str, $count); |
---|
| 86 | } else { |
---|
| 87 | return ''; |
---|
| 88 | } |
---|
| 89 | } |
---|
[2566] | 90 | |
---|
[3781] | 91 | public static function dashboardHeaders() |
---|
| 92 | { |
---|
| 93 | return dcPage::jsLoad(urldecode(dcPage::getPF('antispam/js/dashboard.js'))); |
---|
| 94 | } |
---|
| 95 | |
---|
[3730] | 96 | public static function countSpam($core) |
---|
| 97 | { |
---|
| 98 | return $core->blog->getComments(array('comment_status' => -2), true)->f(0); |
---|
| 99 | } |
---|
[2566] | 100 | |
---|
[3730] | 101 | public static function countPublishedComments($core) |
---|
| 102 | { |
---|
| 103 | return $core->blog->getComments(array('comment_status' => 1), true)->f(0); |
---|
| 104 | } |
---|
[2566] | 105 | |
---|
[3730] | 106 | public static function delAllSpam($core, $beforeDate = null) |
---|
| 107 | { |
---|
| 108 | $strReq = |
---|
| 109 | 'SELECT comment_id ' . |
---|
| 110 | 'FROM ' . $core->prefix . 'comment C ' . |
---|
| 111 | 'JOIN ' . $core->prefix . 'post P ON P.post_id = C.post_id ' . |
---|
| 112 | "WHERE blog_id = '" . $core->con->escape($core->blog->id) . "' " . |
---|
| 113 | 'AND comment_status = -2 '; |
---|
| 114 | if ($beforeDate) { |
---|
| 115 | $strReq .= 'AND comment_dt < \'' . $beforeDate . '\' '; |
---|
| 116 | } |
---|
[2566] | 117 | |
---|
[3730] | 118 | $rs = $core->con->select($strReq); |
---|
| 119 | $r = array(); |
---|
| 120 | while ($rs->fetch()) { |
---|
| 121 | $r[] = (integer) $rs->comment_id; |
---|
| 122 | } |
---|
[2566] | 123 | |
---|
[3730] | 124 | if (empty($r)) { |
---|
| 125 | return; |
---|
| 126 | } |
---|
[2566] | 127 | |
---|
[3730] | 128 | $strReq = |
---|
| 129 | 'DELETE FROM ' . $core->prefix . 'comment ' . |
---|
| 130 | 'WHERE comment_id ' . $core->con->in($r) . ' '; |
---|
[2566] | 131 | |
---|
[3730] | 132 | $core->con->execute($strReq); |
---|
| 133 | } |
---|
[2566] | 134 | |
---|
[3730] | 135 | public static function getUserCode($core) |
---|
| 136 | { |
---|
| 137 | $code = |
---|
| 138 | pack('a32', $core->auth->userID()) . |
---|
| 139 | pack('H*', $core->auth->cryptLegacy($core->auth->getInfo('user_pwd'))); |
---|
| 140 | return bin2hex($code); |
---|
| 141 | } |
---|
[2566] | 142 | |
---|
[3730] | 143 | public static function checkUserCode($core, $code) |
---|
| 144 | { |
---|
| 145 | $code = pack('H*', $code); |
---|
[2566] | 146 | |
---|
[3730] | 147 | $user_id = trim(@pack('a32', substr($code, 0, 32))); |
---|
| 148 | $pwd = @unpack('H*hex', substr($code, 32)); |
---|
[2566] | 149 | |
---|
[3730] | 150 | if ($user_id === false || $pwd === false) { |
---|
| 151 | return false; |
---|
| 152 | } |
---|
[2566] | 153 | |
---|
[3730] | 154 | $pwd = $pwd['hex']; |
---|
[2566] | 155 | |
---|
[3730] | 156 | $strReq = 'SELECT user_id, user_pwd ' . |
---|
| 157 | 'FROM ' . $core->prefix . 'user ' . |
---|
| 158 | "WHERE user_id = '" . $core->con->escape($user_id) . "' "; |
---|
[2566] | 159 | |
---|
[3730] | 160 | $rs = $core->con->select($strReq); |
---|
[2566] | 161 | |
---|
[3730] | 162 | if ($rs->isEmpty()) { |
---|
| 163 | return false; |
---|
| 164 | } |
---|
[2566] | 165 | |
---|
[3730] | 166 | if ($core->auth->cryptLegacy($rs->user_pwd) != $pwd) { |
---|
| 167 | return false; |
---|
| 168 | } |
---|
[2566] | 169 | |
---|
[3730] | 170 | $permissions = $core->getBlogPermissions($core->blog->id); |
---|
[2566] | 171 | |
---|
[3730] | 172 | if (empty($permissions[$rs->user_id])) { |
---|
| 173 | return false; |
---|
| 174 | } |
---|
[2566] | 175 | |
---|
[3730] | 176 | return $rs->user_id; |
---|
| 177 | } |
---|
[2566] | 178 | |
---|
[3730] | 179 | public static function purgeOldSpam($core) |
---|
| 180 | { |
---|
| 181 | $defaultDateLastPurge = time(); |
---|
| 182 | $defaultModerationTTL = '7'; |
---|
| 183 | $init = false; |
---|
[2566] | 184 | |
---|
[3730] | 185 | // settings |
---|
| 186 | $core->blog->settings->addNamespace('antispam'); |
---|
[2566] | 187 | |
---|
[3730] | 188 | $dateLastPurge = $core->blog->settings->antispam->antispam_date_last_purge; |
---|
| 189 | if ($dateLastPurge === null) { |
---|
| 190 | $init = true; |
---|
| 191 | $core->blog->settings->antispam->put('antispam_date_last_purge', $defaultDateLastPurge, 'integer', 'Antispam Date Last Purge (unix timestamp)', true, false); |
---|
| 192 | $dateLastPurge = $defaultDateLastPurge; |
---|
| 193 | } |
---|
| 194 | $moderationTTL = $core->blog->settings->antispam->antispam_moderation_ttl; |
---|
| 195 | if ($moderationTTL === null) { |
---|
| 196 | $core->blog->settings->antispam->put('antispam_moderation_ttl', $defaultModerationTTL, 'integer', 'Antispam Moderation TTL (days)', true, false); |
---|
| 197 | $moderationTTL = $defaultModerationTTL; |
---|
| 198 | } |
---|
[2566] | 199 | |
---|
[3730] | 200 | if ($moderationTTL < 0) { |
---|
| 201 | // disabled |
---|
| 202 | return; |
---|
| 203 | } |
---|
[2566] | 204 | |
---|
[3730] | 205 | // we call the purge every day |
---|
| 206 | if ((time() - $dateLastPurge) > (86400)) { |
---|
| 207 | // update dateLastPurge |
---|
| 208 | if (!$init) { |
---|
| 209 | $core->blog->settings->antispam->put('antispam_date_last_purge', time(), null, null, true, false); |
---|
| 210 | } |
---|
| 211 | $date = date('Y-m-d H:i:s', time() - $moderationTTL * 86400); |
---|
| 212 | dcAntispam::delAllSpam($core, $date); |
---|
| 213 | } |
---|
| 214 | } |
---|
[0] | 215 | } |
---|