Skip to content

Instantly share code, notes, and snippets.

@mrsize
Last active May 12, 2021 07:59
Show Gist options
  • Save mrsize/9be4b9f013078f375b8bf22398fe612a to your computer and use it in GitHub Desktop.
Save mrsize/9be4b9f013078f375b8bf22398fe612a to your computer and use it in GitHub Desktop.
Antispam Wordpress CF7
<?php
/**
* Plugin Name: TD CF7 Antispam
* Plugin URI: https://www.mistersize.com/tag/spam/
* Description: Collection de solutions Antispam pour le plugin CF7 ( Modif du : 12 Mai. 2021 )
* Version: 1
* Author: Thomas Dufranne
* Author URI: http://mistersize.com
* License: GPL2
*/
/*
*
*
** Antispam formulaire de contact
*
* 1 - Functions :
** 1.1 - Function Banned langs
** 1.2 - Function Match Words/Caracters Detection
*
* 2 - Function de verification :
** 2.1 - Mots bloqués, Langues bloquées
** 2.2 - Honeypot
** 2.3 - Telephone
*
* 3 - Filters
*
*
*/
function tdcf7_list_forms() {
$result = '';
$posts = get_posts(array(
'post_type' => 'wpcf7_contact_form',
'numberposts' => -1
));
foreach ( $posts as $p ) {
$result .= $p->ID . ', ' . $p->post_title . '<br/>';
}
return $result;
}
add_shortcode('list_forms', 'tdcf7_list_forms');
/* 1. Functions */
/* 1.1 Banned Langs */
function tdcf7_if_is_russian($text) {
return preg_match('/[А-Яа-яЁё]/u', $text);
}
function tdcf7_if_is_chinese($text) {
return preg_match("/\p{Han}+/u", $text);
}
/* 1.2 Match Words/Caracters Detection */
function tdcf7_if_match_words($needles, $haystack)
{
foreach($needles as $needle){
if (stripos($haystack, $needle) !== false) {
return true;
}
}
return false;
}
function tdcf7_if_has_blocked_words($blocked_words, $input_string){
if ( tdcf7_if_match_words($blocked_words, $input_string) ){
return true;
} else {
return false;
}
}
/* 1.2 Start/Ends With */
function tdcf7_if_starts_with($string, $startString)
{
$length = strlen($startString);
return (substr($string, 0, $length) === $startString);
}
function tdcf7_if_ends_with($string, $endString)
{
$length = strlen($endString);
if ($length == 0) {
return true;
}
return (substr($string, -$length) === $endString);
}
/* 2. Verification */
/* 2.1 Verification : Mots, Langues */
function tdcf7_check_if_is_spam_textarea(){
// Champs concernés :
$message_field = $_POST['your-message'];
// Si mots bloqués :
$banned_words = array('$', 'creating', 'currently', 'thanks', 'yes', 'money', 'yankees', 'have', 'help', 'please', 'would', 'credit', 'where', 'bad', 'href', 'http', 'www.', 'craft', 'cotton', 'compressing','sеx', 'dаting', 'first', 'immеdiаtelу','how ', 'would ', 'like ', 'submit ', 'your ', 'advertisement ', 'thousands', 'advertising', 'every', 'month', 'pay', 'low', 'monthly', 'fee', 'virtually', 'unlimited', 'traffic', 'your', 'forever', 'visiting', 'there', 'found', 'quick', 'showed' ,'near', 'rankings', 'whatever', 'happens', 'anything', 'least', 'disappear', 'getting', 'thought', 'revolutionary', 'literally', 'between', 'everything', 'amazed', 'converting', 'unsubscribe', 'Cаsinо', 'Austrаlia', 'adept', 'group', 'university', 'writers', 'scientists', 'students', 'yield', 'visitor', 'impressive', 'upwards', 'unlock', 'highest', 'tinyurl', 'brokers', 'linked', 'Investment', 'Interest', 'expanding', 'http://', 'handwritten', 'huge', 'common', 'form', 'subject', 'study', 'graphology', 'thousand', 'carried', 'printing', 'Since', 'movements', 'using', 'erased', 'cleaned' );
if( tdcf7_if_has_blocked_words( $banned_words, $message_field ) ):
return true;
// Si message est vide :
elseif( isset($message_field) && empty($message_field) ):
return true;
// Si Langues interdites :
elseif(
tdcf7_if_is_russian($message_field)
||
tdcf7_if_is_chinese($message_field)
):
return true;
// Sinon :
else:
return false;
endif;
}
/*
** Verifier si les messages sont trop courts
*/
function tdcf7_has_too_short_message(){
$isspam = false;
// Champs concernés :
$all_message_fields_names = array('your-message');
foreach ($all_message_fields_names as $message_field_name) {
// $message_field = $_POST['your-message'];
$message_field = $_POST[$message_field_name];
$count_chars = strlen($message_field);
if( $count_chars > 60){
$isspam = false;
} else {
$isspam = true;
}
} // foreach
return $isspam;
}
/*
** Verifier si les mots sont trop longs
*/
function tdcf7_has_too_long_words(){
$isspam = false;
// Champs concernés :
$all_message_fields_names = array('your-message');
foreach ($all_message_fields_names as $message_field_name) {
// $message_field = $_POST['your-message'];
$message_field = $_POST[$message_field_name];
$words = explode(" ", $message_field);
foreach ($words as $word) {
$count_chars = strlen($word);
if( $count_chars < 30){
$isspam = false;
} else {
$isspam = true;
}
} // foreach
}
return $isspam;
}
/* 2.2 Verification : Honeypot */
function tdcf7_check_if_honeypot_filled(){
// Champs concernés :
$spam_field = $_POST['contact-url']; // Fake field
// Si Honeypot renseigné :
if( isset($spam_field) && !empty($spam_field) ):
return true;
// Sinon :
else:
return false;
endif;
}
/* 2.3 Verification : tel */
function tdcf7_check_if_is_spam_tel(){
// Champs concernés :
$tel_field = $_POST['your-tel'];
if( tdcf7_if_starts_with($tel_field, '00') ):
return true;
// non-french phone suspected :
elseif(strpos( $tel_field, '-') !== false):
return true;
// Sinon :
else:
return false;
endif;
}
function tdcf7_check_if_is_spam_email(){
// Champs concernés :
$email_field = $_POST['your-email'];
if( tdcf7_if_ends_with($email_field, '.co.uk') ):
return true;
elseif( tdcf7_if_ends_with($email_field, '.co') ):
return true;
elseif( tdcf7_if_ends_with($email_field, '.de') ):
return true;
elseif( tdcf7_if_ends_with($email_field, '.top') ):
return true;
elseif( tdcf7_if_ends_with($email_field, '.shop') ):
return true;
elseif( tdcf7_if_ends_with($email_field, '.news') ):
return true;
elseif( tdcf7_if_ends_with($email_field, '.online') ):
return true;
elseif( tdcf7_if_ends_with($email_field, '.ru') ):
return true;
// Sinon :
else:
return false;
endif;
}
/* 3. Filters */
add_filter( "wpcf7_validate_textarea", 'tdcf7_filter_wpcf7_validate_textarea', 10, 2 );
add_filter( "wpcf7_validate_textarea*", 'tdcf7_filter_wpcf7_validate_textarea', 10, 2 );
add_filter('wpcf7_validate_text', 'tdcf7_filter_wpcf7_validate_text', 20, 2);
add_filter('wpcf7_validate_text*', 'tdcf7_filter_wpcf7_validate_text', 20, 2);
add_filter('wpcf7_validate_tel', 'tdcf7_filter_wpcf7_validate_tel', 20, 2);
add_filter('wpcf7_validate_tel*', 'tdcf7_filter_wpcf7_validate_tel', 20, 2);
add_filter('wpcf7_validate_email', 'tdcf7_filter_wpcf7_validate_email', 20, 2);
add_filter('wpcf7_validate_email*', 'tdcf7_filter_wpcf7_validate_email', 20, 2);
function tdcf7_filter_wpcf7_validate_textarea( $result, $tag ) {
if( tdcf7_check_if_is_spam_textarea() ){
$result->invalidate( $tag, 'Oups, Votre message à été detecté comme indesirable :(' );
}
if( tdcf7_has_too_long_words() ){
$result->invalidate( $tag, 'Votre message à été identifié comme spam, merci de nous contacter par une autre méthode' );
}
if( tdcf7_has_too_short_message() ){
$result->invalidate( $tag, 'Votre message est trop court, merci de nous en dire un peu plus' );
}
return $result;
}
function tdcf7_filter_wpcf7_validate_text( $result, $tag ) {
if( tdcf7_check_if_honeypot_filled() ){
$result->invalidate( $tag, 'Bzzz, êtes-vous un robot ?' );
}
return $result;
}
function tdcf7_filter_wpcf7_validate_tel( $result, $tag ) {
if( tdcf7_check_if_is_spam_tel() ){
$result->invalidate( $tag, 'Dring! Mauvais format de téléphone' );
}
return $result;
}
function tdcf7_filter_wpcf7_validate_email( $result, $tag ) {
if( tdcf7_check_if_is_spam_email() ){
$result->invalidate( $tag, 'Bing! Il y a une erreur dans adresse e-mail' );
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment