From 7678f3fecc9f41fafc134c11bdaf188b34161ad6 Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 16 Sep 2013 14:12:04 -0700 Subject: [PATCH] Issue #439 CAPTCHA was difficult to read on some backgrounds. Added support for constant e_CAPTCHA_FONTCOLOR (use hex color) which may be used in theme.php --- e107_admin/auth.php | 4 ++ e107_handlers/secure_img_handler.php | 70 ++++++++++++++++++++++++---- e107_images/secimg.php | 13 +++++- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/e107_admin/auth.php b/e107_admin/auth.php index 79cda598f..5799d6174 100644 --- a/e107_admin/auth.php +++ b/e107_admin/auth.php @@ -18,6 +18,10 @@ if (!defined('e107_INIT')) { exit; } + +define('e_CAPTCHA_FONTCOLOR','#F9A533'); + + // Required for a clean v1.x -> v2 upgrade. $core = e107::getConfig('core'); if($core->get('admintheme') != 'bootstrap') diff --git a/e107_handlers/secure_img_handler.php b/e107_handlers/secure_img_handler.php index 5cbad7227..ca55b9e2d 100644 --- a/e107_handlers/secure_img_handler.php +++ b/e107_handlers/secure_img_handler.php @@ -24,6 +24,7 @@ class secure_image protected $MYSQL_INFO; protected $THIS_DIR; protected $BASE_DIR; + public $FONT_COLOR = "90,90,90"; function secure_image() { @@ -135,25 +136,60 @@ class secure_image } - + //XXX Discuss - Add more posibilities for themers? e_CAPTCHA_BGIMAGE, e_CAPTCH_WIDTH, e_CAPTCHA_HEIGHT? function r_image() { if ($user_func = e107::getOverride()->check($this,'r_image')) { return call_user_func($user_func); } - + + if(defined('e_CAPTCHA_FONTCOLOR')) + { + $color = str_replace("#","", e_CAPTCHA_FONTCOLOR); + } + else + { + $color = 'cccccc'; + } + $code = $this->create_code(); - return "Missing Code"; + return "Missing Code"; } + + function renderImage() // Alias of r_image { return $this->r_image(); } + function hex2rgb($hex) + { + $hex = str_replace("#", "", $hex); + + if(strlen($hex) == 3) + { + $r = hexdec(substr($hex,0,1).substr($hex,0,1)); + $g = hexdec(substr($hex,1,1).substr($hex,1,1)); + $b = hexdec(substr($hex,2,1).substr($hex,2,1)); + } + else + { + $r = hexdec(substr($hex,0,2)); + $g = hexdec(substr($hex,2,2)); + $b = hexdec(substr($hex,4,2)); + } + + $rgb = array($r, $g, $b); + + return implode(",", $rgb); + } + + + function renderInput() { @@ -180,8 +216,14 @@ class secure_image /** * Render the generated Image. Called without class2 environment (standalone). */ - function render($qcode) + function render($qcode, $color='') { + if($color) + { + $this->FONT_COLOR = $this->hex2rgb($color); + } + + // echo "COLOR: ".$this->FONT_COLOR; require_once($this->BASE_DIR.$this->HANDLERS_DIRECTORY."override_class.php"); $over = new override; @@ -262,7 +304,7 @@ class secure_image $fontpath = $this->BASE_DIR.$this->FONTS_DIRECTORY; $secureimg['image'] = "generic/code_bg"; $secureimg['angle'] = "0"; - $secureimg['color'] = "90,90,90"; // red,green,blue + $secureimg['color'] = $this->FONT_COLOR; // red,green,blue $secureimg['x'] = "1"; $secureimg['y'] = "21"; @@ -323,18 +365,21 @@ class secure_image // removing the black from the placeholder - + $image = $this->imageCreateTransparent(100,35); //imagecreatetruecolor(100, 35); + + if(isset($secureimg['color'])) { $tmp = explode(",",$secureimg['color']); $text_color = imagecolorallocate($image,$tmp[0],$tmp[1],$tmp[2]); + } else { $text_color = imagecolorallocate($image, 90, 90, 90); } - + header("Content-type: image/{$type}"); if(isset($secureimg['font']) && is_readable($fontpath.$secureimg['font'])) @@ -346,6 +391,8 @@ class secure_image imagestring ($image, 5, 12, 2, $code, $text_color); } + imagesavealpha($image, true); + switch($type) { case "jpeg": @@ -363,7 +410,14 @@ class secure_image } - + function imageCreateTransparent($x, $y) + { + $imageOut = imagecreatetruecolor($x, $y); + $backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127); + imagefill($imageOut, 0, 0, $backgroundColor); + return $imageOut; + } + diff --git a/e107_images/secimg.php b/e107_images/secimg.php index afb36fc65..47c850731 100644 --- a/e107_images/secimg.php +++ b/e107_images/secimg.php @@ -40,7 +40,18 @@ require_once(realpath(e_BASE.$HANDLERS_DIRECTORY.DIRECTORY_SEPARATOR."secure_img $sim = new secure_image(); -$sim->render($_SERVER['QUERY_STRING']); +$code = $_GET['id']; + +if(preg_match('/^[a-f0-9]{6}$/i', $_GET['clr'])) //hex color is valid +{ + $color = $_GET['clr']; +} +else +{ + $color = "cccccc"; +} + +$sim->render($code,$color); exit;