* @return void
*/
public function __construct($blogUrl, $apiKey, $comment) {
+
$this->blogUrl = $blogUrl;
$this->apiKey = $apiKey;
+ $this->urls = array (
+ 'verify' => $this->akismetServer . '/'
+ . $this->akismetVersion
+ . '/verify-key',
+
+ 'commentCheck' => $this->apiKey . '.'
+ . $this->akismetServer . '/'
+ . $this->akismetVersion
+ . '/comment-check',
+
+ 'submitSpam' => $this->apiKey . '.'
+ . $this->akismetServer . '/'
+ . $this->akismetVersion
+ . '/submit-spam',
+
+ 'submitHam' => $this->apiKey . '.'
+ . $this->akismetServer . '/'
+ . $this->akismetVersion
+ . '/submit-ham'
+ );
+
// Populate the comment array with information needed by Akismet
$this->comment = $comment;
$this->formatCommentArray();
? $_SERVER['REMOTE_ADDR']
: getenv('HTTP_X_FORWARDED_FOR');
}
+
if (!isset($this->comment['user_agent'])) {
$this->comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
}
+
if (!isset($this->comment['referrer'])) {
$this->comment['referrer'] = $_SERVER['HTTP_REFERER'];
}
$this->comment['blog'] = $blogUrl;
// Connect to the Akismet server and populate errors if they exist
- $this->http = new tx_wtspamshield_akismet_httpclient($this->akismetServer, $blogUrl, $apiKey);
+ $this->http = new tx_wtspamshield_akismet_httpclient();
if ($this->http->errorsExist()) {
$this->errors = array_merge($this->errors, $this->http->getErrors());
}
* @return boolean
*/
public function isSpam() {
- $response = $this->http->getResponse($this->getQueryString(), 'comment-check');
-
+ $response = $this->http->getResponse($this->getQueryString(), $this->urls['commentCheck']);
return ($response == 'true');
}
* @return void
*/
public function submitSpam() {
- $this->http->getResponse($this->getQueryString(), 'submit-spam');
+ $this->http->getResponse($this->getQueryString(), $this->urls['submitSpam']);
}
/**
* @return void
*/
public function submitHam() {
- $this->http->getResponse($this->getQueryString(), 'submit-ham');
+ $this->http->getResponse($this->getQueryString(), $this->urls['submitHam']);
}
/**
* @return boolean
*/
protected function isValidApiKey() {
- $keyCheck = $this->http->getResponse('key=' . $this->apiKey . '&blog=' . $this->blogUrl, 'verify-key');
+ $keyCheck = $this->http->getResponse('key=' . $this->apiKey . '&blog=' . $this->blogUrl, $this->urls['verify']);
return ($keyCheck == 'valid');
}
/**
* Constructor
*
- * @param string $host
- * @param string $blogUrl
- * @param string $apiKey
* @param integer $port
* @return void
*/
- public function __construct($host, $blogUrl, $apiKey, $port = 80) {
- $this->host = $host;
+ public function __construct($port = 80) {
$this->port = $port;
- $this->blogUrl = $blogUrl;
- $this->apiKey = $apiKey;
}
/**
* server and return that response
*
* @param mixed $request
- * @param string $path
+ * @param string $url
* @param string $type
- * @param integer $responseLength
* @return mixed
*/
- public function getResponse($request, $path, $type = 'post', $responseLength = 1160) {
+ public function getResponse($request, $url) {
$this->connect();
if ($this->con && !$this->isError(AKISMET_SERVER_NOT_FOUND)) {
- $request =
- strToUpper($type) . ' /' .
- $this->akismetVersion . '/' .
- $path .
- " HTTP/1.1\r\n" .
- 'Host: ' .
- ( ( strlen($this->apiKey) > 0 )
- ? $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 = '';
-
- @fwrite($this->con, $request);
-
- while (!feof($this->con)) {
- $response .= @fgets($this->con, $responseLength);
+ curl_setopt($this->con, CURLOPT_URL, $url);
+ curl_setopt($this->con, CURLOPT_POSTFIELDS, $request);
+
+ if(!$response = curl_exec($this->con)) {
+ $this->setError(AKISMET_RESPONSE_FAILED, 'The response could not be retrieved.');
+ return;
}
- $response = explode("\r\n\r\n", $response, 2);
- return $response[1];
- } else {
- $this->setError(AKISMET_RESPONSE_FAILED, 'The response could not be retrieved.');
+ $this->disconnect();
+ return $response;
}
-
- $this->disconnect();
- return NULL;
}
- /**
- * Connect to the Akismet server and store that connection in the
- * instance variable $con
- *
- * @return void
- */
- public function connect() {
- if (!($this->con = @fsockopen($this->host, $this->port))) {
- $this->setError(AKISMET_SERVER_NOT_FOUND, 'Could not connect to akismet server.');
+ /**
+ * Initializes a new cURL session/handle
+ *
+ * @return boolean
+ */
+ public function connect() {
+ if (!is_resource($this->con)) {
+ if(!$this->con = curl_init()) {
+ $this->setError(AKISMET_SERVER_NOT_FOUND, 'Could not connect to akismet server.');
+ return;
+ }
}
- }
+
+ curl_setopt($this->con, CURLOPT_HEADER, 0);
+ curl_setopt($this->con, CURLOPT_POST, 1);
+ curl_setopt($this->con, CURLOPT_TIMEOUT, 0);
+ curl_setopt($this->con, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($this->con, CURLOPT_USERAGENT,
+ "Akismet PHP4 Class");
+ curl_setopt($this->con, CURLOPT_FRESH_CONNECT, 1);
+
+ if ($this->port != 80) {
+ curl_setopt($this->con, CURLOPT_PORT, $this->port);
+ }
+
+ return true;
+ }
/**
* Close the connection to the Akismet server
* @return void
*/
public function disconnect() {
- @fclose($this->con);
+ if (is_resource($this->con)) {
+ if (!curl_close($this->con)) {
+ $this->setError(AKISMET_SERVER_NOT_FOUND, 'Could not close the CURL instance');
+ return;
+ }
+ }
+
+ return true;
}
}