[+] добавлена поддержка каптчи в авторизацию фронтовой части

This commit is contained in:
rsgrinko committed 2023-10-11 22:32:01 +03:00
1 parent 3f9dc5cd0b
commit 890d9b8e0d
7 files changed
+92 -15

No files matched your search

+5 -3
View File
@@ -162,9 +162,11 @@
<?php if (USE_CAPTCHA) { ?>
<script>
$(document).ready(function () {
var captchaSrc = $("#captchaCode").attr('src');
$('#updateCaptcha').click(function () {
$("#captchaCode").attr('src', captchaSrc + `?v=${new Date().getTime()}`);
let $captchaImage = $('#captchaCode'),
$captchaSrc = $captchaImage.attr('src'),
$updateBtn = $('#updateCaptcha');
$updateBtn.click(function () {
$captchaImage.attr('src', $captchaSrc + `?v=${new Date().getTime()}`);
});
});
</script>
+4
View File
@@ -156,6 +156,10 @@ body {
padding: 10px;
}
.main .input.captchaInput {
width: 98%;
}
.main .w100 {
width: 100%;
}
+5
View File
@@ -222,3 +222,8 @@
if (!defined('USE_DDOS_PROTECTION')) {
define('USE_DDOS_PROTECTION', false);
}
/**
* Формат даты/времени
*/
const DATETIME_FORMAT = 'Y-m-d H:i:s';
+27 -3
View File
@@ -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)
+25 -2
View File
@@ -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']);
}
}
+6 -6
View File
@@ -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);
+20 -1
View File
@@ -3,12 +3,31 @@
{% block main %}
<h1>Авторизация</h1>
{% if failed %}
<div class="alert alert-error">Ошибка авторизации</div>
<div class="alert alert-error">{{ errorMessage }}</div>
{% endif %}
<p>
<form action="/login/authorize" method="POST">
<input class="input" type="text" name="login" placeholder="Введите логин">
<input class="input" type="password" name="password" placeholder="Введите пароль">
{% if USE_CAPTCHA %}
<div class="input captchaInput">
<img id="captchaCode" src="{{ SITE_URL }}/core/captcha.php">
<span id="updateCaptcha" class="button" style="vertical-align: super;">Обновить код</span>
</div>
<input type="text" class="input" name="captchaCode" placeholder="Код с картинки">
<script>
$(document).ready(function () {
let $captchaImage = $('#captchaCode'),
$captchaSrc = $captchaImage.attr('src'),
$updateBtn = $('#updateCaptcha');
$updateBtn.click(function () {
$captchaImage.attr('src', $captchaSrc + `?v=${new Date().getTime()}`);
});
});
</script>
{% endif %}
<button class="button w100" type="submit">Войти</button>
</form>
</p>