<?php
/**
* dFramework
*
* The simplest PHP framework for beginners
* Copyright (c) 2019, Dimtrov Sarl
* This content is released under the Mozilla Public License 2 (MPL-2.0)
*
* @package dFramework
* @author Dimitri Sitchet Tomkeu <dev.dst@gmail.com>
* @copyright Copyright (c) 2019, Dimtrov Sarl. (https://dimtrov.hebfree.org)
* @copyright Copyright (c) 2019, Dimitri Sitchet Tomkeu. (https://www.facebook.com/dimtrovich)
* @license https://opensource.org/licenses/MPL-2.0 MPL-2.0 License
* @homepage https://dimtrov.hebfree.org/works/dframework
* @version 3.2.1
*/
//
// A simple PHP CAPTCHA script
//
// Copyright 2011 by Cory LaViska for A Beautiful Site, LLC
//
// See readme.md for usage, demo, and licensing info
//
// Adapter par Dimitric Sitchet Tomkeu pour dFramework
//
// Disponible depuis la version 2.1
if( !function_exists('hex2rgb') )
{
function hex2rgb($hex_str, $return_string = false, $separator = ',')
{
$hex_str = preg_replace("/[^0-9A-Fa-f]/", '', $hex_str); // Gets a proper hex string
$rgb_array = array();
if( strlen($hex_str) == 6 )
{
$color_val = hexdec($hex_str);
$rgb_array['r'] = 0xFF & ($color_val >> 0x10);
$rgb_array['g'] = 0xFF & ($color_val >> 0x8);
$rgb_array['b'] = 0xFF & $color_val;
}
elseif( strlen($hex_str) == 3 )
{
$rgb_array['r'] = hexdec(str_repeat(substr($hex_str, 0, 1), 2));
$rgb_array['g'] = hexdec(str_repeat(substr($hex_str, 1, 1), 2));
$rgb_array['b'] = hexdec(str_repeat(substr($hex_str, 2, 1), 2));
}
else
{
return false;
}
return $return_string ? implode($separator, $rgb_array) : $rgb_array;
}
}
// Draw the image
if( isset($_GET['df_captcha']) )
{
session_start();
if(empty($_SESSION['df_security']['captcha']['config']))
{
exit;
}
$captcha_config = unserialize($_SESSION['df_security']['captcha']['config']);
if(empty($captcha_config) OR !is_array($captcha_config))
{
exit;
}
unset($_SESSION['df_security']['captcha']['config']);
$captcha_config['code'] = base64_decode($captcha_config['code']);
$captcha_config['code'] = substr($captcha_config['code'], 32);
// Pick random background, get info, and start captcha
$background = $captcha_config['bg_path'].$captcha_config['backgrounds'][mt_rand(0, count($captcha_config['backgrounds']) -1)];
// Verify background file exists
if( !file_exists($background))
{
throw new Exception('Background file not found: ' . $background);
}
// Select font randomly
$font = $captcha_config['font_path'].$captcha_config['fonts'][mt_rand(0, count($captcha_config['fonts']) - 1)];
// Verify font file exists
if( !file_exists($font) )
{
throw new Exception('Font file not found: ' . $font);
}
list($bg_width, $bg_height, $bg_type, $bg_attr) = getimagesize($background);
$captcha = imagecreatefrompng($background);
$color = hex2rgb($captcha_config['color']);
$color = imagecolorallocate($captcha, $color['r'], $color['g'], $color['b']);
// Determine text angle
$angle = mt_rand( $captcha_config['angle_min'], $captcha_config['angle_max'] ) * (mt_rand(0, 1) == 1 ? -1 : 1);
//Set the font size.
$font_size = mt_rand($captcha_config['min_font_size'], $captcha_config['max_font_size']);
$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]);
$text_pos_x_min = 0;
$text_pos_x_max = ($bg_width) - ($box_width);
$text_pos_x = mt_rand($text_pos_x_min, $text_pos_x_max);
$text_pos_y_min = $box_height;
$text_pos_y_max = ($bg_height) - ($box_height / 2);
if ($text_pos_y_min > $text_pos_y_max) {
$temp_text_pos_y = $text_pos_y_min;
$text_pos_y_min = $text_pos_y_max;
$text_pos_y_max = $temp_text_pos_y;
}
$text_pos_y = mt_rand($text_pos_y_min, $text_pos_y_max);
// Draw shadow
if( $captcha_config['shadow'] ){
$shadow_color = hex2rgb($captcha_config['shadow_color']);
$shadow_color = imagecolorallocate($captcha, $shadow_color['r'], $shadow_color['g'], $shadow_color['b']);
imagettftext($captcha, $font_size, $angle, $text_pos_x + $captcha_config['shadow_offset_x'], $text_pos_y + $captcha_config['shadow_offset_y'], $shadow_color, $font, $captcha_config['code']);
}
// Draw text
imagettftext($captcha, $font_size, $angle, $text_pos_x, $text_pos_y, $color, $font, $captcha_config['code']);
// Output image
header("Content-type: image/png");
imagepng($captcha);
}
|