Dodaj funkcje czyszczenia starych logów oraz logowania wykrytych prób rejestracji botów; dodaj kolumnę register_ip do tabeli globelus_users dla ograniczenia rejestracji z jednego IP.
This commit is contained in:
@@ -2,6 +2,31 @@
|
||||
namespace front\factory;
|
||||
class GlobelusUser
|
||||
{
|
||||
/**
|
||||
* Logowanie wykrytych prób rejestracji botów
|
||||
* @param string $detection_type Typ wykrycia (HONEYPOT, TIME_BASED, RATE_LIMIT)
|
||||
* @param array $data Dane do zalogowania
|
||||
*/
|
||||
private static function log_bot_detection( $detection_type, $data )
|
||||
{
|
||||
$log_file = 'logs/bot-detection-' . date( 'Y-m-d' ) . '.txt';
|
||||
|
||||
// Odczytaj istniejącą zawartość lub utwórz pusty string
|
||||
$file_content = file_exists( $log_file ) ? file_get_contents( $log_file ) : '';
|
||||
|
||||
$file_content .= PHP_EOL . '-------------------------------------------------------------------------------------------------------------------';
|
||||
$file_content .= PHP_EOL . 'DATE: ' . date( 'Y-m-d H:i:s' );
|
||||
$file_content .= PHP_EOL . 'DETECTION_TYPE: ' . $detection_type;
|
||||
$file_content .= PHP_EOL . 'DATA:';
|
||||
|
||||
foreach ( $data as $key => $val )
|
||||
$file_content .= PHP_EOL . "\t" . $key . ": " . $val;
|
||||
|
||||
$file_content .= PHP_EOL . '-------------------------------------------------------------------------------------------------------------------';
|
||||
|
||||
file_put_contents( $log_file, $file_content );
|
||||
}
|
||||
|
||||
static public function email_user_exists( $email )
|
||||
{
|
||||
global $mdb;
|
||||
@@ -393,6 +418,64 @@ class GlobelusUser
|
||||
|
||||
$result = [ 'status' => 'bad', 'msg' => $lang['rejestracja-blad-ogolny'] ];
|
||||
|
||||
// ============ ZABEZPIECZENIA ANTYSPAMOWE ============
|
||||
|
||||
// 1. HONEYPOT - sprawdzenie ukrytego pola
|
||||
$honeypot = \S::get( 'website' );
|
||||
if ( !empty( $honeypot ) )
|
||||
{
|
||||
// Bot wypełnił ukryte pole - odrzuć rejestrację
|
||||
self::log_bot_detection( 'HONEYPOT', [
|
||||
'ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'email' => $email,
|
||||
'reason' => 'Honeypot field filled'
|
||||
]);
|
||||
return [ 'status' => 'bad', 'msg' => 'Wystąpił błąd podczas rejestracji. Spróbuj ponownie.' ];
|
||||
}
|
||||
|
||||
// 2. TIME-BASED VALIDATION - sprawdzenie minimalnego czasu wypełniania
|
||||
$form_timestamp = \S::get( 'form_timestamp' );
|
||||
if ( $form_timestamp )
|
||||
{
|
||||
$time_elapsed = time() - intval( $form_timestamp );
|
||||
if ( $time_elapsed < 3 ) // Minimalny czas: 3 sekundy
|
||||
{
|
||||
self::log_bot_detection( 'TIME_BASED', [
|
||||
'ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'email' => $email,
|
||||
'time_elapsed' => $time_elapsed . 's',
|
||||
'reason' => 'Form filled too fast'
|
||||
]);
|
||||
return [ 'status' => 'bad', 'msg' => 'Formularz został wypełniony zbyt szybko. Proszę wypełnić ponownie.' ];
|
||||
}
|
||||
}
|
||||
|
||||
// 3. RATE LIMITING - ograniczenie liczby rejestracji z jednego IP
|
||||
$user_ip = $_SERVER['REMOTE_ADDR'];
|
||||
$time_window = 3600; // 1 godzina w sekundach
|
||||
$max_registrations = 3; // Maksymalnie 3 rejestracje na godzinę z jednego IP
|
||||
|
||||
// Sprawdzenie liczby rejestracji z tego IP w ostatniej godzinie
|
||||
$recent_registrations = $mdb -> count( 'globelus_users', [
|
||||
'AND' => [
|
||||
'register_ip' => $user_ip,
|
||||
'register_date[>=]' => date( 'Y-m-d H:i:s', time() - $time_window )
|
||||
]
|
||||
]);
|
||||
|
||||
if ( $recent_registrations >= $max_registrations )
|
||||
{
|
||||
self::log_bot_detection( 'RATE_LIMIT', [
|
||||
'ip' => $user_ip,
|
||||
'email' => $email,
|
||||
'registrations_count' => $recent_registrations,
|
||||
'reason' => 'Rate limit exceeded'
|
||||
]);
|
||||
return [ 'status' => 'bad', 'msg' => 'Przekroczono limit rejestracji. Proszę spróbować później.' ];
|
||||
}
|
||||
|
||||
// ============ STANDARDOWE WALIDACJE ============
|
||||
|
||||
if ( \S::strpos_arr( $email, [ 'UNION', 'SELECT', 'ORDER BY', 'AND' ] ) )
|
||||
return false;
|
||||
|
||||
@@ -406,6 +489,7 @@ class GlobelusUser
|
||||
|
||||
$hash = md5( time() . $email );
|
||||
$register_date = date('Y-m-d H:i:s');
|
||||
$register_ip = $_SERVER['REMOTE_ADDR'];
|
||||
|
||||
$mdb -> insert( 'globelus_users', [
|
||||
'email' => $email,
|
||||
@@ -415,8 +499,9 @@ class GlobelusUser
|
||||
'user_agremment_profile' => $agremment_profile == 'on' ? 1 : 0,
|
||||
'user_agremment_marketing' => $agremment_marketing == 'on' ? 1 : 0,
|
||||
'register_date' => $register_date,
|
||||
'register_ip' => $register_ip,
|
||||
'active_to' => date( 'Y-m-d', strtotime( '+90 days', time() ) ),
|
||||
'auto_create' => $auto_create
|
||||
'auto_create' => $auto_create
|
||||
] );
|
||||
|
||||
$user_id = $mdb -> id();
|
||||
|
||||
Reference in New Issue
Block a user