diff options
-rw-r--r-- | modules/comment/helpers/comment.php | 8 | ||||
-rw-r--r-- | modules/spam_filter/controllers/admin_spam_filter.php | 71 | ||||
-rw-r--r-- | modules/spam_filter/helpers/spam_filter.php | 32 | ||||
-rw-r--r-- | modules/spam_filter/helpers/spam_filter_block.php | 26 | ||||
-rw-r--r-- | modules/spam_filter/helpers/spam_filter_installer.php | 33 | ||||
-rw-r--r-- | modules/spam_filter/helpers/spam_filter_menu.php | 28 | ||||
-rw-r--r-- | modules/spam_filter/js/spam_filter.js | 18 | ||||
-rw-r--r-- | modules/spam_filter/libraries/Spam_Filter.php | 80 | ||||
-rw-r--r-- | modules/spam_filter/libraries/drivers/Akismet.php | 102 | ||||
-rw-r--r-- | modules/spam_filter/libraries/drivers/Mollon.php | 61 | ||||
-rw-r--r-- | modules/spam_filter/libraries/drivers/SpamFilter.php | 48 | ||||
-rw-r--r-- | modules/spam_filter/module.info | 3 | ||||
-rw-r--r-- | modules/spam_filter/tests/Spam_Filter_Helper_Test.php | 32 |
13 files changed, 538 insertions, 4 deletions
diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index e92c59a0..9617b706 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -51,8 +51,8 @@ class comment_Core { $comment->created = time(); // @todo Figure out how to mock up the test of the spam_filter - if (module::is_installed("spam_filter") && !TEST_MODE) { - spam_filter::verify_comment($comment); + if (module::is_installed("spam_filter") && TEST_MODE == 0) { + Spam_Filter::instance()->check_comment($comment); } else { $comment->published = true; } @@ -81,8 +81,8 @@ class comment_Core { $comment->user_agent = Kohana::$user_agent; // @todo Figure out how to mock up the test of the spam_filter - if (module::is_installed("spam_filter") && !TEST_MODE) { - spam_filter::verify_comment($comment); + if (module::is_installed("spam_filter") && TEST_MODE == 0) { + Spam_Filter::instance()->check_comment($comment); } $comment->save(); 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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Admin_Spam_Filter_Controller extends Admin_Controller { + public function index() { + $view = new Admin_View("admin.html"); + $view->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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class spam_filter { + public static function get_driver_names() { + foreach (glob(MODPATH . "spam_filter/libraries/drivers/*.php") as $file) { + if (preg_match("#spam_filter/libraries/drivers/(.*).php$#", $file, $matches)) { + if ($matches[1] != "Spam_Filter") { + $drivers[] = $matches[1]; + } + } + } + + return $drivers; + } +}
\ No newline at end of file diff --git a/modules/spam_filter/helpers/spam_filter_block.php b/modules/spam_filter/helpers/spam_filter_block.php new file mode 100644 index 00000000..30466777 --- /dev/null +++ b/modules/spam_filter/helpers/spam_filter_block.php @@ -0,0 +1,26 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ + +class spam_filter_block_Core { + public static function admin_head($theme) { + $url = url::file("modules/spam_filter/js/spam_filter.js"); + return "<script src=\"$url\" type=\"text/javascript\"></script>\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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class spam_filter_installer { + + public static function install() { + $version = module::get_version("spam_filter"); + + if ($version == 0) { + module::set_version("spam_filter", 1); + } + } + + public static function uninstall() { + module::delete("spam_filter"); + } +} diff --git a/modules/spam_filter/helpers/spam_filter_menu.php b/modules/spam_filter/helpers/spam_filter_menu.php new file mode 100644 index 00000000..06ff14d6 --- /dev/null +++ b/modules/spam_filter/helpers/spam_filter_menu.php @@ -0,0 +1,28 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class spam_filter_menu_Core { + public static function admin($menu, $theme) { + $menu->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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Spam_Filter_Core { + + private static $spam_filter; + + protected $driver; + + public static function instance() { + if (empty(self::$spam_filter)) { + self::$spam_filter = new Spam_Filter(); + } + return self::$spam_filter; + } + + protected function __construct() { + $driver = module::get_var("spam_filter", "driver", null); + $api_key = module::get_var("spam_filter", "api_key", null); + + if (empty($api_key)) { + throw new Exception("@todo SPAM FILTER NOT INITIALIZED"); + } + + // Set driver name + $driver = "{$driver}_Driver"; + + // Load the driver + if (!Kohana::auto_load($driver)) { + throw new Exception("@todo SPAM FILTER DRIVER NO FOUND"); + } + + // Initialize the driver + $this->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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Akismet_Driver extends SpamFilter_Driver { + // Lets not send everything to Akismet + 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; + } + + 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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Mollon_Driver extends SpamFilter_Driver { + // Lets not send everything to Akismet + 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" ); + + protected $_api_key; + + public function verify_key($api_key) { + return true; + } + + public function check_comment($comment_data) { + return true; + } + + public function submit_spam($comment_data) { + return $response[1] == "true"; + } + + public function submit_ham($comment_data) { + } + + public function get_statistics() { + throw new Exception("@todo GET_STATISTICS NOT IMPLEMENTED"); + } + + private function _build_request($function, $host,$comment_data) { + return ""; + } +}
\ No newline at end of file diff --git a/modules/spam_filter/libraries/drivers/SpamFilter.php b/modules/spam_filter/libraries/drivers/SpamFilter.php new file mode 100644 index 00000000..3481aee2 --- /dev/null +++ b/modules/spam_filter/libraries/drivers/SpamFilter.php @@ -0,0 +1,48 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +abstract class SpamFilter_Driver { + public abstract function verify_key($api_key); + + public abstract function check_comment($comment); + + public abstract function submit_spam($comment); + + public abstract function submit_ham($comment); + + public abstract function get_statistics(); + + protected function _http_post($host, $http_request, $port=80, $timeout=5) { + $response = ""; + if (false !== ($fs = @fsockopen($host, $port, $errno, $errstr, $timeout))) { + 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 = array("headers" => $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 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Spam_Filter_Helper_Test extends Unit_Test_Case { + public function get_driver_names_test() { + $current_driver = module::get_var("spam_filter", "driver"); + foreach (glob(MODPATH . "spam_filter/libraries/drivers/*.php") as $file) { + if (preg_match("#spam_filter/libraries/drivers/(.*).php$#", $file, $matches)) { + if ($matches[1] != "Spam_Filter") { + $expected[$matches[1]] = $matches[1] === $current_driver; + } + } + } + $this->assert_equal($expected, spam_filter::get_driver_names()); + } +}
\ No newline at end of file |