blob: 0fed81996a35d510566c75b89b5e831d92e4f15e [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
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 Jonesb41032d2010-03-05 11:22:45 -060019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @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 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
Derek Jonesb41032d2010-03-05 11:22:45 -060097 list($usec, $sec) = explode(" ", microtime());
98 $now = ((float)$usec + (float)$sec);
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Jonesb41032d2010-03-05 11:22:45 -0600100 $current_dir = @opendir($img_path);
Barry Mienydd671972010-10-04 16:33:58 +0200101
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500102 while ($filename = @readdir($current_dir))
Derek Jonesb41032d2010-03-05 11:22:45 -0600103 {
104 if ($filename != "." and $filename != ".." and $filename != "index.html")
105 {
106 $name = str_replace(".jpg", "", $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Jonesb41032d2010-03-05 11:22:45 -0600108 if (($name + $expiration) < $now)
109 {
110 @unlink($img_path.$filename);
111 }
112 }
113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Jonesb41032d2010-03-05 11:22:45 -0600115 @closedir($current_dir);
116
117 // -----------------------------------
118 // Do we have a "word" yet?
119 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Jones37f4b9c2011-07-01 17:56:50 -0500121 if ($word == '')
122 {
Derek Jonesb41032d2010-03-05 11:22:45 -0600123 $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 Mienydd671972010-10-04 16:33:58 +0200130
Derek Jonesb41032d2010-03-05 11:22:45 -0600131 $word = $str;
Derek Jones37f4b9c2011-07-01 17:56:50 -0500132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Jonesb41032d2010-03-05 11:22:45 -0600134 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600136 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Jonesb41032d2010-03-05 11:22:45 -0600138 $length = strlen($word);
139 $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200140 $x_axis = rand(6, (360/$length)-16);
Derek Jonesb41032d2010-03-05 11:22:45 -0600141 $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Jonesb41032d2010-03-05 11:22:45 -0600143 // -----------------------------------
144 // Create image
145 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jonesb41032d2010-03-05 11:22:45 -0600147 // 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 Mienydd671972010-10-04 16:33:58 +0200156
Derek Jonesb41032d2010-03-05 11:22:45 -0600157 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500158 // Assign colors
Derek Jonesb41032d2010-03-05 11:22:45 -0600159 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Jonesb41032d2010-03-05 11:22:45 -0600161 $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 Jones37f4b9c2011-07-01 17:56:50 -0500168 // Create the rectangle
Derek Jonesb41032d2010-03-05 11:22:45 -0600169 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Jonesb41032d2010-03-05 11:22:45 -0600171 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Jonesb41032d2010-03-05 11:22:45 -0600173 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500174 // Create the spiral pattern
Derek Jonesb41032d2010-03-05 11:22:45 -0600175 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Jonesb41032d2010-03-05 11:22:45 -0600177 $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 Jones37f4b9c2011-07-01 17:56:50 -0500198 // Write the text
Derek Jonesb41032d2010-03-05 11:22:45 -0600199 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Jonesb41032d2010-03-05 11:22:45 -0600201 $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Jonesb41032d2010-03-05 11:22:45 -0600203 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 Mienydd671972010-10-04 16:33:58 +0200225 {
Derek Jonesb41032d2010-03-05 11:22:45 -0600226 $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 Mienydd671972010-10-04 16:33:58 +0200231
Derek Jonesb41032d2010-03-05 11:22:45 -0600232
233 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500234 // Create the border
Derek Jonesb41032d2010-03-05 11:22:45 -0600235 // -----------------------------------
236
Barry Mienydd671972010-10-04 16:33:58 +0200237 imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
Derek Jonesb41032d2010-03-05 11:22:45 -0600238
239 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 // Generate the image
Derek Jonesb41032d2010-03-05 11:22:45 -0600241 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Jonesb41032d2010-03-05 11:22:45 -0600243 $img_name = $now.'.jpg';
244
245 ImageJPEG($im, $img_path.$img_name);
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Jonesb41032d2010-03-05 11:22:45 -0600247 $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Jonesb41032d2010-03-05 11:22:45 -0600249 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Jonesb41032d2010-03-05 11:22:45 -0600251 return array('word' => $word, 'time' => $now, 'image' => $img);
252 }
253}
254
255// ------------------------------------------------------------------------
256
Derek Jones1c73f4e2010-03-05 11:27:41 -0600257/* End of file captcha_helper.php */
Bo-Yi Wud3a32a22011-10-13 10:31:43 +0800258/* Location: ./system/helpers/captcha_helper.php */