Cheetah
Akismet.class.php
Go to the documentation of this file.
1 <?php
2 
64 class Akismet {
65  private $version = '0.5';
66  private $wordPressAPIKey;
67  private $blogURL;
68  private $comment;
69  private $apiPort;
70  private $akismetServer;
71  private $akismetVersion;
72  private $requestFactory;
73 
74  // This prevents some potentially sensitive information from being sent accross the wire.
75  private $ignore = array('HTTP_COOKIE',
76  'HTTP_X_FORWARDED_FOR',
77  'HTTP_X_FORWARDED_HOST',
78  'HTTP_MAX_FORWARDS',
79  'HTTP_X_FORWARDED_SERVER',
80  'REDIRECT_STATUS',
81  'SERVER_PORT',
82  'PATH',
83  'DOCUMENT_ROOT',
84  'SERVER_ADMIN',
85  'QUERY_STRING',
86  'PHP_SELF' );
87 
92  public function __construct($blogURL, $wordPressAPIKey) {
93  $this->blogURL = $blogURL;
94  $this->wordPressAPIKey = $wordPressAPIKey;
95 
96  // Set some default values
97  $this->apiPort = 80;
98  $this->akismetServer = 'rest.akismet.com';
99  $this->akismetVersion = '1.1';
100  $this->requestFactory = new SocketWriteReadFactory();
101 
102  // Start to populate the comment data
103  $this->comment['blog'] = $blogURL;
104 
105  if(isset($_SERVER['HTTP_USER_AGENT'])) {
106  $this->comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
107  }
108 
109  if(isset($_SERVER['HTTP_REFERER'])) {
110  $this->comment['referrer'] = $_SERVER['HTTP_REFERER'];
111  }
112 
113  /*
114  * This is necessary if the server PHP5 is running on has been set up to run PHP4 and
115  * PHP5 concurently and is actually running through a separate proxy al a these instructions:
116  * http://www.schlitt.info/applications/blog/archives/83_How_to_run_PHP4_and_PHP_5_parallel.html
117  * and http://wiki.coggeshall.org/37.html
118  * Otherwise the user_ip appears as the IP address of the PHP4 server passing the requests to the
119  * PHP5 one...
120  */
121  if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != getenv('SERVER_ADDR')) {
122  $this->comment['user_ip'] = $_SERVER['REMOTE_ADDR'];
123  } else {
124  $this->comment['user_ip'] = getenv('HTTP_X_FORWARDED_FOR');
125  }
126  }
127 
135  public function isKeyValid() {
136  // Check to see if the key is valid
137  $response = $this->sendRequest('key=' . $this->wordPressAPIKey . '&blog=' . $this->blogURL, $this->akismetServer, '/' . $this->akismetVersion . '/verify-key');
138  return $response[1] == 'valid';
139  }
140 
141  // makes a request to the Akismet service
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;
150 
151  $requestSender = $this->requestFactory->createRequestSender();
152  $response = $requestSender->send($host, $this->apiPort, $http_request);
153 
154  return explode("\r\n\r\n", $response, 2);
155  }
156 
157  // Formats the data for transmission
158  private function getQueryString() {
159  foreach($_SERVER as $key => $value) {
160  if(!in_array($key, $this->ignore)) {
161  if($key == 'REMOTE_ADDR') {
162  $this->comment[$key] = $this->comment['user_ip'];
163  } else {
164  $this->comment[$key] = $value;
165  }
166  }
167  }
168 
169  $query_string = '';
170 
171  foreach($this->comment as $key => $data) {
172  if(!is_array($data)) {
173  $query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
174  }
175  }
176 
177  return $query_string;
178  }
179 
188  public function isCommentSpam() {
189  $response = $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.rest.akismet.com', '/' . $this->akismetVersion . '/comment-check');
190 
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/');
193  }
194 
195  return ($response[1] == 'true');
196  }
197 
203  public function submitSpam() {
204  $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-spam');
205  }
206 
212  public function submitHam() {
213  $this->sendRequest($this->getQueryString(), $this->wordPressAPIKey . '.' . $this->akismetServer, '/' . $this->akismetVersion . '/submit-ham');
214  }
215 
221  public function setUserIP($userip) {
222  $this->comment['user_ip'] = $userip;
223  }
224 
230  public function setReferrer($referrer) {
231  $this->comment['referrer'] = $referrer;
232  }
233 
239  public function setPermalink($permalink) {
240  $this->comment['permalink'] = $permalink;
241  }
242 
248  public function setCommentType($commentType) {
249  $this->comment['comment_type'] = $commentType;
250  }
251 
255  public function setCommentAuthor($commentAuthor) {
256  $this->comment['comment_author'] = $commentAuthor;
257  }
258 
264  public function setCommentAuthorEmail($authorEmail) {
265  $this->comment['comment_author_email'] = $authorEmail;
266  }
267 
271  public function setCommentAuthorURL($authorURL) {
272  $this->comment['comment_author_url'] = $authorURL;
273  }
274 
278  public function setCommentContent($commentBody) {
279  $this->comment['comment_content'] = $commentBody;
280  }
281 
287  public function setCommentUserAgent($userAgent) {
288  $this->comment['user_agent'] = $userAgent;
289  }
290 
294  public function setAPIPort($apiPort) {
295  $this->apiPort = $apiPort;
296  }
297 
301  public function setAkismetServer($akismetServer) {
302  $this->akismetServer = $akismetServer;
303  }
304 
310  public function setAkismetVersion($akismetVersion) {
311  $this->akismetVersion = $akismetVersion;
312  }
313 
319  public function setRequestFactory($requestFactory) {
320  $this->requestFactory = $requestFactory;
321  }
322 }
323 
340  private $response;
341  private $errorNumber;
342  private $errorString;
343 
344  public function __construct() {
345  $this->errorNumber = 0;
346  $this->errorString = '';
347  }
348 
359  public function send($host, $port, $request, $responseLength = 1160) {
360  $response = '';
361 
362  $fs = fsockopen($host, $port, $this->errorNumber, $this->errorString, 3);
363 
364  if($this->errorNumber != 0) {
365  throw new Exception('Error connecting to host: ' . $host . ' Error number: ' . $this->errorNumber . ' Error message: ' . $this->errorString);
366  }
367 
368  if($fs !== false) {
369  @fwrite($fs, $request);
370 
371  while(!feof($fs)) {
372  $response .= fgets($fs, $responseLength);
373  }
374 
375  fclose($fs);
376  }
377 
378  return $response;
379  }
380 
386  public function getResponse() {
387  return $this->response;
388  }
389 
397  public function getErrorNumner() {
398  return $this->errorNumber;
399  }
400 
408  public function getErrorString() {
409  return $this->errorString;
410  }
411 }
412 
426 
427  public function createRequestSender() {
428  return new SocketWriteRead();
429  }
430 }
431 
445 
456  public function send($host, $port, $request, $responseLength = 1160);
457 }
458 
472 
473  public function createRequestSender();
474 }
475 
476 ?>
SocketWriteRead\send
send($host, $port, $request, $responseLength=1160)
Definition: Akismet.class.php:359
AkismetRequestSender\send
send($host, $port, $request, $responseLength=1160)
Akismet\submitSpam
submitSpam()
Definition: Akismet.class.php:203
Akismet\isCommentSpam
isCommentSpam()
Definition: Akismet.class.php:188
Akismet\setPermalink
setPermalink($permalink)
Definition: Akismet.class.php:239
Akismet\setCommentType
setCommentType($commentType)
Definition: Akismet.class.php:248
SocketWriteRead\getErrorString
getErrorString()
Definition: Akismet.class.php:408
php
Akismet\setReferrer
setReferrer($referrer)
Definition: Akismet.class.php:230
Akismet\setCommentAuthorURL
setCommentAuthorURL($authorURL)
Definition: Akismet.class.php:271
Akismet\setCommentAuthorEmail
setCommentAuthorEmail($authorEmail)
Definition: Akismet.class.php:264
SocketWriteRead\getResponse
getResponse()
Definition: Akismet.class.php:386
SocketWriteRead\getErrorNumner
getErrorNumner()
Definition: Akismet.class.php:397
Akismet\setRequestFactory
setRequestFactory($requestFactory)
Definition: Akismet.class.php:319
Akismet\setAkismetServer
setAkismetServer($akismetServer)
Definition: Akismet.class.php:301
Akismet\setCommentAuthor
setCommentAuthor($commentAuthor)
Definition: Akismet.class.php:255
comment
HTML AllowedCommentsRegexp which if it matches the body of a comment
Definition: HTML.AllowedCommentsRegexp.txt:6
Akismet\setCommentContent
setCommentContent($commentBody)
Definition: Akismet.class.php:278
Akismet\submitHam
submitHam()
Definition: Akismet.class.php:212
Akismet\__construct
__construct($blogURL, $wordPressAPIKey)
Definition: Akismet.class.php:92
AkismetRequestFactory
Definition: Akismet.class.php:471
$path
$path
Definition: header.inc.php:12
AkismetRequestSender
Definition: Akismet.class.php:444
AkismetRequestFactory\createRequestSender
createRequestSender()
SocketWriteRead
Definition: Akismet.class.php:339
Akismet\setAPIPort
setAPIPort($apiPort)
Definition: Akismet.class.php:294
Akismet
Definition: Akismet.class.php:64
Akismet\setAkismetVersion
setAkismetVersion($akismetVersion)
Definition: Akismet.class.php:310
Akismet\isKeyValid
isKeyValid()
Definition: Akismet.class.php:135
Akismet\setUserIP
setUserIP($userip)
Definition: Akismet.class.php:221
SocketWriteRead\__construct
__construct()
Definition: Akismet.class.php:344
version
and that you are informed that you can do these things To protect your we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it For if you distribute copies of the whether gratis or for a you must give the recipients all the rights that we gave you You must make sure that receive or can get the source code If you link other code with the you must provide complete object files to the so that they can relink them with the library after making changes to the library and recompiling it And you must show them these terms so they know their rights We protect your rights with a two step which gives you legal permission to distribute and or modify the library To protect each we want to make it very clear that there is no warranty for the free library if the library is modified by someone else and passed the recipients should know that what they have is not the original version
Definition: license.txt:55
SocketWriteReadFactory\createRequestSender
createRequestSender()
Definition: Akismet.class.php:427
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
SocketWriteReadFactory
Definition: Akismet.class.php:425
Akismet\setCommentUserAgent
setCommentUserAgent($userAgent)
Definition: Akismet.class.php:287
exception
if the work is an executable linked with the with the complete machine readable work that uses the as object code and or source so that the user can modify the Library and then relink to produce a modified executable containing the modified rather than copying library functions into the if the user installs as long as the modified version is interface compatible with the version that the work was made with c Accompany the work with a written valid for at least three to give the same user the materials specified in for a charge no more than the cost of performing this distribution d If distribution of the work is made by offering access to copy from a designated offer equivalent access to copy the above specified materials from the same place e Verify that the user has already received a copy of these materials or that you have already sent this user a copy For an the required form of the work that uses the Library must include any data and utility programs needed for reproducing the executable from it as a special exception
Definition: license.txt:320