<?php if (!defined('VB_ENTRY')) die('Access denied.'); class vB_Akismet { use vB_Trait_NoSerialize; protected $options; protected $hostUrl = 'rest.akismet.com'; protected $verified = null; protected $akismetVersion = '1.1'; protected $akismetKey = ''; protected $akismetBoard = ''; protected $_akismetApiUrl = null; protected static $instance; private function __construct() { $this->options = vB::getDatastore()->getValue('options'); if (!isset($this->options['vb_antispam_type']) OR empty($this->options['vb_antispam_key'])) { $this->verified = false; } else { $this->akismetKey = $this->options['vb_antispam_key']; } $this->_build(); } public static function instance() { if (empty(self::$instance)) { self::$instance = new vB_Akismet(); } return self::$instance; } protected function setParams($params) { if (!isset($params['user_ip'])) { $params['user_ip'] = $_SERVER['REMOTE_ADDR']; } if (!isset($params['user_agent'])) { if (defined('USER_AGENT')) { $params['user_agent'] = USER_AGENT; } else { $params['user_agent'] = $_SERVER['HTTP_USER_AGENT']; } } if (!isset($params['referrer'])) { if (defined('HTTP_REFERER')) { $params['referrer'] = HTTP_REFERER; } else if (!empty($_SERVER['HTTP_REFERER'])) { $params['referrer'] = $_SERVER['HTTP_REFERER']; } else { $params['referrer'] = $this->options['bburl']; } } $params['blog'] = $this->options['bburl'];; return $params; } public function verifyText($params) { if (!$this->verified) { return true; } $params = $this->setParams($params); $result = $this->_submit($this->_akismetApiUrl . '/comment-check', $params); return (strpos($result, 'true') !== false) ? 'spam' : 'ham'; } public function markAsSpam($params) { if (!$this->verified) { return true; } if (!$this->_build()) { return false; } $params = $this->setParams($params); $result = $this->_submit($this->_akismetApiUrl . '/submit-spam', $params); return $result; } public function markAsHam($params) { if (!$this->verified) { return true; } if (!$this->_build()) { return false; } $params = $this->setParams($params); $result = $this->_submit($this->_akismetApiUrl . '/submit-ham', $params); return $result; } protected function _build() { if ($this->_akismetApiUrl === null) { if (!$this->options['vb_antispam_type']) { return false; } $check_key = 'http://' . $this->hostUrl . '/' . $this->akismetVersion . '/verify-key'; if ($this->akismetKey == $this->options['vb_antispam_key'] OR strpos($this->_submit($check_key, array('key' => $this->akismetKey)), 'invalid') === false) { $this->_akismetApiUrl = 'http://' . $this->akismetKey . '.' . $this->hostUrl . '/' . $this->akismetVersion; $this->verified = true; } } return true; } protected function _submit($submitUrl, $params) { $vurl = vB::getUrlLoader(); $vurl->setOption(vB_Utility_Url::CLOSECONNECTION, 1); $result = $vurl->post($submitUrl, $params); return $result['body']; } } 