From ba052c0cc94a1b1acbe3322e9e3705f71f7134ac Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 7 Jan 2009 07:36:48 +0000 Subject: Extract the Akismet driver from the Spam_Filter module into a module in its own right. Clean up the tests, streamline the code and improve the admin interaction. Add a working stats page. --- modules/akismet/helpers/akismet.php | 142 ++++++++++++++++++++++++++ modules/akismet/helpers/akismet_installer.php | 32 ++++++ modules/akismet/helpers/akismet_menu.php | 36 +++++++ 3 files changed, 210 insertions(+) create mode 100644 modules/akismet/helpers/akismet.php create mode 100644 modules/akismet/helpers/akismet_installer.php create mode 100644 modules/akismet/helpers/akismet_menu.php (limited to 'modules/akismet/helpers') diff --git a/modules/akismet/helpers/akismet.php b/modules/akismet/helpers/akismet.php new file mode 100644 index 00000000..7dbbe98e --- /dev/null +++ b/modules/akismet/helpers/akismet.php @@ -0,0 +1,142 @@ +group("configure_akismet")->label(_("Configure Akismet")); + $group->input("api_key")->label(_("API Key"))->value(module::get_var("akismet", "api_key")); + $group->api_key->error_messages("invalid", _("The API key you provided is invalid.")); + $group->submit(_("Save")); + return $form; + } + + public static function check_comment($comment) { + $request = self::_build_request("comment-check", $comment); + $response = self::_http_post($request); + return "true" == $response->body[0]; + } + + public static function submit_spam($comment) { + $request = self::_build_request("submit-spam", $comment); + $response = self::_http_post($request); + return "true" == $response->body[0]; + } + + public static function submit_ham($comment) { + $request = self::_build_request("submit-ham", $comment); + $response = self::_http_post($request); + return "true" == $response->body[0]; + } + + public static function validate_key($api_key) { + $request = self::_build_verify_request($api_key); + $response = self::_http_post($request, "rest.akismet.com"); + return "valid" == $response->body[0]; + } + + public static function _build_verify_request($api_key) { + $base_url = url::base(false, "http"); + $query_string = "key={$api_key}&blog=$base_url"; + + $version = module::get_version("akismet"); + $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 | Akismet/$version\r\n"; + $http_request .= "\r\n"; + $http_request .= $query_string; + + return $http_request; + } + + public static 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(false, "http"); + $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"] = $comment->url; + $comment_data["comment_content"] = $comment->text; + + foreach (self::$white_list as $key) { + if (array_key_exists($key, $_SERVER)) { + $comment_data[$key] = $_SERVER[$key]; + } + } + + $query_string = array(); + foreach ($comment_data as $key => $data) { + if (!is_array($data)) { + $query_string[] = "$key=" . urlencode($data); + } + } + $query_string = join("&", $query_string); + + $version = module::get_version("akismet"); + $http_request = "POST /1.1/$function HTTP/1.0\r\n"; + $http_request .= "Host: " . module::get_var("akismet", "api_key") . ".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 | Akismet/$version\r\n"; + $http_request .= "\r\n"; + $http_request .= $query_string; + + return $http_request; + } + + private static function _http_post($http_request, $host=null) { + if (!$host) { + $host = module::get_var("akismet", "api_key") . ".rest.akismet.com"; + } + $response = ""; + + Kohana::log("debug", "Send request\n" . print_r($http_request, 1)); + if (false !== ($fs = @fsockopen($host, 80, $errno, $errstr, 5))) { + fwrite($fs, $http_request); + while ( !feof($fs) ) { + $response .= fgets($fs, 1160); // One TCP-IP packet + } + fclose($fs); + list($headers, $body) = explode("\r\n\r\n", $response); + $headers = explode("\r\n", $headers); + $body = explode("\r\n", $body); + $response = new ArrayObject( + array("headers" => $headers, "body" => $body), ArrayObject::ARRAY_AS_PROPS); + } else { + throw new Exception("@todo CONNECTION TO SPAM SERVICE FAILED"); + } + Kohana::log("debug", "Received response\n" . print_r($response, 1)); + + return $response; + } +} diff --git a/modules/akismet/helpers/akismet_installer.php b/modules/akismet/helpers/akismet_installer.php new file mode 100644 index 00000000..cb1bff34 --- /dev/null +++ b/modules/akismet/helpers/akismet_installer.php @@ -0,0 +1,32 @@ +get("settings_menu") + ->append(Menu::factory("link") + ->id("akismet") + ->label(_("Akismet")) + ->url(url::site("admin/akismet"))); + + if (module::get_var("akismet", "api_key")) { + $menu->get("statistics_menu") + ->append(Menu::factory("link") + ->id("akismet") + ->label(_("Akismet")) + ->url(url::site("admin/akismet/stats"))); + } + } +} -- cgit v1.2.3