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(-) 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(-) 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 3fd9bf8777d6aea531a765f4bc0d995d24395221 Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 12:40:31 +0100 Subject: Updated documenation --- user_guide_src/source/helpers/captcha_helper.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/helpers/captcha_helper.rst b/user_guide_src/source/helpers/captcha_helper.rst index 17462a8de..598fdb4c6 100644 --- a/user_guide_src/source/helpers/captcha_helper.rst +++ b/user_guide_src/source/helpers/captcha_helper.rst @@ -62,7 +62,9 @@ Once loaded you can generate a captcha like this:: 'font_path' => './path/to/fonts/texb.ttf', 'img_width' => '150', 'img_height' => 30, - 'expiration' => 7200 + 'expiration' => 7200, + 'captcha_word_length' => 8, + 'character_pool_for_generated_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ); $cap = create_captcha($vals); @@ -140,4 +142,4 @@ this:: if ($row->count == 0) {      echo 'You must submit the word that appears in the image.'; - } \ No newline at end of file + } -- cgit v1.2.3-24-g4f1b From 9c7e651922a78cefb1806becd0c5618fa23384c7 Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 12:43:21 +0100 Subject: documenation edit --- user_guide_src/source/helpers/captcha_helper.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/helpers/captcha_helper.rst b/user_guide_src/source/helpers/captcha_helper.rst index 598fdb4c6..f924bd717 100644 --- a/user_guide_src/source/helpers/captcha_helper.rst +++ b/user_guide_src/source/helpers/captcha_helper.rst @@ -81,6 +81,7 @@ Once loaded you can generate a captcha like this:: - The **expiration** (in seconds) signifies how long an image will remain in the captcha folder before it will be deleted. The default is two hours. +- **captcha_word_length** defaults to 8 but a sanity check will enforce it to a minimum length of 4 or maximum length of 15, **character_pool_for_generated_word** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' Adding a Database ----------------- -- cgit v1.2.3-24-g4f1b From 4fa6385ff8232273f15f08fe8ef2799c9305419b Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 12:44:57 +0100 Subject: Documentation change - explanation of captcha_word_length and character_pool_for_generated_word --- user_guide_src/source/helpers/captcha_helper.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/helpers/captcha_helper.rst b/user_guide_src/source/helpers/captcha_helper.rst index f924bd717..7dd642ae7 100644 --- a/user_guide_src/source/helpers/captcha_helper.rst +++ b/user_guide_src/source/helpers/captcha_helper.rst @@ -81,7 +81,7 @@ Once loaded you can generate a captcha like this:: - The **expiration** (in seconds) signifies how long an image will remain in the captcha folder before it will be deleted. The default is two hours. -- **captcha_word_length** defaults to 8 but a sanity check will enforce it to a minimum length of 4 or maximum length of 15, **character_pool_for_generated_word** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +- **captcha_word_length** defaults to 8 but a sanity check in /system/helpers/captcha_helper.php will enforce it to a minimum length of 4 or maximum length of 15, **character_pool_for_generated_word** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' Adding a Database ----------------- -- 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 +++--------------- user_guide_src/source/changelog.rst | 2 ++ user_guide_src/source/helpers/captcha_helper.rst | 6 +++--- 3 files changed, 8 insertions(+), 18 deletions(-) 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 */ - diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 65e210761..8ab363981 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -105,6 +105,8 @@ Release Date: Not Released - :doc:`Directory Helper ` :php:func:`directory_map()` will now append ``DIRECTORY_SEPARATOR`` to directory names in the returned array. - :doc:`Language Helper ` :php:func:`lang()` now accepts an optional list of additional HTML attributes. - Deprecated the :doc:`Email Helper ` as its ``valid_email()``, ``send_email()`` functions are now only aliases for PHP native functions ``filter_var()`` and ``mail()`` respectively. + - :doc:`CAPTCHA Helper ` changes include: + - :php:func:`create_captcha` added word_length and pool options for setting length of randomly generated captcha word, and what characters to select from. - Database diff --git a/user_guide_src/source/helpers/captcha_helper.rst b/user_guide_src/source/helpers/captcha_helper.rst index 7dd642ae7..c6fb00280 100644 --- a/user_guide_src/source/helpers/captcha_helper.rst +++ b/user_guide_src/source/helpers/captcha_helper.rst @@ -63,8 +63,8 @@ Once loaded you can generate a captcha like this:: 'img_width' => '150', 'img_height' => 30, 'expiration' => 7200, - 'captcha_word_length' => 8, - 'character_pool_for_generated_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'word_length' => 8, + 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ); $cap = create_captcha($vals); @@ -81,7 +81,7 @@ Once loaded you can generate a captcha like this:: - The **expiration** (in seconds) signifies how long an image will remain in the captcha folder before it will be deleted. The default is two hours. -- **captcha_word_length** defaults to 8 but a sanity check in /system/helpers/captcha_helper.php will enforce it to a minimum length of 4 or maximum length of 15, **character_pool_for_generated_word** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +- **word_length** defaults to 8, **pool** defaults to '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' Adding a Database ----------------- -- cgit v1.2.3-24-g4f1b From ba957133b0d02ceb3a7efe33e08a3b3d408f30d3 Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 14:01:45 +0100 Subject: changed changelog format. --- user_guide_src/source/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 8ab363981..30a22259d 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -106,7 +106,7 @@ Release Date: Not Released - :doc:`Language Helper ` :php:func:`lang()` now accepts an optional list of additional HTML attributes. - Deprecated the :doc:`Email Helper ` as its ``valid_email()``, ``send_email()`` functions are now only aliases for PHP native functions ``filter_var()`` and ``mail()`` respectively. - :doc:`CAPTCHA Helper ` changes include: - - :php:func:`create_captcha` added word_length and pool options for setting length of randomly generated captcha word, and what characters to select from. + - :php:func:`create_captcha` added word_length and pool options for setting length of randomly generated captcha word, and what characters to select from. - Database -- cgit v1.2.3-24-g4f1b From 68b285f2d28e2bc469f87cdaf2ee5592e2cdbb1b Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 10 Apr 2013 14:16:27 +0100 Subject: changelog change --- user_guide_src/source/changelog.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 30a22259d..742dacbae 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -105,8 +105,7 @@ Release Date: Not Released - :doc:`Directory Helper ` :php:func:`directory_map()` will now append ``DIRECTORY_SEPARATOR`` to directory names in the returned array. - :doc:`Language Helper ` :php:func:`lang()` now accepts an optional list of additional HTML attributes. - Deprecated the :doc:`Email Helper ` as its ``valid_email()``, ``send_email()`` functions are now only aliases for PHP native functions ``filter_var()`` and ``mail()`` respectively. - - :doc:`CAPTCHA Helper ` changes include: - - :php:func:`create_captcha` added word_length and pool options for setting length of randomly generated captcha word, and what characters to select from. + - :doc:`CAPTCHA Helper ` :php:func:`create_captcha` added word_length and pool options for setting length of randomly generated captcha word, and what characters to select from. - Database -- 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(-) 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(+) 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(-) 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(-) 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 3a66facb66d66aa8b1d564b0ad675240e629b041 Mon Sep 17 00:00:00 2001 From: ash Date: Sun, 14 Apr 2013 14:38:03 +0100 Subject: user guide newline. --- user_guide_src/source/helpers/captcha_helper.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/helpers/captcha_helper.rst b/user_guide_src/source/helpers/captcha_helper.rst index c6fb00280..ca24e011f 100644 --- a/user_guide_src/source/helpers/captcha_helper.rst +++ b/user_guide_src/source/helpers/captcha_helper.rst @@ -143,4 +143,4 @@ this:: if ($row->count == 0) {      echo 'You must submit the word that appears in the image.'; - } + } \ 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(-) 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