Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter CAPTCHA Helper |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Helpers |
| 35 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/helpers/xml_helper.html |
| 38 | */ |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | /** |
| 43 | * Create CAPTCHA |
| 44 | * |
| 45 | * @access public |
| 46 | * @param array array of data for the CAPTCHA |
| 47 | * @param string path to create the image in |
| 48 | * @param string URL to the CAPTCHA image folder |
| 49 | * @param string server path to font |
| 50 | * @return string |
| 51 | */ |
| 52 | if ( ! function_exists('create_captcha')) |
| 53 | { |
| 54 | function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | { |
| 56 | $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200); |
| 57 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 58 | foreach ($defaults as $key => $val) |
| 59 | { |
| 60 | if ( ! is_array($data)) |
| 61 | { |
| 62 | if ( ! isset($$key) OR $$key == '') |
| 63 | { |
| 64 | $$key = $val; |
| 65 | } |
| 66 | } |
| 67 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 69 | $$key = ( ! isset($data[$key])) ? $val : $data[$key]; |
| 70 | } |
| 71 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 73 | if ($img_path == '' OR $img_url == '') |
| 74 | { |
| 75 | return FALSE; |
| 76 | } |
| 77 | |
| 78 | if ( ! @is_dir($img_path)) |
| 79 | { |
| 80 | return FALSE; |
| 81 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 83 | if ( ! is_writable($img_path)) |
| 84 | { |
| 85 | return FALSE; |
| 86 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 88 | if ( ! extension_loaded('gd')) |
| 89 | { |
| 90 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 93 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | // Remove old images |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 95 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame^] | 97 | $now = microtime(TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 98 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 99 | $current_dir = @opendir($img_path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 100 | |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 101 | while ($filename = @readdir($current_dir)) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 102 | { |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame^] | 103 | if ($filename != '.' && $filename != '..' && $filename != 'index.html') |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 104 | { |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame^] | 105 | $name = str_replace('.jpg', '', $filename); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 107 | if (($name + $expiration) < $now) |
| 108 | { |
| 109 | @unlink($img_path.$filename); |
| 110 | } |
| 111 | } |
| 112 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 114 | @closedir($current_dir); |
| 115 | |
| 116 | // ----------------------------------- |
| 117 | // Do we have a "word" yet? |
| 118 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 120 | if ($word == '') |
| 121 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 122 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 123 | |
| 124 | $str = ''; |
| 125 | for ($i = 0; $i < 8; $i++) |
| 126 | { |
| 127 | $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1); |
| 128 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 130 | $word = $str; |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 131 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 133 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 134 | // Determine angle and position |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 135 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 137 | $length = strlen($word); |
| 138 | $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | $x_axis = rand(6, (360/$length)-16); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 140 | $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 142 | // ----------------------------------- |
| 143 | // Create image |
| 144 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 146 | // PHP.net recommends imagecreatetruecolor(), but it isn't always available |
| 147 | if (function_exists('imagecreatetruecolor')) |
| 148 | { |
| 149 | $im = imagecreatetruecolor($img_width, $img_height); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | $im = imagecreate($img_width, $img_height); |
| 154 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 156 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 157 | // Assign colors |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 158 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 159 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 160 | $bg_color = imagecolorallocate ($im, 255, 255, 255); |
| 161 | $border_color = imagecolorallocate ($im, 153, 102, 102); |
| 162 | $text_color = imagecolorallocate ($im, 204, 153, 153); |
| 163 | $grid_color = imagecolorallocate($im, 255, 182, 182); |
| 164 | $shadow_color = imagecolorallocate($im, 255, 240, 240); |
| 165 | |
| 166 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 167 | // Create the rectangle |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 168 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 170 | ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 172 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 173 | // Create the spiral pattern |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 174 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 176 | $theta = 1; |
| 177 | $thetac = 7; |
| 178 | $radius = 16; |
| 179 | $circles = 20; |
| 180 | $points = 32; |
| 181 | |
| 182 | for ($i = 0; $i < ($circles * $points) - 1; $i++) |
| 183 | { |
| 184 | $theta = $theta + $thetac; |
| 185 | $rad = $radius * ($i / $points ); |
| 186 | $x = ($rad * cos($theta)) + $x_axis; |
| 187 | $y = ($rad * sin($theta)) + $y_axis; |
| 188 | $theta = $theta + $thetac; |
| 189 | $rad1 = $radius * (($i + 1) / $points); |
| 190 | $x1 = ($rad1 * cos($theta)) + $x_axis; |
| 191 | $y1 = ($rad1 * sin($theta )) + $y_axis; |
| 192 | imageline($im, $x, $y, $x1, $y1, $grid_color); |
| 193 | $theta = $theta - $thetac; |
| 194 | } |
| 195 | |
| 196 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 197 | // Write the text |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 198 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame^] | 200 | $use_font = ($font_path != '' && file_exists($font_path) && function_exists('imagettftext')) ? TRUE : FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 202 | if ($use_font == FALSE) |
| 203 | { |
| 204 | $font_size = 5; |
| 205 | $x = rand(0, $img_width/($length/3)); |
| 206 | $y = 0; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | $font_size = 16; |
| 211 | $x = rand(0, $img_width/($length/1.5)); |
| 212 | $y = $font_size+2; |
| 213 | } |
| 214 | |
| 215 | for ($i = 0; $i < strlen($word); $i++) |
| 216 | { |
| 217 | if ($use_font == FALSE) |
| 218 | { |
| 219 | $y = rand(0 , $img_height/2); |
| 220 | imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color); |
| 221 | $x += ($font_size*2); |
| 222 | } |
| 223 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 225 | $y = rand($img_height/2, $img_height-3); |
| 226 | imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1)); |
| 227 | $x += $font_size; |
| 228 | } |
| 229 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 231 | |
| 232 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 233 | // Create the border |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 234 | // ----------------------------------- |
| 235 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 237 | |
| 238 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 239 | // Generate the image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 240 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 242 | $img_name = $now.'.jpg'; |
| 243 | |
| 244 | ImageJPEG($im, $img_path.$img_name); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 246 | $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 248 | ImageDestroy($im); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 249 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 250 | return array('word' => $word, 'time' => $now, 'image' => $img); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // ------------------------------------------------------------------------ |
| 255 | |
Derek Jones | 1c73f4e | 2010-03-05 11:27:41 -0600 | [diff] [blame] | 256 | /* End of file captcha_helper.php */ |
Bo-Yi Wu | d3a32a2 | 2011-10-13 10:31:43 +0800 | [diff] [blame] | 257 | /* Location: ./system/helpers/captcha_helper.php */ |