Last active
June 19, 2024 11:55
-
-
Save patrickatwsrn/374c6219acddec1c90fb4be128a02795 to your computer and use it in GitHub Desktop.
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 | |
/* | |
snippet: spamFilter | |
purpose: Check message of email for chinese/russian characters and badwords | |
usage: add snippet to validate rules and customValidator property | |
&validate=`message:email:spamFilter:required` | |
&customValidators=`spamFilter` | |
*/ | |
$contact = $modx->getOption('emailsender'); | |
$contact = '[email protected]'; | |
/* | |
&validate=`message:emailDetectSpam:required` | |
*/ | |
if (!function_exists('filterCyrillic')) { | |
// Test: Выйграйтe дeньги в казино | |
function filterCyrillic($value){ | |
// 1 if found | |
// 0 if not found | |
// false if an error occurs | |
return preg_match( '/[\p{Cyrillic}]/u', $value); | |
} | |
} | |
if (!function_exists('filterHan')) { | |
// Test: 期期发计划给你 | |
function filterHan($value){ | |
return preg_match("/\p{Han}+/u", $value); | |
} | |
} | |
if (!function_exists('filterURL')) { | |
function filterURL($value){ | |
// test: https://google.com | |
return preg_match('/http/',$value); | |
} | |
} | |
if (!function_exists('filterBlacklist')) { | |
// test: crypto, currency, USD, girls | |
function filterBlacklist($value){ | |
$blacklist = array( | |
'bitcoin' | |
,'sex' | |
,'USD' | |
); | |
$success = 0; | |
foreach($blacklist as $badword){ | |
if(preg_match('/'.$badword.'/mi',$value) == 1){ | |
$success = 1; | |
break; | |
} | |
} | |
return $success; | |
} | |
} | |
if(!function_exists('filterEmoji')){ | |
function filterEmoji ($value){ | |
// Kein Test. Emojis wwerden in MODX beim speichern gelöscht ;) | |
// hat mit einem Textschnipsel von Wikipedia nicht funktioniert... | |
// https://de.wikipedia.org/wiki/Emoji | |
return preg_match("/[\u{1f300}-\u{1f5ff}\u{e000}-\u{f8ff}]/u", $value); | |
return preg_match('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/' , $value); | |
} | |
} | |
$success = true; | |
$filters = array( | |
'filterCyrillic' => 1 | |
,'filterHan' => 1 | |
,'filterEmoji' => 1 | |
,'filterURL' => 1 | |
,'filterBlacklist' => 1 | |
); | |
if($filters['filterCyrillic'] == 1 AND filterCyrillic($value) == 1){ | |
$error = 'A1'; | |
$success = false; | |
} | |
elseif($filters['filterHan'] == 1 AND filterHan($value) == 1){ | |
$error = 'A2'; | |
$success = false; | |
} | |
elseif($filters['filterEmoji'] == 1 AND filterEmoji($value == 1)){ | |
$error = 'A3'; | |
$success = false; | |
} | |
elseif($filters['filterURL'] == 1 AND filterURL($value) == 1){ | |
$error = 'A4'; | |
$success = false; | |
} | |
elseif($filters['filterBlacklist'] == 1 AND filterBlacklist($value) == 1){ | |
$error = 'A5'; | |
$success = false; | |
} | |
if($success == false){ | |
$msg = 'Das Kontaktformular kann Ihre Nachricht leider nicht versenden (Fehler: '.$error.'). Senden Sie ihre Nachricht bitte per E-Mail an ' . $contact ; | |
$validator->addError($key, $msg ); | |
} | |
return $success; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment