Dotclear

source: plugins/akismet/class.dc.filter.akismet.php @ 1179:a43a29427ef3

Revision 1179:a43a29427ef3, 6.0 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Update copyright notice

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                    http::redirect($url.'&up=1');
120               }
121               catch (Exception $e)
122               {
123                    $this->core->error->add($e->getMessage());
124               }
125          }
126         
127          if ($blog->settings->akismet->ak_key)
128          {
129               try {
130                    $ak = new akismet($blog->url,$blog->settings->akismet->ak_key);
131                    $ak_verified = $ak->verify();
132               } catch (Exception $e) {
133                    $this->core->error->add($e->getMessage());
134               }
135          }
136         
137          $res =
138          '<form action="'.html::escapeURL($url).'" method="post" class="fieldset">'.
139          '<p><label for="ak_key" class="classic">'.__('Akismet API key:').' '.
140          form::field('ak_key',12,128,$ak_key).'</label>';
141         
142          if ($ak_verified !== null) {
143               if ($ak_verified) {
144                    $res .= ' <img src="images/check-on.png" alt="" /> '.__('API key verified');
145               } else {
146                    $res .= ' <img src="images/check-off.png" alt="" /> '.__('API key not verified');
147               }
148          }
149         
150          $res .= '</p>';
151         
152          $res .=
153          '<p><a href="http://akismet.com/">'.__('Get your own API key').'</a></p>'.
154          '<p><input type="submit" value="'.__('Save').'" />'.
155          $this->core->formNonce().'</p>'.
156          '</form>';
157         
158          return $res;
159     }
160}
161
162class akismet extends netHttp
163{
164     protected $base_host = 'rest.akismet.com';
165     protected $ak_host = '';
166     protected $ak_version = '1.1';
167     protected $ak_path = '/%s/%s';
168     
169     protected $ak_key = null;
170     protected $blog_url;
171     
172     protected $timeout = 3;
173     
174     public function __construct($blog_url,$api_key)
175     {
176          $this->blog_url = $blog_url;
177          $this->ak_key = $api_key;
178         
179          $this->ak_path = sprintf($this->ak_path,$this->ak_version,'%s');
180          $this->ak_host = $this->ak_key.'.'.$this->base_host;
181         
182          parent::__construct($this->ak_host,80);
183     }
184     
185     public function verify()
186     {
187          $this->host = $this->base_host;
188          $path = sprintf($this->ak_path,'verify-key');
189         
190          $data = array(
191               'key' => $this->ak_key,
192               'blog' => $this->blog_url
193          );
194         
195          if ($this->post($path,$data,'UTF-8'))
196          {
197               return $this->getContent() == 'valid';
198          }
199         
200          return false;
201     }
202     
203     public function comment_check($permalink,$type,$author,$email,$url,$content)
204     {
205          $info_ignore = array('HTTP_COOKIE');
206          $info = array();
207         
208          foreach ($_SERVER as $k => $v) {
209               if (strpos($k,'HTTP_') === 0 && !in_array($k,$info_ignore)) {
210                    $info[$k] = $v;
211               }
212          }
213         
214          return $this->callFunc('comment-check',$permalink,$type,$author,$email,$url,$content,$info);
215     }
216     
217     public function submit_spam($permalink,$type,$author,$email,$url,$content)
218     {
219          $this->callFunc('submit-spam',$permalink,$type,$author,$email,$url,$content);
220          return true;
221     }
222     
223     public function submit_ham($permalink,$type,$author,$email,$url,$content)
224     {
225          $this->callFunc('submit-ham',$permalink,$type,$author,$email,$url,$content);
226          return true;
227     }
228     
229     protected function callFunc($function,$permalink,$type,$author,$email,$url,$content,$info=array())
230     {
231          $ua = isset($info['HTTP_USER_AGENT']) ? $info['HTTP_USER_AGENT'] : '';
232          $referer = isset($info['HTTP_REFERER']) ? $info['HTTP_REFERER'] : '';
233         
234          # Prepare comment data
235          $data = array(
236               'blog' => $this->blog_url,
237               'user_ip' => http::realIP(),
238               'user_agent' => $ua,
239               'referrer' => $referer,
240               'permalink' => $permalink,
241               'comment_type' => $type,
242               'comment_author' => $author,
243               'comment_author_email' => $email,
244               'comment_author_url' => $url,
245               'comment_content' => $content
246          );
247         
248          $data = array_merge($data,$info);
249         
250          $this->host = $this->ak_host;
251          $path = sprintf($this->ak_path,$function);
252         
253          if (!$this->post($path,$data,'UTF-8')) {
254               throw new Exception('HTTP error: '.$this->getError());
255          }
256         
257          return $this->getContent() == 'true';
258     }
259}
260?>
Note: See TracBrowser for help on using the repository browser.

Sites map