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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 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 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 97 | list($usec, $sec) = explode(" ", microtime()); |
| 98 | $now = ((float)$usec + (float)$sec); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 100 | $current_dir = @opendir($img_path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 101 | |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 102 | while ($filename = @readdir($current_dir)) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 103 | { |
| 104 | if ($filename != "." and $filename != ".." and $filename != "index.html") |
| 105 | { |
| 106 | $name = str_replace(".jpg", "", $filename); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 108 | if (($name + $expiration) < $now) |
| 109 | { |
| 110 | @unlink($img_path.$filename); |
| 111 | } |
| 112 | } |
| 113 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 115 | @closedir($current_dir); |
| 116 | |
| 117 | // ----------------------------------- |
| 118 | // Do we have a "word" yet? |
| 119 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 121 | if ($word == '') |
| 122 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 123 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 124 | |
| 125 | $str = ''; |
| 126 | for ($i = 0; $i < 8; $i++) |
| 127 | { |
| 128 | $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1); |
| 129 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 131 | $word = $str; |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 132 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 134 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | // Determine angle and position |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 136 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 138 | $length = strlen($word); |
| 139 | $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 140 | $x_axis = rand(6, (360/$length)-16); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 141 | $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] | 142 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 143 | // ----------------------------------- |
| 144 | // Create image |
| 145 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 147 | // PHP.net recommends imagecreatetruecolor(), but it isn't always available |
| 148 | if (function_exists('imagecreatetruecolor')) |
| 149 | { |
| 150 | $im = imagecreatetruecolor($img_width, $img_height); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | $im = imagecreate($img_width, $img_height); |
| 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 157 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 158 | // Assign colors |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 159 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 161 | $bg_color = imagecolorallocate ($im, 255, 255, 255); |
| 162 | $border_color = imagecolorallocate ($im, 153, 102, 102); |
| 163 | $text_color = imagecolorallocate ($im, 204, 153, 153); |
| 164 | $grid_color = imagecolorallocate($im, 255, 182, 182); |
| 165 | $shadow_color = imagecolorallocate($im, 255, 240, 240); |
| 166 | |
| 167 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 168 | // Create the rectangle |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 169 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 170 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 171 | ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 173 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 174 | // Create the spiral pattern |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 175 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 177 | $theta = 1; |
| 178 | $thetac = 7; |
| 179 | $radius = 16; |
| 180 | $circles = 20; |
| 181 | $points = 32; |
| 182 | |
| 183 | for ($i = 0; $i < ($circles * $points) - 1; $i++) |
| 184 | { |
| 185 | $theta = $theta + $thetac; |
| 186 | $rad = $radius * ($i / $points ); |
| 187 | $x = ($rad * cos($theta)) + $x_axis; |
| 188 | $y = ($rad * sin($theta)) + $y_axis; |
| 189 | $theta = $theta + $thetac; |
| 190 | $rad1 = $radius * (($i + 1) / $points); |
| 191 | $x1 = ($rad1 * cos($theta)) + $x_axis; |
| 192 | $y1 = ($rad1 * sin($theta )) + $y_axis; |
| 193 | imageline($im, $x, $y, $x1, $y1, $grid_color); |
| 194 | $theta = $theta - $thetac; |
| 195 | } |
| 196 | |
| 197 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 198 | // Write the text |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 199 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 201 | $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 203 | if ($use_font == FALSE) |
| 204 | { |
| 205 | $font_size = 5; |
| 206 | $x = rand(0, $img_width/($length/3)); |
| 207 | $y = 0; |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | $font_size = 16; |
| 212 | $x = rand(0, $img_width/($length/1.5)); |
| 213 | $y = $font_size+2; |
| 214 | } |
| 215 | |
| 216 | for ($i = 0; $i < strlen($word); $i++) |
| 217 | { |
| 218 | if ($use_font == FALSE) |
| 219 | { |
| 220 | $y = rand(0 , $img_height/2); |
| 221 | imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color); |
| 222 | $x += ($font_size*2); |
| 223 | } |
| 224 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 226 | $y = rand($img_height/2, $img_height-3); |
| 227 | imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1)); |
| 228 | $x += $font_size; |
| 229 | } |
| 230 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 232 | |
| 233 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 234 | // Create the border |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 235 | // ----------------------------------- |
| 236 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 237 | imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 238 | |
| 239 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 240 | // Generate the image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 241 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 243 | $img_name = $now.'.jpg'; |
| 244 | |
| 245 | ImageJPEG($im, $img_path.$img_name); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 246 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 247 | $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] | 248 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 249 | ImageDestroy($im); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 251 | return array('word' => $word, 'time' => $now, 'image' => $img); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // ------------------------------------------------------------------------ |
| 256 | |
Derek Jones | 1c73f4e | 2010-03-05 11:27:41 -0600 | [diff] [blame] | 257 | /* End of file captcha_helper.php */ |
Bo-Yi Wu | d3a32a2 | 2011-10-13 10:31:43 +0800 | [diff] [blame] | 258 | /* Location: ./system/helpers/captcha_helper.php */ |