diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-01-06 23:40:17 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-01-06 23:40:17 +0000 |
commit | 8e16931afd363212e409b4b58a59b0d609f99ab3 (patch) | |
tree | bb047d46e33f70ba69ac3ba159a33e6241892329 | |
parent | 4645c459e14c5dbd2330ea176396784049a1cdc8 (diff) |
The Akismet driver. This will now verify the api key and attempt to check the comment
as spam. For some reason, it is always returning all comments as spam.
-rw-r--r-- | modules/spam_filter/libraries/drivers/Akismet.php | 117 | ||||
-rw-r--r-- | modules/spam_filter/libraries/drivers/Mollom.php | 21 |
2 files changed, 88 insertions, 50 deletions
diff --git a/modules/spam_filter/libraries/drivers/Akismet.php b/modules/spam_filter/libraries/drivers/Akismet.php index 7d0eee78..67b4a88d 100644 --- a/modules/spam_filter/libraries/drivers/Akismet.php +++ b/modules/spam_filter/libraries/drivers/Akismet.php @@ -19,50 +19,40 @@ */ class Akismet_Driver extends SpamFilter_Driver { // Lets not send everything to Akismet - // @todo change to a white list - private $ignore = array("HTTP_COOKIE", - "HTTP_USER_AGENT", - "HTTP_X_FORWARDED_FOR", - "HTTP_X_FORWARDED_HOST", - "HTTP_MAX_FORWARDS", - "HTTP_X_FORWARDED_SERVER", - "REDIRECT_STATUS", - "SERVER_PORT", - "PATH", - "DOCUMENT_ROOT", - "REMOTE_ADDR", - "SERVER_ADMIN", - "QUERY_STRING", - "PHP_SELF" ); - -// public function verify_key($api_key) { -//// $url = url::base(); -//// $response = $this->_http_post("rest.akismet.com", "key={$api_key}&blog=$url"); -//// if ("valid" != $response[1]) { -//// throw new Exception("@todo INVALID AKISMET KEY"); -//// } -// return true; -// } + // @todo provide an admin option to send or not send this information + private static $white_list = array("HTTP_USER_AGENT", + "HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET", "HTTP_ACCEPT_ENCODING", + "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION", "HTTP_HOST", + "HTTP_KEEP_ALIVE", "HTTP_REFERER", "HTTP_USER_AGENT", "QUERY_STRING", + "REMOTE_ADDR", "REMOTE_HOST", "REMOTE_PORT" ); public function check_comment($comment) { -// $request = $this->_build_request("comment-check", $comment); -// $response = $this->_http_post($this->_get_host_url(), $request); -// return $reponse[1] == "true"; - return true; + $request = $this->_build_request("comment-check", $comment); + $response = $this->_http_post($this->_get_host_url(), $request); + + Kohana::log("debug", print_r($response, 1)); + if ($response["body"][0] != "true" && $response["body"][0] != "false") { + Kohana::log("alert", $response["body"][0]); + } + return $response["body"][0] == "true"; } public function submit_spam($comment) { -// $request = $this->_build_request("submit-spam", $comment); -// $response = $this->_http_post($this->_get_host_url(), $request); -// return $response[1] == "true"; - return true; + $request = $this->_build_request("submit-spam", $comment); + $response = $this->_http_post($this->_get_host_url(), $request); + if ($response["body"][0] != "true" && $response["body"][0] != "false") { + Kohana::log("alert", $response["body"][0]); + } + return $response["body"][0] == "true"; } public function submit_ham($comment) { -// $request = $this->_build_request("submit-ham", $comment); -// $response = $this->_http_post($this->_get_host_url(), $request); -// return $reponse[1] == "true"; - return true; + $request = $this->_build_request("submit-ham", $comment); + $response = $this->_http_post($this->_get_host_url(), $request); + if ($response["body"][0] != "true" && $response["body"][0] != "false") { + Kohana::log("alert", $response["body"][0]); + } + return $response["body"][0] == "true"; } public function get_statistics() { @@ -70,7 +60,7 @@ class Akismet_Driver extends SpamFilter_Driver { } public function get_admin_fields($post) { - $view = new View("spam_filter_admin_akismet.html"); + $view = new View("admin_spam_filter_akismet.html"); $view->api_key = empty($post) ? module::get_var("spam_filter", "api_key") : $post->api_key; @@ -83,31 +73,62 @@ class Akismet_Driver extends SpamFilter_Driver { $post->add_callbacks("api_key", array($this, "validate_key")); } - public function validate_key(Validation $array, $field) { - // @todo verify key values - Kohana::log("debug", "Akismet::validate_key"); - Kohana::log("debug", print_r($array, 1)); - Kohana::log("debug", "field: $field"); + public function validate_key(Validation $post, $field) { + $request = $this->_build_verify_request($post->api_key); + $response = $this->_http_post("rest.akismet.com", $request); + Kohana::log("debug", print_r($response, 1)); + if ("valid" != $response["body"][0]) { + $post->add_error("api_key", "invalid"); + Kohana::log("alert", "Failed to verify Akismet Key:\n" . print_r($response["headers"], 1)); + } + } + + public function _build_verify_request($api_key) { + $base_url = url::base(true, true); + $query_string = "key={$api_key}&blog=$base_url"; + + $http_request = "POST /1.1/verify-key HTTP/1.0\r\n"; + $http_request .= "Host: rest.akismet.com\r\n"; + $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n"; + $http_request .= "Content-Length: " . strlen($query_string) . "\r\n"; + $http_request .= "User-Agent: Gallery 3.0 | Akismet/1.11 \r\n"; + $http_request .= "\r\n"; + $http_request .= $query_string; + + return $http_request; } public function set_api_data($post) { module::set_var("spam_filter", "api_key", $post->api_key); } - private function _build_request($function, $comment) { + public function _build_request($function, $comment) { $comment_data = array(); + $comment_data["user_ip"] = $comment->ip_addr; + $comment_data["permalink"] = url::site("comments/{$comment->id}"); + $comment_data["blog"] = url::base(true, true); + $comment_data["user_agent"] = $comment->user_agent; + $comment_data["referrer"] = $_SERVER["HTTP_REFERER"]; + $comment_data["comment_type"] = "comment"; + $comment_data["comment_author"] = $comment->author; + $comment_data["comment_author_email"] = $comment->email; + $comment_data["comment_author_url"] = str_replace(array("http://", "https://"), "", $comment->url); + $comment_data["comment_content"] = $comment->text; + foreach($_SERVER as $key => $value) { - if(!in_array($key, $this->ignore)) { + if(in_array($key, self::$white_list)) { $comment_data[$key] = $value; } } - $query_string = ""; + $query_string = array(); foreach($comment_data as $key => $data) { if(!is_array($data)) { - $query_string .= $key . "=" . urlencode(stripslashes($data)) . "&"; +// $query_string .= $key . "=" . urlencode(stripslashes($data)) . "&"; + $query_string[] = "$key=" . urlencode($data); } } + $query_string = join("&", $query_string); $host = $this->_get_host_url(); $http_request = "POST /1.1/$function HTTP/1.0\r\n"; @@ -118,6 +139,8 @@ class Akismet_Driver extends SpamFilter_Driver { $http_request .= "\r\n"; $http_request .= $query_string; + Kohana::log("debug", $http_request); + return $http_request; } @@ -125,4 +148,4 @@ class Akismet_Driver extends SpamFilter_Driver { $api_key = module::get_var("spam_filter", "api_key"); return "$api_key.rest.akismet.com"; } -}
\ No newline at end of file +} diff --git a/modules/spam_filter/libraries/drivers/Mollom.php b/modules/spam_filter/libraries/drivers/Mollom.php index 1b8c9aa5..6887a1be 100644 --- a/modules/spam_filter/libraries/drivers/Mollom.php +++ b/modules/spam_filter/libraries/drivers/Mollom.php @@ -34,7 +34,7 @@ class Mollom_Driver extends SpamFilter_Driver { } public function get_admin_fields($post) { - $view = new View("spam_filter_admin_mollom.html"); + $view = new View("admin_spam_filter_mollom.html"); $view->private_key = empty($post) ? module::get_var("spam_filter", "private_key") : $post->private_key; $view->public_key = empty($post) ? module::get_var("spam_filter", "public_key") : @@ -62,7 +62,22 @@ class Mollom_Driver extends SpamFilter_Driver { module::set_var("spam_filter", "public_key", $post->public_key); } - private function _build_request($function, $host,$comment_data) { + private function _build_request($function, $host, $comment_data) { return ""; } -}
\ No newline at end of file + + public function _retrieve_serverList() { + $server_list = module::get_var("spam_filter", "server_list"); + if (empty($server_list)) { + $servers = array("http://xmlrpc1.mollom.com", "http://xmlrpc2.mollom.com", "http://xmlrpc1.mollom.com"); + foreach (array("http://xmlrpc1.mollom.com", "http://xmlrpc2.mollom.com", "http://xmlrpc1.mollom.com") as $server) { + $result = xmlrpc($server . "/1.0"); + if (!xmplrpc_errno()) { + module::set_var("spam_filter", "server_list", $result); + $server_list = $result; + } + } + } + return $server_list; + } +} |