2021-07-10 19:18:17 +01:00
|
|
|
<?php
|
2021-07-17 18:29:36 -04:00
|
|
|
|
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
|
|
|
|
|
|
|
#[ArrayShape(['code' => "mixed|string", 'image_src' => "string"])]
|
2022-07-30 20:28:53 -04:00
|
|
|
function captcha($color, $mul, $allowed) : array {
|
2022-07-30 17:55:17 -04:00
|
|
|
$bg_path = __DIR__ . '/../public/assets/img/captcha/';
|
|
|
|
$font_path = __DIR__ . '/../public/assets/fonts/';
|
2022-07-30 20:28:53 -04:00
|
|
|
$fonts = [
|
|
|
|
$font_path . 'LMS Pretty Pony.ttf',
|
|
|
|
$font_path . 'PonyvilleMedium0.4.ttf'
|
|
|
|
];
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2022-07-30 20:28:53 -04:00
|
|
|
$backgrounds = [
|
|
|
|
$bg_path . 'text3.png',
|
|
|
|
$bg_path . 'text2.png',
|
|
|
|
$bg_path . 'text1.png'
|
|
|
|
];
|
|
|
|
|
|
|
|
$captcha_config = [
|
|
|
|
'min_length' => 5,
|
|
|
|
'max_length' => 5,
|
|
|
|
'backgrounds' => $backgrounds,
|
|
|
|
'fonts' => $fonts,
|
|
|
|
'characters' => $allowed,
|
|
|
|
'min_font_size' => 28,
|
|
|
|
'max_font_size' => 28,
|
|
|
|
'color' => $color,
|
|
|
|
'angle_min' => 0,
|
|
|
|
'angle_max' => 10,
|
|
|
|
'shadow' => true,
|
|
|
|
'shadow_color' => '#fff',
|
|
|
|
'shadow_offset_x' => -1,
|
|
|
|
'shadow_offset_y' => 1
|
|
|
|
];
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2021-07-10 19:18:17 +01:00
|
|
|
// Overwrite defaults with custom config values
|
2021-07-14 18:02:39 -04:00
|
|
|
if (!empty($config) && is_array($config)) {
|
2022-07-30 20:28:53 -04:00
|
|
|
foreach ($config as $key => $value) {
|
2021-07-10 19:18:17 +01:00
|
|
|
$captcha_config[$key] = $value;
|
2022-07-30 20:28:53 -04:00
|
|
|
}
|
2021-07-10 19:18:17 +01:00
|
|
|
}
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2021-07-10 19:18:17 +01:00
|
|
|
// Restrict certain values
|
2022-07-30 20:28:53 -04:00
|
|
|
if ($captcha_config['min_length'] < 1) {
|
2021-07-10 19:18:17 +01:00
|
|
|
$captcha_config['min_length'] = 1;
|
2022-07-30 20:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($captcha_config['angle_min'] < 0) {
|
2021-07-10 19:18:17 +01:00
|
|
|
$captcha_config['angle_min'] = 0;
|
2022-07-30 20:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($captcha_config['angle_max'] > 10) {
|
2021-07-10 19:18:17 +01:00
|
|
|
$captcha_config['angle_max'] = 10;
|
2022-07-30 20:28:53 -04:00
|
|
|
}
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2022-07-30 20:28:53 -04:00
|
|
|
if ($captcha_config['angle_max'] < $captcha_config['angle_min']) {
|
|
|
|
$captcha_config['angle_max'] = $captcha_config['angle_min'];
|
|
|
|
}
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2022-07-30 20:28:53 -04:00
|
|
|
if ($captcha_config['min_font_size'] < 10) {
|
|
|
|
$captcha_config['min_font_size'] = 10;
|
2021-07-10 19:18:17 +01:00
|
|
|
}
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2022-07-30 20:28:53 -04:00
|
|
|
if ($captcha_config['max_font_size'] < $captcha_config['min_font_size']) {
|
|
|
|
$captcha_config['max_font_size'] = $captcha_config['min_font_size'];
|
|
|
|
}
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2022-07-30 20:28:53 -04:00
|
|
|
$captcha_config['code'] = '';
|
|
|
|
$length = rand($captcha_config['min_length'], $captcha_config['max_length']);
|
|
|
|
while (strlen($captcha_config['code']) < $length) {
|
|
|
|
$captcha_config['code'] .= substr($captcha_config['characters'], rand() % (strlen($captcha_config['characters'])), 1);
|
|
|
|
}
|
2021-07-12 09:03:02 -04:00
|
|
|
|
2022-07-30 20:28:53 -04:00
|
|
|
return $captcha_config;
|
2021-07-10 19:18:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!function_exists('hex2rgb')) {
|
2021-08-22 22:05:26 -04:00
|
|
|
function hex2rgb($hex_str) : array|null {
|
2021-07-12 09:03:02 -04:00
|
|
|
$hex_str = preg_replace("/[^0-9A-Fa-f]/", '', $hex_str); // Gets a proper hex string
|
2021-08-22 21:45:26 -04:00
|
|
|
|
2021-07-10 19:18:17 +01:00
|
|
|
if (strlen($hex_str) == 6) {
|
2021-07-12 09:03:02 -04:00
|
|
|
$color_val = hexdec($hex_str);
|
2021-08-22 21:45:26 -04:00
|
|
|
return [
|
|
|
|
'r' => 0xFF & ($color_val >> 0x10),
|
|
|
|
'g' => 0xFF & ($color_val >> 0x8),
|
|
|
|
'b' => 0xFF & $color_val
|
|
|
|
];
|
2021-07-10 19:18:17 +01:00
|
|
|
} elseif (strlen($hex_str) == 3) {
|
2021-08-22 21:45:26 -04:00
|
|
|
return [
|
|
|
|
'r' => hexdec(str_repeat(substr($hex_str, 0, 1), 2)),
|
|
|
|
'g' => hexdec(str_repeat(substr($hex_str, 1, 1), 2)),
|
|
|
|
'b' => hexdec(str_repeat(substr($hex_str, 2, 1), 2))
|
|
|
|
];
|
2021-07-10 19:18:17 +01:00
|
|
|
}
|
2021-08-22 21:45:26 -04:00
|
|
|
|
|
|
|
return null;
|
2021-07-10 19:18:17 +01:00
|
|
|
}
|
|
|
|
}
|