From 74d10754574278424268e765cc879031e5bf8809 Mon Sep 17 00:00:00 2001 From: Clay Dowling Date: Sat, 7 May 2016 10:33:32 -0400 Subject: [PATCH 1/3] Added DISCUSSION_SPAM_CHECK trigger Added the trigger and performed a small refactor to put all of the spam checking logic into a single method. A note was also added to the README concerning use of the trigger. --- .gitignore | 1 + README | 8 ++++++++ action.php | 44 ++++++++++++++++++++++++++++---------------- 3 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README b/README index eba4a1c..fb0e72f 100644 --- a/README +++ b/README @@ -8,3 +8,11 @@ All documentation for the Discussion Plugin is available online at: (c) 2008 - 2009 by Gina Häußge, Michael Klier (c) 2013 by Michael Hamann See COPYING for license info. + +The Discussion Plugin adds the following triggers: + +=== DISCUSSION_SPAM_CHECK === + +The data argument is the comment array. Developers implementing handlers for the DISCUSSION_SPAM_CHECK trigger +should be aware that the only field assured of being present is *raw* which contains the raw user input. Checks +against other fields should first check for the presence of those fields. diff --git a/action.php b/action.php index 345871e..5289520 100644 --- a/action.php +++ b/action.php @@ -489,14 +489,12 @@ protected function _flattenThreads($comments, $keys = null) { } /** - * Adds a new comment and then displays all comments + * Checks for blocking conditions which would prevent this comment from being saved. * * @param array $comment - * @param string $parent * @return bool */ - protected function _add($comment, $parent) { - global $ID; + protected function _not_spam($comment) { global $TEXT; $otxt = $TEXT; // set $TEXT to comment text for wordblock check @@ -508,13 +506,35 @@ protected function _add($comment, $parent) { return false; } + $TEXT = $otxt; // restore global $TEXT + + if (trigger_event('DISCUSSION_SPAM_CHECK', $comment)) { + msg($this->getLang('wordblock'), -1); + return false; // Comment identified as spam + } + + return true; + } + + /** + * Adds a new comment and then displays all comments + * + * @param array $comment + * @param string $parent + * @return bool + */ + protected function _add($comment, $parent) { + global $ID; + if ((!$this->getConf('allowguests')) - && ($comment['user']['id'] != $_SERVER['REMOTE_USER']) + && ($comment['user']['id'] != $_SERVER['REMOTE_USER']) ) { return false; // guest comments not allowed } - $TEXT = $otxt; // restore global $TEXT + if ($this->_not_spam($comment) === false) { + return false; + } // get discussion meta file name $file = metaFN($ID, '.comments'); @@ -615,18 +635,10 @@ public function save($cids, $raw, $act = NULL) { if(!$cids) return false; // do nothing if we get no comment id if ($raw) { - global $TEXT; - - $otxt = $TEXT; // set $TEXT to comment text for wordblock check - $TEXT = $raw; - - // spamcheck against the DokuWiki blacklist - if (checkwordblock()) { - msg($this->getLang('wordblock'), -1); + $comment['raw'] = $raw; + if ($this->_not_spam($comment) === false) { return false; } - - $TEXT = $otxt; // restore global $TEXT } // get discussion meta file name From 145458c1861ff0c1d43f2910fb92a18cd4f4c9eb Mon Sep 17 00:00:00 2001 From: Clay Dowling Date: Sat, 7 May 2016 12:09:39 -0400 Subject: [PATCH 2/3] Updated documentation --- README | 2 ++ plugin.info.txt | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README b/README index fb0e72f..37f03de 100644 --- a/README +++ b/README @@ -16,3 +16,5 @@ The Discussion Plugin adds the following triggers: The data argument is the comment array. Developers implementing handlers for the DISCUSSION_SPAM_CHECK trigger should be aware that the only field assured of being present is *raw* which contains the raw user input. Checks against other fields should first check for the presence of those fields. + +If the comment is spam the trigger handler should return true, which will prevent the message from being saved. diff --git a/plugin.info.txt b/plugin.info.txt index 15c7d7c..f07f971 100644 --- a/plugin.info.txt +++ b/plugin.info.txt @@ -1,5 +1,5 @@ base discussion -author Michael Hamann, Gerrit Uitslag, Gina Häussge, Christopher Smith, Michael Klier, Esther Brunner, Matthias Schulte +author Michael Hamann, Gerrit Uitslag, Gina Häussge, Christopher Smith, Michael Klier, Esther Brunner, Matthias Schulte, Clay Dowling email michael@content-space.de date 2016-04-23 name discussion plugin From 255de81c5bebb79ecc304c17b252963c43dffd49 Mon Sep 17 00:00:00 2001 From: Clay Dowling Date: Sat, 7 May 2016 14:54:21 -0400 Subject: [PATCH 3/3] Removed tedious double negative --- action.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/action.php b/action.php index 5289520..274cb23 100644 --- a/action.php +++ b/action.php @@ -494,7 +494,7 @@ protected function _flattenThreads($comments, $keys = null) { * @param array $comment * @return bool */ - protected function _not_spam($comment) { + protected function isSpam($comment) { global $TEXT; $otxt = $TEXT; // set $TEXT to comment text for wordblock check @@ -532,7 +532,7 @@ protected function _add($comment, $parent) { return false; // guest comments not allowed } - if ($this->_not_spam($comment) === false) { + if ($this->isSpam($comment)) { return false; } @@ -636,7 +636,7 @@ public function save($cids, $raw, $act = NULL) { if ($raw) { $comment['raw'] = $raw; - if ($this->_not_spam($comment) === false) { + if ($this->isSpam($comment)) { return false; } }