From 4fc0b0df945ab8096df4673cddf2dff0231a738e Mon Sep 17 00:00:00 2001 From: Jacques Malgrange Date: Sat, 12 Mar 2016 23:35:25 +0100 Subject: [PATCH] V1.1.1 --- README.md | 1 + simple-php-captcha/readme.md | 2 +- simple-php-captcha/simple-php-captcha.php | 310 +++++++++++----------- version.json | 2 +- 4 files changed, 160 insertions(+), 155 deletions(-) mode change 100755 => 100644 simple-php-captcha/readme.md diff --git a/README.md b/README.md index bb75235..1566e27 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Added with a Shortcode in the content of the page or directly in the template. ### Versions ### +* 1.1.1 - 12/03/2016 : Update Simple-php-captcha. * 1.1 - 21/11/2015 : Remove Table structure. * 1.0.1 - 05/11/2015 : Fix bugs and auto set default admin email * 1.0 - 05/10/2015 : First stable version \ No newline at end of file diff --git a/simple-php-captcha/readme.md b/simple-php-captcha/readme.md old mode 100755 new mode 100644 index d2c301d..171c241 --- a/simple-php-captcha/readme.md +++ b/simple-php-captcha/readme.md @@ -1,6 +1,6 @@ # A simple PHP CAPTCHA script -_Copyright 2011 Cory LaViska for A Beautiful Site, LLC. (http://abeautifulsite.net/)_ +_Written by Cory LaViska for A Beautiful Site, LLC. (http://abeautifulsite.net/)_ _Licensed under the MIT license: http://opensource.org/licenses/MIT_ diff --git a/simple-php-captcha/simple-php-captcha.php b/simple-php-captcha/simple-php-captcha.php index 5505734..2547908 100755 --- a/simple-php-captcha/simple-php-captcha.php +++ b/simple-php-captcha/simple-php-captcha.php @@ -1,168 +1,172 @@ '', - 'min_length' => 5, - 'max_length' => 5, - 'backgrounds' => array( - $bg_path . '45-degree-fabric.png', - $bg_path . 'cloth-alike.png', - $bg_path . 'grey-sandbag.png', - $bg_path . 'kinda-jean.png', - $bg_path . 'polyester-lite.png', - $bg_path . 'stitched-wool.png', - $bg_path . 'white-carbon.png', - $bg_path . 'white-wave.png' - ), - 'fonts' => array( - $font_path . 'times_new_yorker.ttf' - ), - 'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789', - 'min_font_size' => 24, - 'max_font_size' => 35, - 'color' => '#666', - 'angle_min' => 0, - 'angle_max' => 12, - 'shadow' => true, - 'shadow_color' => '#fff', - 'shadow_offset_x' => -1, - 'shadow_offset_y' => 1 - ); - - // Overwrite defaults with custom config values - if( is_array($config) ) { - foreach( $config as $key => $value ) $captcha_config[$key] = $value; - } - - // Restrict certain values - if( $captcha_config['min_length'] < 1 ) $captcha_config['min_length'] = 1; - if( $captcha_config['angle_min'] < 0 ) $captcha_config['angle_min'] = 0; - if( $captcha_config['angle_max'] > 10 ) $captcha_config['angle_max'] = 10; - if( $captcha_config['angle_max'] < $captcha_config['angle_min'] ) $captcha_config['angle_max'] = $captcha_config['angle_min']; - if( $captcha_config['min_font_size'] < 10 ) $captcha_config['min_font_size'] = 10; - if( $captcha_config['max_font_size'] < $captcha_config['min_font_size'] ) $captcha_config['max_font_size'] = $captcha_config['min_font_size']; - - // Use milliseconds instead of seconds - srand(microtime() * 100); - - // 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 = substr(__FILE__, strlen($_SERVER['DOCUMENT_ROOT'])) . '?_CAPTCHA&t=' . urlencode(microtime()); // ***** pas satisfaisant ***** /htdocs/serv/jack/uno/plugins/contact/simple-php-captcha/simple-php-captcha.php?_CAPTCHA&t=0.05574500+1404040932 - $image_src = dirname($_SERVER['PHP_SELF']) . '/simple-php-captcha/simple-php-captcha.php?_CAPTCHA&t=' . urlencode(microtime()); // OK - $image_src = '/' . ltrim(preg_replace('/\\\\/', '/', $image_src), '/'); - - $_SESSION['_CAPTCHA']['config'] = serialize($captcha_config); - - return array( - 'code' => $captcha_config['code'], - 'image_src' => $image_src - ); - + + // Check for GD library + if( !function_exists('gd_info') ) { + throw new Exception('Required GD library is missing'); + } + + $bg_path = dirname(__FILE__) . '/backgrounds/'; + $font_path = dirname(__FILE__) . '/fonts/'; + + // Default values + $captcha_config = array( + 'code' => '', + 'min_length' => 5, + 'max_length' => 5, + 'backgrounds' => array( + $bg_path . '45-degree-fabric.png', + $bg_path . 'cloth-alike.png', + $bg_path . 'grey-sandbag.png', + $bg_path . 'kinda-jean.png', + $bg_path . 'polyester-lite.png', + $bg_path . 'stitched-wool.png', + $bg_path . 'white-carbon.png', + $bg_path . 'white-wave.png' + ), + 'fonts' => array( + $font_path . 'times_new_yorker.ttf' + ), + 'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789', + 'min_font_size' => 28, + 'max_font_size' => 28, + 'color' => '#666', + 'angle_min' => 0, + 'angle_max' => 10, + 'shadow' => true, + 'shadow_color' => '#fff', + 'shadow_offset_x' => -1, + 'shadow_offset_y' => 1 + ); + + // Overwrite defaults with custom config values + if( is_array($config) ) { + foreach( $config as $key => $value ) $captcha_config[$key] = $value; + } + + // Restrict certain values + if( $captcha_config['min_length'] < 1 ) $captcha_config['min_length'] = 1; + if( $captcha_config['angle_min'] < 0 ) $captcha_config['angle_min'] = 0; + if( $captcha_config['angle_max'] > 10 ) $captcha_config['angle_max'] = 10; + if( $captcha_config['angle_max'] < $captcha_config['angle_min'] ) $captcha_config['angle_max'] = $captcha_config['angle_min']; + if( $captcha_config['min_font_size'] < 10 ) $captcha_config['min_font_size'] = 10; + 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 = mt_rand($captcha_config['min_length'], $captcha_config['max_length']); + while( strlen($captcha_config['code']) < $length ) { + $captcha_config['code'] .= substr($captcha_config['characters'], mt_rand() % (strlen($captcha_config['characters'])), 1); + } + } + + // Generate HTML for image src + if ( strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) ) { + $image_src = substr(__FILE__, strlen( realpath($_SERVER['DOCUMENT_ROOT']) )) . '?_CAPTCHA&t=' . urlencode(microtime()); + $image_src = '/' . ltrim(preg_replace('/\\\\/', '/', $image_src), '/'); + } else { + $_SERVER['WEB_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']); + $image_src = substr(__FILE__, strlen( realpath($_SERVER['WEB_ROOT']) )) . '?_CAPTCHA&t=' . urlencode(microtime()); + $image_src = '/' . ltrim(preg_replace('/\\\\/', '/', $image_src), '/'); + } + + $_SESSION['_CAPTCHA']['config'] = serialize($captcha_config); + + return array( + 'code' => $captcha_config['code'], + 'image_src' => $image_src + ); + } 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; - } + 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['_CAPTCHA']) ) { - - session_start(); - - $captcha_config = unserialize($_SESSION['_CAPTCHA']['config']); - if( !$captcha_config ) exit(); - - unset($_SESSION['_CAPTCHA']); - - // Use milliseconds instead of seconds - srand(microtime() * 100); - - // Pick random background, get info, and start captcha - $background = $captcha_config['backgrounds'][rand(0, count($captcha_config['backgrounds']) -1)]; - 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 = rand( $captcha_config['angle_min'], $captcha_config['angle_max'] ) * (rand(0, 1) == 1 ? -1 : 1); - - // Select font randomly - $font = $captcha_config['fonts'][rand(0, count($captcha_config['fonts']) - 1)]; - - // Verify font file exists - if( !file_exists($font) ) throw new Exception('Font file not found: ' . $font); - - //Set the font size. - $font_size = 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 = 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 = 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); - + + session_start(); + + $captcha_config = unserialize($_SESSION['_CAPTCHA']['config']); + if( !$captcha_config ) exit(); + + unset($_SESSION['_CAPTCHA']); + + // Pick random background, get info, and start captcha + $background = $captcha_config['backgrounds'][mt_rand(0, count($captcha_config['backgrounds']) -1)]; + 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); + + // Select font randomly + $font = $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); + + //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); + } \ No newline at end of file diff --git a/version.json b/version.json index bead5e7..29d59c7 100644 --- a/version.json +++ b/version.json @@ -1 +1 @@ -{"version":"1.1","host":"https://github.com/cmsunoPlugins/contact/"} \ No newline at end of file +{"version":"1.1.1","host":"https://github.com/cmsunoPlugins/contact/"} \ No newline at end of file