Permalink
Browse files

Initial release of Akismet datasource

  • Loading branch information...
1 parent aff3b75 commit 658f071189f93fb653140e08b776ffcc73b10f7b @felixge committed Sep 1, 2008
Showing with 146 additions and 0 deletions.
  1. +91 −0 cakephp/datasources/akismet/akismet_source.php
  2. +55 −0 cakephp/datasources/akismet/readme.textile
@@ -0,0 +1,91 @@
+<?php
+/**
+ * A CakePHP datasource for interacting with the Akismet spam protection API.
+ *
+ * Copyright 2008, Debuggable, Ltd.
+ * Hibiskusweg 26c
+ * 13089 Berlin, Germany
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2008, Debuggable, Ltd.
+ * @version 1.0
+ * @author Felix Geisendörfer <felix@debuggable.com>, Tim Koschützki <tim@debuggable.com>
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+class AkismetSource extends DataSource{
+ var $description = "Akismet";
+ var $Http = null;
+
+ function __construct($config) {
+ parent::__construct($config);
+ App::import('HttpSocket');
+ $this->Http = new HttpSocket();
+ }
+
+ function verifyKey($config = array()) {
+ if (is_string($config)) {
+ $config = array('key' => $config);
+ }
+ $config = Set::merge($this->config, $config);
+ $r = $this->Http->post('http://rest.akismet.com/1.1/verify-key', array(
+ 'key' => $config['key'],
+ 'blog' => $config['blog'],
+ ));
+ return $r === 'valid';
+ }
+
+ function isSpam($comment, $config = array()) {
+ return $this->submit('check', $comment, $config);
+ }
+
+ function submit($what, $comment, $confg = array()) {
+ $map = array(
+ 'check' => 'comment-check',
+ 'spam' => 'submit-spam',
+ 'ham' => 'submit-ham',
+ );
+ if (!isset($map[$what])) {
+ trigger_error('AkismetSource: Unknown submit method '.$what);
+ return false;
+ }
+ $method = $map[$what];
+
+ if (is_string($config)) {
+ $config = array('permalink' => $config);
+ }
+ $config = Set::merge($this->config, $config);
+ $comment = $this->normalize($comment);
+ $comment = am(array(
+ 'blog' => $config['blog'],
+ 'referrer' => env('HTTP_REFERER'),
+ 'permalink' => $config['permalink'],
+
+ ), $comment);
+ $r = $this->Http->post(sprintf('http://%s.rest.akismet.com/1.1/%s', $config['key'], $method), $comment);
+ if ($r === 'invalid') {
+ return array('error' => 'Invalid Key!');
+ }
+ return $r === 'true';
+ }
+
+ function normalize($comment, $map = array()) {
+ if (!$map) {
+ $map = $this->config['map'];
+ }
+ $r = array();
+ foreach ($map as $to => $from) {
+ if (isset($comment[$from])) {
+ $r[$to] = $comment[$from];
+ }
+ }
+ return $r;
+ }
+
+ function close() {
+ return true;
+ }
+}
+
+?>
@@ -0,0 +1,55 @@
+h1. CakePHP Amazon Associates Data Source
+
+A CakePHP datasource for interacting with the Akismet spam protection API.
+
+If you don't have an Akismet API key you can get one here: Get "Akismet API key":http://akismet.com/personal/
+
+h2. Usage / Docs
+
+1. Copy akismet_source.php to app/models/datasources/<br>
+2. Open your app/config/database.php and add the following:
+
+<pre><code>
+var $akismet = array(
+ 'datasource' => 'akismet',
+ 'blog' => 'http://your-website-url.com',
+ 'key' => 'your-api-key-here',
+ 'map' => array(
+ 'comment_author' => 'author_name',
+ 'comment_author_email' => 'author_email',
+ 'comment_author_url' => 'author_url',
+ 'user_ip' => 'author_ip',
+ 'user_agent' => 'author_agent',
+ 'comment_type' => 'type',
+ 'comment_content' => 'text',
+ )
+);
+</code></pre>
+
+The map key above tells the API what model fields of yours map to what fields the API expects to receive. If you use this API with multiple models you can dynamically pass different maps upon each request, or perform the normalization yourself before passing your data to the datasource.
+
+3. Add this validation logic to your Comment model:
+
+<pre><code>
+class Comment extends AppModel{
+ var $validate = array(
+ 'text' => array(
+ array(
+ 'rule' => array('notSpam'),
+ 'message' => 'This comment appears to be spam. Please contact us if the problem persists.',
+ 'required' => true,
+ ),
+ ),
+ );
+
+ function notSpam($fields) {
+ $akismet = ConnectionManager::getDataSource('akismet');
+ return !$akismet->isSpam($this->data['Comment'], Post::url($this->data['Comment']['post_id']));
+ }
+
+}
+</code></pre>
+
+h2. Known Bugs
+
+None

0 comments on commit 658f071

Please sign in to comment.