<?php

    
/**
     * Lightweight Akismet API interface using cURL.
     * Aspektas http://www.aspektas.com/ 2009, some rights reserved. Distribution governed by the 
     * Creative Commons Attribution-Share Alike 3.0 Unported license,
     * http://creativecommons.org/licenses/by-sa/3.0/.
     * This notice must remain intact. That is all.
     */
     
    
class Akismet {
    
        private 
$blog;
        private 
$api_key;
        private 
$comment_fields
        
        function 
__construct($blog$api_key) {
            if (!
$this->is_valid_key($blog$api_key)) throw new Exception("Invalid key.");
            else {
                
$this->blog $blog;
                
$this->api_key $api_key;
                
$this->comment_fields['blog'] = $this->blog;
                
$this->comment_fields['user_ip'] = $_SERVER['REMOTE_ADDR'];
                
$this->comment_fields['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
            }
        }
        
        function 
is_valid_key($blog$api_key) {
            
$fields = array(
                
"key" => $api_key,
                
"blog" => $blog
            
);
            
$result $this->send($fields"verify-key");
            if (
strpos($result"invalid") !== false) return false;
            else return 
true;
        }
        
        function 
is_spam() {
            
$result $this->send($this->comment_fields);
            if (
strpos($result"true") !== false) return true;
            else return 
false;
        }
        
        function 
submit_spam() {
            
$this->send($this->comment_fields"submit-spam");
        }
        
        function 
submit_ham() {
            
$this->send($this->comment_fields"submit-ham");
        }
        
        function 
add_field($field$value) {
            
$this->comment_fields[$field] = $value;
        }
        
        function 
add_fields($array) { //e.g. add_fields($_SERVER)
            
foreach ($array as $key => $value$this->comment_field[$key] = $value;
        }
        
        private function 
send($fields$method "comment-check") {
            
$curl curl_init();
            
$url "http://" . ($method == "verify-key" "" "{$this->api_key}.") . "rest.akismet.com/1.1/$method";
            
            
curl_setopt($curlCURLOPT_URL$url);
            
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
            
//curl_setopt($curl, CURLOPT_HEADER, true);
            
curl_setopt($curlCURLOPT_POSTtrue);
            
curl_setopt($curlCURLOPT_USERAGENT"Aspektas/5 | Akismet/0.1");
            
curl_setopt($curlCURLOPT_POSTFIELDS$fields);
            
$result curl_exec($curl);
            return 
$result;
        }
    }
?>