fixes #83: Class-named constructors in Akisment plugin; also: PHP warnings fixed

This commit is contained in:
azett 2022-10-11 21:11:30 +02:00
parent bdf9e780e1
commit 5ad886b894
3 changed files with 153 additions and 130 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Akismet anti-comment spam service * Akismet anti-comment spam service
* *
@ -6,11 +7,11 @@
* *
* This service performs a number of checks on submitted data and returns whether or not the data is likely to be spam. * This service performs a number of checks on submitted data and returns whether or not the data is likely to be spam.
* *
* Please note that in order to use this class, you must have a vaild {@link http://wordpress.com/api-keys/ WordPress API key}. They are free for non/small-profit types and getting one will only take a couple of minutes. * Please note that in order to use this class, you must have a vaild {@link http://wordpress.com/api-keys/ WordPress API key}. They are free for non/small-profit types and getting one will only take a couple of minutes.
* *
* For commercial use, please {@link http://akismet.com/commercial/ visit the Akismet commercial licensing page}. * For commercial use, please {@link http://akismet.com/commercial/ visit the Akismet commercial licensing page}.
* *
* Please be aware that this class is PHP5 only. Attempts to run it under PHP4 will most likely fail. * Please be aware that this class is PHP5 only. Attempts to run it under PHP4 will most likely fail.
* *
* See the Akismet class documentation page linked to below for usage information. * See the Akismet class documentation page linked to below for usage information.
* *
@ -22,64 +23,73 @@
*/ */
/** /**
* The Akismet PHP4 Class * The Akismet PHP4 Class
* *
* This class takes the functionality from the Akismet WordPress plugin written by {@link http://photomatt.net/ Matt Mullenweg} and allows it to be integrated into any PHP5 application or website. * This class takes the functionality from the Akismet WordPress plugin written by {@link http://photomatt.net/ Matt Mullenweg} and allows it to be integrated into any PHP5 application or website.
* *
* The original plugin is {@link http://akismet.com/download/ available on the Akismet website}. * The original plugin is {@link http://akismet.com/download/ available on the Akismet website}.
* *
* <b>Usage:</b> * <b>Usage:</b>
* <code> * <code>
* $akismet = new Akismet('http://www.example.com/blog/', 'aoeu1aoue'); * $akismet = new Akismet('http://www.example.com/blog/', 'aoeu1aoue');
* $akismet->setCommentAuthor($name); * $akismet->setCommentAuthor($name);
* $akismet->setCommentAuthorEmail($email); * $akismet->setCommentAuthorEmail($email);
* $akismet->setCommentAuthorURL($url); * $akismet->setCommentAuthorURL($url);
* $akismet->setCommentContent($comment); * $akismet->setCommentContent($comment);
* $akismet->setPermalink('http://www.example.com/blog/alex/someurl/'); * $akismet->setPermalink('http://www.example.com/blog/alex/someurl/');
* if($akismet->isCommentSpam()) * if($akismet->isCommentSpam())
* // store the comment but mark it as spam (in case of a mis-diagnosis) * // store the comment but mark it as spam (in case of a mis-diagnosis)
* else * else
* // store the comment normally * // store the comment normally
* </code> * </code>
* *
* @package akismet * @package akismet
* @name Akismet * @name Akismet
* @version 0.2 * @version 0.2
* @author Alex Potsides (converted to PHP4 by Bret Kuhns) * @author Alex Potsides (converted to PHP4 by Bret Kuhns)
* @link http://www.achingbrain.net/ * @link http://www.achingbrain.net/
*/ */
class Akismet { class Akismet {
var $version = '0.2'; var $version = '0.2';
var $wordPressAPIKey; var $wordPressAPIKey;
var $blogURL; var $blogURL;
var $comment; var $comment;
var $apiPort; var $apiPort;
var $akismetServer; var $akismetServer;
var $akismetVersion; var $akismetVersion;
// This prevents some potentially sensitive information from being sent accross the wire. // This prevents some potentially sensitive information from being sent accross the wire.
var $ignore = array( var $ignore = array(
'HTTP_COOKIE', 'HTTP_COOKIE',
'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED_HOST', 'HTTP_X_FORWARDED_HOST',
'HTTP_MAX_FORWARDS', 'HTTP_MAX_FORWARDS',
'HTTP_X_FORWARDED_SERVER', 'HTTP_X_FORWARDED_SERVER',
'REDIRECT_STATUS', 'REDIRECT_STATUS',
'SERVER_PORT', 'SERVER_PORT',
'PATH', 'PATH',
'DOCUMENT_ROOT', 'DOCUMENT_ROOT',
'SERVER_ADMIN', 'SERVER_ADMIN',
'QUERY_STRING', 'QUERY_STRING',
'PHP_SELF' 'PHP_SELF'
); );
/** /**
* @throws Exception An exception is thrown if your API key is invalid. *
* @param string Your WordPress API key. * @throws Exception An exception is thrown if your API key is invalid.
* @param string $blogURL The URL of your blog. * @param
* string Your WordPress API key.
* @param string $blogURL
* The URL of your blog.
*/ */
function Akismet($blogURL, $wordPressAPIKey) { function __construct($blogURL, $wordPressAPIKey) {
$this->blogURL = $blogURL; $this->blogURL = $blogURL;
$this->wordPressAPIKey = $wordPressAPIKey; $this->wordPressAPIKey = $wordPressAPIKey;
@ -89,9 +99,9 @@ class Akismet {
$this->akismetVersion = '1.1'; $this->akismetVersion = '1.1';
// Start to populate the comment data // Start to populate the comment data
$this->comment['blog'] = $blogURL; $this->comment ['blog'] = $blogURL;
$this->comment['user_agent'] = $_SERVER['HTTP_USER_AGENT']; $this->comment ['user_agent'] = $_SERVER ['HTTP_USER_AGENT'];
$this->comment['referrer'] = $_SERVER['HTTP_REFERER']; $this->comment ['referrer'] = $_SERVER ['HTTP_REFERER'];
// This is necessary if the server PHP5 is running on has been set up to run PHP4 and // This is necessary if the server PHP5 is running on has been set up to run PHP4 and
// PHP5 concurently and is actually running through a separate proxy al a these instructions: // PHP5 concurently and is actually running through a separate proxy al a these instructions:
@ -99,27 +109,25 @@ class Akismet {
// and http://wiki.coggeshall.org/37.html // and http://wiki.coggeshall.org/37.html
// Otherwise the user_ip appears as the IP address of the PHP4 server passing the requests to the // Otherwise the user_ip appears as the IP address of the PHP4 server passing the requests to the
// PHP5 one... // PHP5 one...
$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');
// Check to see if the key is valid // Check to see if the key is valid
$response = $this->http_post('key=' . $this->wordPressAPIKey . '&blog=' . $this->blogURL, $this->akismetServer, '/' . $this->akismetVersion . '/verify-key'); $response = $this->http_post('key=' . $this->wordPressAPIKey . '&blog=' . $this->blogURL, $this->akismetServer, '/' . $this->akismetVersion . '/verify-key');
if($response[1] != 'valid') { if ($response [1] != 'valid') {
// Whoops, no it's not. Throw an exception as we can't proceed without a valid API key. // Whoops, no it's not. Throw an exception as we can't proceed without a valid API key.
trigger_error('Invalid API key. Please obtain one from http://wordpress.com/api-keys/', E_USER_ERROR); trigger_error('Invalid API key. Please obtain one from http://wordpress.com/api-keys/', E_USER_ERROR);
} }
} }
function http_post($request, $host, $path) { function http_post($request, $host, $path) {
$http_request = $http_request = "POST " . $path . " HTTP/1.0\r\n" . //
"POST " . $path . " HTTP/1.0\r\n" . "Host: " . $host . "\r\n" . //
"Host: " . $host . "\r\n" . "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" . //
"Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" . "Content-Length: " . strlen($request) . "\r\n" . //
"Content-Length: " . strlen($request) . "\r\n" . "User-Agent: Akismet PHP5 Class " . $this->version . " | Akismet/1.11\r\n" . //
"User-Agent: Akismet PHP5 Class " . $this->version . " | Akismet/1.11\r\n" . "\r\n" . //
"\r\n" . $request;
$request
;
$socketWriteRead = new SocketWriteRead($host, $this->apiPort, $http_request); $socketWriteRead = new SocketWriteRead($host, $this->apiPort, $http_request);
$socketWriteRead->send(); $socketWriteRead->send();
@ -127,22 +135,22 @@ class Akismet {
return explode("\r\n\r\n", $socketWriteRead->getResponse(), 2); return explode("\r\n\r\n", $socketWriteRead->getResponse(), 2);
} }
// Formats the data for transmission echo $sql; // Formats the data for transmission echo $sql;
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) {
@$query_string .= $key . '=' . urlencode(stripslashes($data)) . '&'; @$query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
} }
@ -150,156 +158,171 @@ class Akismet {
} }
/** /**
* Tests for spam. * Tests for spam.
* *
* Uses the web service provided by {@link http://www.akismet.com Akismet} to see whether or not the submitted comment is spam. Returns a boolean value. * Uses the web service provided by {@link http://www.akismet.com Akismet} to see whether or not the submitted comment is spam. Returns a boolean value.
* *
* @return bool True if the comment is spam, false if not * @return bool True if the comment is spam, false if not
*/ */
function isSpam() { function isSpam() {
$response = $this->http_post($this->getQueryString(), $this->wordPressAPIKey . '.rest.akismet.com', '/' . $this->akismetVersion . '/comment-check'); $response = $this->http_post($this->getQueryString(), $this->wordPressAPIKey . '.rest.akismet.com', '/' . $this->akismetVersion . '/comment-check');
return ($response[1] == 'true'); return ($response [1] == 'true');
} }
/** /**
* Submit spam that is incorrectly tagged as ham. * Submit spam that is incorrectly tagged as ham.
* *
* Using this function will make you a good citizen as it helps Akismet to learn from its mistakes. This will improve the service for everybody. * Using this function will make you a good citizen as it helps Akismet to learn from its mistakes. This will improve the service for everybody.
*/ */
function submitSpam() { function submitSpam() {
$this->http_post($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-spam'); $this->http_post($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-spam');
} }
/** /**
* Submit ham that is incorrectly tagged as spam. * Submit ham that is incorrectly tagged as spam.
* *
* Using this function will make you a good citizen as it helps Akismet to learn from its mistakes. This will improve the service for everybody. * Using this function will make you a good citizen as it helps Akismet to learn from its mistakes. This will improve the service for everybody.
*/ */
function submitHam() { function submitHam() {
$this->http_post($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-ham'); $this->http_post($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-ham');
} }
/** /**
* To override the user IP address when submitting spam/ham later on * To override the user IP address when submitting spam/ham later on
* *
* @param string $userip An IP address. Optional. * @param string $userip
* An IP address. Optional.
*/ */
function setUserIP($userip) { function setUserIP($userip) {
$this->comment['user_ip'] = $userip; $this->comment ['user_ip'] = $userip;
} }
/** /**
* To override the referring page when submitting spam/ham later on * To override the referring page when submitting spam/ham later on
* *
* @param string $referrer The referring page. Optional. * @param string $referrer
* The referring page. Optional.
*/ */
function setReferrer($referrer) { function setReferrer($referrer) {
$this->comment['referrer'] = $referrer; $this->comment ['referrer'] = $referrer;
} }
/** /**
* A permanent URL referencing the blog post the comment was submitted to. * A permanent URL referencing the blog post the comment was submitted to.
* *
* @param string $permalink The URL. Optional. * @param string $permalink
* The URL. Optional.
*/ */
function setPermalink($permalink) { function setPermalink($permalink) {
$this->comment['permalink'] = $permalink; $this->comment ['permalink'] = $permalink;
} }
/** /**
* The type of comment being submitted. * The type of comment being submitted.
* *
* May be blank, comment, trackback, pingback, or a made up value like "registration" or "wiki". * May be blank, comment, trackback, pingback, or a made up value like "registration" or "wiki".
*/ */
function setType($commentType) { function setType($commentType) {
$this->comment['comment_type'] = $commentType; $this->comment ['comment_type'] = $commentType;
} }
/** /**
* The name that the author submitted with the comment. * The name that the author submitted with the comment.
*/ */
function setAuthor($commentAuthor) { function setAuthor($commentAuthor) {
$this->comment['comment_author'] = $commentAuthor; $this->comment ['comment_author'] = $commentAuthor;
} }
/** /**
* The email address that the author submitted with the comment. * The email address that the author submitted with the comment.
* *
* The address is assumed to be valid. * The address is assumed to be valid.
*/ */
function setAuthorEmail($authorEmail) { function setAuthorEmail($authorEmail) {
$this->comment['comment_author_email'] = $authorEmail; $this->comment ['comment_author_email'] = $authorEmail;
} }
/** /**
* The URL that the author submitted with the comment. * The URL that the author submitted with the comment.
*/ */
function setAuthorURL($authorURL) { function setAuthorURL($authorURL) {
$this->comment['comment_author_url'] = $authorURL; $this->comment ['comment_author_url'] = $authorURL;
} }
/** /**
* The comment's body text. * The comment's body text.
*/ */
function setContent($commentBody) { function setContent($commentBody) {
$this->comment['comment_content'] = $commentBody; $this->comment ['comment_content'] = $commentBody;
} }
/** /**
* Defaults to 80 * Defaults to 80
*/ */
function setAPIPort($apiPort) { function setAPIPort($apiPort) {
$this->apiPort = $apiPort; $this->apiPort = $apiPort;
} }
/** /**
* Defaults to rest.akismet.com * Defaults to rest.akismet.com
*/ */
function setAkismetServer($akismetServer) { function setAkismetServer($akismetServer) {
$this->akismetServer = $akismetServer; $this->akismetServer = $akismetServer;
} }
/** /**
* Defaults to '1.1' * Defaults to '1.1'
*/ */
function setAkismetVersion($akismetVersion) { function setAkismetVersion($akismetVersion) {
$this->akismetVersion = $akismetVersion; $this->akismetVersion = $akismetVersion;
} }
} }
/** /**
* Utility class used by Akismet * Utility class used by Akismet
* *
* This class is used by Akismet to do the actual sending and receiving of data. It opens a connection to a remote host, sends some data and the reads the response and makes it available to the calling program. * This class is used by Akismet to do the actual sending and receiving of data. It opens a connection to a remote host, sends some data and the reads the response and makes it available to the calling program.
* *
* The code that makes up this class originates in the Akismet WordPress plugin, which is {@link http://akismet.com/download/ available on the Akismet website}. * The code that makes up this class originates in the Akismet WordPress plugin, which is {@link http://akismet.com/download/ available on the Akismet website}.
* *
* N.B. It is not necessary to call this class directly to use the Akismet class. This is included here mainly out of a sense of completeness. * N.B. It is not necessary to call this class directly to use the Akismet class. This is included here mainly out of a sense of completeness.
* *
* @package akismet * @package akismet
* @name SocketWriteRead * @name SocketWriteRead
* @version 0.1 * @version 0.1
* @author Alex Potsides * @author Alex Potsides
* @link http://www.achingbrain.net/ * @link http://www.achingbrain.net/
*/ */
class SocketWriteRead { class SocketWriteRead {
var $host; var $host;
var $port; var $port;
var $request; var $request;
var $response; var $response;
var $responseLength; var $responseLength;
var $errorNumber; var $errorNumber;
var $errorString; var $errorString;
/** /**
* @param string $host The host to send/receive data. *
* @param int $port The port on the remote host. * @param string $host
* @param string $request The data to send. * The host to send/receive data.
* @param int $responseLength The amount of data to read. Defaults to 1160 bytes. * @param int $port
* The port on the remote host.
* @param string $request
* The data to send.
* @param int $responseLength
* The amount of data to read. Defaults to 1160 bytes.
*/ */
function SocketWriteRead($host, $port, $request, $responseLength = 1160) { function __construct($host, $port, $request, $responseLength = 1160) {
$this->host = $host; $this->host = $host;
$this->port = $port; $this->port = $port;
$this->request = $request; $this->request = $request;
@ -309,24 +332,23 @@ class SocketWriteRead {
} }
/** /**
* Sends the data to the remote host. * Sends the data to the remote host.
* *
* @throws An exception is thrown if a connection cannot be made to the remote host. * @throws An exception is thrown if a connection cannot be made to the remote host.
*/ */
function send() { function send() {
$this->response = ''; $this->response = '';
$fs = fsockopen($this->host, $this->port, $this->errorNumber, $this->errorString, $fs = fsockopen($this->host, $this->port, $this->errorNumber, $this->errorString, AKISMET_TIMEOUT);
AKISMET_TIMEOUT);
if($this->errorNumber != 0) { if ($this->errorNumber != 0) {
trigger_error('Error connecting to host: ' . $this->host . ' Error number: ' . $this->errorNumber . ' Error message: ' . $this->errorString, E_USER_ERROR); trigger_error('Error connecting to host: ' . $this->host . ' Error number: ' . $this->errorNumber . ' Error message: ' . $this->errorString, E_USER_ERROR);
} }
if($fs !== false) { if ($fs !== false) {
@fwrite($fs, $this->request); @fwrite($fs, $this->request);
while(!feof($fs)) { while (!feof($fs)) {
$this->response .= fgets($fs, $this->responseLength); $this->response .= fgets($fs, $this->responseLength);
} }
@ -335,34 +357,35 @@ class SocketWriteRead {
} }
/** /**
* Returns the server response text * Returns the server response text
* *
* @return string * @return string
*/ */
function getResponse() { function getResponse() {
return $this->response; return $this->response;
} }
/** /**
* Returns the error number * Returns the error number
* *
* If there was no error, 0 will be returned. * If there was no error, 0 will be returned.
* *
* @return int * @return int
*/ */
function getErrorNumber() { function getErrorNumber() {
return $this->errorNumber; return $this->errorNumber;
} }
/** /**
* Returns the error string * Returns the error string
* *
* If there was no error, an empty string will be returned. * If there was no error, an empty string will be returned.
* *
* @return string * @return string
*/ */
function getErrorString() { function getErrorString() {
return $this->errorString; return $this->errorString;
} }
} }
?> ?>

View File

@ -62,7 +62,7 @@ if (class_exists('AdminPanelAction')) {
$this->smarty->assign('akismetconf', $akismetconf); $this->smarty->assign('akismetconf', $akismetconf);
} }
function onsubmit() { function onsubmit($data = null) {
global $fp_config; global $fp_config;
if ($_POST ['wp-apikey']) { if ($_POST ['wp-apikey']) {

View File

@ -8,7 +8,7 @@
{html_form} {html_form}
<h4><label for="wp-apikey">{$plang.apikey}</label></h4> <h4><label for="wp-apikey">{$plang.apikey}</label></h4>
<p><input id="wp-apikey" type="text" name="wp-apikey" value="{$akismetconf.apikey}" /> <p><input id="wp-apikey" type="text" name="wp-apikey" value="{$akismetconf.apikey|default:''}" />
<input type="submit" value="{$plang.submit}"/> </p> <input type="submit" value="{$plang.submit}"/> </p>
<p> {$plang.whatis} </p> <p> {$plang.whatis} </p>