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