From 517ddf53883214f225ad76997fd033faae276a42 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Wed, 31 Dec 2008 06:19:35 +0000 Subject: First look at the spam filter module. At this point, it does not connect to any spam services. But you can go into the admin screen and choose the driver and set the api key. --- .../spam_filter/controllers/admin_spam_filter.php | 71 ++++++++++++++ modules/spam_filter/helpers/spam_filter.php | 32 +++++++ modules/spam_filter/helpers/spam_filter_block.php | 26 ++++++ .../spam_filter/helpers/spam_filter_installer.php | 33 +++++++ modules/spam_filter/helpers/spam_filter_menu.php | 28 ++++++ modules/spam_filter/js/spam_filter.js | 18 ++++ modules/spam_filter/libraries/Spam_Filter.php | 80 ++++++++++++++++ modules/spam_filter/libraries/drivers/Akismet.php | 102 +++++++++++++++++++++ modules/spam_filter/libraries/drivers/Mollon.php | 61 ++++++++++++ .../spam_filter/libraries/drivers/SpamFilter.php | 48 ++++++++++ modules/spam_filter/module.info | 3 + .../spam_filter/tests/Spam_Filter_Helper_Test.php | 32 +++++++ 12 files changed, 534 insertions(+) create mode 100644 modules/spam_filter/controllers/admin_spam_filter.php create mode 100644 modules/spam_filter/helpers/spam_filter.php create mode 100644 modules/spam_filter/helpers/spam_filter_block.php create mode 100644 modules/spam_filter/helpers/spam_filter_installer.php create mode 100644 modules/spam_filter/helpers/spam_filter_menu.php create mode 100644 modules/spam_filter/js/spam_filter.js create mode 100644 modules/spam_filter/libraries/Spam_Filter.php create mode 100644 modules/spam_filter/libraries/drivers/Akismet.php create mode 100644 modules/spam_filter/libraries/drivers/Mollon.php create mode 100644 modules/spam_filter/libraries/drivers/SpamFilter.php create mode 100644 modules/spam_filter/module.info create mode 100644 modules/spam_filter/tests/Spam_Filter_Helper_Test.php (limited to 'modules/spam_filter') diff --git a/modules/spam_filter/controllers/admin_spam_filter.php b/modules/spam_filter/controllers/admin_spam_filter.php new file mode 100644 index 00000000..cf06d012 --- /dev/null +++ b/modules/spam_filter/controllers/admin_spam_filter.php @@ -0,0 +1,71 @@ +content = $this->get_edit_form(); + + print $view; + } + + public function get_edit_form() { + $form = new Forge("admin/spam_filter/edit", "", "post"); + $group = $form->group("edit_spam_filter")->label(_("Configure Spam Filter")); + $drivers = spam_filter::get_driver_names(); + $current_driver = module::get_var("spam_filter", "driver"); + + $selected = -1; + foreach ($drivers as $idx => $driver) { + if ($driver == $current_driver) { + $selected = $idx; + } + } + $group->dropdown("drivers")->label(_("Available Drivers")) + ->options(spam_filter::get_driver_names()) + ->rules("required") + ->selected($selected); + $group->input("api_key")->label(_("Api Key")) + ->rules("required") + ->value(module::get_var("spam_filter", "api_key")); + $group->submit(_("Configure")); + + return $form; + } + + public function edit() { + $form = $this->get_edit_form(); + if ($form->validate()) { + $driver_index = $form->edit_spam_filter->drivers->value; + $drivers = spam_filter::get_driver_names(); + module::set_var("spam_filter", "driver", $drivers[$driver_index]); + // @todo do verify key + module::set_var("spam_filter", "api_key", $form->edit_spam_filter->api_key->value); + log::success("spam_filter", _("Spam Filter configured")); + message::success(_("Spam Filter configured")); + print json_encode( + array("result" => "success", + "location" => url::site("admin/spam_filter"))); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); + } + } +} \ No newline at end of file diff --git a/modules/spam_filter/helpers/spam_filter.php b/modules/spam_filter/helpers/spam_filter.php new file mode 100644 index 00000000..bc6f277d --- /dev/null +++ b/modules/spam_filter/helpers/spam_filter.php @@ -0,0 +1,32 @@ +\n"; + } +} \ No newline at end of file diff --git a/modules/spam_filter/helpers/spam_filter_installer.php b/modules/spam_filter/helpers/spam_filter_installer.php new file mode 100644 index 00000000..4d0edde9 --- /dev/null +++ b/modules/spam_filter/helpers/spam_filter_installer.php @@ -0,0 +1,33 @@ +get("content_menu") + ->append(Menu::factory("link") + ->id("spam_filter") + ->label(_("Configure Spam Filter")) + ->url(url::site("admin/spam_filter"))); + } +} diff --git a/modules/spam_filter/js/spam_filter.js b/modules/spam_filter/js/spam_filter.js new file mode 100644 index 00000000..175732ab --- /dev/null +++ b/modules/spam_filter/js/spam_filter.js @@ -0,0 +1,18 @@ +$("document").ready(function() { + ajaxify_spam_filter_form(); +}); + +function ajaxify_spam_filter_form() { + $("#gContent form").ajaxForm({ + dataType: "json", + success: function(data) { + if (data.form) { + $("#gContent form").replaceWith(data.form); + ajaxify_spam_filter_form(); + } + if (data.result == "success") { + window.location.reload(); + } + } + }); +}; diff --git a/modules/spam_filter/libraries/Spam_Filter.php b/modules/spam_filter/libraries/Spam_Filter.php new file mode 100644 index 00000000..47f45eb9 --- /dev/null +++ b/modules/spam_filter/libraries/Spam_Filter.php @@ -0,0 +1,80 @@ +driver = new $driver(); + + // Validate the driver + if (!($this->driver instanceof SpamFilter_Driver)) { + throw new Exception("@todo SPAM FILTER DRIVER NOT IMPLEMENTED"); + } + } + + public function verify_key($api_key) { + return $this->driver->verify_key($api_key); + } + + public function check_comment($comment) { + $is_valid = $this->driver->check_comment($comment); + $comment->published = $is_valid; + return $is_valid; + } + + public function submit_spam($comment) { + return $this->driver->submit_spam($comment); + } + + public function submit_ham($comment) { + return $this->driver->submit_ham($comment); + } + + public function get_statistics() { + return $this->driver->get_statistics(); + } +} + diff --git a/modules/spam_filter/libraries/drivers/Akismet.php b/modules/spam_filter/libraries/drivers/Akismet.php new file mode 100644 index 00000000..73e1e4e4 --- /dev/null +++ b/modules/spam_filter/libraries/drivers/Akismet.php @@ -0,0 +1,102 @@ +_http_post("rest.akismet.com", "key={$api_key}&blog=$url"); +// if ("valid" != $response[1]) { +// throw new Exception("@todo INVALID AKISMET KEY"); +// } + return true; + } + + 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; + } + + 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; + } + + 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; + } + + public function get_statistics() { + throw new Exception("@todo GET_STATISTICS NOT SUPPORTED"); + } + + private function _build_request($function, $comment) { + $comment_data = array(); + foreach($_SERVER as $key => $value) { + if(!in_array($key, $this->ignore)) { + $comment_data[$key] = $value; + } + } + + $query_string = ""; + foreach($comment_data as $key => $data) { + if(!is_array($data)) { + $query_string .= $key . "=" . urlencode(stripslashes($data)) . "&"; + } + } + + $host = $this->_get_host_url(); + $http_request = "POST /1.1/$function HTTP/1.0\r\n"; + $http_request .= "Host: $host\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; + } + + private function _get_host_url() { + $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/Mollon.php b/modules/spam_filter/libraries/drivers/Mollon.php new file mode 100644 index 00000000..cf6972f5 --- /dev/null +++ b/modules/spam_filter/libraries/drivers/Mollon.php @@ -0,0 +1,61 @@ + $headers, "body" => $body); + } else { + throw new Exception("@todo CONNECTION TO SPAM SERVICE FAILED"); + } + return $response; + } +} \ No newline at end of file diff --git a/modules/spam_filter/module.info b/modules/spam_filter/module.info new file mode 100644 index 00000000..5909e987 --- /dev/null +++ b/modules/spam_filter/module.info @@ -0,0 +1,3 @@ +name = Spam Filter +description = Flag comments based on whether they are spam or not. +version = 1 diff --git a/modules/spam_filter/tests/Spam_Filter_Helper_Test.php b/modules/spam_filter/tests/Spam_Filter_Helper_Test.php new file mode 100644 index 00000000..c52883d8 --- /dev/null +++ b/modules/spam_filter/tests/Spam_Filter_Helper_Test.php @@ -0,0 +1,32 @@ +assert_equal($expected, spam_filter::get_driver_names()); + } +} \ No newline at end of file -- cgit v1.2.3