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 ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | class dcSpamFilters |
---|
15 | { |
---|
16 | private $filters = array(); |
---|
17 | private $filters_opt = array(); |
---|
18 | private $core; |
---|
19 | |
---|
20 | public function __construct($core) |
---|
21 | { |
---|
22 | $this->core =& $core; |
---|
23 | } |
---|
24 | |
---|
25 | public function init($filters) |
---|
26 | { |
---|
27 | foreach ($filters as $f) |
---|
28 | { |
---|
29 | if (!class_exists($f)) { |
---|
30 | continue; |
---|
31 | } |
---|
32 | |
---|
33 | $r = new ReflectionClass($f); |
---|
34 | $p = $r->getParentClass(); |
---|
35 | |
---|
36 | if (!$p || $p->name != 'dcSpamFilter') { |
---|
37 | continue; |
---|
38 | } |
---|
39 | |
---|
40 | $this->filters[$f] = new $f($this->core); |
---|
41 | } |
---|
42 | |
---|
43 | $this->setFilterOpts(); |
---|
44 | if (!empty($this->filters_opt)) { |
---|
45 | uasort($this->filters,array($this,'orderCallBack')); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | public function getFilters() |
---|
50 | { |
---|
51 | return $this->filters; |
---|
52 | } |
---|
53 | |
---|
54 | public function isSpam($cur) |
---|
55 | { |
---|
56 | foreach ($this->filters as $fid => $f) |
---|
57 | { |
---|
58 | if (!$f->active) { |
---|
59 | continue; |
---|
60 | } |
---|
61 | |
---|
62 | $type = $cur->comment_trackback ? 'trackback' : 'comment'; |
---|
63 | $author = $cur->comment_author; |
---|
64 | $email = $cur->comment_email; |
---|
65 | $site = $cur->comment_site; |
---|
66 | $ip = $cur->comment_ip; |
---|
67 | $content = $cur->comment_content; |
---|
68 | $post_id = $cur->post_id; |
---|
69 | |
---|
70 | $is_spam = $f->isSpam($type,$author,$email,$site,$ip,$content,$post_id,$status); |
---|
71 | |
---|
72 | if ($is_spam === true) { |
---|
73 | if ($f->auto_delete) { |
---|
74 | $cur->clean(); |
---|
75 | } else { |
---|
76 | $cur->comment_status = -2; |
---|
77 | $cur->comment_spam_status = $status; |
---|
78 | $cur->comment_spam_filter = $fid; |
---|
79 | } |
---|
80 | return true; |
---|
81 | } elseif ($is_spam === false) { |
---|
82 | return false; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | return false; |
---|
87 | } |
---|
88 | |
---|
89 | public function trainFilters($rs,$status,$filter_name) |
---|
90 | { |
---|
91 | foreach ($this->filters as $fid => $f) |
---|
92 | { |
---|
93 | if (!$f->active) { |
---|
94 | continue; |
---|
95 | } |
---|
96 | |
---|
97 | $type = $rs->comment_trackback ? 'trackback' : 'comment'; |
---|
98 | $author = $rs->comment_author; |
---|
99 | $email = $rs->comment_email; |
---|
100 | $site = $rs->comment_site; |
---|
101 | $ip = $rs->comment_ip; |
---|
102 | $content = $rs->comment_content; |
---|
103 | |
---|
104 | $f->trainFilter($status,$filter_name,$type,$author,$email,$site,$ip,$content,$rs); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | public function statusMessage($rs,$filter_name) |
---|
109 | { |
---|
110 | $f = isset($this->filters[$filter_name]) ? $this->filters[$filter_name] : null; |
---|
111 | |
---|
112 | if ($f === null) |
---|
113 | { |
---|
114 | return __('Unknown filter.'); |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | $status = $rs->exists('comment_spam_status') ? $rs->comment_spam_status : null; |
---|
119 | |
---|
120 | return $f->getStatusMessage($status,$rs->comment_id); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | public function saveFilterOpts($opts,$global=false) |
---|
125 | { |
---|
126 | $this->core->blog->settings->addNamespace('antispam'); |
---|
127 | if ($global) { |
---|
128 | $this->core->blog->settings->antispam->drop('antispam_filters'); |
---|
129 | } |
---|
130 | $this->core->blog->settings->antispam->put('antispam_filters',serialize($opts),'string','Antispam Filters',true,$global); |
---|
131 | } |
---|
132 | |
---|
133 | private function setFilterOpts() |
---|
134 | { |
---|
135 | if ($this->core->blog->settings->antispam->antispam_filters !== null) { |
---|
136 | $this->filters_opt = @unserialize($this->core->blog->settings->antispam->antispam_filters); |
---|
137 | } |
---|
138 | |
---|
139 | # Create default options if needed |
---|
140 | if (!is_array($this->filters_opt)) { |
---|
141 | $this->saveFilterOpts(array(),true); |
---|
142 | $this->filters_opt = array(); |
---|
143 | } |
---|
144 | |
---|
145 | foreach ($this->filters_opt as $k => $o) |
---|
146 | { |
---|
147 | if (isset($this->filters[$k]) && is_array($o)) { |
---|
148 | $this->filters[$k]->active = isset($o[0])?$o[0]:false; |
---|
149 | $this->filters[$k]->order = isset($o[1])?$o[1]:0; |
---|
150 | $this->filters[$k]->auto_delete = isset($o[2])?$o[2]:false; |
---|
151 | } |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | private function orderCallBack($a,$b) |
---|
156 | { |
---|
157 | if ($a->order == $b->order) { |
---|
158 | return 0; |
---|
159 | } |
---|
160 | |
---|
161 | return $a->order > $b->order ? 1 : -1; |
---|
162 | } |
---|
163 | } |
---|
164 | ?> |
---|