diff options
-rw-r--r-- | modules/comment/helpers/comment.php | 4 | ||||
-rw-r--r-- | modules/comment/helpers/comment_installer.php | 4 | ||||
-rw-r--r-- | modules/spam_filter/controllers/admin_spam_filter.php | 124 | ||||
-rw-r--r-- | modules/spam_filter/helpers/spam_filter.php | 1 | ||||
-rw-r--r-- | modules/spam_filter/helpers/spam_filter_block.php | 26 | ||||
-rw-r--r-- | modules/spam_filter/js/spam_filter.js | 18 | ||||
-rw-r--r-- | modules/spam_filter/libraries/SpamFilter.php (renamed from modules/spam_filter/libraries/Spam_Filter.php) | 46 | ||||
-rw-r--r-- | modules/spam_filter/libraries/drivers/Akismet.php | 42 | ||||
-rw-r--r-- | modules/spam_filter/libraries/drivers/Mollom.php | 51 | ||||
-rw-r--r-- | modules/spam_filter/libraries/drivers/SpamFilter.php | 8 | ||||
-rw-r--r-- | modules/spam_filter/tests/Spam_Filter_Helper_Test.php | 4 | ||||
-rw-r--r-- | modules/spam_filter/views/spam_filter_admin.html.php | 48 | ||||
-rw-r--r-- | modules/spam_filter/views/spam_filter_admin_akismet.html.php | 12 | ||||
-rw-r--r-- | modules/spam_filter/views/spam_filter_admin_mollom.html.php | 19 |
14 files changed, 288 insertions, 119 deletions
diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index 9617b706..82f31747 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -52,7 +52,7 @@ class comment_Core { // @todo Figure out how to mock up the test of the spam_filter if (module::is_installed("spam_filter") && TEST_MODE == 0) { - Spam_Filter::instance()->check_comment($comment); + SpamFilter::instance()->check_comment($comment); } else { $comment->published = true; } @@ -82,7 +82,7 @@ class comment_Core { // @todo Figure out how to mock up the test of the spam_filter if (module::is_installed("spam_filter") && TEST_MODE == 0) { - Spam_Filter::instance()->check_comment($comment); + SpamFilter::instance()->check_comment($comment); } $comment->save(); diff --git a/modules/comment/helpers/comment_installer.php b/modules/comment/helpers/comment_installer.php index b38b127c..3e7b2f49 100644 --- a/modules/comment/helpers/comment_installer.php +++ b/modules/comment/helpers/comment_installer.php @@ -23,6 +23,10 @@ class comment_installer { $version = module::get_version("comment"); if ($version == 0) { + /** + * @todo change published flag to char(xx) with values published, unpublished, moderation + * unreviewed, spam + */ $db->query("CREATE TABLE IF NOT EXISTS `comments` ( `id` int(9) NOT NULL auto_increment, `author` varchar(128) default NULL, diff --git a/modules/spam_filter/controllers/admin_spam_filter.php b/modules/spam_filter/controllers/admin_spam_filter.php index cf06d012..24ae5149 100644 --- a/modules/spam_filter/controllers/admin_spam_filter.php +++ b/modules/spam_filter/controllers/admin_spam_filter.php @@ -25,47 +25,119 @@ class Admin_Spam_Filter_Controller extends Admin_Controller { 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")); + public function get_edit_form($driver_name=null, $post=null) { + $form = new View("spam_filter_admin.html"); + $drivers = spam_filter::get_driver_names(); - $current_driver = module::get_var("spam_filter", "driver"); + $current_driver = empty($driver_name) ? module::get_var("spam_filter", "driver") : $driver_name; + $current_driver = !empty($current_driver) ? $current_driver : $drivers[0]; - $selected = -1; + $selected = 0; + $driver_options = array(); foreach ($drivers as $idx => $driver) { if ($driver == $current_driver) { $selected = $idx; } + $driver_options[] = array("name" => $driver, "selected" => $driver == $current_driver); } - $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")); + $form->drivers = $driver_options; + + $form->filter_data = empty($selected) ? "" : + SpamFilter::instance($current_driver)->get_admin_fields($post); 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"))); + $selected = Input::instance()->post("drivers"); + $drivers = spam_filter::get_driver_names(); + $driver_name = $drivers[$selected]; + + if (!empty($selected)) { + $post = new Validation($_POST); + SpamFilter::instance($driver_name)->get_validation_rules($post); + if ($post->validate()) { + module::set_var("spam_filter", "driver", $drivers[$selected]); + SpamFilter::instance($driver_name)->set_api_data($post); + + 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 { + $form = $this->get_edit_form($driver_name, $post); + print json_encode( + array("result" => "error", + "form" => $form->__toString())); + + } } else { + $form = $this->get_edit_form(); print json_encode( - array("result" => "error", + array("result" => "continue", "form" => $form->__toString())); } +// $selected = Input::instance()->post("selected"); +// $new_driver_idx = Input::instance()->post("drivers"); +// +// if ($selected != $new_driver_idx) { +// $drivers = spam_filter::get_driver_names(); +// $form = $this->get_edit_form($drivers[$new_driver_idx]); +// $form->edit_spam_filter->selected = $new_driver_idx; +// unset($_POST["drivers"]) +// print json_encode( +// array("result" => "continue", +// "form" => $form->__toString())); +// } else { +// Kohana::log("debug", "validate form"); +// $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]); +// +// if (SpamFilter::instance()->set_admin_fields($form->edit_spam_filter->api_data)) { +// $key_verified = module::set_var("spam_filter", "key_verified", true); +// 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())); +// } +// } else { +// print json_encode( +// array("result" => "error", +// "form" => $form->__toString())); +// } +// } + } + + public function callback() { + $driver_name = Input::instance()->post("driver"); + + $selected = $this->_get_selected_index($driver_name); + if (!empty($selected)) { + print SpamFilter::instance($driver_name)->get_admin_fields(); + } else { + print ""; + } + } + + public function _get_selected_index($driver_name) { + $drivers = spam_filter::get_driver_names(); + + $selected = 0; + foreach ($drivers as $idx => $driver) { + if ($driver == $driver_name) { + $selected = $idx; + } + } + + return $selected; } }
\ No newline at end of file diff --git a/modules/spam_filter/helpers/spam_filter.php b/modules/spam_filter/helpers/spam_filter.php index c93c9de4..2531392b 100644 --- a/modules/spam_filter/helpers/spam_filter.php +++ b/modules/spam_filter/helpers/spam_filter.php @@ -19,6 +19,7 @@ */ class spam_filter { public static function get_driver_names() { + $drivers = array(_("Select Driver")); foreach (glob(MODPATH . "spam_filter/libraries/drivers/*.php") as $file) { if (preg_match("#spam_filter/libraries/drivers/(.*).php$#", $file, $matches)) { if ($matches[1] != "SpamFilter") { diff --git a/modules/spam_filter/helpers/spam_filter_block.php b/modules/spam_filter/helpers/spam_filter_block.php deleted file mode 100644 index 30466777..00000000 --- a/modules/spam_filter/helpers/spam_filter_block.php +++ /dev/null @@ -1,26 +0,0 @@ -<?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/js/spam_filter.js b/modules/spam_filter/js/spam_filter.js deleted file mode 100644 index 175732ab..00000000 --- a/modules/spam_filter/js/spam_filter.js +++ /dev/null @@ -1,18 +0,0 @@ -$("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/SpamFilter.php index 47f45eb9..7b826a21 100644 --- a/modules/spam_filter/libraries/Spam_Filter.php +++ b/modules/spam_filter/libraries/SpamFilter.php @@ -17,25 +17,22 @@ * 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 { +class SpamFilter_Core { private static $spam_filter; protected $driver; - public static function instance() { + public static function instance($driver=null) { if (empty(self::$spam_filter)) { - self::$spam_filter = new Spam_Filter(); + self::$spam_filter = new SpamFilter($driver); } 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"); + protected function __construct($driver=null) { + if (empty($driver)) { + $driver = module::get_var("spam_filter", "driver", null); } // Set driver name @@ -55,26 +52,49 @@ class Spam_Filter_Core { } } - public function verify_key($api_key) { - return $this->driver->verify_key($api_key); - } - public function check_comment($comment) { + $this->_is_initialized(); + $is_valid = $this->driver->check_comment($comment); $comment->published = $is_valid; return $is_valid; } public function submit_spam($comment) { + $this->_is_initialized(); + return $this->driver->submit_spam($comment); } public function submit_ham($comment) { + $this->_is_initialized(); + return $this->driver->submit_ham($comment); } public function get_statistics() { + $this->_is_initialized(); return $this->driver->get_statistics(); } + + public function get_admin_fields($post=null) { + return $this->driver->get_admin_fields($post); + } + + public function get_validation_rules($post) { + $this->driver->get_validation_rules($post); + } + + public function set_api_data($post) { + $this->driver->set_api_data($post); + module::set_var("spam_filter", "key_verified", true); + } + + private function _is_initialized() { + $key_verified = module::get_var("spam_filter", "key_verified", null); + if (empty($key_verified)) { + throw new Exception("@todo SPAM FILTER NOT INITIALIZED"); + } + } } diff --git a/modules/spam_filter/libraries/drivers/Akismet.php b/modules/spam_filter/libraries/drivers/Akismet.php index 73e1e4e4..7d0eee78 100644 --- a/modules/spam_filter/libraries/drivers/Akismet.php +++ b/modules/spam_filter/libraries/drivers/Akismet.php @@ -19,6 +19,7 @@ */ 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", @@ -34,14 +35,14 @@ class Akismet_Driver extends SpamFilter_Driver { "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 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); @@ -68,6 +69,31 @@ class Akismet_Driver extends SpamFilter_Driver { throw new Exception("@todo GET_STATISTICS NOT SUPPORTED"); } + public function get_admin_fields($post) { + $view = new View("spam_filter_admin_akismet.html"); + $view->api_key = empty($post) ? module::get_var("spam_filter", "api_key") : + $post->api_key; + + $view->errors = $post ? $post->errors() : null; + return $view; + } + + public function get_validation_rules($post) { + $post->add_rules("api_key", "required"); + $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 set_api_data($post) { + module::set_var("spam_filter", "api_key", $post->api_key); + } + private function _build_request($function, $comment) { $comment_data = array(); foreach($_SERVER as $key => $value) { diff --git a/modules/spam_filter/libraries/drivers/Mollom.php b/modules/spam_filter/libraries/drivers/Mollom.php index 45b978c1..1b8c9aa5 100644 --- a/modules/spam_filter/libraries/drivers/Mollom.php +++ b/modules/spam_filter/libraries/drivers/Mollom.php @@ -18,28 +18,6 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Mollom_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; } @@ -55,6 +33,35 @@ class Mollom_Driver extends SpamFilter_Driver { throw new Exception("@todo GET_STATISTICS NOT IMPLEMENTED"); } + public function get_admin_fields($post) { + $view = new View("spam_filter_admin_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") : + $post->private_key; + + $view->errors = $post ? $post->errors() : null; + return $view; + } + + public function get_validation_rules($post) { + $post->add_rules("private_key", "required"); + $post->add_rules("public_key", "required"); + $post->add_callbacks("private_key", array($this, "validate_key")); + } + + public function validate_key(Validation $array, $field) { + // @todo verify key values + Kohana::log("debug", "Mollom::validate_key"); + Kohana::log("debug", print_r($array, 1)); + Kohana::log("debug", "field: $field"); + } + + public function set_api_data($post) { + module::set_var("spam_filter", "private_key", $post->private_key); + module::set_var("spam_filter", "public_key", $post->public_key); + } + private function _build_request($function, $host,$comment_data) { return ""; } diff --git a/modules/spam_filter/libraries/drivers/SpamFilter.php b/modules/spam_filter/libraries/drivers/SpamFilter.php index 3481aee2..6ece0dd4 100644 --- a/modules/spam_filter/libraries/drivers/SpamFilter.php +++ b/modules/spam_filter/libraries/drivers/SpamFilter.php @@ -18,8 +18,6 @@ * 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); @@ -28,6 +26,12 @@ abstract class SpamFilter_Driver { public abstract function get_statistics(); + public abstract function get_admin_fields($post); + + public abstract function get_validation_rules($post); + + public abstract function set_api_data($post); + protected function _http_post($host, $http_request, $port=80, $timeout=5) { $response = ""; if (false !== ($fs = @fsockopen($host, $port, $errno, $errstr, $timeout))) { diff --git a/modules/spam_filter/tests/Spam_Filter_Helper_Test.php b/modules/spam_filter/tests/Spam_Filter_Helper_Test.php index c52883d8..8abd8ed6 100644 --- a/modules/spam_filter/tests/Spam_Filter_Helper_Test.php +++ b/modules/spam_filter/tests/Spam_Filter_Helper_Test.php @@ -17,12 +17,12 @@ * 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 { +class SpamFilter_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") { + if ($matches[1] != "SpamFilter") { $expected[$matches[1]] = $matches[1] === $current_driver; } } diff --git a/modules/spam_filter/views/spam_filter_admin.html.php b/modules/spam_filter/views/spam_filter_admin.html.php new file mode 100644 index 00000000..db584f55 --- /dev/null +++ b/modules/spam_filter/views/spam_filter_admin.html.php @@ -0,0 +1,48 @@ +<?php defined("SYSPATH") or die("No direct script access.");?> +<script type="text/javascript"> + $("document").ready(function() { + ajaxify_spam_filter_form(); + $("#gContent #drivers").change(function() { + data = $("#gContent #drivers :selected").text(); + $("#gContent #filter_data").load("<?= url::site("admin/spam_filter/callback") ?>", + {driver: $("#gContent #drivers :selected").text(), + csrf: "<?= access::csrf_token() ?>"}); + }); + }); + 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(); + } + } + }); + }; +</script> +<form action="<?= url::site("admin/spam_filter/edit") ?>" method="post" class="form"> + <?= access::csrf_form_field() ?> + <fieldset> + <legend><?= _("Configure Spam Filter") ?></legend> + <ul> + <li> + <label for="drivers" >Available Drivers</label> + <select id="drivers" name="drivers" class="dropdown" > + <? foreach ($drivers as $index => $driver): ?> + <option value="<?= $index ?>"<? if (!empty($driver["selected"])): ?> selected="selected"<? endif?>><?= $driver["name"]?></option> + <? endforeach ?> + </select> + </li> + <div id="filter_data" > + <?= $filter_data ?> + </div> + <li> + <button type="submit" class="submit" ><?= _("Configure") ?></button> + </li> + </ul> + </fieldset> +</form> diff --git a/modules/spam_filter/views/spam_filter_admin_akismet.html.php b/modules/spam_filter/views/spam_filter_admin_akismet.html.php new file mode 100644 index 00000000..35c8ec7c --- /dev/null +++ b/modules/spam_filter/views/spam_filter_admin_akismet.html.php @@ -0,0 +1,12 @@ +<?php defined("SYSPATH") or die("No direct script access.");?> +<li <? if (!empty($errors["api_key"])): ?> class="gError" <? endif ?>> + <label for="api_key"><?= _("Api Key")?></label> + <input name="api_key" id="gApiKey" class="textbox" type="text" value="<?= $api_key ?>" /> + <? if (!empty($errors["api_key"]) && $errors["api_key"] == "required"): ?> + <p class="gError"><?= _("Api Key is required.") ?> + <? endif ?> + <? if (!empty($errors["api_key"]) && $errors["api_key"] == "invalid"): ?> + <p class="gError"><?= _("Api Key is invalid.") ?> + <? endif ?> +</li> + diff --git a/modules/spam_filter/views/spam_filter_admin_mollom.html.php b/modules/spam_filter/views/spam_filter_admin_mollom.html.php new file mode 100644 index 00000000..3008f0d0 --- /dev/null +++ b/modules/spam_filter/views/spam_filter_admin_mollom.html.php @@ -0,0 +1,19 @@ +<?php defined("SYSPATH") or die("No direct script access.");?> +<li <? if (!empty($errors["public_key"])): ?> class="gError" <? endif ?>> + <label for="public_key"><?= _("Public Key")?></label> + <input name="public_key" id="gPublicKey" class="textbox" type="text" value="<?= $public_key ?>" size="72" /> + <? if (!empty($errors["public_key"]) && $errors["public_key"] == "required"): ?> + <p class="gError"><?= _("Public Key is required.") ?> + <? endif ?> + <? if (!empty($errors["public_key"]) && $errors["public_key"] == "invalid"): ?> + <p class="gError"><?= _("Private Key / Public Key combination is invalid.") ?> + <? endif ?> +</li> +<li <? if (!empty($errors["private_key"])): ?> class="gError" <? endif ?>> + <label for="private_key"><?= _("Private Key")?></label> + <input name="private_key" id="gPrivateKey" class="textbox" type="text" value="<?= $private_key ?>" size="72" /> + <? if (!empty($errors["private_key"]) && $errors["private_key"] == "required"): ?> + <p class="gError"><?= _("Private Key is required.") ?> + <? endif ?> +</li> + |