1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Antispam, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 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 dcFilterAkismet extends dcSpamFilter |
---|
15 | { |
---|
16 | public $name = 'Akismet'; |
---|
17 | public $has_gui = true; |
---|
18 | public $active = false; |
---|
19 | public $help = 'akismet-filter'; |
---|
20 | |
---|
21 | public function __construct($core) |
---|
22 | { |
---|
23 | parent::__construct($core); |
---|
24 | |
---|
25 | if (defined('DC_AKISMET_SUPER') && DC_AKISMET_SUPER && !$core->auth->isSuperAdmin()) { |
---|
26 | $this->has_gui = false; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | protected function setInfo() |
---|
31 | { |
---|
32 | $this->description = __('Akismet spam filter'); |
---|
33 | } |
---|
34 | |
---|
35 | public function getStatusMessage($status,$comment_id) |
---|
36 | { |
---|
37 | return sprintf(__('Filtered by %s.'),$this->guiLink()); |
---|
38 | } |
---|
39 | |
---|
40 | private function akInit() |
---|
41 | { |
---|
42 | $blog =& $this->core->blog; |
---|
43 | |
---|
44 | if (!$blog->settings->akismet->ak_key) { |
---|
45 | return false; |
---|
46 | } |
---|
47 | |
---|
48 | return new akismet($blog->url,$blog->settings->akismet->ak_key); |
---|
49 | } |
---|
50 | |
---|
51 | public function isSpam($type,$author,$email,$site,$ip,$content,$post_id,&$status) |
---|
52 | { |
---|
53 | if (($ak = $this->akInit()) === false) { |
---|
54 | return; |
---|
55 | } |
---|
56 | |
---|
57 | $blog =& $this->core->blog; |
---|
58 | |
---|
59 | try |
---|
60 | { |
---|
61 | if ($ak->verify()) |
---|
62 | { |
---|
63 | $post = $blog->getPosts(array('post_id' => $post_id)); |
---|
64 | |
---|
65 | $c = $ak->comment_check( |
---|
66 | $post->getURL(), |
---|
67 | $type, |
---|
68 | $author, |
---|
69 | $email, |
---|
70 | $site, |
---|
71 | $content |
---|
72 | ); |
---|
73 | |
---|
74 | if ($c) { |
---|
75 | $status = 'Filtered by Akismet'; |
---|
76 | return true; |
---|
77 | } |
---|
78 | } |
---|
79 | } catch (Exception $e) {} # If http or akismet is dead, we don't need to know it |
---|
80 | } |
---|
81 | |
---|
82 | public function trainFilter($status,$filter,$type,$author,$email,$site,$ip,$content,$rs) |
---|
83 | { |
---|
84 | # We handle only false positive from akismet |
---|
85 | if ($status == 'spam' && $filter != 'dcFilterAkismet') |
---|
86 | { |
---|
87 | return; |
---|
88 | } |
---|
89 | |
---|
90 | $f = $status == 'spam' ? 'submit_spam' : 'submit_ham'; |
---|
91 | |
---|
92 | if (($ak = $this->akInit()) === false) { |
---|
93 | return; |
---|
94 | } |
---|
95 | |
---|
96 | try |
---|
97 | { |
---|
98 | if ($ak->verify()) { |
---|
99 | $ak->{$f}($rs->getPostURL(),$type,$author,$email,$site,$content); |
---|
100 | } |
---|
101 | } catch (Exception $e) {} # If http or akismet is dead, we don't need to know it |
---|
102 | } |
---|
103 | |
---|
104 | public function gui($url) |
---|
105 | { |
---|
106 | $blog =& $this->core->blog; |
---|
107 | |
---|
108 | $ak_key = $blog->settings->akismet->ak_key; |
---|
109 | $ak_verified = null; |
---|
110 | |
---|
111 | if (isset($_POST['ak_key'])) |
---|
112 | { |
---|
113 | try |
---|
114 | { |
---|
115 | $ak_key = $_POST['ak_key']; |
---|
116 | |
---|
117 | $blog->settings->addNamespace('akismet'); |
---|
118 | $blog->settings->akismet->put('ak_key',$ak_key,'string'); |
---|
119 | |
---|
120 | dcPage::addSuccessNotice(__('Filter configuration have been successfully saved.')); |
---|
121 | http::redirect($url); |
---|
122 | } |
---|
123 | catch (Exception $e) |
---|
124 | { |
---|
125 | $this->core->error->add($e->getMessage()); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | if ($blog->settings->akismet->ak_key) |
---|
130 | { |
---|
131 | try { |
---|
132 | $ak = new akismet($blog->url,$blog->settings->akismet->ak_key); |
---|
133 | $ak_verified = $ak->verify(); |
---|
134 | } catch (Exception $e) { |
---|
135 | $this->core->error->add($e->getMessage()); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | $res = dcPage::notices(); |
---|
140 | |
---|
141 | $res .= |
---|
142 | '<form action="'.html::escapeURL($url).'" method="post" class="fieldset">'. |
---|
143 | '<p><label for="ak_key" class="classic">'.__('Akismet API key:').'</label> '. |
---|
144 | form::field('ak_key',12,128,$ak_key); |
---|
145 | |
---|
146 | if ($ak_verified !== null) { |
---|
147 | if ($ak_verified) { |
---|
148 | $res .= ' <img src="images/check-on.png" alt="" /> '.__('API key verified'); |
---|
149 | } else { |
---|
150 | $res .= ' <img src="images/check-off.png" alt="" /> '.__('API key not verified'); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | $res .= '</p>'; |
---|
155 | |
---|
156 | $res .= |
---|
157 | '<p><a href="http://akismet.com/">'.__('Get your own API key').'</a></p>'. |
---|
158 | '<p><input type="submit" value="'.__('Save').'" />'. |
---|
159 | $this->core->formNonce().'</p>'. |
---|
160 | '</form>'; |
---|
161 | |
---|
162 | return $res; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | class akismet extends netHttp |
---|
167 | { |
---|
168 | protected $base_host = 'rest.akismet.com'; |
---|
169 | protected $ak_host = ''; |
---|
170 | protected $ak_version = '1.1'; |
---|
171 | protected $ak_path = '/%s/%s'; |
---|
172 | |
---|
173 | protected $ak_key = null; |
---|
174 | protected $blog_url; |
---|
175 | |
---|
176 | protected $timeout = 3; |
---|
177 | |
---|
178 | public function __construct($blog_url,$api_key) |
---|
179 | { |
---|
180 | $this->blog_url = $blog_url; |
---|
181 | $this->ak_key = $api_key; |
---|
182 | |
---|
183 | $this->ak_path = sprintf($this->ak_path,$this->ak_version,'%s'); |
---|
184 | $this->ak_host = $this->ak_key.'.'.$this->base_host; |
---|
185 | |
---|
186 | parent::__construct($this->ak_host,80); |
---|
187 | } |
---|
188 | |
---|
189 | public function verify() |
---|
190 | { |
---|
191 | $this->host = $this->base_host; |
---|
192 | $path = sprintf($this->ak_path,'verify-key'); |
---|
193 | |
---|
194 | $data = array( |
---|
195 | 'key' => $this->ak_key, |
---|
196 | 'blog' => $this->blog_url |
---|
197 | ); |
---|
198 | |
---|
199 | if ($this->post($path,$data,'UTF-8')) |
---|
200 | { |
---|
201 | return $this->getContent() == 'valid'; |
---|
202 | } |
---|
203 | |
---|
204 | return false; |
---|
205 | } |
---|
206 | |
---|
207 | public function comment_check($permalink,$type,$author,$email,$url,$content) |
---|
208 | { |
---|
209 | $info_ignore = array('HTTP_COOKIE'); |
---|
210 | $info = array(); |
---|
211 | |
---|
212 | foreach ($_SERVER as $k => $v) { |
---|
213 | if (strpos($k,'HTTP_') === 0 && !in_array($k,$info_ignore)) { |
---|
214 | $info[$k] = $v; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | return $this->callFunc('comment-check',$permalink,$type,$author,$email,$url,$content,$info); |
---|
219 | } |
---|
220 | |
---|
221 | public function submit_spam($permalink,$type,$author,$email,$url,$content) |
---|
222 | { |
---|
223 | $this->callFunc('submit-spam',$permalink,$type,$author,$email,$url,$content); |
---|
224 | return true; |
---|
225 | } |
---|
226 | |
---|
227 | public function submit_ham($permalink,$type,$author,$email,$url,$content) |
---|
228 | { |
---|
229 | $this->callFunc('submit-ham',$permalink,$type,$author,$email,$url,$content); |
---|
230 | return true; |
---|
231 | } |
---|
232 | |
---|
233 | protected function callFunc($function,$permalink,$type,$author,$email,$url,$content,$info=array()) |
---|
234 | { |
---|
235 | $ua = isset($info['HTTP_USER_AGENT']) ? $info['HTTP_USER_AGENT'] : ''; |
---|
236 | $referer = isset($info['HTTP_REFERER']) ? $info['HTTP_REFERER'] : ''; |
---|
237 | |
---|
238 | # Prepare comment data |
---|
239 | $data = array( |
---|
240 | 'blog' => $this->blog_url, |
---|
241 | 'user_ip' => http::realIP(), |
---|
242 | 'user_agent' => $ua, |
---|
243 | 'referrer' => $referer, |
---|
244 | 'permalink' => $permalink, |
---|
245 | 'comment_type' => $type, |
---|
246 | 'comment_author' => $author, |
---|
247 | 'comment_author_email' => $email, |
---|
248 | 'comment_author_url' => $url, |
---|
249 | 'comment_content' => $content |
---|
250 | ); |
---|
251 | |
---|
252 | $data = array_merge($data,$info); |
---|
253 | |
---|
254 | $this->host = $this->ak_host; |
---|
255 | $path = sprintf($this->ak_path,$function); |
---|
256 | |
---|
257 | if (!$this->post($path,$data,'UTF-8')) { |
---|
258 | throw new Exception('HTTP error: '.$this->getError()); |
---|
259 | } |
---|
260 | |
---|
261 | return $this->getContent() == 'true'; |
---|
262 | } |
---|
263 | } |
---|
264 | ?> |
---|