Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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);
# Profanity
word1
word2
A profanity phrase
# Spam
spam1
spam2
A spam phrase
<?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';
@jreviews

This comment has been minimized.

Copy link
Owner Author

@jreviews jreviews commented Nov 15, 2019

Follow the instructions in the How to create your own profanity and spam blocker blog post to use these files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment