Simple Akismet library for CodeIgniter –
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * mBlog Akismet library * Mark LaDoux <http://markladoux.com/> * * Inspired by the DBlog Akismet library * David Behler <http://www.davidbehler.de/> * * Provides functions and utilities for checking comments against Akismet * for spam, and for reporting spam and ham. * * Changes: * * Simplified initialization process * Added internationalization support for error logs * Added more tests for logging purposes * Added library version reporting * Updated for efficiency and simplified naming conventions * Removed depreciated code * Added API key validity check reporting * Not entirely backwards compatible with 2.x * * @version 3.0.0 * @author Mark LaDoux <me@markladoux.com> * @copyright Copyright (c) 2012, Mark LaDoux * @license http://www.gnu.org/licenses/gpl.html */ class mAkismet { /** * Library Version * * @access protected * @static string */ protected static $version = '3.0.0'; /** * api version * * @access protected * @static string */ protected static $api = '1.1'; /** * User Agent String * * @access protected * @static string */ protected static $user_agent; /** * WordPress api Key * * @access protected * @static string */ protected static $api_key; /** * Valid api Key * * @access protected * @static bool */ protected static $valid_key = FALSE; /** * Akismet Server Address * * @access protected * @static string */ protected static $server = 'rest.akismet.com'; /** * Users preferred langauge * * @access protected * @static string */ protected static $lang; /** * Class Constructor * * Prepares the library for use * * @access public * @return void */ public function __construct($args = array()) { // load config files $this->load->config('mAkismet', TRUE); // Configure server address if( $this->config->item('server', 'mAkismet') !== FALSE && $this->config->item('server', 'mAkismet') != '') { self::$server = $this->config->item('server', 'mAkismet'); } // configure api key self::$api_key = $this->config->item('api_key', 'mAkismet'); // configured preferred language // ( only the admin will recieve these errors ) self::$lang = $this->config->item('language'); // configure User Agent self::$user_agent = 'CodeIgniter/'.CI_VERSION.' | mAkismet/'.self::$version; // override config values if(isset($args['server'])) self::$server = $args['server']; if(isset($args['api_key'])) // load language files TODO // $this->lang->load('mAkismet', self::$lang); // verify api key $post_values['key'] = self::$api_key; $post_values['blog'] = site_url(); $response = $this->$_request( $this->_query_string($post_values), self::$server, '/'.self::$api.'/verify-key'); self::$valid_key = (strtolower($response[1]) == 'valid') ? TRUE : FALSE; } /** * Magic Get * * Allows use of the CI SuperGlobal without defining extra variables * * @access public * @param string * @return object */ public function __get($object) { return get_instance()->$object; } /** * Verify API key (testing purposes) * * @access public * @return bool */ public function verify_api_key() { return self::$valid_key; } /** * Reports the version of the library being used * * @access public * @return string */ public function version() { return self::$version; } /** * Checks if a comment is spam * * @access public * @param array * @return bool */ public function is_spam($comment) { $output = FALSE; if(self::$valid_key) { // prepare array for query generation $post['blog'] = site_url(); $post['user_ip'] = $this->input->ip_address(); $post['user_agent'] = $this->input->user_agent(); $post['referrer'] = $this->input->server('HTTP_REFERER'); $post['comment_type'] = $comment['comment_type']; $post['comment_author'] = $comment['comment_author']; $post['comment_author_email'] = $comment['comment_author_email']; $post['comment_author_url'] = $comment['comment_author_url']; $post['comment_content'] = $comment['comment_content']; $post['permalink'] = $comment['permalink']; // get server response $response = $this->_request( $this->_query_string($post), self::$api_key.'.'.self::$server, '/'.self::$api.'/comment-check' ); // set output based on response $output = (strtolower($response[1]) == 'true') ? TRUE : FALSE; } else { // log errors log_message('error',$this->lang->line('akismet_api_error')); } return $output; } /** * Submit a comment to the Akismet server as spam * * @access public * @param array * @return void */ public function submit_spam($comment) { if(self::$valid_key) { // prepare array for query generation $post['blog'] = site_url(); $post['user_ip'] = $this->input->ip_address(); $post['user_agent'] = $this->input->user_agent(); $post['referrer'] = $this->input->server('HTTP_REFERER'); $post['comment_type'] = $comment['comment_type']; $post['comment_author'] = $comment['comment_author']; $post['comment_author_email'] = $comment['comment_author_email']; $post['comment_author_url'] = $comment['comment_author_url']; $post['comment_content'] = $comment['comment_content']; $post['permalink'] = $comment['permalink']; // submit to server $this->_request( $this->_query_string($post), self::$api_key.'.'.self::$server, '/'.self::$api.'/submit-spam' ); } else { // log errors log_message('error',$this->lang->line('akismet_api_error')); } } /** * Submit comment to the Akismet server as ham * * @access public * @param array * @return void */ public function submit_ham($comment) { if(self::$valid_key) { // prepare array for query generation $post['blog'] = site_url(); $post['user_ip'] = $this->input->ip_address(); $post['user_agent'] = $this->input->user_agent(); $post['referrer'] = $this->input->server('HTTP_REFERER'); $post['comment_type'] = $comment['comment_type']; $post['comment_author'] = $comment['comment_author']; $post['comment_author_email'] = $comment['comment_author_email']; $post['comment_author_url'] = $comment['comment_author_url']; $post['comment_content'] = $comment['comment_content']; $post['permalink'] = $comment['permalink']; // submit to server $this->_request( $this->_query_string($post), self::$api_key.'.'.self::$server, '/'.self::$api.'/submit-ham' ); } else { // log errors log_message('error',$this->lang->line('akismet_api_error')); } } /** * Generate a query string from an array * * @access protected * @param array $post_values * @return string */ protected function _query_string($post_values) { // initialize query string $query_string = ''; // generate query string foreach ($post_values as $key => $value) { if($query_string == '') { $query_string .= $key.'='.urlencode($value); } else { $query_string .= '&'.$key.'='.urlencode($value); } } // return completed string for processing return $query_string; } /** * Send a request to Akismet server and return the results as an array * * @access protected * @param string $query_string * @param string $host * @param string $path * @param string $port * @return array */ protected function _request($query_string, $host, $path, $port = 80) { // initialize the request $req = "POST {$path} HTTP/1.0n"; $req .= "Host: {$host} n"; $req .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8n"; $req .= "Content-Length: ".strlen(self::$user_agent)."n"; $req .= "User-Agent: {self::$user_agent}nn"; $req .= $query_string; //initialize the response container. $response = ''; // retrieve the response if(($fs = @fsockopen($host, $port, $errno, $errstr, 3)) !== FALSE) { fwrite($fs, $req); while(!feof($fs)) { $response .= fgets($fs, 1160); } fclose($fs); $response = explode("n", $response, 2); } // return response for processing return $response; } } /* End of file mAkismet.php */ /* Location: ./application/libraries/mAkismet.php */