From 1aa336d53b686c07291bda4f8e9dd8ac23614fc3 Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 12:30:12 +0100 Subject: Add options in create_captcha() to specify the randomly generated captcha word length and character pool Uses the same defaults as were hard coded in (8 chars in length, 0-9a-aA-Z). Small change in this file means less code elsewhere when generating random character strings for the captcha word. --- system/helpers/captcha_helper.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 78e255a15..fe6c340be 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -51,7 +51,7 @@ if ( ! function_exists('create_captcha')) */ function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { - $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200); + $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'captcha_word_length' => 8, 'character_pool_for_generted_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); foreach ($defaults as $key => $val) { @@ -72,6 +72,17 @@ if ( ! function_exists('create_captcha')) return FALSE; } + + + // ----------------------------------- + // Make sure captcha max length is a valid/realistic value. + // ----------------------------------- + + $captcha_word_length = (int) $captcha_word_length; + if ($captcha_word_length < 4) { $captcha_word_length = 4;} + if ($captcha_word_length > 15) { $captcha_word_length = 15; } + + // ----------------------------------- // Remove old images // ----------------------------------- @@ -95,11 +106,10 @@ if ( ! function_exists('create_captcha')) if (empty($word)) { - $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $word = ''; - for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++) + for ($i = 0, $mt_rand_max = strlen($character_pool_for_generted_word) - 1; $i < $captcha_word_length; $i++) { - $word .= $pool[mt_rand(0, $mt_rand_max)]; + $word .= $character_pool_for_generted_word[mt_rand(0, $mt_rand_max)]; } } elseif ( ! is_string($word)) @@ -206,4 +216,5 @@ if ( ! function_exists('create_captcha')) } /* End of file captcha_helper.php */ -/* Location: ./system/helpers/captcha_helper.php */ \ No newline at end of file +/* Location: ./system/helpers/captcha_helper.php */ + -- cgit v1.2.3-24-g4f1b From 79dfac7d2b8628d114b02493aa842acd39d39ede Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 12:36:49 +0100 Subject: typo change Uses the same defaults as were hard coded in (8 chars in length, 0-9a-aA-Z). Small change in this file means less code elsewhere when generating random character strings for the captcha word. all existing code will work the same. --- system/helpers/captcha_helper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index fe6c340be..731b59e14 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -51,7 +51,7 @@ if ( ! function_exists('create_captcha')) */ function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { - $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'captcha_word_length' => 8, 'character_pool_for_generted_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'captcha_word_length' => 8, 'character_pool_for_generated_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); foreach ($defaults as $key => $val) { @@ -107,9 +107,9 @@ if ( ! function_exists('create_captcha')) if (empty($word)) { $word = ''; - for ($i = 0, $mt_rand_max = strlen($character_pool_for_generted_word) - 1; $i < $captcha_word_length; $i++) + for ($i = 0, $mt_rand_max = strlen($character_pool_for_generated_word) - 1; $i < $captcha_word_length; $i++) { - $word .= $character_pool_for_generted_word[mt_rand(0, $mt_rand_max)]; + $word .= $character_pool_for_generated_word[mt_rand(0, $mt_rand_max)]; } } elseif ( ! is_string($word)) -- cgit v1.2.3-24-g4f1b From 29ae72d893627edb07ad4fa124f4f8c4e1e0df34 Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 13:59:42 +0100 Subject: removed sanity checks (developer-supplied value, not user input), added changelog entry, changed variable names --- system/helpers/captcha_helper.php | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 731b59e14..61a478e9d 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -51,7 +51,7 @@ if ( ! function_exists('create_captcha')) */ function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { - $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'captcha_word_length' => 8, 'character_pool_for_generated_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'word_length' => 8, 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); foreach ($defaults as $key => $val) { @@ -72,17 +72,6 @@ if ( ! function_exists('create_captcha')) return FALSE; } - - - // ----------------------------------- - // Make sure captcha max length is a valid/realistic value. - // ----------------------------------- - - $captcha_word_length = (int) $captcha_word_length; - if ($captcha_word_length < 4) { $captcha_word_length = 4;} - if ($captcha_word_length > 15) { $captcha_word_length = 15; } - - // ----------------------------------- // Remove old images // ----------------------------------- @@ -107,9 +96,9 @@ if ( ! function_exists('create_captcha')) if (empty($word)) { $word = ''; - for ($i = 0, $mt_rand_max = strlen($character_pool_for_generated_word) - 1; $i < $captcha_word_length; $i++) + for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++) { - $word .= $character_pool_for_generated_word[mt_rand(0, $mt_rand_max)]; + $word .= $pool[mt_rand(0, $mt_rand_max)]; } } elseif ( ! is_string($word)) @@ -217,4 +206,3 @@ if ( ! function_exists('create_captcha')) /* End of file captcha_helper.php */ /* Location: ./system/helpers/captcha_helper.php */ - -- cgit v1.2.3-24-g4f1b From a0c7ae665a181e0489ebb9a0366051669786cb44 Mon Sep 17 00:00:00 2001 From: ash Date: Sun, 14 Apr 2013 14:24:46 +0100 Subject: eol test. --- system/helpers/captcha_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 61a478e9d..d536246dc 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -51,7 +51,7 @@ if ( ! function_exists('create_captcha')) */ function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { - $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'word_length' => 8, 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'word_length' => 8, 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); foreach ($defaults as $key => $val) { -- cgit v1.2.3-24-g4f1b From 19bafaf4eab644aac68d564d4eec36fa497aaa9f Mon Sep 17 00:00:00 2001 From: ash Date: Sun, 14 Apr 2013 14:29:43 +0100 Subject: eol 2 --- system/helpers/captcha_helper.php | 1 + 1 file changed, 1 insertion(+) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index d536246dc..f2e155646 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -206,3 +206,4 @@ if ( ! function_exists('create_captcha')) /* End of file captcha_helper.php */ /* Location: ./system/helpers/captcha_helper.php */ + -- cgit v1.2.3-24-g4f1b From 6e35774fecf392111840816cad08dd63e0463b23 Mon Sep 17 00:00:00 2001 From: ash Date: Sun, 14 Apr 2013 14:33:25 +0100 Subject: eol --- system/helpers/captcha_helper.php | 1 - 1 file changed, 1 deletion(-) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index f2e155646..d536246dc 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -206,4 +206,3 @@ if ( ! function_exists('create_captcha')) /* End of file captcha_helper.php */ /* Location: ./system/helpers/captcha_helper.php */ - -- cgit v1.2.3-24-g4f1b From c3dcf9f7e853e641ea710edfdd4454eabd591f30 Mon Sep 17 00:00:00 2001 From: ash Date: Sun, 14 Apr 2013 14:36:22 +0100 Subject: newline at end --- system/helpers/captcha_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index d536246dc..1982f0489 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -205,4 +205,4 @@ if ( ! function_exists('create_captcha')) } /* End of file captcha_helper.php */ -/* Location: ./system/helpers/captcha_helper.php */ +/* Location: ./system/helpers/captcha_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From ffe1bd215fe64dc054296ed8aa1ac253bbf1962b Mon Sep 17 00:00:00 2001 From: ash Date: Sun, 14 Apr 2013 14:38:48 +0100 Subject: final change (extra space) --- system/helpers/captcha_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system') diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 1982f0489..f3b9c6cc4 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -51,7 +51,7 @@ if ( ! function_exists('create_captcha')) */ function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { - $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'word_length' => 8, 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'word_length' => 8, 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); foreach ($defaults as $key => $val) { -- cgit v1.2.3-24-g4f1b