[Solar-svn] Revision 3075

pmjones at solarphp.com pmjones at solarphp.com
Sat Apr 5 10:20:42 CDT 2008


Solar_Service_Akismet: [NEW] First attempt at a web service class for Akismet spam-checking.


Added: trunk/Solar/Service/Akismet.php
===================================================================
--- trunk/Solar/Service/Akismet.php	                        (rev 0)
+++ trunk/Solar/Service/Akismet.php	2008-04-05 15:20:32 UTC (rev 3075)
@@ -0,0 +1,367 @@
+<?php
+/**
+ * 
+ * Acts as an interface to the Akismet spam-checking service.
+ * 
+ *   <http://akismet.com>
+ * 
+ * You will need to register for an Akismet API key to use the service. Once
+ * you have that, you can use the service like so:
+ * 
+ * {{code: php
+ *     $akismet = Solar::factory('Solar_Service_Akismet', array(
+ *         'key'  => 'apikeyvalue',
+ *         'blog' => 'http://example.com',
+ *     ));
+ *     
+ *     $comment = array(
+ *         'permalink'             => 'http://example.net/blog/read/1',
+ *         'comment_type'          => 'comment',
+ *         'comment_author'        => 'nobody',
+ *         'comment_author_email'  => 'nobody at example.com',
+ *         'comment_author_url'    => 'http://example.org/nobody.html',
+ *         'comment_content'       => 'The comment text.',
+ *     );
+ *     
+ *     $is_spam = $akismet->commentCheck($data);
+ * }}
+ * 
+ * @category Solar
+ * 
+ * @package Solar_Service
+ * 
+ * @subpackage Solar_Service_Akismet
+ * 
+ * @author Paul M. Jones <pmjones at solarphp.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version $Id: Filter.php 2976 2008-01-03 14:05:49Z pmjones $
+ * 
+ */
+class Solar_Service_Akismet extends Solar_Base {
+    
+    /**
+     * 
+     * User-defined configuration values.
+     * 
+     * Keys are ...
+     * 
+     * `key`
+     * : (string) The Akismet service API key.
+     * 
+     * `blog`
+     * : (string) The front page to the blog, wiki, site, etc.
+     * 
+     * `http`
+     * : (array) A configuration array for the Solar_Http_Request object to
+     *   be used internally.
+     * 
+     * `request`
+     * : (dependency) A Solar_Request dependency injection. Default 'request'.
+     * 
+     * @var array
+     * 
+     */
+    protected $_Solar_Service_Akismet = array(
+        'key'     => null,      // api key
+        'blog'    => null,      // blog uri
+        'http'    => array(),   // http request config elements
+        'request' => 'request', // DI for request environment
+    );
+    
+    /**
+     * 
+     * Has the API key been verified?
+     * 
+     * @var bool
+     * 
+     */
+    protected $_key_verified = false;
+    
+    /**
+     * 
+     * The request environment.
+     * 
+     * @var Solar_Request
+     * 
+     */
+    protected $_request;
+    
+    /**
+     * 
+     * The most-recent HTTP response from Akismet.
+     * 
+     * @var Solar_Http_Response
+     * 
+     * @see getResponse()
+     * 
+     */
+    protected $_response;
+    
+    /**
+     * 
+     * Constructor.
+     * 
+     * @param array $config User-defined configuration values.
+     * 
+     */
+    public function __construct($config = null)
+    {
+        parent::__construct($config);
+        $this->_request = Solar::dependency(
+            'Solar_Request',
+            $this->_config['request']
+        );
+    }
+    
+    /**
+     * 
+     * Get the most-recent response sent from Akismet.
+     * 
+     * @return Solar_Http_Response
+     * 
+     */
+    public function getResponse()
+    {
+        return $this->_response;
+    }
+    
+    /**
+     * 
+     * Verifies the API key with Akismet.
+     * 
+     * @return bool True if the key is valid, false if not.
+     * 
+     */
+    public function verifyKey()
+    {
+        if ($this->_key_verified) {
+            return true;
+        }
+        
+        $response = $this->_fetchResponse('verify-key', array(
+            'key'  => $this->_config['key'],
+            'blog' => $this->_config['blog'],
+        ));
+        
+        if ($response->content == 'valid') {
+            $this->_key_verified = true;
+        } else {
+            $this->_key_verified = false;
+            throw $this->_exception('ERR_INVALID_KEY', array(
+                'key'   => $this->_config['key'],
+                'blog'  => $this->_config['blog'],
+                'debug' => $response->getHeader('X-Akismet-Debug-Help'),
+            ));
+        }
+    }
+    
+    /**
+     * 
+     * Checks the comment data with Akismet to see if it is spam.
+     * 
+     * See the [[_prepareData()]] method for the list of data keys.
+     * 
+     * @param array $data The comment data to be checked for spam.
+     * 
+     * @return bool True if the comment data is spam, false if not.
+     * 
+     * @see _prepareData()
+     * 
+     */
+    public function commentCheck($data)
+    {
+        // verify the key
+        $this->verifyKey();
+        
+        // prep the data elements
+        $this->_prepareData($data);
+        
+        // check the comment
+        $response = $this->_fetchResponse('comment-check', $data);
+        if ($response->content == 'true') {
+            return true;
+        } elseif ($response->content == 'false') {
+            return false;
+        } else {
+            throw $this->_exception('ERR_STRANGE_RESPONSE', array(
+                'response' => $response,
+                'data'     => $data,
+            ));
+        }
+    }
+    
+    /**
+     * 
+     * Submits data to Akismet to establish it as ham (i.e., not spam).
+     * 
+     * See the [[_prepareData()]] method for the list of data keys.
+     * 
+     * @param array $data The comment data to be established as ham.
+     * 
+     * @return void
+     * 
+     * @see _prepareData()
+     * 
+     */
+    public function submitHam($data)
+    {
+        // verify the key
+        $this->verifyKey();
+        
+        // prep the data elements
+        $this->_prepareData($data);
+        
+        // submit as ham
+        $this->_fetchResponse('submit-ham', $data);
+    }
+    
+    /**
+     * 
+     * Submits data to Akismet to establish it as spam.
+     * 
+     * See the [[_prepareData()]] method for the list of data keys.
+     * 
+     * @param array $data The comment data to be established as spam.
+     * 
+     * @return void
+     * 
+     * @see _prepareData()
+     * 
+     */
+    public function submitSpam($data)
+    {
+        // verify the key
+        $this->verifyKey();
+        
+        // prep the data elements
+        $this->_prepareData($data);
+        
+        // submit as spam
+        $this->_fetchResponse('submit-spam', $data);
+    }
+    
+    /**
+     * 
+     * Calls the Akismet REST API via an HTTP POST request, and returns the
+     * HTTP response.
+     * 
+     * @param string $call The Akismet REST call to make: 'verify-key', 
+     * 'comment-check', 'submit-ham', or 'submit-spam'.
+     * 
+     * @param array $data Data to send via POST.
+     * 
+     * @return Solar_Http_Response
+     * 
+     */
+    protected function _fetchResponse($call, $data)
+    {
+        $key = $this->_config['key'];
+        
+        // get a URI based on the call type
+        switch ($call) {
+        case 'verify-key':
+            $uri = "http://rest.akismet.com/1.1/verify-key";
+            break;
+        case 'comment-check':
+            $uri = "http://$key.rest.akismet.com/1.1/comment-check";
+            break;
+        case 'submit-ham':
+            $uri = "http://$key.rest.akismet.com/1.1/submit-ham";
+            break;
+        case 'submit-spam':
+            $uri = "http://$key.rest.akismet.com/1.1/submit-spam";
+            break;
+        default:
+            throw $this->_exception('ERR_UNKNOWN_CALL', array(
+                'call' => $call,
+                'data' => $data,
+            ));
+            break;
+        }
+        
+        // build the request
+        $request = Solar::factory(
+            'Solar_Http_Request',
+            $this->_config['http']
+        );
+        
+        $request->setUri($uri)
+                ->setUserAgent('Solar/1.0 | Akismet/1.1')
+                ->setMethod('post')
+                ->setContent($data);
+        
+        // get the response, and done
+        $this->_response = $request->fetch();
+        return $this->_response;
+    }
+    
+    /**
+     * 
+     * Prepares the comment, spam, or ham comment data **by reference** for
+     * submission to Akismet.
+     * 
+     * The $data keys are:
+     * 
+     * `blog`
+     * : The front page or home URL of the instance making the
+     *   request. For a blog or wiki this would be the front page. Must be a 
+     *   full URI, including 'http://'. Default is the config value for
+     *   `blog`.
+     * 
+     * `user_ip`
+     * : IP address of the comment submitter.  Default is the
+     *   server REMOTE_ADDR value.
+     * 
+     * `user_agent`
+     * : User agent information.  Default is the server 
+     *   HTTP_USER_AGENT value.
+     * 
+     * `referrer` (note spelling)
+     * : Default is the HTTP_REFERER value.
+     * 
+     * `permalink`
+     * : The permanent location of the entry the comment was submitted to.
+     * 
+     * `comment_type`
+     * : May be blank, 'comment', 'trackback', 'pingback', or any other value 
+     *   (e.g., 'registration').  Default blank.
+     * 
+     * `comment_author`
+     * : Submitted name with the comment.  Default blank. Leaving blank is 
+     *   highly likely to result in a "spam" result.
+     * 
+     * `comment_author_email`
+     * : Submitted email address
+     * 
+     * `comment_author_url`
+     * : Commenter URL.
+     * 
+     * `comment_content`
+     * : The content that was submitted.
+     * 
+     * @param array &$data The data to prepare **by reference**.
+     * 
+     * @return void
+     * 
+     */
+    protected function _prepareData(&$data)
+    {
+        $base = array(
+            'blog'                  => $this->_config['blog'],
+            'user_ip'               => $this->_request->server('REMOTE_ADDR'),
+            'user_agent'            => $this->_request->http('user_agent'),
+            'referrer'              => $this->_request->http('referer'),
+            'permalink'             => null,
+            'comment_type'          => null,
+            'comment_author'        => null,
+            'comment_author_email'  => null,
+            'comment_author_url'    => null,
+            'comment_content'       => null,
+        );
+        
+        // merge the base info, data overrides, and the server info
+        $data = array_merge($base, $data, $this->_request->server());
+    }
+}
\ No newline at end of file




More information about the Solar-svn mailing list