mirror of
https://github.com/Neetpone/ponepaste.git
synced 2025-03-12 14:40:09 +01:00
Simplify CAPTCHA and make it work properly.
This commit is contained in:
parent
0038d5ecf1
commit
0f519a8ced
3 changed files with 63 additions and 90 deletions
|
@ -3,47 +3,25 @@
|
||||||
use JetBrains\PhpStorm\ArrayShape;
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
|
|
||||||
#[ArrayShape(['code' => "mixed|string", 'image_src' => "string"])]
|
#[ArrayShape(['code' => "mixed|string", 'image_src' => "string"])]
|
||||||
function captcha($color, $mode, $mul, $allowed) : array {
|
function captcha($color, $mul, $allowed) : array {
|
||||||
$bg_path = __DIR__ . '/../public/assets/img/captcha/';
|
$bg_path = __DIR__ . '/../public/assets/img/captcha/';
|
||||||
$font_path = __DIR__ . '/../public/assets/fonts/';
|
$font_path = __DIR__ . '/../public/assets/fonts/';
|
||||||
|
$fonts = [
|
||||||
|
$font_path . 'LMS Pretty Pony.ttf',
|
||||||
|
$font_path . 'PonyvilleMedium0.4.ttf'
|
||||||
|
];
|
||||||
|
|
||||||
if ($mul == "on") {
|
$backgrounds = [
|
||||||
$captcha_config = array(
|
|
||||||
'code' => '',
|
|
||||||
'min_length' => 5,
|
|
||||||
'max_length' => 6,
|
|
||||||
'backgrounds' => array(
|
|
||||||
$bg_path . 'text3.png',
|
$bg_path . 'text3.png',
|
||||||
$bg_path . 'text2.png',
|
$bg_path . 'text2.png',
|
||||||
$bg_path . 'text1.png'
|
$bg_path . 'text1.png'
|
||||||
),
|
];
|
||||||
'fonts' => array(
|
|
||||||
$font_path . 'LMS Pretty Pony.ttf',
|
$captcha_config = [
|
||||||
$font_path . 'PonyvilleMedium0.4.ttf',
|
|
||||||
$font_path . 'PonyvilleMedium0.4.ttf'
|
|
||||||
),
|
|
||||||
'characters' => $allowed,
|
|
||||||
'min_font_size' => 20,
|
|
||||||
'max_font_size' => 28,
|
|
||||||
'color' => $color,
|
|
||||||
'angle_min' => 0,
|
|
||||||
'angle_max' => 5,
|
|
||||||
'shadow' => true,
|
|
||||||
'shadow_color' => '#fff',
|
|
||||||
'shadow_offset_x' => -2,
|
|
||||||
'shadow_offset_y' => 4
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$captcha_config = array(
|
|
||||||
'code' => '',
|
|
||||||
'min_length' => 5,
|
'min_length' => 5,
|
||||||
'max_length' => 5,
|
'max_length' => 5,
|
||||||
'backgrounds' => array(
|
'backgrounds' => $backgrounds,
|
||||||
$bg_path . 'text2.png'
|
'fonts' => $fonts,
|
||||||
),
|
|
||||||
'fonts' => array(
|
|
||||||
$font_path . 'times_new_yorker.ttf'
|
|
||||||
),
|
|
||||||
'characters' => $allowed,
|
'characters' => $allowed,
|
||||||
'min_font_size' => 28,
|
'min_font_size' => 28,
|
||||||
'max_font_size' => 28,
|
'max_font_size' => 28,
|
||||||
|
@ -54,48 +32,47 @@ function captcha($color, $mode, $mul, $allowed) : array {
|
||||||
'shadow_color' => '#fff',
|
'shadow_color' => '#fff',
|
||||||
'shadow_offset_x' => -1,
|
'shadow_offset_x' => -1,
|
||||||
'shadow_offset_y' => 1
|
'shadow_offset_y' => 1
|
||||||
);
|
];
|
||||||
}
|
|
||||||
|
|
||||||
// Overwrite defaults with custom config values
|
// Overwrite defaults with custom config values
|
||||||
if (!empty($config) && is_array($config)) {
|
if (!empty($config) && is_array($config)) {
|
||||||
foreach ($config as $key => $value)
|
foreach ($config as $key => $value) {
|
||||||
$captcha_config[$key] = $value;
|
$captcha_config[$key] = $value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Restrict certain values
|
// Restrict certain values
|
||||||
if ($captcha_config['min_length'] < 1)
|
if ($captcha_config['min_length'] < 1) {
|
||||||
$captcha_config['min_length'] = 1;
|
$captcha_config['min_length'] = 1;
|
||||||
if ($captcha_config['angle_min'] < 0)
|
}
|
||||||
|
|
||||||
|
if ($captcha_config['angle_min'] < 0) {
|
||||||
$captcha_config['angle_min'] = 0;
|
$captcha_config['angle_min'] = 0;
|
||||||
if ($captcha_config['angle_max'] > 10)
|
}
|
||||||
|
|
||||||
|
if ($captcha_config['angle_max'] > 10) {
|
||||||
$captcha_config['angle_max'] = 10;
|
$captcha_config['angle_max'] = 10;
|
||||||
if ($captcha_config['angle_max'] < $captcha_config['angle_min'])
|
}
|
||||||
|
|
||||||
|
if ($captcha_config['angle_max'] < $captcha_config['angle_min']) {
|
||||||
$captcha_config['angle_max'] = $captcha_config['angle_min'];
|
$captcha_config['angle_max'] = $captcha_config['angle_min'];
|
||||||
if ($captcha_config['min_font_size'] < 10)
|
}
|
||||||
|
|
||||||
|
if ($captcha_config['min_font_size'] < 10) {
|
||||||
$captcha_config['min_font_size'] = 10;
|
$captcha_config['min_font_size'] = 10;
|
||||||
if ($captcha_config['max_font_size'] < $captcha_config['min_font_size'])
|
}
|
||||||
|
|
||||||
|
if ($captcha_config['max_font_size'] < $captcha_config['min_font_size']) {
|
||||||
$captcha_config['max_font_size'] = $captcha_config['min_font_size'];
|
$captcha_config['max_font_size'] = $captcha_config['min_font_size'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Generate CAPTCHA code if not set by user
|
|
||||||
if (empty($captcha_config['code'])) {
|
|
||||||
$captcha_config['code'] = '';
|
$captcha_config['code'] = '';
|
||||||
$length = rand($captcha_config['min_length'], $captcha_config['max_length']);
|
$length = rand($captcha_config['min_length'], $captcha_config['max_length']);
|
||||||
while (strlen($captcha_config['code']) < $length) {
|
while (strlen($captcha_config['code']) < $length) {
|
||||||
$captcha_config['code'] .= substr($captcha_config['characters'], rand() % (strlen($captcha_config['characters'])), 1);
|
$captcha_config['code'] .= substr($captcha_config['characters'], rand() % (strlen($captcha_config['characters'])), 1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Generate HTML for image src
|
return $captcha_config;
|
||||||
$image_src = '/captcha?_CAPTCHA&_R=' . urlencode(rand());
|
|
||||||
|
|
||||||
$_SESSION['_CAPTCHA']['config'] = serialize($captcha_config);
|
|
||||||
|
|
||||||
return [
|
|
||||||
'code' => $captcha_config['code'],
|
|
||||||
'image_src' => $image_src
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists('hex2rgb')) {
|
if (!function_exists('hex2rgb')) {
|
||||||
|
|
|
@ -4,10 +4,7 @@ define('IN_PONEPASTE', 1);
|
||||||
require_once(__DIR__ . '/../includes/common.php');
|
require_once(__DIR__ . '/../includes/common.php');
|
||||||
require_once(__DIR__ . '/../includes/captcha.php');
|
require_once(__DIR__ . '/../includes/captcha.php');
|
||||||
|
|
||||||
$captcha_config = unserialize(@$_SESSION['_CAPTCHA']['config']);
|
$captcha_config = captcha($captcha_config['colour'], $captcha_config['multiple'], $captcha_config['allowed']);
|
||||||
if (!$captcha_config) {
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pick random background, get info, and start captcha
|
// Pick random background, get info, and start captcha
|
||||||
$background = $captcha_config['backgrounds'][rand(0, count($captcha_config['backgrounds']) - 1)];
|
$background = $captcha_config['backgrounds'][rand(0, count($captcha_config['backgrounds']) - 1)];
|
||||||
|
@ -34,13 +31,13 @@ $font_size = rand($captcha_config['min_font_size'], $captcha_config['max_font_si
|
||||||
$text_box_size = imagettfbbox($font_size, $angle, $font, $captcha_config['code']);
|
$text_box_size = imagettfbbox($font_size, $angle, $font, $captcha_config['code']);
|
||||||
|
|
||||||
// Determine text position
|
// Determine text position
|
||||||
$box_width = abs($text_box_size[6] - $text_box_size[2]);
|
$box_width = (int) abs($text_box_size[6] - $text_box_size[2]);
|
||||||
$box_height = abs($text_box_size[5] - $text_box_size[1]);
|
$box_height = (int) abs($text_box_size[5] - $text_box_size[1]);
|
||||||
$text_pos_x_min = 0;
|
$text_pos_x_min = 0;
|
||||||
$text_pos_x_max = ($bg_width) - ($box_width);
|
$text_pos_x_max = (int) ($bg_width - $box_width);
|
||||||
$text_pos_x = rand($text_pos_x_min, $text_pos_x_max);
|
$text_pos_x = rand($text_pos_x_min, $text_pos_x_max);
|
||||||
$text_pos_y_min = $box_height;
|
$text_pos_y_min = $box_height;
|
||||||
$text_pos_y_max = ($bg_height) - ($box_height / 2);
|
$text_pos_y_max = (int) ($bg_height - ($box_height / 2));
|
||||||
$text_pos_y = rand($text_pos_y_min, $text_pos_y_max);
|
$text_pos_y = rand($text_pos_y_min, $text_pos_y_max);
|
||||||
|
|
||||||
// Draw shadow
|
// Draw shadow
|
||||||
|
|
|
@ -67,7 +67,6 @@ $changefreq = 'weekly';
|
||||||
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
if ($captcha_config['enabled']) {
|
if ($captcha_config['enabled']) {
|
||||||
$_SESSION['captcha'] = captcha($captcha_config['colour'], $captcha_config['mode'], $captcha_config['multiple'], $captcha_config['allowed']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue