<?php
/**
Provides an object-oriented PHP5 class for interacting with Akismet.
@author Tristan Roberts
@date 2010-02-13
@see http://akismet.com/development/api/
@version 0.2a1
*/
class Akismet {
/**
The user agent to use when making API calls.
This can be set in __construct() or config()
It should follow the format:
Application Name/Version | Plugin Name/Version
@see http://akismet.com/development/api/
@see setUserAgent(), getUserAgent()
@var string
*/
protected static $user_agent;
/**
The API key to use when connecting.
This can be set in __construct() or config()
@see http://en.support.wordpress.com/api-keys/
@see setAPIKey(), getAPIKey()
@var string
*/
protected static $api_key;
/**
The application URL.
@see setAppURL(), getAppURL()
@var string
*/
protected static $app_url;
/**
The comment data
@see setData(), getData()
@var array
*/
protected static $data;
/**
Sets the user agent.
@param $user_agent required string The user agent to set it to.
@see getUserAgent()
*/
public function setUserAgent( $user_agent ) {
if ( !empty( $user_agent ) ) {
self::$user_agent = $user_agent;
}
}
/**
Gets the user agent.
@param $user_agent optional string The user agent to try to use.
@return string The user agent.
@see setUserAgent()
*/
public function getUserAgent( $user_agent = "" ) {
if ( empty( $user_agent ) ) {
return (string) self::$user_agent;
} else {
return (string) $user_agent;
}
}
/**
Sets the API key
@param $api_key required string The API key to set it to.
@see getAPIKey()
*/
public function setAPIKey( $api_key ) {
if ( !empty( $api_key ) ) {
self::$api_key = $api_key;
}
}
/**
Gets the API key.
@param $key optional string Another string t ouse instead.
@return string The API key.
@see setAPIKey()
*/
protected function getAPIKey( $key = "" ) {
if ( empty( $key ) ) {
return (string) self::$api_key;
} else {
return (string) $key;
}
}
/**
Sets the application URL.
@param $app_url optional string The URL of the application.
@param $auto_detect optional bool Whether to automatically detect
the URL if $app_url is empty.
@see getAppURL()
*/
public function setAppURL( $url = "", $auto_detect = true ) {
if ( !empty( $url ) ) {
self::$app_url = $url;
} else if ( $auto_detect || empty( self::$app_url ) ) {
self::$app_url = "http" .
( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == "on" ? "s" : "" ) . "://" . // Protocol
$_SERVER['SERVER_NAME'] . // Domain
( $_SERVER['SERVER_PORT'] == "80" // Port (next 2 lines as well)
|| ( $_SERVER['HTTPS'] == "on"
&& $_SERVER['SERVER_PORT'] == "443" ) ? "" : ":" . $_SERVER['SERVER_PORT'] ) .
$_SERVER['REQUEST_URI']; // Filename
}
}
/**
Gets the application URL.
@param $url optional string A URL to use instead.
@return string The application URL.
@see setAppURL()
*/
public function getAppURL( $url = "" ) {
if ( empty( $url ) ) {
return (string) self::$app_url;
} else {
return (string) $url;
}
}
/**
Sets the required data fields if not already done.
@param $data required array The data to check
@return array The checked (and updated) data.
@see setData(), getData()
*/
public function requiredData( $data ) {
if ( !isset( $data['blog'] ) ) {
$data['blog'] = self::getAppURL( );
}
if ( !isset( $data['user_ip'] ) ) {
$data['user_ip'] = $_SERVER['REMOTE_ADDR'];
}
if ( !isset( $data['user_agent'] ) ) {
$data['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
}
return (array) $data;
}
/**
Sets the comment data for later use.
@param $data required mixed An array or object of the data.
@see getData(), requiredData()
*/
public function setData( $data ) {
if ( is_array( $data ) || is_object( $data ) ) {
$data = self::requiredData( $data );
self::$data = (array) $data;
}
}
/**
Gets the comment data.
@param $data optional mixed The data to use instead.
@return array The comment data
@see setData, requiredData()
*/
public function getData( $data = "" ) {
if ( !empty( $data ) && ( is_array( $data ) || is_object( $data ) ) ) {
return (array) self::requiredData( $data );
} else {
return (array) self::requiredData( self::$data );
}
}
/**
Sets the configuration variables if provided.
@param $user_agent optional string The user agent to use.
@param $api_key optional string The API key to use.
@param $app_url optional string The application URL.
@see __construct(), setUserAgent(), setAPIKey(), setAppURL()
*/
public function config( $user_agent = "", $api_key = "", $app_url = "" ) {
self::setUserAgent( $user_agent );
self::setAPIKey( $api_key );
self::setAppURL( $app_url );
}
/**
Provides a wrapper for config().
@param $user_agent optional string The user agent to use.
@param $api_key optional string The API key to use.
@param $app_url optional string The application URL.
@see config(), setUserAgent(), setAPIKey(), setAppURL()
*/
public function __construct( $user_agent = "", $api_key = "", $app_url = "" ) {
self::config( $user_agent, $api_key, $app_url );
}
/**
Make a POST request using cURL.
@param $action required string The action (verify-key, etc) to perform.
@param $data required mixed Either an array/object or string of the POST data
@param $version optional string The API version to use.
@param $host optional string The host to connect to
@see http://au2.php.net/manual/en/book.curl.php
*/
protected function request( $action, $data, $version = "1.1", $host = "default" ) {
$request = $data;
if ( is_array( $data ) || is_object( $data ) ) {
foreach ( $data as $name => $value ) {
$data[$name] = $name . "=" . urlencode( $value );
}
$request = implode( "&", $data );
}
if ( $host == "default" ) {
$host = self::getAPIKey( ) . ".rest.akismet.com";
}
$url = "http://" . $host . "/" . $version . "/" . $action;
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $request );
curl_setopt( $curl, CURLOPT_USERAGENT, self::$user_agent );
return (string) curl_exec( $curl );
}
/**
Verifies an API key. Note: Akismet also requires your app/blog URL.
@param $key optional string The API key to use. If none is specified, use the preset one.
@param $url optional string The URL of the application. If none is specified, use the preset one.
@return bool TRUE for valid key, FALSE for invalid key or error
@see http://akismet.com/development/api/#verify-key
@see getAPIKey(), getAppURL(), request()
*/
public function verify( $key = "", $url = "" ) {
$key = self::getAPIKey( $key );
$url = self::getAppURL( $url );
$string = self::request( "verify-key", array( "key" => $key, "blog" => $url ), "1.1", "rest.akismet.com" );
return (bool) ( $string == "valid" ? true : false );
}
/**
Check whether the comment/data is spam or not.
@param $data optional array The data to check (uses getData() by default).
@return bool TRUE for spam, FALSE for not spam
@see http://akismet.com/development/api/#comment-check
@see setData(), getData(), request()
*/
public function check( $data = "" ) {
$data = self::getData( $data );
return (bool) ( self::request( "comment-check", $data ) != "false" ? true : false );
}
/**
Reports a false positive.
@param $data optional array The data to report (uses getData() by default).
@see http://akismet.com/development/api/#submit-ham
@see setData(), getData(), request()
*/
public function reportHam( $data = "" ) {
$data = self::getData( $data );
self::request( "submit-ham", $data );
}
/**
Reports a false negative.
@param $data optional array The data to report (uses getData() by default).
@see http://akismet.com/development/api/#submit-spam
@see setData(), getData(), request()
*/
public function reportSpam( $data = "" ) {
$data = self::getData( $data );
self::request( "submit-spam", $data );
}
}
?>