fixes #308
This commit is contained in:
parent
15e62f7aa9
commit
13aaa4fbce
@ -33,69 +33,65 @@
|
|||||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Error constants
|
// Error constants
|
||||||
define("AKISMET_SERVER_NOT_FOUND", 0);
|
define("AKISMET_SERVER_NOT_FOUND", 0);
|
||||||
define("AKISMET_RESPONSE_FAILED", 1);
|
define("AKISMET_RESPONSE_FAILED", 1);
|
||||||
define("AKISMET_INVALID_KEY", 2);
|
define("AKISMET_INVALID_KEY", 2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Base class to assist in error handling between Akismet classes
|
// Base class to assist in error handling between Akismet classes
|
||||||
class AkismetObject {
|
class AkismetObject {
|
||||||
var $errors = array();
|
|
||||||
|
|
||||||
|
var $errors = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a new error to the errors array in the object
|
* Add a new error to the errors array in the object
|
||||||
*
|
*
|
||||||
* @param String $name A name (array key) for the error
|
* @param String $name
|
||||||
* @param String $string The error message
|
* A name (array key) for the error
|
||||||
|
* @param String $string
|
||||||
|
* The error message
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
// Set an error in the object
|
// Set an error in the object
|
||||||
function setError($name, $message) {
|
function setError($name, $message) {
|
||||||
$this->errors[$name] = $message;
|
$this->errors [$name] = $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a specific error message from the errors array
|
* Return a specific error message from the errors array
|
||||||
*
|
*
|
||||||
* @param String $name The name of the error you want
|
* @param String $name
|
||||||
|
* The name of the error you want
|
||||||
* @return mixed Returns a String if the error exists, a false boolean if it does not exist
|
* @return mixed Returns a String if the error exists, a false boolean if it does not exist
|
||||||
*/
|
*/
|
||||||
function getError($name) {
|
function getError($name) {
|
||||||
if($this->isError($name)) {
|
if ($this->isError($name)) {
|
||||||
return $this->errors[$name];
|
return $this->errors [$name];
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return all errors in the object
|
* Return all errors in the object
|
||||||
*
|
*
|
||||||
* @return String[]
|
* @return String[]
|
||||||
*/
|
*/
|
||||||
function getErrors() {
|
function getErrors() {
|
||||||
return (array)$this->errors;
|
return (array) $this->errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a certain error exists
|
* Check if a certain error exists
|
||||||
*
|
*
|
||||||
* @param String $name The name of the error you want
|
* @param String $name
|
||||||
|
* The name of the error you want
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
function isError($name) {
|
function isError($name) {
|
||||||
return isset($this->errors[$name]);
|
return isset($this->errors [$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if any errors exist
|
* Check if any errors exist
|
||||||
*
|
*
|
||||||
@ -105,23 +101,24 @@ class AkismetObject {
|
|||||||
return (count($this->errors) > 0);
|
return (count($this->errors) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Used by the Akismet class to communicate with the Akismet service
|
// Used by the Akismet class to communicate with the Akismet service
|
||||||
class AkismetHttpClient extends AkismetObject {
|
class AkismetHttpClient extends AkismetObject {
|
||||||
var $akismetVersion = '1.1';
|
|
||||||
var $con;
|
|
||||||
var $host;
|
|
||||||
var $port;
|
|
||||||
var $apiKey;
|
|
||||||
var $blogUrl;
|
|
||||||
var $errors = array();
|
|
||||||
|
|
||||||
|
var $akismetVersion = '1.1';
|
||||||
|
|
||||||
|
var $con;
|
||||||
|
|
||||||
|
var $host;
|
||||||
|
|
||||||
|
var $port;
|
||||||
|
|
||||||
|
var $apiKey;
|
||||||
|
|
||||||
|
var $blogUrl;
|
||||||
|
|
||||||
|
var $errors = array();
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
function __construct($host, $blogUrl, $apiKey, $port = 80) {
|
function __construct($host, $blogUrl, $apiKey, $port = 80) {
|
||||||
@ -131,31 +128,22 @@ class AkismetHttpClient extends AkismetObject {
|
|||||||
$this->apiKey = $apiKey;
|
$this->apiKey = $apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Use the connection active in $con to get a response from the server and return that response
|
// Use the connection active in $con to get a response from the server and return that response
|
||||||
function getResponse($request, $path, $type = "post", $responseLength = 1160) {
|
function getResponse($request, $path, $type = "post", $responseLength = 1160) {
|
||||||
$this->_connect();
|
$this->_connect();
|
||||||
|
|
||||||
if($this->con && !$this->isError(AKISMET_SERVER_NOT_FOUND)) {
|
if ($this->con && !$this->isError(AKISMET_SERVER_NOT_FOUND)) {
|
||||||
$request =
|
$request = strToUpper($type) . " /{$this->akismetVersion}/$path HTTP/1.0\r\n" . "Host: " . ((!empty($this->apiKey)) ? $this->apiKey . "." : null) . "{$this->host}\r\n" . "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" . "Content-Length: " . strlen($request) . "\r\n" . "User-Agent: Akismet PHP4 Class\r\n" . "\r\n" . $request;
|
||||||
strToUpper($type)." /{$this->akismetVersion}/$path HTTP/1.0\r\n" .
|
|
||||||
"Host: ".((!empty($this->apiKey)) ? $this->apiKey."." : null)."{$this->host}\r\n" .
|
|
||||||
"Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" .
|
|
||||||
"Content-Length: ".strlen($request)."\r\n" .
|
|
||||||
"User-Agent: Akismet PHP4 Class\r\n" .
|
|
||||||
"\r\n" .
|
|
||||||
$request
|
|
||||||
;
|
|
||||||
$response = "";
|
$response = "";
|
||||||
|
|
||||||
@fwrite($this->con, $request);
|
@fwrite($this->con, $request);
|
||||||
|
|
||||||
while(!feof($this->con)) {
|
while (!feof($this->con)) {
|
||||||
$response .= @fgets($this->con, $responseLength);
|
$response .= @fgets($this->con, $responseLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = explode("\r\n\r\n", $response, 2);
|
$response = explode("\r\n\r\n", $response, 2);
|
||||||
return $response[1];
|
return $response [1];
|
||||||
} else {
|
} else {
|
||||||
$this->setError(AKISMET_RESPONSE_FAILED, "The response could not be retrieved.");
|
$this->setError(AKISMET_RESPONSE_FAILED, "The response could not be retrieved.");
|
||||||
}
|
}
|
||||||
@ -163,33 +151,30 @@ class AkismetHttpClient extends AkismetObject {
|
|||||||
$this->_disconnect();
|
$this->_disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Connect to the Akismet server and store that connection in the instance variable $con
|
// Connect to the Akismet server and store that connection in the instance variable $con
|
||||||
function _connect() {
|
function _connect() {
|
||||||
if(!($this->con = @fsockopen($this->host, $this->port))) {
|
if (!($this->con = @fsockopen($this->host, $this->port))) {
|
||||||
$this->setError(AKISMET_SERVER_NOT_FOUND, "Could not connect to akismet server.");
|
$this->setError(AKISMET_SERVER_NOT_FOUND, "Could not connect to akismet server.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Close the connection to the Akismet server
|
// Close the connection to the Akismet server
|
||||||
function _disconnect() {
|
function _disconnect() {
|
||||||
@fclose($this->con);
|
@fclose($this->con);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// The controlling class. This is the ONLY class the user should instantiate in
|
// The controlling class. This is the ONLY class the user should instantiate in
|
||||||
// order to use the Akismet service!
|
// order to use the Akismet service!
|
||||||
class Akismet extends AkismetObject {
|
class Akismet extends AkismetObject {
|
||||||
|
|
||||||
var $apiPort = 80;
|
var $apiPort = 80;
|
||||||
|
|
||||||
var $akismetServer = 'rest.akismet.com';
|
var $akismetServer = 'rest.akismet.com';
|
||||||
|
|
||||||
var $akismetVersion = '1.1';
|
var $akismetVersion = '1.1';
|
||||||
|
|
||||||
var $http;
|
var $http;
|
||||||
|
|
||||||
var $ignore = array(
|
var $ignore = array(
|
||||||
@ -209,18 +194,22 @@ class Akismet extends AkismetObject {
|
|||||||
);
|
);
|
||||||
|
|
||||||
var $blogUrl = "";
|
var $blogUrl = "";
|
||||||
var $apiKey = "";
|
|
||||||
var $comment = array();
|
|
||||||
|
|
||||||
|
var $apiKey = "";
|
||||||
|
|
||||||
|
var $comment = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* Set instance variables, connect to Akismet, and check API key
|
* Set instance variables, connect to Akismet, and check API key
|
||||||
*
|
*
|
||||||
* @param String $blogUrl The URL to your own blog
|
* @param String $blogUrl
|
||||||
* @param String $apiKey Your wordpress API key
|
* The URL to your own blog
|
||||||
* @param String[] $comment A formatted comment array to be examined by the Akismet service
|
* @param String $apiKey
|
||||||
|
* Your wordpress API key
|
||||||
|
* @param String[] $comment
|
||||||
|
* A formatted comment array to be examined by the Akismet service
|
||||||
* @return Akismet
|
* @return Akismet
|
||||||
*/
|
*/
|
||||||
function __construct($blogUrl, $apiKey, $comment = array()) {
|
function __construct($blogUrl, $apiKey, $comment = array()) {
|
||||||
@ -230,17 +219,16 @@ class Akismet extends AkismetObject {
|
|||||||
|
|
||||||
// Connect to the Akismet server and populate errors if they exist
|
// Connect to the Akismet server and populate errors if they exist
|
||||||
$this->http = new AkismetHttpClient($this->akismetServer, $blogUrl, $apiKey);
|
$this->http = new AkismetHttpClient($this->akismetServer, $blogUrl, $apiKey);
|
||||||
if($this->http->errorsExist()) {
|
if ($this->http->errorsExist()) {
|
||||||
$this->errors = array_merge($this->errors, $this->http->getErrors());
|
$this->errors = array_merge($this->errors, $this->http->getErrors());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the API key is valid
|
// Check if the API key is valid
|
||||||
if(!$this->_isValidApiKey($apiKey)) {
|
if (!$this->_isValidApiKey($apiKey)) {
|
||||||
$this->setError(AKISMET_INVALID_KEY, "Your Akismet API key is not valid.");
|
$this->setError(AKISMET_INVALID_KEY, "Your Akismet API key is not valid.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query the Akismet and determine if the comment is spam or not
|
* Query the Akismet and determine if the comment is spam or not
|
||||||
*
|
*
|
||||||
@ -252,7 +240,6 @@ class Akismet extends AkismetObject {
|
|||||||
return ($response == "true");
|
return ($response == "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Submit this comment as an unchecked spam to the Akismet server
|
* Submit this comment as an unchecked spam to the Akismet server
|
||||||
*
|
*
|
||||||
@ -262,7 +249,6 @@ class Akismet extends AkismetObject {
|
|||||||
$this->http->getResponse($this->_getQueryString(), 'submit-spam');
|
$this->http->getResponse($this->_getQueryString(), 'submit-spam');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Submit a false-positive comment as "ham" to the Akismet server
|
* Submit a false-positive comment as "ham" to the Akismet server
|
||||||
*
|
*
|
||||||
@ -272,7 +258,6 @@ class Akismet extends AkismetObject {
|
|||||||
$this->http->getResponse($this->_getQueryString(), 'submit-ham');
|
$this->http->getResponse($this->_getQueryString(), 'submit-ham');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manually set the comment value of the instantiated object.
|
* Manually set the comment value of the instantiated object.
|
||||||
*
|
*
|
||||||
@ -281,13 +266,12 @@ class Akismet extends AkismetObject {
|
|||||||
*/
|
*/
|
||||||
function setComment($comment) {
|
function setComment($comment) {
|
||||||
$this->comment = $comment;
|
$this->comment = $comment;
|
||||||
if(!empty($comment)) {
|
if (!empty($comment)) {
|
||||||
$this->_formatCommentArray();
|
$this->_formatCommentArray();
|
||||||
$this->_fillCommentValues();
|
$this->_fillCommentValues();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current value of the object's comment array.
|
* Returns the current value of the object's comment array.
|
||||||
*
|
*
|
||||||
@ -297,21 +281,20 @@ class Akismet extends AkismetObject {
|
|||||||
return $this->comment;
|
return $this->comment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check with the Akismet server to determine if the API key is valid
|
* Check with the Akismet server to determine if the API key is valid
|
||||||
*
|
*
|
||||||
* @access Protected
|
* @access Protected
|
||||||
* @param String $key The Wordpress API key passed from the constructor argument
|
* @param String $key
|
||||||
|
* The Wordpress API key passed from the constructor argument
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
function _isValidApiKey($key) {
|
function _isValidApiKey($key) {
|
||||||
$keyCheck = $this->http->getResponse("key=".$this->apiKey."&blog=".$this->blogUrl, 'verify-key');
|
$keyCheck = $this->http->getResponse("key=" . $this->apiKey . "&blog=" . $this->blogUrl, 'verify-key');
|
||||||
|
|
||||||
return ($keyCheck == "valid");
|
return ($keyCheck == "valid");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format the comment array in accordance to the Akismet API
|
* Format the comment array in accordance to the Akismet API
|
||||||
*
|
*
|
||||||
@ -327,36 +310,34 @@ class Akismet extends AkismetObject {
|
|||||||
'body' => 'comment_content'
|
'body' => 'comment_content'
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach($format as $short => $long) {
|
foreach ($format as $short => $long) {
|
||||||
if(isset($this->comment[$short])) {
|
if (isset($this->comment [$short])) {
|
||||||
$this->comment[$long] = $this->comment[$short];
|
$this->comment [$long] = $this->comment [$short];
|
||||||
unset($this->comment[$short]);
|
unset($this->comment [$short]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fill any values not provided by the developer with available values.
|
* Fill any values not provided by the developer with available values.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function _fillCommentValues() {
|
function _fillCommentValues() {
|
||||||
if(!isset($this->comment['user_ip'])) {
|
if (!isset($this->comment ['user_ip'])) {
|
||||||
$this->comment['user_ip'] = ($_SERVER['REMOTE_ADDR'] != getenv('SERVER_ADDR')) ? $_SERVER['REMOTE_ADDR'] : getenv('HTTP_X_FORWARDED_FOR');
|
$this->comment ['user_ip'] = ($_SERVER ['REMOTE_ADDR'] != getenv('SERVER_ADDR')) ? $_SERVER ['REMOTE_ADDR'] : getenv('HTTP_X_FORWARDED_FOR');
|
||||||
}
|
}
|
||||||
if(!isset($this->comment['user_agent'])) {
|
if (!isset($this->comment ['user_agent'])) {
|
||||||
$this->comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
|
$this->comment ['user_agent'] = $_SERVER ['HTTP_USER_AGENT'];
|
||||||
}
|
}
|
||||||
if(!isset($this->comment['referrer'])) {
|
if (!isset($this->comment ['referrer'])) {
|
||||||
$this->comment['referrer'] = $_SERVER['HTTP_REFERER'];
|
$this->comment ['referrer'] = $_SERVER ['HTTP_REFERER'];
|
||||||
}
|
}
|
||||||
if(!isset($this->comment['blog'])) {
|
if (!isset($this->comment ['blog'])) {
|
||||||
$this->comment['blog'] = $this->blogUrl;
|
$this->comment ['blog'] = $this->blogUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a query string for use with HTTP requests
|
* Build a query string for use with HTTP requests
|
||||||
*
|
*
|
||||||
@ -364,25 +345,25 @@ class Akismet extends AkismetObject {
|
|||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
function _getQueryString() {
|
function _getQueryString() {
|
||||||
foreach($_SERVER as $key => $value) {
|
foreach ($_SERVER as $key => $value) {
|
||||||
if(!in_array($key, $this->ignore)) {
|
if (!in_array($key, $this->ignore)) {
|
||||||
if($key == 'REMOTE_ADDR') {
|
if ($key == 'REMOTE_ADDR') {
|
||||||
$this->comment[$key] = $this->comment['user_ip'];
|
$this->comment [$key] = $this->comment ['user_ip'];
|
||||||
} else {
|
} else {
|
||||||
$this->comment[$key] = $value;
|
$this->comment [$key] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$query_string = '';
|
$query_string = '';
|
||||||
|
|
||||||
foreach($this->comment as $key => $data) {
|
foreach ($this->comment as $key => $data) {
|
||||||
|
$data = isset($data) ? $data : ''; // make sure $data is a string
|
||||||
$query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
|
$query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $query_string;
|
return $query_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
x
Reference in New Issue
Block a user