[+] Добавил функционал каптчи

This commit is contained in:
rsgrinko committed 2023-07-24 00:14:43 +03:00
1 parent 6b81e0b01c
commit 95a3ff5b36
3 files changed
+119 -1

No files matched your search

+2 -1
View File
@@ -26,7 +26,8 @@
"ext-pdo": "*",
"ext-mbstring": "*",
"dompdf/dompdf": "^2.0",
"robmorgan/phinx": "^0.13.4"
"robmorgan/phinx": "^0.13.4",
"ext-gd": "*"
},
"require-dev": {
"vimeo/psalm": "^4.27",
+28
View File
@@ -0,0 +1,28 @@
<?php
/**
* Copyright (c) 2022 Roman Grinko <rsgrinko@gmail.com>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
use Core\Helpers\SystemFunctions;
require_once __DIR__ . '/bootstrap.php';
$_SESSION['captchaCode'] = SystemFunctions::generateCode(8, 8);
SystemFunctions::showCaptcha($_SESSION['captchaCode']);
@@ -965,4 +965,93 @@
}
return implode('', $explodedString);
}
/**
* Генерация кода для каптчи
* @param int $min Минимальное количество символов
* @param int $max Максимальное количество символов
*
* @return void
*/
public static function generateCode(int $min = 4, int $max = 8): string
{
$chars = 'ABCDEFGHJKLMNPRSTUVXYZ23456789';
$length = rand($min, $max);
$numChars = strlen($chars);
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, rand(1, $numChars) - 1, 1);
}
// Перемешиваем, на всякий случай
$array_mix = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
srand ((float)microtime()*1000000);
shuffle ($array_mix);
return implode('', $array_mix);
}
/**
* Генерация и отображение картинки-каптчи
*
* @param string $code Код
*
* @return void
*/
public static function showCaptcha(string $code = 'test'): void
{
$width = 80;
$height = 30;
$img = imagecreate($width, $height);
// Задаем фон
imagecolorallocate($img, 255, 255, 255);
// Задаем цвета
$lineColor = imagecolorallocate($img, 150, 150, 150);
$pixelColor = imagecolorallocate($img, 150, 150, 150);
$whiteColor = imagecolorallocate($img, 255, 255, 255);
$blackColor = imagecolorallocate($img, 0, 0, 0);
// Рисуем линии 1
for ($i = 0; $i < 3; $i++) {
$x1 = 0;
$x2 = $width;
$y1 = rand(0, $height);
$y2 = rand(00, $height);
imageline($img, $x1, $y1, $x2, $y2, $lineColor);
}
// Рисуем линии 2
for ($i = 0; $i < 3; $i++) {
$x1 = rand(0, $width);
$x2 = rand(0, $width);
$y1 = 0;
$y2 = $height;
imageline($img, $x1, $y1, $x2, $y2, $lineColor);
}
// Рисуем пиксели
for ($i = 0; $i < 200; $i++) {
imagesetpixel($img, rand() % $width, rand() % $height, $pixelColor);
}
// Рисуем тень для кода
imagestring($img, 5, 4, 7, $code, $blackColor);
imagestring($img, 5, 6, 7, $code, $blackColor);
imagestring($img, 5, 5, 6, $code, $blackColor);
imagestring($img, 5, 5, 8, $code, $blackColor);
// Рисуем сам код
imagestring($img, 5, 5, 7, $code, $whiteColor); //main
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', 10000) . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Content-Type: image/png');
imagepng($img);
}
}