mirror of
https://github.com/Neetpone/ponepaste.git
synced 2025-03-12 06:30:07 +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;
|
||||
|
||||
#[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/';
|
||||
$font_path = __DIR__ . '/../public/assets/fonts/';
|
||||
$fonts = [
|
||||
$font_path . 'LMS Pretty Pony.ttf',
|
||||
$font_path . 'PonyvilleMedium0.4.ttf'
|
||||
];
|
||||
|
||||
if ($mul == "on") {
|
||||
$captcha_config = array(
|
||||
'code' => '',
|
||||
'min_length' => 5,
|
||||
'max_length' => 6,
|
||||
'backgrounds' => array(
|
||||
$backgrounds = [
|
||||
$bg_path . 'text3.png',
|
||||
$bg_path . 'text2.png',
|
||||
$bg_path . 'text1.png'
|
||||
),
|
||||
'fonts' => array(
|
||||
$font_path . 'LMS Pretty Pony.ttf',
|
||||
$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' => '',
|
||||
];
|
||||
|
||||
$captcha_config = [
|
||||
'min_length' => 5,
|
||||
'max_length' => 5,
|
||||
'backgrounds' => array(
|
||||
$bg_path . 'text2.png'
|
||||
),
|
||||
'fonts' => array(
|
||||
$font_path . 'times_new_yorker.ttf'
|
||||
),
|
||||
'backgrounds' => $backgrounds,
|
||||
'fonts' => $fonts,
|
||||
'characters' => $allowed,
|
||||
'min_font_size' => 28,
|
||||
'max_font_size' => 28,
|
||||
|
@ -54,48 +32,47 @@ function captcha($color, $mode, $mul, $allowed) : array {
|
|||
'shadow_color' => '#fff',
|
||||
'shadow_offset_x' => -1,
|
||||
'shadow_offset_y' => 1
|
||||
);
|
||||
}
|
||||
];
|
||||
|
||||
// Overwrite defaults with custom config values
|
||||
if (!empty($config) && is_array($config)) {
|
||||
foreach ($config as $key => $value)
|
||||
foreach ($config as $key => $value) {
|
||||
$captcha_config[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Restrict certain values
|
||||
if ($captcha_config['min_length'] < 1)
|
||||
if ($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;
|
||||
if ($captcha_config['angle_max'] > 10)
|
||||
}
|
||||
|
||||
if ($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'];
|
||||
if ($captcha_config['min_font_size'] < 10)
|
||||
}
|
||||
|
||||
if ($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'];
|
||||
}
|
||||
|
||||
|
||||
// Generate CAPTCHA code if not set by user
|
||||
if (empty($captcha_config['code'])) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate HTML for image src
|
||||
$image_src = '/captcha?_CAPTCHA&_R=' . urlencode(rand());
|
||||
|
||||
$_SESSION['_CAPTCHA']['config'] = serialize($captcha_config);
|
||||
|
||||
return [
|
||||
'code' => $captcha_config['code'],
|
||||
'image_src' => $image_src
|
||||
];
|
||||
return $captcha_config;
|
||||
}
|
||||
|
||||
if (!function_exists('hex2rgb')) {
|
||||
|
|
|
@ -4,10 +4,7 @@ define('IN_PONEPASTE', 1);
|
|||
require_once(__DIR__ . '/../includes/common.php');
|
||||
require_once(__DIR__ . '/../includes/captcha.php');
|
||||
|
||||
$captcha_config = unserialize(@$_SESSION['_CAPTCHA']['config']);
|
||||
if (!$captcha_config) {
|
||||
exit();
|
||||
}
|
||||
$captcha_config = captcha($captcha_config['colour'], $captcha_config['multiple'], $captcha_config['allowed']);
|
||||
|
||||
// Pick random background, get info, and start captcha
|
||||
$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']);
|
||||
|
||||
// Determine text position
|
||||
$box_width = abs($text_box_size[6] - $text_box_size[2]);
|
||||
$box_height = abs($text_box_size[5] - $text_box_size[1]);
|
||||
$box_width = (int) abs($text_box_size[6] - $text_box_size[2]);
|
||||
$box_height = (int) abs($text_box_size[5] - $text_box_size[1]);
|
||||
$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_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);
|
||||
|
||||
// Draw shadow
|
||||
|
|
|
@ -67,7 +67,6 @@ $changefreq = 'weekly';
|
|||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
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