65 private $version =
'0.5';
66 private $wordPressAPIKey;
70 private $akismetServer;
71 private $akismetVersion;
72 private $requestFactory;
75 private $ignore = array(
'HTTP_COOKIE',
76 'HTTP_X_FORWARDED_FOR',
77 'HTTP_X_FORWARDED_HOST',
79 'HTTP_X_FORWARDED_SERVER',
93 $this->blogURL = $blogURL;
94 $this->wordPressAPIKey = $wordPressAPIKey;
98 $this->akismetServer =
'rest.akismet.com';
99 $this->akismetVersion =
'1.1';
103 $this->
comment[
'blog'] = $blogURL;
105 if(isset($_SERVER[
'HTTP_USER_AGENT'])) {
106 $this->
comment[
'user_agent'] = $_SERVER[
'HTTP_USER_AGENT'];
109 if(isset($_SERVER[
'HTTP_REFERER'])) {
110 $this->
comment[
'referrer'] = $_SERVER[
'HTTP_REFERER'];
121 if(isset($_SERVER[
'REMOTE_ADDR']) && $_SERVER[
'REMOTE_ADDR'] != getenv(
'SERVER_ADDR')) {
122 $this->
comment[
'user_ip'] = $_SERVER[
'REMOTE_ADDR'];
124 $this->
comment[
'user_ip'] = getenv(
'HTTP_X_FORWARDED_FOR');
137 $response = $this->sendRequest(
'key=' . $this->wordPressAPIKey .
'&blog=' . $this->blogURL, $this->akismetServer,
'/' . $this->akismetVersion .
'/verify-key');
138 return $response[1] ==
'valid';
142 private function sendRequest($request, $host,
$path) {
143 $http_request =
"POST " .
$path .
" HTTP/1.0\r\n";
144 $http_request .=
"Host: " . $host .
"\r\n";
145 $http_request .=
"Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
146 $http_request .=
"Content-Length: " . strlen($request) .
"\r\n";
147 $http_request .=
"User-Agent: Akismet PHP5 Class " . $this->
version .
" | Akismet/1.11\r\n";
148 $http_request .=
"\r\n";
149 $http_request .= $request;
151 $requestSender = $this->requestFactory->createRequestSender();
152 $response = $requestSender->send($host, $this->apiPort, $http_request);
154 return explode(
"\r\n\r\n", $response, 2);
158 private function getQueryString() {
159 foreach($_SERVER
as $key => $value) {
160 if(!in_array($key, $this->ignore)) {
161 if($key ==
'REMOTE_ADDR') {
171 foreach($this->
comment as $key => $data) {
172 if(!is_array($data)) {
173 $query_string .= $key .
'=' . urlencode(stripslashes($data)) .
'&';
177 return $query_string;
189 $response = $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey .
'.rest.akismet.com',
'/' . $this->akismetVersion .
'/comment-check');
191 if($response[1] ==
'invalid' && !$this->
isKeyValid()) {
192 throw new exception(
'The Wordpress API key passed to the Akismet constructor is invalid. Please obtain a valid one from http://wordpress.com/api-keys/');
195 return ($response[1] ==
'true');
204 $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey .
'.' . $this->akismetServer,
'/' . $this->akismetVersion .
'/submit-spam');
213 $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey .
'.' . $this->akismetServer,
'/' . $this->akismetVersion .
'/submit-ham');
222 $this->
comment[
'user_ip'] = $userip;
231 $this->
comment[
'referrer'] = $referrer;
240 $this->
comment[
'permalink'] = $permalink;
249 $this->
comment[
'comment_type'] = $commentType;
256 $this->
comment[
'comment_author'] = $commentAuthor;
265 $this->
comment[
'comment_author_email'] = $authorEmail;
272 $this->
comment[
'comment_author_url'] = $authorURL;
279 $this->
comment[
'comment_content'] = $commentBody;
288 $this->
comment[
'user_agent'] = $userAgent;
295 $this->apiPort = $apiPort;
302 $this->akismetServer = $akismetServer;
311 $this->akismetVersion = $akismetVersion;
320 $this->requestFactory = $requestFactory;
341 private $errorNumber;
342 private $errorString;
345 $this->errorNumber = 0;
346 $this->errorString =
'';
359 public function send($host, $port, $request, $responseLength = 1160) {
362 $fs = fsockopen($host, $port, $this->errorNumber, $this->errorString, 3);
364 if($this->errorNumber != 0) {
365 throw new Exception(
'Error connecting to host: ' . $host .
' Error number: ' . $this->errorNumber .
' Error message: ' . $this->errorString);
369 @fwrite($fs, $request);
372 $response .= fgets($fs, $responseLength);
387 return $this->response;
398 return $this->errorNumber;
409 return $this->errorString;
456 public function send($host, $port, $request, $responseLength = 1160);