diff options
| author | Tristan Zur <tzur@webserver.ccwn.org> | 2015-06-10 20:55:53 +0200 |
|---|---|---|
| committer | Tristan Zur <tzur@webserver.ccwn.org> | 2015-06-10 20:55:53 +0200 |
| commit | 406abd7c4df1ace2cd3e4e17159e8941a2e8c0c4 (patch) | |
| tree | a324be16021f44f2fd6d55e609f47024e945b1db /modules/akismet | |
Initial import
Diffstat (limited to 'modules/akismet')
| -rw-r--r-- | modules/akismet/controllers/admin_akismet.php | 67 | ||||
| -rw-r--r-- | modules/akismet/helpers/akismet.php | 193 | ||||
| -rw-r--r-- | modules/akismet/helpers/akismet_event.php | 70 | ||||
| -rw-r--r-- | modules/akismet/helpers/akismet_installer.php | 28 | ||||
| -rw-r--r-- | modules/akismet/module.info | 7 | ||||
| -rw-r--r-- | modules/akismet/views/admin_akismet.html.php | 18 | ||||
| -rw-r--r-- | modules/akismet/views/admin_akismet_stats.html.php | 11 |
7 files changed, 394 insertions, 0 deletions
diff --git a/modules/akismet/controllers/admin_akismet.php b/modules/akismet/controllers/admin_akismet.php new file mode 100644 index 0000000..c907966 --- /dev/null +++ b/modules/akismet/controllers/admin_akismet.php @@ -0,0 +1,67 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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_Akismet_Controller extends Admin_Controller { + public function index() { + $form = akismet::get_configure_form(); + + if (request::method() == "post") { + // @todo move the "post" handler part of this code into a separate function + access::verify_csrf(); + + if ($form->validate()) { + $new_key = $form->configure_akismet->api_key->value; + $old_key = module::get_var("akismet", "api_key"); + if ($old_key && !$new_key) { + message::success(t("Your Akismet key has been cleared.")); + } else if ($old_key && $new_key && $old_key != $new_key) { + message::success(t("Your Akismet key has been changed.")); + } else if (!$old_key && $new_key) { + message::success(t("Your Akismet key has been saved.")); + } + + log::success("akismet", t("Akismet key changed to %new_key", + array("new_key" => $new_key))); + module::set_var("akismet", "api_key", $new_key); + akismet::check_config(); + url::redirect("admin/akismet"); + } else { + $valid_key = false; + } + } else { + $valid_key = module::get_var("akismet", "api_key") ? 1 : 0; + } + + akismet::check_config(); + $view = new Admin_View("admin.html"); + $view->page_title = t("Akismet spam filtering"); + $view->content = new View("admin_akismet.html"); + $view->content->valid_key = $valid_key; + $view->content->form = $form; + print $view; + } + + public function stats() { + $view = new Admin_View("admin.html"); + $view->content = new View("admin_akismet_stats.html"); + $view->content->api_key = module::get_var("akismet", "api_key"); + $view->content->blog_url = url::base(false, "http"); + print $view; + } +}
\ No newline at end of file diff --git a/modules/akismet/helpers/akismet.php b/modules/akismet/helpers/akismet.php new file mode 100644 index 0000000..a7927d6 --- /dev/null +++ b/modules/akismet/helpers/akismet.php @@ -0,0 +1,193 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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_Core { + public static $test_mode = TEST_MODE; + + static function get_configure_form() { + $form = new Forge("admin/akismet", "", "post", array("id" => "g-configure-akismet-form")); + $group = $form->group("configure_akismet")->label(t("Configure Akismet")); + $group->input("api_key")->label(t("API Key"))->value(module::get_var("akismet", "api_key")) + ->callback("akismet::validate_key") + ->error_messages("invalid", t("The API key you provided is invalid.")); + $group->submit("")->value(t("Save")); + return $form; + } + + /** + * Check a comment against Akismet and return "spam", "ham" or "unknown". + * @param Comment_Model $comment A comment to check + * @return $string "spam", "ham" or "unknown" + */ + static function check_comment($comment) { + if (akismet::$test_mode) { + return; + } + + $request = self::_build_request("comment-check", $comment); + $response = self::_http_post($request); + $answer = $response->body[0]; + if ($answer == "true") { + return "spam"; + } else if ($answer == "false") { + return "ham"; + } else { + return "unknown"; + } + } + + /** + * Tell Akismet that this comment is spam + * @param Comment_Model $comment A comment to check + */ + static function submit_spam($comment) { + if (akismet::$test_mode) { + return; + } + + $request = self::_build_request("submit-spam", $comment); + self::_http_post($request); + } + + /** + * Tell Akismet that this comment is ham + * @param Comment_Model $comment A comment to check + */ + static function submit_ham($comment) { + if (akismet::$test_mode) { + return; + } + + $request = self::_build_request("submit-ham", $comment); + self::_http_post($request); + } + + /** + * Check an API Key against Akismet to make sure that it's valid + * @param string $api_key the API key + * @return boolean + */ + static function validate_key($api_key_input) { + if ($api_key_input->value) { + $request = self::_build_verify_request($api_key_input->value); + $response = self::_http_post($request, "rest.akismet.com"); + if ("valid" != $response->body[0]) { + $api_key_input->add_error("invalid", 1); + } + } + } + + + static function check_config() { + $api_key = module::get_var("akismet", "api_key"); + if (empty($api_key)) { + site_status::warning( + t("Akismet is not quite ready! Please provide an <a href=\"%url\">API Key</a>", + array("url" => html::mark_clean(url::site("admin/akismet")))), + "akismet_config"); + } else { + site_status::clear("akismet_config"); + } + } + + + 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; + } + + static function _build_request($function, $comment) { + $comment_data = array(); + $comment_data["HTTP_ACCEPT"] = $comment->server_http_accept; + $comment_data["HTTP_ACCEPT_ENCODING"] = $comment->server_http_accept_encoding; + $comment_data["HTTP_ACCEPT_LANGUAGE"] = $comment->server_http_accept_language; + $comment_data["HTTP_CONNECTION"] = $comment->server_http_connection; + $comment_data["HTTP_HOST"] = $comment->server_http_host; + $comment_data["HTTP_USER_AGENT"] = $comment->server_http_user_agent; + $comment_data["QUERY_STRING"] = $comment->server_query_string; + $comment_data["REMOTE_ADDR"] = $comment->server_remote_addr; + $comment_data["REMOTE_HOST"] = $comment->server_remote_host; + $comment_data["REMOTE_PORT"] = $comment->server_remote_port; + $comment_data["SERVER_HTTP_ACCEPT_CHARSET"] = $comment->server_http_accept_charset; + $comment_data["blog"] = url::base(false, "http"); + $comment_data["comment_author"] = $comment->author_name(); + $comment_data["comment_author_email"] = $comment->author_email(); + $comment_data["comment_author_url"] = $comment->author_url(); + $comment_data["comment_content"] = $comment->text; + $comment_data["comment_type"] = "comment"; + $comment_data["permalink"] = url::site("comments/{$comment->id}"); + $comment_data["referrer"] = $comment->server_http_referer; + $comment_data["user_agent"] = $comment->server_http_user_agent; + $comment_data["user_ip"] = $comment->server_remote_addr; + + $query_string = array(); + foreach ($comment_data as $key => $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::add("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::add("debug", "Received response\n" . print_r($response, 1)); + + return $response; + } +} diff --git a/modules/akismet/helpers/akismet_event.php b/modules/akismet/helpers/akismet_event.php new file mode 100644 index 0000000..038e487 --- /dev/null +++ b/modules/akismet/helpers/akismet_event.php @@ -0,0 +1,70 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2013 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_event_Core { + static function comment_created($comment) { + if (!module::get_var("akismet", "api_key")) { + return; + } + + switch(akismet::check_comment($comment)) { + case "spam": + $comment->state = "spam"; + module::incr_var("comment", "spam_caught"); + break; + + case "ham": + $comment->state = "published"; + break; + + case "unknown": + $comment->state = "unpublished"; + break; + } + $comment->save(); + } + + static function comment_updated($original, $new) { + if (!module::get_var("akismet", "api_key")) { + return; + } + + if ($original->state != "spam" && $new->state == "spam") { + akismet::submit_spam($new); + } else if ($original->state == "spam" && $new->state != "spam") { + akismet::submit_ham($new); + } + } + + static function admin_menu($menu, $theme) { + $menu->get("settings_menu") + ->append(Menu::factory("link") + ->id("akismet") + ->label(t("Akismet")) + ->url(url::site("admin/akismet"))); + + if (module::get_var("akismet", "api_key")) { + $menu->get("statistics_menu") + ->append(Menu::factory("link") + ->id("akismet") + ->label(t("Akismet")) + ->url(url::site("admin/akismet/stats"))); + } + } +} diff --git a/modules/akismet/helpers/akismet_installer.php b/modules/akismet/helpers/akismet_installer.php new file mode 100644 index 0000000..bd16a15 --- /dev/null +++ b/modules/akismet/helpers/akismet_installer.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-2013 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_installer { + static function activate() { + akismet::check_config(); + } + + static function deactivate() { + site_status::clear("akismet_config"); + } +} diff --git a/modules/akismet/module.info b/modules/akismet/module.info new file mode 100644 index 0000000..263b7b8 --- /dev/null +++ b/modules/akismet/module.info @@ -0,0 +1,7 @@ +name = "Akismet" +description = "Filter comments through the Akismet web service to detect and eliminate spam (http://akismet.com). You'll need a WordPress.com API key to use it." +version = 1 +author_name = "Gallery Team" +author_url = "http://codex.galleryproject.org/Gallery:Team" +info_url = "http://codex.galleryproject.org/Gallery3:Modules:akismet" +discuss_url = "http://galleryproject.org/forum_module_akismet" diff --git a/modules/akismet/views/admin_akismet.html.php b/modules/akismet/views/admin_akismet.html.php new file mode 100644 index 0000000..399053d --- /dev/null +++ b/modules/akismet/views/admin_akismet.html.php @@ -0,0 +1,18 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<div id="g-admin-akismet" class="g-block"> + <h1> <?= t("Akismet spam filtering") ?> </h1> + <p> + <?= t("Akismet is a free, automated spam filtering service. In order to use it, you need to sign up for a <a href=\"%api_key_url\">Wordpress.com API Key</a>, which is also free. Your comments will be automatically relayed to <a href=\"%akismet_url\">Akismet.com</a> where they'll be scanned for spam. Spam messages will be flagged accordingly and hidden from your vistors until you approve or delete them.", + array("api_key_url" => "http://wordpress.com/api-keys", + "akismet_url" => "http://akismet.com")) ?> + </p> + <div class="g-block-content"> + <? if ($valid_key): ?> + <div class="g-module-status g-success"> + <?= t("Your API key is valid. Your comments will be filtered!") ?> + </div> + <? endif ?> + + <?= $form ?> + </div> +</div> diff --git a/modules/akismet/views/admin_akismet_stats.html.php b/modules/akismet/views/admin_akismet_stats.html.php new file mode 100644 index 0000000..32908ba --- /dev/null +++ b/modules/akismet/views/admin_akismet_stats.html.php @@ -0,0 +1,11 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<script type="text/javscript"> + $(document).ready(function() { + $("#g-akismet-external-stats").css("height", "600"); + }); +</script> +<div id="g-akismet-stats"> + <iframe id="g-akismet-external-stats" width="100%" height="500" frameborder="0" + src="http://<?= $api_key ?>.web.akismet.com/1.0/user-stats.php?blog=<?= urlencode($blog_url) ?>"> + </iframe> +</div> |
