[+] добавлена поддержка каптчи в авторизацию фронтовой части
This commit is contained in:
1 parent
3f9dc5cd0b
commit
890d9b8e0d
7 files changed
+92
-15
No files matched your search
+5
-3
@@ -162,9 +162,11 @@
|
|||||||
<?php if (USE_CAPTCHA) { ?>
|
<?php if (USE_CAPTCHA) { ?>
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
var captchaSrc = $("#captchaCode").attr('src');
|
let $captchaImage = $('#captchaCode'),
|
||||||
$('#updateCaptcha').click(function () {
|
$captchaSrc = $captchaImage.attr('src'),
|
||||||
$("#captchaCode").attr('src', captchaSrc + `?v=${new Date().getTime()}`);
|
$updateBtn = $('#updateCaptcha');
|
||||||
|
$updateBtn.click(function () {
|
||||||
|
$captchaImage.attr('src', $captchaSrc + `?v=${new Date().getTime()}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -156,6 +156,10 @@ body {
|
|||||||
padding: 10px;
|
padding: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.main .input.captchaInput {
|
||||||
|
width: 98%;
|
||||||
|
}
|
||||||
|
|
||||||
.main .w100 {
|
.main .w100 {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,3 +222,8 @@
|
|||||||
if (!defined('USE_DDOS_PROTECTION')) {
|
if (!defined('USE_DDOS_PROTECTION')) {
|
||||||
define('USE_DDOS_PROTECTION', false);
|
define('USE_DDOS_PROTECTION', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Формат даты/времени
|
||||||
|
*/
|
||||||
|
const DATETIME_FORMAT = 'Y-m-d H:i:s';
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
namespace Core;
|
namespace Core;
|
||||||
|
|
||||||
|
use Core\Helpers\Captcha;
|
||||||
use Core\Helpers\Pagination;
|
use Core\Helpers\Pagination;
|
||||||
use Core\Helpers\Registry;
|
use Core\Helpers\Registry;
|
||||||
use Core\Helpers\SystemFunctions;
|
use Core\Helpers\SystemFunctions;
|
||||||
@@ -49,6 +50,8 @@
|
|||||||
$memoryUse = memory_get_usage() - START_MEMORY;
|
$memoryUse = memory_get_usage() - START_MEMORY;
|
||||||
|
|
||||||
return [
|
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)),
|
'salt' => md5(random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999)),
|
||||||
'currentPage' => $currentPage,
|
'currentPage' => $currentPage,
|
||||||
'memoryUse' => SystemFunctions::convertBytes($memoryUse),
|
'memoryUse' => SystemFunctions::convertBytes($memoryUse),
|
||||||
@@ -149,20 +152,41 @@
|
|||||||
|
|
||||||
public static function test($a = null, $b = null, $c = null, $d = null)
|
public static function test($a = null, $b = null, $c = null, $d = null)
|
||||||
{
|
{
|
||||||
|
ddd($_SESSION);
|
||||||
print_r([$a, $b, $c, $d]);
|
print_r([$a, $b, $c, $d]);
|
||||||
self::render('test.twig');
|
self::render('test.twig');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function logout()
|
public static function logout()
|
||||||
{
|
{
|
||||||
|
Captcha::clearSession();
|
||||||
User::logout();
|
User::logout();
|
||||||
header('Location: /');
|
header('Location: /');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function loginAuthorize()
|
public static function loginAuthorize()
|
||||||
{
|
{
|
||||||
if (User::securityAuthorize($_REQUEST['login'], $_REQUEST['password'], false)) {
|
$captchaCorrect = true;
|
||||||
header('Location: /');
|
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 {
|
} else {
|
||||||
header('Location: /login/failed');
|
header('Location: /login/failed');
|
||||||
}
|
}
|
||||||
@@ -175,7 +199,7 @@
|
|||||||
|
|
||||||
public static function loginFailed()
|
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)
|
public static function userProfile(int $id)
|
||||||
|
|||||||
@@ -21,6 +21,8 @@
|
|||||||
|
|
||||||
namespace Core\Helpers;
|
namespace Core\Helpers;
|
||||||
|
|
||||||
|
use mysql_xdevapi\Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Класс для работы с Captcha
|
* Класс для работы с Captcha
|
||||||
*/
|
*/
|
||||||
@@ -36,9 +38,15 @@
|
|||||||
{
|
{
|
||||||
if ($code === null) {
|
if ($code === null) {
|
||||||
$code = SystemFunctions::generateCode(8, 8);
|
$code = SystemFunctions::generateCode(8, 8);
|
||||||
$_SESSION['captchaCode'] = $code;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$_SESSION['captcha'] = [
|
||||||
|
'captchaCode' => $code,
|
||||||
|
'verified' => false,
|
||||||
|
'generatedTime' => time(),
|
||||||
|
'generatedDate' => date(DATETIME_FORMAT),
|
||||||
|
];
|
||||||
|
|
||||||
$width = 80;
|
$width = 80;
|
||||||
$height = 30;
|
$height = 30;
|
||||||
$img = imagecreate($width, $height);
|
$img = imagecreate($width, $height);
|
||||||
@@ -103,6 +111,21 @@
|
|||||||
*/
|
*/
|
||||||
public static function isValidCaptcha(string $code): bool
|
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']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -720,12 +720,12 @@
|
|||||||
*/
|
*/
|
||||||
public static function logout(): void
|
public static function logout(): void
|
||||||
{
|
{
|
||||||
$_SESSION['id'] = '';
|
unset($_SESSION['id']);
|
||||||
$_SESSION['authorize'] = '';
|
unset($_SESSION['authorize']);
|
||||||
$_SESSION['login'] = '';
|
unset($_SESSION['login']);
|
||||||
$_SESSION['password'] = '';
|
unset($_SESSION['password']);
|
||||||
$_SESSION['token'] = '';
|
unset($_SESSION['token']);
|
||||||
$_SESSION['user'] = '';
|
unset($_SESSION['user']);
|
||||||
|
|
||||||
setcookie('userId', '', time() - 3600);
|
setcookie('userId', '', time() - 3600);
|
||||||
setcookie('userLogin', '', time() - 3600);
|
setcookie('userLogin', '', time() - 3600);
|
||||||
|
|||||||
+20
-1
@@ -3,12 +3,31 @@
|
|||||||
{% block main %}
|
{% block main %}
|
||||||
<h1>Авторизация</h1>
|
<h1>Авторизация</h1>
|
||||||
{% if failed %}
|
{% if failed %}
|
||||||
<div class="alert alert-error">Ошибка авторизации</div>
|
<div class="alert alert-error">{{ errorMessage }}</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<p>
|
<p>
|
||||||
<form action="/login/authorize" method="POST">
|
<form action="/login/authorize" method="POST">
|
||||||
<input class="input" type="text" name="login" placeholder="Введите логин">
|
<input class="input" type="text" name="login" placeholder="Введите логин">
|
||||||
<input class="input" type="password" name="password" 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>
|
<button class="button w100" type="submit">Войти</button>
|
||||||
</form>
|
</form>
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
Reference in new issue
Block a user