[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/plugins/akismet/ -> class.dc.filter.akismet.php (source)

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


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7