From 890d9b8e0d15c9fac38e4dbeebb26bc314fb5b4d Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 11 Oct 2023 22:32:01 +0300 Subject: [PATCH] =?UTF-8?q?[+]=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BA=D0=B0=D0=BF=D1=82=D1=87=D0=B8=20=D0=B2=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20=D1=84=D1=80=D0=BE=D0=BD=D1=82=D0=BE=D0=B2=D0=BE=D0=B9?= =?UTF-8?q?=20=D1=87=D0=B0=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/login.php | 8 ++++--- assets/css/style.css | 4 ++++ core/config.php | 5 +++++ core/lib/Core/App.class.php | 30 ++++++++++++++++++++++--- core/lib/Core/Helpers/Captcha.class.php | 27 ++++++++++++++++++++-- core/lib/Core/Models/User.class.php | 12 +++++----- templates/login.twig | 21 ++++++++++++++++- 7 files changed, 92 insertions(+), 15 deletions(-) diff --git a/admin/login.php b/admin/login.php index 4bf5b70..c2e9b33 100644 --- a/admin/login.php +++ b/admin/login.php @@ -162,9 +162,11 @@ diff --git a/assets/css/style.css b/assets/css/style.css index 2f62f6b..af43dd8 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -156,6 +156,10 @@ body { padding: 10px; } +.main .input.captchaInput { + width: 98%; +} + .main .w100 { width: 100%; } diff --git a/core/config.php b/core/config.php index 70b2303..ce6096a 100644 --- a/core/config.php +++ b/core/config.php @@ -222,3 +222,8 @@ if (!defined('USE_DDOS_PROTECTION')) { define('USE_DDOS_PROTECTION', false); } + + /** + * Формат даты/времени + */ + const DATETIME_FORMAT = 'Y-m-d H:i:s'; diff --git a/core/lib/Core/App.class.php b/core/lib/Core/App.class.php index 0d1d14f..37b1668 100644 --- a/core/lib/Core/App.class.php +++ b/core/lib/Core/App.class.php @@ -21,6 +21,7 @@ namespace Core; + use Core\Helpers\Captcha; use Core\Helpers\Pagination; use Core\Helpers\Registry; use Core\Helpers\SystemFunctions; @@ -49,6 +50,8 @@ $memoryUse = memory_get_usage() - START_MEMORY; return [ + 'SITE_URL' => SITE_URL, + 'USE_CAPTCHA' => USE_CAPTCHA, 'salt' => md5(random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999)), 'currentPage' => $currentPage, 'memoryUse' => SystemFunctions::convertBytes($memoryUse), @@ -149,20 +152,41 @@ public static function test($a = null, $b = null, $c = null, $d = null) { + ddd($_SESSION); print_r([$a, $b, $c, $d]); self::render('test.twig'); } public static function logout() { + Captcha::clearSession(); User::logout(); header('Location: /'); } public static function loginAuthorize() { - if (User::securityAuthorize($_REQUEST['login'], $_REQUEST['password'], false)) { - header('Location: /'); + $captchaCorrect = true; + if (USE_CAPTCHA) { + if (empty($_REQUEST['captchaCode'])) { + $_SESSION['authErrorMessage'] = 'Не введен код с картинки'; + $captchaCorrect = false; + } else { + $captchaCorrect = Captcha::isValidCaptcha($_REQUEST['captchaCode']); + if ($captchaCorrect === false) { + $_SESSION['authErrorMessage'] = 'Неверный код с картинки'; + } + } + } + + if ($captchaCorrect) { + if (User::securityAuthorize($_REQUEST['login'], $_REQUEST['password'], false)) { + unset($_SESSION['authErrorMessage']); + header('Location: /'); + } else { + $_SESSION['authErrorMessage'] = 'Неверный логин или пароль'; + header('Location: /login/failed'); + } } else { header('Location: /login/failed'); } @@ -175,7 +199,7 @@ public static function loginFailed() { - self::render('login.twig', ['failed' => true]); + self::render('login.twig', ['failed' => true, 'errorMessage' => $_SESSION['authErrorMessage']]); } public static function userProfile(int $id) diff --git a/core/lib/Core/Helpers/Captcha.class.php b/core/lib/Core/Helpers/Captcha.class.php index 219df7d..2b368e8 100644 --- a/core/lib/Core/Helpers/Captcha.class.php +++ b/core/lib/Core/Helpers/Captcha.class.php @@ -21,6 +21,8 @@ namespace Core\Helpers; + use mysql_xdevapi\Result; + /** * Класс для работы с Captcha */ @@ -36,9 +38,15 @@ { if ($code === null) { $code = SystemFunctions::generateCode(8, 8); - $_SESSION['captchaCode'] = $code; } + $_SESSION['captcha'] = [ + 'captchaCode' => $code, + 'verified' => false, + 'generatedTime' => time(), + 'generatedDate' => date(DATETIME_FORMAT), + ]; + $width = 80; $height = 30; $img = imagecreate($width, $height); @@ -103,6 +111,21 @@ */ public static function isValidCaptcha(string $code): bool { - return (string)$_SESSION['captchaCode'] === $code; + $result = (string)$_SESSION['captcha']['captchaCode'] === $code; + $_SESSION['captcha']['verified'] = $result; + $_SESSION['captcha']['verifiedTime'] = time(); + $_SESSION['captcha']['verifiedDate'] = date(DATETIME_FORMAT); + + return $result; + } + + /** + * Очистка данных каптчи + * + * @return void + */ + public static function clearSession(): void + { + unset($_SESSION['captcha']); } } \ No newline at end of file diff --git a/core/lib/Core/Models/User.class.php b/core/lib/Core/Models/User.class.php index ea4c53f..694cc61 100644 --- a/core/lib/Core/Models/User.class.php +++ b/core/lib/Core/Models/User.class.php @@ -720,12 +720,12 @@ */ public static function logout(): void { - $_SESSION['id'] = ''; - $_SESSION['authorize'] = ''; - $_SESSION['login'] = ''; - $_SESSION['password'] = ''; - $_SESSION['token'] = ''; - $_SESSION['user'] = ''; + unset($_SESSION['id']); + unset($_SESSION['authorize']); + unset($_SESSION['login']); + unset($_SESSION['password']); + unset($_SESSION['token']); + unset($_SESSION['user']); setcookie('userId', '', time() - 3600); setcookie('userLogin', '', time() - 3600); diff --git a/templates/login.twig b/templates/login.twig index 4dad686..efe7893 100644 --- a/templates/login.twig +++ b/templates/login.twig @@ -3,12 +3,31 @@ {% block main %}

Авторизация

{% if failed %} -
Ошибка авторизации
+
{{ errorMessage }}
{% endif %}

+ + {% if USE_CAPTCHA %} +
+ + Обновить код +
+ + + {% endif %} +