Created
November 15, 2019 15:31
-
-
Save jreviews/fb9e31f811023b6f2c7fc839b62e55f4 to your computer and use it in GitHub Desktop.
Create your own profanity and spam blocker https://www.jreviews.com/blog/create-your-own-profanity-and-spam-blocker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('MVC_FRAMEWORK') or die; | |
function antispam_filter($validation, $params) | |
{ | |
$files = glob(__DIR__.DS.'spamlists/*.conf'); | |
$list = ''; | |
foreach ($files as $file) | |
{ | |
$list .= file_get_contents($file)."\n"; | |
} | |
$list = preg_split("/((\r?\n)|(\r\n?))/", $list, NULL, PREG_SPLIT_NO_EMPTY); | |
$text = strip_tags(implode(' ',S2Array::flatten($params['data']))); | |
$badWords = []; | |
$context = []; | |
foreach ($list as $regex) | |
{ | |
$regex = preg_replace('/(^\s+|\s+$|\s*#.*$)/i', "", $regex); | |
if (empty($regex)) continue; | |
$match = preg_match('/(?:[^ ]+|(?:[^ ]+ )){0,1}('.$regex.')(?: [^ ]+){0,1}/i',$text,$matches); | |
// Blocked word found | |
if ($match) | |
{ | |
$badWords[$matches[1]] = $matches[1]; | |
$context[$matches[0]] = $matches[0]; | |
} | |
} | |
if ( !empty($badWords) ) | |
{ | |
// $validation[] = 'Your message doesn\'t comply with our submission guidelines'; | |
$validation[] = 'Your message doesn\'t comply with our submission guidelines. The following words are not allowed and need to be removed before your submission is accepted: <strong>'.implode(', ',$badWords).'</strong>'; | |
antispam_filter_log($context); | |
} | |
return $validation; | |
} | |
function antispam_filter_log($words) | |
{ | |
$datetime = '['.date('D Y-m-d h:i:s A').'] [IP '.$_SERVER['REMOTE_ADDR'].'] '; | |
$entry = $datetime; | |
foreach ( $words as $word ) | |
{ | |
$entry .= $word."\r\n"; | |
} | |
$filename = 'spamlog_'.date('Ymd').'.txt'; | |
$filepath = __DIR__; // Current directory | |
$handle = fopen($filepath.DS.$filename,'a+'); | |
fwrite($handle,$entry); | |
fclose($handle); | |
} | |
Clickfwd\Hook\Filter::add('listing_submit_validation', 'antispam_filter', 10); | |
Clickfwd\Hook\Filter::add('inquiry_submit_validation', 'antispam_filter', 10); | |
Clickfwd\Hook\Filter::add('review_submit_validation', 'antispam_filter', 10); | |
Clickfwd\Hook\Filter::add('discussion_submit_validation', 'antispam_filter', 10); | |
Clickfwd\Hook\Filter::add('owner_reply_submit_validation', 'antispam_filter', 10); | |
Clickfwd\Hook\Filter::add('report_submit_validation', 'antispam_filter', 10); | |
Clickfwd\Hook\Filter::add('claim_submit_validation', 'antispam_filter', 10); | |
Clickfwd\Hook\Filter::add('resources_submit_validation', 'antispam_filter', 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Profanity | |
word1 | |
word2 | |
A profanity phrase | |
# Spam | |
spam1 | |
spam2 | |
A spam phrase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('MVC_FRAMEWORK') or die; | |
/** | |
* Using require as shown below instead of pasting the filter code in this file allows for better origanization | |
* of your developer filters | |
*/ | |
require_once 'antispam_filter.php'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Follow the instructions in the How to create your own profanity and spam blocker blog post to use these files.