blob: 4a48df27eac8697680d1c0fb2229dc3174f63815 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jonesb41032d2010-03-05 11:22:45 -06002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Jonesb41032d2010-03-05 11:22:45 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Michiel Vugteveenc2659b82012-03-07 21:34:52 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Michiel Vugteveenc2659b82012-03-07 21:34:52 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Jonesb41032d2010-03-05 11:22:45 -060019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Jonesb41032d2010-03-05 11:22:45 -060023 * @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 Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Jonesb41032d2010-03-05 11:22:45 -060037 * @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 */
52if ( ! function_exists('create_captcha'))
53{
54 function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
Barry Mienydd671972010-10-04 16:33:58 +020055 {
56 $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
57
Derek Jonesb41032d2010-03-05 11:22:45 -060058 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 Mienydd671972010-10-04 16:33:58 +020068 {
Derek Jonesb41032d2010-03-05 11:22:45 -060069 $$key = ( ! isset($data[$key])) ? $val : $data[$key];
70 }
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Jonesb41032d2010-03-05 11:22:45 -060073 if ($img_path == '' OR $img_url == '')
74 {
75 return FALSE;
76 }
77
78 if ( ! @is_dir($img_path))
79 {
80 return FALSE;
81 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Jonesb41032d2010-03-05 11:22:45 -060083 if ( ! is_writable($img_path))
84 {
85 return FALSE;
86 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Jonesb41032d2010-03-05 11:22:45 -060088 if ( ! extension_loaded('gd'))
89 {
90 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020091 }
92
Derek Jonesb41032d2010-03-05 11:22:45 -060093 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020094 // Remove old images
Derek Jonesb41032d2010-03-05 11:22:45 -060095 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020096
Michiel Vugteveenc2659b82012-03-07 21:34:52 +010097 $now = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Jonesb41032d2010-03-05 11:22:45 -060099 $current_dir = @opendir($img_path);
Barry Mienydd671972010-10-04 16:33:58 +0200100
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500101 while ($filename = @readdir($current_dir))
Derek Jonesb41032d2010-03-05 11:22:45 -0600102 {
Michiel Vugteveenc2659b82012-03-07 21:34:52 +0100103 if ($filename != '.' && $filename != '..' && $filename != 'index.html')
Derek Jonesb41032d2010-03-05 11:22:45 -0600104 {
Michiel Vugteveenc2659b82012-03-07 21:34:52 +0100105 $name = str_replace('.jpg', '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Jonesb41032d2010-03-05 11:22:45 -0600107 if (($name + $expiration) < $now)
108 {
109 @unlink($img_path.$filename);
110 }
111 }
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Jonesb41032d2010-03-05 11:22:45 -0600114 @closedir($current_dir);
115
116 // -----------------------------------
117 // Do we have a "word" yet?
118 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Jones37f4b9c2011-07-01 17:56:50 -0500120 if ($word == '')
121 {
Derek Jonesb41032d2010-03-05 11:22:45 -0600122 $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 Mienydd671972010-10-04 16:33:58 +0200129
Derek Jonesb41032d2010-03-05 11:22:45 -0600130 $word = $str;
Derek Jones37f4b9c2011-07-01 17:56:50 -0500131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Jonesb41032d2010-03-05 11:22:45 -0600133 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600135 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Jonesb41032d2010-03-05 11:22:45 -0600137 $length = strlen($word);
138 $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200139 $x_axis = rand(6, (360/$length)-16);
Derek Jonesb41032d2010-03-05 11:22:45 -0600140 $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Jonesb41032d2010-03-05 11:22:45 -0600142 // -----------------------------------
143 // Create image
144 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Jonesb41032d2010-03-05 11:22:45 -0600146 // 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 Mienydd671972010-10-04 16:33:58 +0200155
Derek Jonesb41032d2010-03-05 11:22:45 -0600156 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500157 // Assign colors
Derek Jonesb41032d2010-03-05 11:22:45 -0600158 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Jonesb41032d2010-03-05 11:22:45 -0600160 $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 Jones37f4b9c2011-07-01 17:56:50 -0500167 // Create the rectangle
Derek Jonesb41032d2010-03-05 11:22:45 -0600168 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Jonesb41032d2010-03-05 11:22:45 -0600170 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Jonesb41032d2010-03-05 11:22:45 -0600172 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500173 // Create the spiral pattern
Derek Jonesb41032d2010-03-05 11:22:45 -0600174 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jonesb41032d2010-03-05 11:22:45 -0600176 $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 Jones37f4b9c2011-07-01 17:56:50 -0500197 // Write the text
Derek Jonesb41032d2010-03-05 11:22:45 -0600198 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Michiel Vugteveenc2659b82012-03-07 21:34:52 +0100200 $use_font = ($font_path != '' && file_exists($font_path) && function_exists('imagettftext')) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Jonesb41032d2010-03-05 11:22:45 -0600202 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 Mienydd671972010-10-04 16:33:58 +0200224 {
Derek Jonesb41032d2010-03-05 11:22:45 -0600225 $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 Mienydd671972010-10-04 16:33:58 +0200230
Derek Jonesb41032d2010-03-05 11:22:45 -0600231
232 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500233 // Create the border
Derek Jonesb41032d2010-03-05 11:22:45 -0600234 // -----------------------------------
235
Barry Mienydd671972010-10-04 16:33:58 +0200236 imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
Derek Jonesb41032d2010-03-05 11:22:45 -0600237
238 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500239 // Generate the image
Derek Jonesb41032d2010-03-05 11:22:45 -0600240 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Jonesb41032d2010-03-05 11:22:45 -0600242 $img_name = $now.'.jpg';
243
244 ImageJPEG($im, $img_path.$img_name);
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Jonesb41032d2010-03-05 11:22:45 -0600246 $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Jonesb41032d2010-03-05 11:22:45 -0600248 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Jonesb41032d2010-03-05 11:22:45 -0600250 return array('word' => $word, 'time' => $now, 'image' => $img);
251 }
252}
253
254// ------------------------------------------------------------------------
255
Derek Jones1c73f4e2010-03-05 11:27:41 -0600256/* End of file captcha_helper.php */
Bo-Yi Wud3a32a22011-10-13 10:31:43 +0800257/* Location: ./system/helpers/captcha_helper.php */