akismet.class.php

Go to the documentation of this file.
00001 <?php
00038 // Error constants
00039 define("AKISMET_SERVER_NOT_FOUND",      0);
00040 define("AKISMET_RESPONSE_FAILED",       1);
00041 define("AKISMET_INVALID_KEY",           2);
00042 
00043 
00044 
00045 // Base class to assist in error handling between Akismet classes
00046 class AkismetObject {
00047         var $errors = array();
00048         
00049         
00057         // Set an error in the object
00058         function setError($name, $message) {
00059                 $this->errors[$name] = $message;
00060         }
00061         
00062 
00069         function getError($name) {
00070                 if($this->isError($name)) {
00071                         return $this->errors[$name];
00072                 } else {
00073                         return false;
00074                 }
00075         }
00076         
00077         
00083         function getErrors() {
00084                 return (array)$this->errors;
00085         }
00086         
00087         
00094         function isError($name) {
00095                 return isset($this->errors[$name]);
00096         }
00097         
00098         
00104         function errorsExist() {
00105                 return (count($this->errors) > 0);
00106         }
00107         
00108         
00109 }
00110 
00111 
00112 
00113 
00114 
00115 // Used by the Akismet class to communicate with the Akismet service
00116 class AkismetHttpClient extends AkismetObject {
00117         var $akismetVersion = '1.1';
00118         var $con;
00119         var $host;
00120         var $port;
00121         var $apiKey;
00122         var $blogUrl;
00123         var $errors = array();
00124         
00125         
00126         // Constructor
00127         function AkismetHttpClient($host, $blogUrl, $apiKey, $port = 80) {
00128                 $this->host = $host;
00129                 $this->port = $port;
00130                 $this->blogUrl = $blogUrl;
00131                 $this->apiKey = $apiKey;
00132         }
00133         
00134         
00135         // Use the connection active in $con to get a response from the server and return that response
00136         function getResponse($request, $path, $type = "post", $responseLength = 1160) {
00137                 $this->_connect();
00138                 
00139                 if($this->con && !$this->isError(AKISMET_SERVER_NOT_FOUND)) {
00140                         $request  = 
00141                                         strToUpper($type)." /{$this->akismetVersion}/$path HTTP/1.1\r\n" .
00142                                         "Host: ".((!empty($this->apiKey)) ? $this->apiKey."." : null)."{$this->host}\r\n" .
00143                                         "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" .
00144                                         "Content-Length: ".strlen($request)."\r\n" .
00145                                         "User-Agent: Akismet PHP4 Class\r\n" .
00146                                         "\r\n" .
00147                                         $request
00148                                 ;
00149                         $response = "";
00150 
00151                         @fwrite($this->con, $request);
00152 
00153                         while(!feof($this->con)) {
00154                                 $response .= @fgets($this->con, $responseLength);
00155                         }
00156 
00157                         $response = explode("\r\n\r\n", $response, 2);
00158                         return $response[1];
00159                 } else {
00160                         $this->setError(AKISMET_RESPONSE_FAILED, "The response could not be retrieved.");
00161                 }
00162                 
00163                 $this->_disconnect();
00164         }
00165         
00166         
00167         // Connect to the Akismet server and store that connection in the instance variable $con
00168         function _connect() {
00169                 if(!($this->con = @fsockopen($this->host, $this->port))) {
00170                         $this->setError(AKISMET_SERVER_NOT_FOUND, "Could not connect to akismet server.");
00171                 }
00172         }
00173         
00174         
00175         // Close the connection to the Akismet server
00176         function _disconnect() {
00177                 @fclose($this->con);
00178         }
00179         
00180         
00181 }
00182 
00183 
00184 
00185 
00186 
00187 // The controlling class. This is the ONLY class the user should instantiate in
00188 // order to use the Akismet service!
00189 class Akismet extends AkismetObject {
00190         var $apiPort = 80;
00191         var $akismetServer = 'rest.akismet.com';
00192         var $akismetVersion = '1.1';
00193         var $http;
00194         
00195         var $ignore = array(
00196                         'HTTP_COOKIE',
00197                         'HTTP_X_FORWARDED_FOR',
00198                         'HTTP_X_FORWARDED_HOST',
00199                         'HTTP_MAX_FORWARDS',
00200                         'HTTP_X_FORWARDED_SERVER',
00201                         'REDIRECT_STATUS',
00202                         'SERVER_PORT',
00203                         'PATH',
00204                         'DOCUMENT_ROOT',
00205                         'SERVER_ADMIN',
00206                         'QUERY_STRING',
00207                         'PHP_SELF',
00208                         'argv'
00209                 );
00210         
00211         var $blogUrl = "";
00212         var $apiKey  = "";
00213         var $comment = array();
00214         
00215         
00226         function Akismet($blogUrl, $apiKey, $comment = array()) {
00227                 $this->blogUrl = $blogUrl;
00228                 $this->apiKey  = $apiKey;
00229                 $this->setComment($comment);
00230                 
00231                 // Connect to the Akismet server and populate errors if they exist
00232                 $this->http = new AkismetHttpClient($this->akismetServer, $blogUrl, $apiKey);
00233                 if($this->http->errorsExist()) {
00234                         $this->errors = array_merge($this->errors, $this->http->getErrors());
00235                 }
00236                 
00237                 // Check if the API key is valid
00238                 if(!$this->_isValidApiKey($apiKey)) {
00239                         $this->setError(AKISMET_INVALID_KEY, "Your Akismet API key is not valid.");
00240                 }
00241         }
00242         
00243         
00249         function isSpam() {
00250                 $response = $this->http->getResponse($this->_getQueryString(), 'comment-check');
00251                 
00252                 return ($response == "true");
00253         }
00254         
00255         
00261         function submitSpam() {
00262                 $this->http->getResponse($this->_getQueryString(), 'submit-spam');
00263         }
00264         
00265         
00271         function submitHam() {
00272                 $this->http->getResponse($this->_getQueryString(), 'submit-ham');
00273         }
00274         
00275         
00282         function setComment($comment) {
00283                 $this->comment = $comment;
00284                 if(!empty($comment)) {
00285                         $this->_formatCommentArray();
00286                         $this->_fillCommentValues();
00287                 }
00288         }
00289         
00290         
00296         function getComment() {
00297                 return $this->comment;
00298         }
00299         
00300         
00308         function _isValidApiKey($key) {
00309                 $keyCheck = $this->http->getResponse("key=".$this->apiKey."&blog=".$this->blogUrl, 'verify-key');
00310                         
00311                 return ($keyCheck == "valid");
00312         }
00313         
00314         
00321         function _formatCommentArray() {
00322                 $format = array(
00323                                 'type' => 'comment_type',
00324                                 'author' => 'comment_author',
00325                                 'email' => 'comment_author_email',
00326                                 'website' => 'comment_author_url',
00327                                 'body' => 'comment_content'
00328                         );
00329                 
00330                 foreach($format as $short => $long) {
00331                         if(isset($this->comment[$short])) {
00332                                 $this->comment[$long] = $this->comment[$short];
00333                                 unset($this->comment[$short]);
00334                         }
00335                 }
00336         }
00337         
00338         
00344         function _fillCommentValues() {
00345                 if(!isset($this->comment['user_ip'])) {
00346                         $this->comment['user_ip'] = ($_SERVER['REMOTE_ADDR'] != getenv('SERVER_ADDR')) ? $_SERVER['REMOTE_ADDR'] : getenv('HTTP_X_FORWARDED_FOR');
00347                 }
00348                 if(!isset($this->comment['user_agent'])) {
00349                         $this->comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
00350                 }
00351                 if(!isset($this->comment['referrer'])) {
00352                         $this->comment['referrer'] = $_SERVER['HTTP_REFERER'];
00353                 }
00354                 if(!isset($this->comment['blog'])) {
00355                         $this->comment['blog'] = $this->blogUrl;
00356                 }
00357         }
00358         
00359         
00366         function _getQueryString() {
00367                 foreach($_SERVER as $key => $value) {
00368                         if(!in_array($key, $this->ignore)) {
00369                                 if($key == 'REMOTE_ADDR') {
00370                                         $this->comment[$key] = $this->comment['user_ip'];
00371                                 } else {
00372                                         $this->comment[$key] = $value;
00373                                 }
00374                         }
00375                 }
00376 
00377                 $query_string = '';
00378 
00379                 foreach($this->comment as $key => $data) {
00380                         $query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
00381                 }
00382 
00383                 return $query_string;
00384         }
00385         
00386         
00387 }
00388 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6