From 389bff49f7181aef0d37a596f99fef3d4175eea3 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Mon, 13 Oct 2008 18:05:37 -0400 Subject: [PATCH] Setup our own Akismet module fork. Signed-off-by: Edward Z. Yang --- .../htmlpurifier-akismet/htmlpurifier-akismet.php | 187 +++++++++++++++++++++ misc/phorum/htmlpurifier-akismet/info.txt | 7 + misc/phorum/htmlpurifier-akismet/lang/dutch.php | 9 + .../htmlpurifier-akismet/lang/dutch_informal.php | 9 + misc/phorum/htmlpurifier-akismet/lang/english.php | 9 + misc/phorum/htmlpurifier-akismet/readme.txt | 36 ++++ misc/phorum/htmlpurifier-akismet/settings.php | 56 ++++++ symlink.sh | 9 + 8 files changed, 322 insertions(+) create mode 100644 misc/phorum/htmlpurifier-akismet/htmlpurifier-akismet.php create mode 100644 misc/phorum/htmlpurifier-akismet/info.txt create mode 100644 misc/phorum/htmlpurifier-akismet/lang/dutch.php create mode 100644 misc/phorum/htmlpurifier-akismet/lang/dutch_informal.php create mode 100644 misc/phorum/htmlpurifier-akismet/lang/english.php create mode 100644 misc/phorum/htmlpurifier-akismet/readme.txt create mode 100644 misc/phorum/htmlpurifier-akismet/settings.php create mode 100644 symlink.sh diff --git a/misc/phorum/htmlpurifier-akismet/htmlpurifier-akismet.php b/misc/phorum/htmlpurifier-akismet/htmlpurifier-akismet.php new file mode 100644 index 0000000..1af0500 --- /dev/null +++ b/misc/phorum/htmlpurifier-akismet/htmlpurifier-akismet.php @@ -0,0 +1,187 @@ + $PHORUM["user"]["post_count"])))){ + + $return = phorum_akismet_http_request( $message, "comment-check" ); + + if($return=="true"){ + echo 'Akismet thinks your message is spam. You posted:

';
+            echo htmlspecialchars($message['body']);
+            echo '
'; + exit; + } + } + + return $message; +} + +/** + * Moderation hook for giving feedback to Akismet service + */ +function phorum_akismet_moderation($mod_step){ + switch($mod_step){ + case PHORUM_SPAM_DELETE_MESSAGE: + // replace with next mod_step to be executed + $mod_step = PHORUM_DELETE_MESSAGE; + $message = phorum_db_get_message($GLOBALS["msgthd_id"]); + $return = phorum_akismet_http_request( $message, "submit-spam" ); + break; + case PHORUM_SPAM_DELETE_TREE: + // replace with next mod_step to be executed + $mod_step = PHORUM_DELETE_TREE; + $message = phorum_db_get_message($GLOBALS["msgthd_id"]); + $return = phorum_akismet_http_request( $message, "submit-spam" ); + break; + case PHORUM_APPROVE_MESSAGE: + case PHORUM_APPROVE_MESSAGE_TREE: + $message = phorum_db_get_message($GLOBALS["msgthd_id"]); + $return = phorum_akismet_http_request( $message, "submit-ham" ); + break; + } + return $mod_step; +} + +/** + * Read hook for setting up URLs to submit appropriate spam for messages. + */ +function phorum_akismet_read_url_change($messages) { + global $PHORUM; + if($PHORUM["DATA"]["MODERATOR"]){ + foreach($messages as $key=>$message){ + $message["spam_url1"] = phorum_get_url(PHORUM_MODERATION_URL, PHORUM_SPAM_DELETE_MESSAGE, $message["message_id"]); + $message["spam_url2"] = phorum_get_url(PHORUM_MODERATION_URL, PHORUM_SPAM_DELETE_TREE, $message["message_id"]); + $messages[$key] = $message; + } + } + return $messages; +} + +function phorum_akismet_verify_key($key){ + return phorum_akismet_http_request( $key, "verify-key" ); +} + +/** + * Generic HTTP request for Akismet server. + */ +function phorum_akismet_http_request( $message, $mode ) { + + global $PHORUM; + $buf = ''; + + if($mode == "verify-key"){ + $host = "rest.akismet.com"; + + $data = "key=".urlencode($message)."&blog=".urlencode($PHORUM["http_path"]); + + } else { + + $host = $PHORUM["akismet"]["key"].".rest.akismet.com"; + + $data = "blog=".urlencode($PHORUM["http_path"]); + $data.= "&user_ip=".urlencode($_SERVER["REMOTE_ADDR"]); + $data.= "&user_agent=".urlencode($_SERVER["HTTP_USER_AGENT"]); + if (isset($_SERVER["HTTP_REFERER"])) { + $data.= "&referrer=".urlencode($_SERVER["HTTP_REFERER"]); + } else { + // optional, but we'll include it anyway to signal it was empty + $data.= "&referrer="; + } + if($message["thread"]){ + $data.= "&permalink=".urlencode(phorum_get_url(PHORUM_FOREIGN_READ_URL, $message["forum_id"], $message["thread"])); + } else { + $data.= "&permalink=".urlencode(phorum_get_url(PHORUM_LIST_URL, $message["forum_id"])); + } + $data.= "&comment_type=".urlencode("forum"); + $data.= "&comment_author=".urlencode($message["author"]); + $data.= "&comment_author_email=".urlencode($message["email"]); + $data.= "&comment_content=".urlencode($message["body"]); + } + + $fp = @fsockopen( $host, 80, $errno, $errstr, 8 ); // timeout set to 8 seconds + + if (FALSE !== $fp) { + stream_set_timeout ( $fp, 10 ); // timeout changed to 10 seconds (why?) + + // Although the Akismet server may be tolerant, the request as constructed + // here did not conform to either RFC 1945 (HTTP 1.0) or RFC 2616 (HTTP 2616). + // I've adapted the code to conform to (both) HTTP standards. MK 2007-03-17 + // + // 1) RFC 1945: "HTTP/1.0 defines the octet sequence CR LF as the end-of-line + // marker for all protocol elements except the Entity-Body" - and: + // HTTP-header = field-name ":" [ field-value ] CRLF + // RFC 2616: "HTTP/1.1 defines the sequence CR LF as the end-of-line marker + // for all protocol elements except the entity-body" - and: + // "generic-message = start-line + // *(message-header CRLF) + // CRLF + // [ message-body ] + // start-line = Request-Line | Status-Line" + // MK: every \n in this section changed to \r\n + // cf. http://www.isi.edu/in-notes/rfc1945.txt + // cf. ftp://ftp.isi.edu/in-notes/rfc2616.txt + // cf. http://akismet.com/development/api/ + // + // 2) RFC 1945: (HTTP/1.0) "When no explicit charset parameter is provided by + // the sender, media subtypes of the "text" type are defined to have + // a default charset value of "ISO-8859-1" when received via HTTP." + // RFC 2616: "HTTP/1.1 recipients MUST respect the charset label provided + // by the sender (...) When no explicit charset parameter is + // provided by the sender, media subtypes of the "text" type are + // defined to have a default charset value of "ISO-8859-1" when + // received via HTTP. Data in character sets other than "ISO-8859-1" + // or its subsets MUST be labeled with an appropriate charset value." + // MK: this means that if Phorum is configured to use another charset than + // ISO-8859-1, the charset MUST be specified in the Content-type + // header! (And it's not wrong to specify ISO-8859-1.) + // cf. http://www.isi.edu/in-notes/rfc1945.txt + // cf. ftp://ftp.isi.edu/in-notes/rfc2616.txt + // cf. http://akismet.com/development/api/ + fputs( $fp, "POST /1.1/$mode HTTP/1.0\r\n" ); + fputs( $fp, "Host: $host\r\n" ); + fputs( $fp, "Content-type: application/x-www-form-urlencoded; charset=" . $PHORUM['DATA']['CHARSET'] . "\r\n" ); // charset added + fputs( $fp, "Content-length: " . strlen( $data ) . "\r\n" ); + fputs( $fp, "User-Agent: Phorum ".PHORUM."\r\n" ); + fputs( $fp, "Connection: close\r\n\r\n" ); + fputs( $fp, $data ); + + $x=1; + while ( !feof( $fp ) ) { + $buf .= fgets( $fp, 1024 ); + + // if the fgets returns nothing on the first return, + // the remote server is timing out. + if($x==1 && $buf==""){ + $errstr="timeout waiting for data"; + break; + } + $x++; + } + + fclose( $fp ); + + } + + if (FALSE === $fp || empty($buf)){ + trigger_error("Could not open $host: $errstr", E_USER_WARNING); + } + + $buf = str_replace( "\r", "", $buf ); + list( $page_data["headers"], $page_data["content"] ) = explode( "\n\n", $buf, 2 ); + + return $page_data["content"]; +} diff --git a/misc/phorum/htmlpurifier-akismet/info.txt b/misc/phorum/htmlpurifier-akismet/info.txt new file mode 100644 index 0000000..d439be6 --- /dev/null +++ b/misc/phorum/htmlpurifier-akismet/info.txt @@ -0,0 +1,7 @@ +hook: before_post|phorum_akismet_check_spam +hook: moderation|phorum_akismet_moderation +hook: read|phorum_akismet_read_url_change +hook: lang| +title: Akismet Spam Checker for HTML Purifier +desc: Akismet is a free spam checker; this modded version for HTML Purifier does not submit spammy posts to a queue, but immediately errors to the user. +version: dev diff --git a/misc/phorum/htmlpurifier-akismet/lang/dutch.php b/misc/phorum/htmlpurifier-akismet/lang/dutch.php new file mode 100644 index 0000000..8bfa834 --- /dev/null +++ b/misc/phorum/htmlpurifier-akismet/lang/dutch.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/misc/phorum/htmlpurifier-akismet/lang/dutch_informal.php b/misc/phorum/htmlpurifier-akismet/lang/dutch_informal.php new file mode 100644 index 0000000..13a1dca --- /dev/null +++ b/misc/phorum/htmlpurifier-akismet/lang/dutch_informal.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/misc/phorum/htmlpurifier-akismet/lang/english.php b/misc/phorum/htmlpurifier-akismet/lang/english.php new file mode 100644 index 0000000..9f46b3d --- /dev/null +++ b/misc/phorum/htmlpurifier-akismet/lang/english.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/misc/phorum/htmlpurifier-akismet/readme.txt b/misc/phorum/htmlpurifier-akismet/readme.txt new file mode 100644 index 0000000..70bd695 --- /dev/null +++ b/misc/phorum/htmlpurifier-akismet/readme.txt @@ -0,0 +1,36 @@ +Akismet Module +=============== + +To block spam +-------------- +1. Go to Akismet.com and get a key. +2. Unpack the akismet module in your mods dir. +3. Go to your Phorum admin and turn the module on. +4. Edit the Settins for the module and enter you key. + + +To report spam +--------------- +This is a little trickier. You have to edit your template to insert the Spam +links on the read page. + +In read.tpl, you will find something like: + +{LANG->DeleteThread} + +{LANG->DeleteMessage} + +{LANG->DelMessReplies} + +You will want to add another link just before each of those: + +{LANG->Spam} + +{LANG->Spam} + +{LANG->Spam} + +These links will delete the spam and submit it to Akismet. + +For read_threads.tpl, look for the same URLs. + diff --git a/misc/phorum/htmlpurifier-akismet/settings.php b/misc/phorum/htmlpurifier-akismet/settings.php new file mode 100644 index 0000000..768f1b5 --- /dev/null +++ b/misc/phorum/htmlpurifier-akismet/settings.php @@ -0,0 +1,56 @@ + $_POST["key"], + "check_reg" => $_POST["check_reg"], + "min_posts" => $_POST["min_posts"] + ); + + if(!phorum_db_update_settings(array("akismet"=>$akismet))){ + $error="Database error while updating settings."; + } else { + $PHORUM["akismet"] = $akismet; + phorum_admin_okmsg("Akismet Settings Saved"); + } + + } + + } + +} + +if ( $error ) { + phorum_admin_error( $error ); +} + +include_once "./include/admin/PhorumInputForm.php"; + +$frm =& new PhorumInputForm ("", "post", "Save"); + +$frm->hidden("module", "modsettings"); +$frm->hidden("mod", "htmlpurifier-akismet"); +$frm->addrow("Akismet Key
See: Akismet.com to get a key", $frm->text_box("key", $PHORUM["akismet"]["key"], 50)); +$frm->addrow( "Check Registered Users", $frm->select_tag( "check_reg", array( "No", "Yes" ), $PHORUM["akismet"]["check_reg"] ) ); +$frm->addrow( "Skip user with this many posts or more:", $frm->text_box("min_posts", $PHORUM["akismet"]["min_posts"], 20) ); + +$frm->show(); diff --git a/symlink.sh b/symlink.sh new file mode 100644 index 0000000..cc25472 --- /dev/null +++ b/symlink.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# To minimize code duplication, there are numerous symlinks setup in the root. +# In order to ensure continued functionality, these must be recreated if we +# perform a fresh install of htmlpurifier-web + +ln -s "dev/plugins/phorum" "phorums/mods/htmlpurifier" +ln -s "misc/phorum/htmlpurifier-custom" "phorums/mods/htmlpurifier-custom" +ln -s "misc/phorum/htmlpurifier-akismet" "phorums/mods/htmlpurifier-akismet" -- 2.11.4.GIT