blob: 40b6e5b23a114ef2d8aae92905cc145e870eedb9 [file] [log] [blame]
Andrey Andreevf1b43d42012-01-06 14:41:50 +02001<?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
Andrey Andreevf1b43d42012-01-06 14:41:50 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevf1b43d42012-01-06 14:41:50 +020010 *
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 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020060 if ( ! is_array($data) && ( ! isset($$key) OR $$key == ''))
Derek Jonesb41032d2010-03-05 11:22:45 -060061 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020062 $$key = $val;
Derek Jonesb41032d2010-03-05 11:22:45 -060063 }
64 else
Barry Mienydd671972010-10-04 16:33:58 +020065 {
Derek Jonesb41032d2010-03-05 11:22:45 -060066 $$key = ( ! isset($data[$key])) ? $val : $data[$key];
67 }
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Andrey Andreevf1b43d42012-01-06 14:41:50 +020070 if ($img_path == '' OR $img_url == '' OR ! @is_dir($img_path) OR ! is_writeable($img_path) OR ! extension_loaded('gd'))
Derek Jonesb41032d2010-03-05 11:22:45 -060071 {
72 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020073 }
74
Derek Jonesb41032d2010-03-05 11:22:45 -060075 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020076 // Remove old images
Derek Jonesb41032d2010-03-05 11:22:45 -060077 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020078
Andrey Andreevf1b43d42012-01-06 14:41:50 +020079 list($usec, $sec) = explode(' ', microtime());
Derek Jonesb41032d2010-03-05 11:22:45 -060080 $now = ((float)$usec + (float)$sec);
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Jonesb41032d2010-03-05 11:22:45 -060082 $current_dir = @opendir($img_path);
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050083 while ($filename = @readdir($current_dir))
Derek Jonesb41032d2010-03-05 11:22:45 -060084 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020085 if ($filename !== '.' && $filename !== '..' && $filename !== 'index.html'
86 && (str_replace('.jpg', '', $filename) + $expiration) < $now)
Derek Jonesb41032d2010-03-05 11:22:45 -060087 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020088 @unlink($img_path.$filename);
Derek Jonesb41032d2010-03-05 11:22:45 -060089 }
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Jonesb41032d2010-03-05 11:22:45 -060092 @closedir($current_dir);
93
94 // -----------------------------------
95 // Do we have a "word" yet?
96 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020097
Andrey Andreevf1b43d42012-01-06 14:41:50 +020098 if ($word == '')
99 {
Derek Jonesb41032d2010-03-05 11:22:45 -0600100 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
101
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200102 $word = '';
103 for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600104 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200105 $word .= substr($pool, mt_rand(0, $mt_rand_max), 1);
Derek Jonesb41032d2010-03-05 11:22:45 -0600106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Jonesb41032d2010-03-05 11:22:45 -0600110 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200111 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600112 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Jonesb41032d2010-03-05 11:22:45 -0600114 $length = strlen($word);
115 $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200116 $x_axis = rand(6, (360/$length)-16);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200117 $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Jonesb41032d2010-03-05 11:22:45 -0600119 // -----------------------------------
120 // Create image
121 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Jonesb41032d2010-03-05 11:22:45 -0600123 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
124 if (function_exists('imagecreatetruecolor'))
125 {
126 $im = imagecreatetruecolor($img_width, $img_height);
127 }
128 else
129 {
130 $im = imagecreate($img_width, $img_height);
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Jonesb41032d2010-03-05 11:22:45 -0600133 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500134 // Assign colors
Derek Jonesb41032d2010-03-05 11:22:45 -0600135 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Jonesb41032d2010-03-05 11:22:45 -0600137 $bg_color = imagecolorallocate ($im, 255, 255, 255);
138 $border_color = imagecolorallocate ($im, 153, 102, 102);
139 $text_color = imagecolorallocate ($im, 204, 153, 153);
140 $grid_color = imagecolorallocate($im, 255, 182, 182);
141 $shadow_color = imagecolorallocate($im, 255, 240, 240);
142
143 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500144 // Create the rectangle
Derek Jonesb41032d2010-03-05 11:22:45 -0600145 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jonesb41032d2010-03-05 11:22:45 -0600147 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Jonesb41032d2010-03-05 11:22:45 -0600149 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500150 // Create the spiral pattern
Derek Jonesb41032d2010-03-05 11:22:45 -0600151 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Jonesb41032d2010-03-05 11:22:45 -0600153 $theta = 1;
154 $thetac = 7;
155 $radius = 16;
156 $circles = 20;
157 $points = 32;
158
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200159 for ($i = 0, $cp = $circles * $points; $i < $cp - 1; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600160 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200161 $theta += $thetac;
162 $rad = $radius * ($i / $points);
Derek Jonesb41032d2010-03-05 11:22:45 -0600163 $x = ($rad * cos($theta)) + $x_axis;
164 $y = ($rad * sin($theta)) + $y_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200165 $theta += $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600166 $rad1 = $radius * (($i + 1) / $points);
167 $x1 = ($rad1 * cos($theta)) + $x_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200168 $y1 = ($rad1 * sin($theta)) + $y_axis;
Derek Jonesb41032d2010-03-05 11:22:45 -0600169 imageline($im, $x, $y, $x1, $y1, $grid_color);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200170 $theta -= $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600171 }
172
173 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500174 // Write the text
Derek Jonesb41032d2010-03-05 11:22:45 -0600175 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Jonesb41032d2010-03-05 11:22:45 -0600177 $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Jonesb41032d2010-03-05 11:22:45 -0600179 if ($use_font == FALSE)
180 {
181 $font_size = 5;
182 $x = rand(0, $img_width/($length/3));
183 $y = 0;
184 }
185 else
186 {
187 $font_size = 16;
188 $x = rand(0, $img_width/($length/1.5));
189 $y = $font_size+2;
190 }
191
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200192 for ($i = 0; $i < $length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600193 {
194 if ($use_font == FALSE)
195 {
196 $y = rand(0 , $img_height/2);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200197 imagestring($im, $font_size, $x, $y, $word[$i], $text_color);
Derek Jonesb41032d2010-03-05 11:22:45 -0600198 $x += ($font_size*2);
199 }
200 else
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Derek Jonesb41032d2010-03-05 11:22:45 -0600202 $y = rand($img_height/2, $img_height-3);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200203 imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, $word[$i]);
Derek Jonesb41032d2010-03-05 11:22:45 -0600204 $x += $font_size;
205 }
206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Jonesb41032d2010-03-05 11:22:45 -0600208
209 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500210 // Create the border
Derek Jonesb41032d2010-03-05 11:22:45 -0600211 // -----------------------------------
212
Barry Mienydd671972010-10-04 16:33:58 +0200213 imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
Derek Jonesb41032d2010-03-05 11:22:45 -0600214
215 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500216 // Generate the image
Derek Jonesb41032d2010-03-05 11:22:45 -0600217 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Jonesb41032d2010-03-05 11:22:45 -0600219 $img_name = $now.'.jpg';
220
221 ImageJPEG($im, $img_path.$img_name);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Jonesb41032d2010-03-05 11:22:45 -0600223 $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Jonesb41032d2010-03-05 11:22:45 -0600225 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Jonesb41032d2010-03-05 11:22:45 -0600227 return array('word' => $word, 'time' => $now, 'image' => $img);
228 }
229}
230
231// ------------------------------------------------------------------------
232
Derek Jones1c73f4e2010-03-05 11:27:41 -0600233/* End of file captcha_helper.php */
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200234/* Location: ./system/helpers/captcha_helper.php */