blob: 3c80bb75605d968b730f92e6d85df9ca7bcd7e2b [file] [log] [blame]
Derek Jonesb41032d2010-03-05 11:22:45 -06001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
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 */
40if ( ! function_exists('create_captcha'))
41{
42 function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
Barry Mienydd671972010-10-04 16:33:58 +020043 {
44 $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
45
Derek Jonesb41032d2010-03-05 11:22:45 -060046 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 Mienydd671972010-10-04 16:33:58 +020056 {
Derek Jonesb41032d2010-03-05 11:22:45 -060057 $$key = ( ! isset($data[$key])) ? $val : $data[$key];
58 }
59 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Jonesb41032d2010-03-05 11:22:45 -060061 if ($img_path == '' OR $img_url == '')
62 {
63 return FALSE;
64 }
65
66 if ( ! @is_dir($img_path))
67 {
68 return FALSE;
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Jonesb41032d2010-03-05 11:22:45 -060071 if ( ! is_writable($img_path))
72 {
73 return FALSE;
74 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Jonesb41032d2010-03-05 11:22:45 -060076 if ( ! extension_loaded('gd'))
77 {
78 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020079 }
80
Derek Jonesb41032d2010-03-05 11:22:45 -060081 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020082 // Remove old images
Derek Jonesb41032d2010-03-05 11:22:45 -060083 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Jonesb41032d2010-03-05 11:22:45 -060085 list($usec, $sec) = explode(" ", microtime());
86 $now = ((float)$usec + (float)$sec);
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Jonesb41032d2010-03-05 11:22:45 -060088 $current_dir = @opendir($img_path);
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Jonesb41032d2010-03-05 11:22:45 -060090 while($filename = @readdir($current_dir))
91 {
92 if ($filename != "." and $filename != ".." and $filename != "index.html")
93 {
94 $name = str_replace(".jpg", "", $filename);
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Jonesb41032d2010-03-05 11:22:45 -060096 if (($name + $expiration) < $now)
97 {
98 @unlink($img_path.$filename);
99 }
100 }
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Jonesb41032d2010-03-05 11:22:45 -0600103 @closedir($current_dir);
104
105 // -----------------------------------
106 // Do we have a "word" yet?
107 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Jonesb41032d2010-03-05 11:22:45 -0600109 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 Mienydd671972010-10-04 16:33:58 +0200118
Derek Jonesb41032d2010-03-05 11:22:45 -0600119 $word = $str;
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Jonesb41032d2010-03-05 11:22:45 -0600122 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200123 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600124 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Jonesb41032d2010-03-05 11:22:45 -0600126 $length = strlen($word);
127 $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200128 $x_axis = rand(6, (360/$length)-16);
Derek Jonesb41032d2010-03-05 11:22:45 -0600129 $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Jonesb41032d2010-03-05 11:22:45 -0600131 // -----------------------------------
132 // Create image
133 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Jonesb41032d2010-03-05 11:22:45 -0600135 // 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 Mienydd671972010-10-04 16:33:58 +0200144
Derek Jonesb41032d2010-03-05 11:22:45 -0600145 // -----------------------------------
146 // Assign colors
147 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Jonesb41032d2010-03-05 11:22:45 -0600149 $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 Mienydd671972010-10-04 16:33:58 +0200158
Derek Jonesb41032d2010-03-05 11:22:45 -0600159 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Jonesb41032d2010-03-05 11:22:45 -0600161 // -----------------------------------
162 // Create the spiral pattern
163 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Jonesb41032d2010-03-05 11:22:45 -0600165 $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 Mienydd671972010-10-04 16:33:58 +0200188
Derek Jonesb41032d2010-03-05 11:22:45 -0600189 $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Jonesb41032d2010-03-05 11:22:45 -0600191 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 Mienydd671972010-10-04 16:33:58 +0200213 {
Derek Jonesb41032d2010-03-05 11:22:45 -0600214 $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 Mienydd671972010-10-04 16:33:58 +0200219
Derek Jonesb41032d2010-03-05 11:22:45 -0600220
221 // -----------------------------------
222 // Create the border
223 // -----------------------------------
224
Barry Mienydd671972010-10-04 16:33:58 +0200225 imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
Derek Jonesb41032d2010-03-05 11:22:45 -0600226
227 // -----------------------------------
228 // Generate the image
229 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Jonesb41032d2010-03-05 11:22:45 -0600231 $img_name = $now.'.jpg';
232
233 ImageJPEG($im, $img_path.$img_name);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Jonesb41032d2010-03-05 11:22:45 -0600235 $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Jonesb41032d2010-03-05 11:22:45 -0600237 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Jonesb41032d2010-03-05 11:22:45 -0600239 return array('word' => $word, 'time' => $now, 'image' => $img);
240 }
241}
242
243// ------------------------------------------------------------------------
244
Derek Jones1c73f4e2010-03-05 11:27:41 -0600245/* End of file captcha_helper.php */
246/* Location: ./system/heleprs/captcha_helper.php */