From f1b43d43385c81788dab9103a4f9be7e02432852 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 6 Jan 2012 14:41:50 +0200 Subject: Improve array, captcha & cookie helpers --- system/helpers/array_helper.php | 27 ++++---------- system/helpers/captcha_helper.php | 78 ++++++++++++++------------------------- system/helpers/cookie_helper.php | 17 +++------ 3 files changed, 39 insertions(+), 83 deletions(-) diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index c46c4d103..881f57db3 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -1,13 +1,13 @@ - $val) { - if ( ! is_array($data)) + if ( ! is_array($data) && ( ! isset($$key) OR $$key == '')) { - if ( ! isset($$key) OR $$key == '') - { - $$key = $val; - } + $$key = $val; } else { @@ -70,22 +67,7 @@ if ( ! function_exists('create_captcha')) } } - if ($img_path == '' OR $img_url == '') - { - return FALSE; - } - - if ( ! @is_dir($img_path)) - { - return FALSE; - } - - if ( ! is_writable($img_path)) - { - return FALSE; - } - - if ( ! extension_loaded('gd')) + if ($img_path == '' OR $img_url == '' OR ! @is_dir($img_path) OR ! is_writeable($img_path) OR ! extension_loaded('gd')) { return FALSE; } @@ -94,21 +76,16 @@ if ( ! function_exists('create_captcha')) // Remove old images // ----------------------------------- - list($usec, $sec) = explode(" ", microtime()); + list($usec, $sec) = explode(' ', microtime()); $now = ((float)$usec + (float)$sec); $current_dir = @opendir($img_path); - while ($filename = @readdir($current_dir)) { - if ($filename != "." and $filename != ".." and $filename != "index.html") + if ($filename !== '.' && $filename !== '..' && $filename !== 'index.html' + && (str_replace('.jpg', '', $filename) + $expiration) < $now) { - $name = str_replace(".jpg", "", $filename); - - if (($name + $expiration) < $now) - { - @unlink($img_path.$filename); - } + @unlink($img_path.$filename); } } @@ -118,18 +95,17 @@ if ( ! function_exists('create_captcha')) // Do we have a "word" yet? // ----------------------------------- - if ($word == '') - { + if ($word == '') + { $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - $str = ''; - for ($i = 0; $i < 8; $i++) + $word = ''; + for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++) { - $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1); + $word .= substr($pool, mt_rand(0, $mt_rand_max), 1); } - $word = $str; - } + } // ----------------------------------- // Determine angle and position @@ -138,7 +114,7 @@ if ( ! function_exists('create_captcha')) $length = strlen($word); $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; $x_axis = rand(6, (360/$length)-16); - $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height); + $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height); // ----------------------------------- // Create image @@ -180,18 +156,18 @@ if ( ! function_exists('create_captcha')) $circles = 20; $points = 32; - for ($i = 0; $i < ($circles * $points) - 1; $i++) + for ($i = 0, $cp = $circles * $points; $i < $cp - 1; $i++) { - $theta = $theta + $thetac; - $rad = $radius * ($i / $points ); + $theta += $thetac; + $rad = $radius * ($i / $points); $x = ($rad * cos($theta)) + $x_axis; $y = ($rad * sin($theta)) + $y_axis; - $theta = $theta + $thetac; + $theta += $thetac; $rad1 = $radius * (($i + 1) / $points); $x1 = ($rad1 * cos($theta)) + $x_axis; - $y1 = ($rad1 * sin($theta )) + $y_axis; + $y1 = ($rad1 * sin($theta)) + $y_axis; imageline($im, $x, $y, $x1, $y1, $grid_color); - $theta = $theta - $thetac; + $theta -= $thetac; } // ----------------------------------- @@ -213,18 +189,18 @@ if ( ! function_exists('create_captcha')) $y = $font_size+2; } - for ($i = 0; $i < strlen($word); $i++) + for ($i = 0; $i < $length; $i++) { if ($use_font == FALSE) { $y = rand(0 , $img_height/2); - imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color); + imagestring($im, $font_size, $x, $y, $word[$i], $text_color); $x += ($font_size*2); } else { $y = rand($img_height/2, $img_height-3); - imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1)); + imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, $word[$i]); $x += $font_size; } } @@ -255,4 +231,4 @@ 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 */ diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php index 7b439c47f..d6f836670 100644 --- a/system/helpers/cookie_helper.php +++ b/system/helpers/cookie_helper.php @@ -1,13 +1,13 @@ -input->cookie($prefix.$index, $xss_clean); } @@ -110,6 +104,5 @@ if ( ! function_exists('delete_cookie')) } } - /* End of file cookie_helper.php */ -/* Location: ./system/helpers/cookie_helper.php */ \ No newline at end of file +/* Location: ./system/helpers/cookie_helper.php */ -- cgit v1.2.3-24-g4f1b From 4f2933d28370ad965059222553d8ace1dd2fe602 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 8 Jan 2012 06:49:49 +0200 Subject: Some more misc. stuff --- system/helpers/array_helper.php | 2 -- system/helpers/captcha_helper.php | 37 +++++++------------------------------ 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index 881f57db3..a454f936d 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * CodeIgniter Array Helpers * diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 40b6e5b23..89bff2366 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -67,7 +67,9 @@ if ( ! function_exists('create_captcha')) } } - if ($img_path == '' OR $img_url == '' OR ! @is_dir($img_path) OR ! is_writeable($img_path) OR ! extension_loaded('gd')) + if ($img_path == '' OR $img_url == '' + OR ! @is_dir($img_path) OR ! is_writeable($img_path) + OR ! extension_loaded('gd')) { return FALSE; } @@ -98,11 +100,10 @@ if ( ! function_exists('create_captcha')) if ($word == '') { $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - $word = ''; for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++) { - $word .= substr($pool, mt_rand(0, $mt_rand_max), 1); + $word .= $pool[mt_rand(0, $mt_rand_max)]; } } @@ -110,46 +111,32 @@ if ( ! function_exists('create_captcha')) // ----------------------------------- // Determine angle and position // ----------------------------------- - $length = strlen($word); $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; $x_axis = rand(6, (360/$length)-16); $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height); - // ----------------------------------- // Create image - // ----------------------------------- - // PHP.net recommends imagecreatetruecolor(), but it isn't always available - if (function_exists('imagecreatetruecolor')) - { - $im = imagecreatetruecolor($img_width, $img_height); - } - else - { - $im = imagecreate($img_width, $img_height); - } + $im = function_exists('imagecreatetruecolor') + ? imagecreatetruecolor($img_width, $img_height) + : imagecreate($img_width, $img_height); // ----------------------------------- // Assign colors // ----------------------------------- - $bg_color = imagecolorallocate ($im, 255, 255, 255); $border_color = imagecolorallocate ($im, 153, 102, 102); $text_color = imagecolorallocate ($im, 204, 153, 153); $grid_color = imagecolorallocate($im, 255, 182, 182); $shadow_color = imagecolorallocate($im, 255, 240, 240); - // ----------------------------------- // Create the rectangle - // ----------------------------------- - ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); // ----------------------------------- // Create the spiral pattern // ----------------------------------- - $theta = 1; $thetac = 7; $radius = 16; @@ -173,7 +160,6 @@ if ( ! function_exists('create_captcha')) // ----------------------------------- // Write the text // ----------------------------------- - $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE; if ($use_font == FALSE) @@ -206,29 +192,20 @@ if ( ! function_exists('create_captcha')) } - // ----------------------------------- // Create the border - // ----------------------------------- - imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color); // ----------------------------------- // Generate the image // ----------------------------------- - $img_name = $now.'.jpg'; - ImageJPEG($im, $img_path.$img_name); - $img = "\""; - ImageDestroy($im); return array('word' => $word, 'time' => $now, 'image' => $img); } } -// ------------------------------------------------------------------------ - /* End of file captcha_helper.php */ /* Location: ./system/helpers/captcha_helper.php */ -- cgit v1.2.3-24-g4f1b From 63a21005ec657aa004b5ffc2b0965c1a201e4637 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 19 Jan 2012 15:13:32 +0200 Subject: Some more cleaning --- system/helpers/array_helper.php | 13 ++++--------- system/helpers/captcha_helper.php | 8 ++++---- system/helpers/cookie_helper.php | 4 ++-- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index a454f936d..82f0eb16c 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it @@ -53,12 +53,7 @@ if ( ! function_exists('element')) { function element($item, $array, $default = FALSE) { - if ( ! isset($array[$item]) OR $array[$item] == "") - { - return $default; - } - - return $array[$item]; + return ( ! isset($array[$item]) OR $array[$item] == '') ? $default : $array[$item]; } } @@ -75,7 +70,7 @@ if ( ! function_exists('random_element')) { function random_element($array) { - return (is_array($array)) ? $array[array_rand($array)] : $array; + return is_array($array) ? $array[array_rand($array)] : $array; } } @@ -105,7 +100,7 @@ if ( ! function_exists('elements')) foreach ($items as $item) { - $return[$item] = (isset($array[$item])) ? $array[$item] : $default; + $return[$item] = isset($array[$item]) ? $array[$item] : $default; } return $return; diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 89bff2366..0565457e2 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it @@ -170,7 +170,7 @@ if ( ! function_exists('create_captcha')) } else { - $font_size = 16; + $font_size = 16; $x = rand(0, $img_width/($length/1.5)); $y = $font_size+2; } @@ -192,7 +192,7 @@ if ( ! function_exists('create_captcha')) } - // Create the border + // Create the border imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color); // ----------------------------------- @@ -200,7 +200,7 @@ if ( ! function_exists('create_captcha')) // ----------------------------------- $img_name = $now.'.jpg'; ImageJPEG($im, $img_path.$img_name); - $img = "\""; + $img = ' '; ImageDestroy($im); return array('word' => $word, 'time' => $now, 'image' => $img); diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php index d6f836670..52f489b39 100644 --- a/system/helpers/cookie_helper.php +++ b/system/helpers/cookie_helper.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it @@ -91,7 +91,7 @@ if ( ! function_exists('get_cookie')) * Delete a COOKIE * * @param mixed - * @param string the cookie domain. Usually: .yourdomain.com + * @param string the cookie domain. Usually: .yourdomain.com * @param string the cookie path * @param string the cookie prefix * @return void -- cgit v1.2.3-24-g4f1b From 5ad9e4ebba4ab8a8eb36952430d5a18978697360 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 20 Jan 2012 13:10:22 +0200 Subject: Replace AND with && --- system/helpers/captcha_helper.php | 4 +--- system/helpers/cookie_helper.php | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 0565457e2..3b62da929 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * CodeIgniter CAPTCHA Helper * @@ -160,7 +158,7 @@ if ( ! function_exists('create_captcha')) // ----------------------------------- // Write the text // ----------------------------------- - $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE; + $use_font = ($font_path != '' && file_exists($font_path) && function_exists('imagettftext')); if ($use_font == FALSE) { diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php index 52f489b39..f32a1a5ae 100644 --- a/system/helpers/cookie_helper.php +++ b/system/helpers/cookie_helper.php @@ -25,8 +25,6 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** * CodeIgniter Cookie Helpers * -- cgit v1.2.3-24-g4f1b From 51a22815994cfd4522c78177e41f064ab8e53415 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 24 Jan 2012 15:27:34 +0200 Subject: Revert a space in the license agreement :) --- system/helpers/array_helper.php | 2 +- system/helpers/captcha_helper.php | 2 +- system/helpers/cookie_helper.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index 82f0eb16c..45eaa4c10 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 3b62da929..c302a828b 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php index f32a1a5ae..af2e1df80 100644 --- a/system/helpers/cookie_helper.php +++ b/system/helpers/cookie_helper.php @@ -9,7 +9,7 @@ * Licensed under the Open Software License version 3.0 * * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is + * bundled with this package in the files license.txt / license.rst. It is * also available through the world wide web at this URL: * http://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to obtain it -- cgit v1.2.3-24-g4f1b From 6b47711e85671fcfe1bd08ce80d23ca2e91e8e55 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 13 Mar 2012 12:24:07 +0200 Subject: Remove access description line --- 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 9ae959fbe..7dc5b3eec 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -40,7 +40,6 @@ /** * Create CAPTCHA * - * @access public * @param array array of data for the CAPTCHA * @param string path to create the image in * @param string URL to the CAPTCHA image folder -- cgit v1.2.3-24-g4f1b From e6f7d610b189e243ad48dcc3900a5c53cab2498d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 20 Mar 2012 15:39:22 +0200 Subject: Remove EOF newlines --- system/helpers/array_helper.php | 2 +- system/helpers/captcha_helper.php | 2 +- system/helpers/cookie_helper.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/system/helpers/array_helper.php b/system/helpers/array_helper.php index 464d1d112..6f56d9db9 100644 --- a/system/helpers/array_helper.php +++ b/system/helpers/array_helper.php @@ -103,4 +103,4 @@ if ( ! function_exists('elements')) } /* End of file array_helper.php */ -/* Location: ./system/helpers/array_helper.php */ +/* Location: ./system/helpers/array_helper.php */ \ No newline at end of file diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 7dc5b3eec..96e8c51a3 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -204,4 +204,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 diff --git a/system/helpers/cookie_helper.php b/system/helpers/cookie_helper.php index ec8aa3250..06560e723 100644 --- a/system/helpers/cookie_helper.php +++ b/system/helpers/cookie_helper.php @@ -102,4 +102,4 @@ if ( ! function_exists('delete_cookie')) } /* End of file cookie_helper.php */ -/* Location: ./system/helpers/cookie_helper.php */ +/* Location: ./system/helpers/cookie_helper.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 33101f42a8111a6e070cae495ec13e1e79207997 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 26 Mar 2012 21:53:37 +0300 Subject: Merge upstream branch --- system/helpers/captcha_helper.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index 96e8c51a3..bdc3910db 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -101,7 +101,6 @@ if ( ! function_exists('create_captcha')) { $word .= $pool[mt_rand(0, $mt_rand_max)]; } - } // ----------------------------------- @@ -121,10 +120,10 @@ if ( ! function_exists('create_captcha')) // ----------------------------------- // Assign colors // ----------------------------------- - $bg_color = imagecolorallocate ($im, 255, 255, 255); - $border_color = imagecolorallocate ($im, 153, 102, 102); - $text_color = imagecolorallocate ($im, 204, 153, 153); - $grid_color = imagecolorallocate($im, 255, 182, 182); + $bg_color = imagecolorallocate($im, 255, 255, 255); + $border_color = imagecolorallocate($im, 153, 102, 102); + $text_color = imagecolorallocate($im, 204, 153, 153); + $grid_color = imagecolorallocate($im, 255, 182, 182); $shadow_color = imagecolorallocate($im, 255, 240, 240); // Create the rectangle -- cgit v1.2.3-24-g4f1b From a720961bcfa6b70b8d672b4861f8665763fc881a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 26 Mar 2012 21:55:43 +0300 Subject: Some more minor improvements --- system/helpers/captcha_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/helpers/captcha_helper.php b/system/helpers/captcha_helper.php index bdc3910db..1e6ee3c07 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -54,13 +54,13 @@ if ( ! function_exists('create_captcha')) foreach ($defaults as $key => $val) { - if ( ! is_array($data) && ( ! isset($$key) OR $$key == '')) + if ( ! is_array($data) && empty($$key)) { $$key = $val; } else { - $$key = ( ! isset($data[$key])) ? $val : $data[$key]; + $$key = isset($data[$key]) ? $data[$key] : $val; } } -- cgit v1.2.3-24-g4f1b From 3419db1a5cc949e578dc5883d87a2ae45c375188 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 26 Mar 2012 22:00:07 +0300 Subject: Further improve a for() loop --- 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 1e6ee3c07..bdbc62097 100644 --- a/system/helpers/captcha_helper.php +++ b/system/helpers/captcha_helper.php @@ -138,7 +138,7 @@ if ( ! function_exists('create_captcha')) $circles = 20; $points = 32; - for ($i = 0, $cp = $circles * $points; $i < $cp - 1; $i++) + for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++) { $theta += $thetac; $rad = $radius * ($i / $points); -- cgit v1.2.3-24-g4f1b