Dotclear

source: plugins/akismet/class.dc.filter.akismet.php @ 2205:4d5c86bc51e9

Revision 2205:4d5c86bc51e9, 6.1 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Add success messages using the new notices system. Closes #1494, #1495

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

Sites map