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