Posted By

mladoux on 05/27/12


Tagged

codeigniter mBlog Akismet


Versions (?)

mBlog Akismet Library


 / Published in: PHP
 

URL: http://markladoux.com

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.

  • Simplified initialization process
  • Added internationalization support for error logs
  • Added more tests for logging purposes
  • Added library version reporting

version 3.0.0 license http://www.gnu.org/licenses/gpl.html

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * mBlog Akismet library
  4.  * Mark LaDoux <http://markladoux.com/>
  5.  *
  6.  * Inspired by the DBlog Akismet library
  7.  * David Behler <http://www.davidbehler.de/>
  8.  *
  9.  * Provides functions and utilities for checking comments against Akismet
  10.  * for spam, and for reporting spam and ham.
  11.  *
  12.  * Changes:
  13.  *
  14.  * Simplified initialization process
  15.  * Added internationalization support for error logs
  16.  * Added more tests for logging purposes
  17.  * Added library version reporting
  18.  * Updated for efficiency and simplified naming conventions
  19.  * Removed depreciated code
  20.  * Added API key validity check reporting
  21.  * Not entirely backwards compatible with 2.x
  22.  *
  23.  * @version 3.0.0
  24.  * @author Mark LaDoux <[email protected]>
  25.  * @copyright Copyright (c) 2012, Mark LaDoux
  26.  * @license http://www.gnu.org/licenses/gpl.html
  27.  */
  28.  
  29. class mAkismet
  30. {
  31. /**
  32.   * Library Version
  33.   *
  34.   * @access protected
  35.   * @static string
  36.   */
  37. protected static $version = '3.0.0';
  38.  
  39. /**
  40.   * api version
  41.   *
  42.   * @access protected
  43.   * @static string
  44.   */
  45. protected static $api = '1.1';
  46.  
  47. /**
  48.   * User Agent String
  49.   *
  50.   * @access protected
  51.   * @static string
  52.   */
  53. protected static $user_agent;
  54.  
  55. /**
  56.   * WordPress api Key
  57.   *
  58.   * @access protected
  59.   * @static string
  60.   */
  61. protected static $api_key;
  62.  
  63. /**
  64.   * Valid api Key
  65.   *
  66.   * @access protected
  67.   * @static bool
  68.   */
  69. protected static $valid_key = FALSE;
  70.  
  71. /**
  72.   * Akismet Server Address
  73.   *
  74.   * @access protected
  75.   * @static string
  76.   */
  77. protected static $server = 'rest.akismet.com';
  78.  
  79. /**
  80.   * Users preferred langauge
  81.   *
  82.   * @access protected
  83.   * @static string
  84.   */
  85. protected static $lang;
  86.  
  87. /**
  88.   * Class Constructor
  89.   *
  90.   * Prepares the library for use
  91.   *
  92.   * @access public
  93.   * @return void
  94.   */
  95. public function __construct($args = array())
  96. {
  97. // load config files
  98. $this->load->config('mAkismet', TRUE);
  99.  
  100. // Configure server address
  101. if(
  102. $this->config->item('server', 'mAkismet') !== FALSE &&
  103. $this->config->item('server', 'mAkismet') != '')
  104. {
  105. self::$server = $this->config->item('server', 'mAkismet');
  106. }
  107.  
  108. // configure api key
  109. self::$api_key = $this->config->item('api_key', 'mAkismet');
  110.  
  111. // configured preferred language
  112. // ( only the admin will recieve these errors )
  113. self::$lang = $this->config->item('language');
  114.  
  115. // configure User Agent
  116. self::$user_agent = 'CodeIgniter/'.CI_VERSION.' | mAkismet/'.self::$version;
  117.  
  118. // override config values
  119. if(isset($args['server'])) self::$server = $args['server'];
  120. if(isset($args['api_key']))
  121.  
  122. // load language files TODO
  123. // $this->lang->load('mAkismet', self::$lang);
  124.  
  125. // verify api key
  126. $post_values['key'] = self::$api_key;
  127. $post_values['blog'] = site_url();
  128. $response = $this->$_request(
  129. $this->_query_string($post_values),
  130. self::$server,
  131. '/'.self::$api.'/verify-key');
  132. self::$valid_key = (strtolower($response[1]) == 'valid') ? TRUE : FALSE;
  133. }
  134.  
  135. /**
  136.   * Magic Get
  137.   *
  138.   * Allows use of the CI SuperGlobal without defining extra variables
  139.   *
  140.   * @access public
  141.   * @param string
  142.   * @return object
  143.   */
  144. public function __get($object)
  145. {
  146. return get_instance()->$object;
  147. }
  148.  
  149. /**
  150.   * Verify API key (testing purposes)
  151.   *
  152.   * @access public
  153.   * @return bool
  154.   */
  155. public function verify_api_key()
  156. {
  157. return self::$valid_key;
  158. }
  159.  
  160. /**
  161.   * Reports the version of the library being used
  162.   *
  163.   * @access public
  164.   * @return string
  165.   */
  166. public function version()
  167. {
  168. return self::$version;
  169. }
  170.  
  171. /**
  172.   * Checks if a comment is spam
  173.   *
  174.   * @access public
  175.   * @param array
  176.   * @return bool
  177.   */
  178. public function is_spam($comment)
  179. {
  180. $output = FALSE;
  181. if(self::$valid_key)
  182. {
  183. // prepare array for query generation
  184. $post['blog'] = site_url();
  185. $post['user_ip'] = $this->input->ip_address();
  186. $post['user_agent'] = $this->input->user_agent();
  187. $post['referrer'] = $this->input->server('HTTP_REFERER');
  188. $post['comment_type'] = $comment['comment_type'];
  189. $post['comment_author'] = $comment['comment_author'];
  190. $post['comment_author_email'] = $comment['comment_author_email'];
  191. $post['comment_author_url'] = $comment['comment_author_url'];
  192. $post['comment_content'] = $comment['comment_content'];
  193. $post['permalink'] = $comment['permalink'];
  194.  
  195. // get server response
  196. $response = $this->_request(
  197. $this->_query_string($post),
  198. self::$api_key.'.'.self::$server,
  199. '/'.self::$api.'/comment-check'
  200. );
  201.  
  202. // set output based on response
  203. $output = (strtolower($response[1]) == 'true') ? TRUE : FALSE;
  204. }
  205. else
  206. {
  207. // @todo setup up language files and log errors
  208. // log_message('error',$this->lang->line('akismet_api_error'));
  209. }
  210. return $output;
  211. }
  212.  
  213. /**
  214.   * Submit a comment to the Akismet server as spam
  215.   *
  216.   * @access public
  217.   * @param array
  218.   * @return void
  219.   */
  220. public function submit_spam($comment)
  221. {
  222. if(self::$valid_key)
  223. {
  224. // prepare array for query generation
  225. $post['blog'] = site_url();
  226. $post['user_ip'] = $this->input->ip_address();
  227. $post['user_agent'] = $this->input->user_agent();
  228. $post['referrer'] = $this->input->server('HTTP_REFERER');
  229. $post['comment_type'] = $comment['comment_type'];
  230. $post['comment_author'] = $comment['comment_author'];
  231. $post['comment_author_email'] = $comment['comment_author_email'];
  232. $post['comment_author_url'] = $comment['comment_author_url'];
  233. $post['comment_content'] = $comment['comment_content'];
  234. $post['permalink'] = $comment['permalink'];
  235.  
  236. // submit to server
  237. $this->_request(
  238. $this->_query_string($post),
  239. self::$api_key.'.'.self::$server,
  240. '/'.self::$api.'/submit-spam'
  241. );
  242. }
  243. else
  244. {
  245. // @todo setup up language files and log errors
  246. // log_message('error',$this->lang->line('akismet_api_error'));
  247. }
  248. }
  249.  
  250. /**
  251.   * Submit comment to the Akismet server as ham
  252.   *
  253.   * @access public
  254.   * @param array
  255.   * @return void
  256.   */
  257. public function submit_ham($comment)
  258. {
  259. if(self::$valid_key)
  260. {
  261. // prepare array for query generation
  262. $post['blog'] = site_url();
  263. $post['user_ip'] = $this->input->ip_address();
  264. $post['user_agent'] = $this->input->user_agent();
  265. $post['referrer'] = $this->input->server('HTTP_REFERER');
  266. $post['comment_type'] = $comment['comment_type'];
  267. $post['comment_author'] = $comment['comment_author'];
  268. $post['comment_author_email'] = $comment['comment_author_email'];
  269. $post['comment_author_url'] = $comment['comment_author_url'];
  270. $post['comment_content'] = $comment['comment_content'];
  271. $post['permalink'] = $comment['permalink'];
  272.  
  273. // submit to server
  274. $this->_request(
  275. $this->_query_string($post),
  276. self::$api_key.'.'.self::$server,
  277. '/'.self::$api.'/submit-ham'
  278. );
  279. }
  280. else
  281. {
  282. // @todo setup up language files and log errors
  283. // log_message('error',$this->lang->line('akismet_api_error'));
  284. }
  285. }
  286.  
  287. /**
  288.   * Generate a query string from an array
  289.   *
  290.   * @access protected
  291.   * @param array $post_values
  292.   * @return string
  293.   */
  294. protected function _query_string($post_values)
  295. {
  296. // initialize query string
  297. $query_string = '';
  298.  
  299. // generate query string
  300. foreach ($post_values as $key => $value) {
  301. if($query_string == '')
  302. {
  303. $query_string .= $key.'='.urlencode($value);
  304. }
  305. else
  306. {
  307. $query_string .= '&'.$key.'='.urlencode($value);
  308. }
  309. }
  310.  
  311. // return completed string for processing
  312. return $query_string;
  313. }
  314.  
  315. /**
  316.   * Send a request to Akismet server and return the results as an array
  317.   *
  318.   * @access protected
  319.   * @param string $query_string
  320.   * @param string $host
  321.   * @param string $path
  322.   * @param string $port
  323.   * @return array
  324.   */
  325. protected function _request($query_string, $host, $path, $port = 80)
  326. {
  327. // initialize the request
  328. $req = "POST {$path} HTTP/1.0\n";
  329. $req .= "Host: {$host} \n";
  330. $req .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\n";
  331. $req .= "Content-Length: ".strlen(self::$user_agent)."\n";
  332. $req .= "User-Agent: {self::$user_agent}\n\n";
  333. $req .= $query_string;
  334.  
  335. //initialize the response container.
  336. $response = '';
  337.  
  338. // retrieve the response
  339. if(($fs = @fsockopen($host, $port, $errno, $errstr, 3)) !== FALSE)
  340. {
  341. fwrite($fs, $req);
  342. while(!feof($fs))
  343. {
  344. $response .= fgets($fs, 1160);
  345. }
  346. fclose($fs);
  347. $response = explode("\n", $response, 2);
  348. }
  349.  
  350. // return response for processing
  351. return $response;
  352. }
  353. }
  354.  
  355. /* End of file mAkismet.php */
  356. /* Location: ./application/libraries/mAkismet.php */

Report this snippet  

You need to login to post a comment.