blob: 865a6754d01328fdafb307751d884a2318835fb9 [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
Andrey Andreev51a22812012-01-24 15:27:34 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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
Derek Jonesb41032d2010-03-05 11:22:45 -060028/**
29 * CodeIgniter CAPTCHA Helper
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Jonesb41032d2010-03-05 11:22:45 -060035 * @link http://codeigniter.com/user_guide/helpers/xml_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
40/**
41 * Create CAPTCHA
42 *
43 * @access public
44 * @param array array of data for the CAPTCHA
45 * @param string path to create the image in
46 * @param string URL to the CAPTCHA image folder
47 * @param string server path to font
48 * @return string
49 */
50if ( ! function_exists('create_captcha'))
51{
52 function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
Barry Mienydd671972010-10-04 16:33:58 +020053 {
54 $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
55
Derek Jonesb41032d2010-03-05 11:22:45 -060056 foreach ($defaults as $key => $val)
57 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020058 if ( ! is_array($data) && ( ! isset($$key) OR $$key == ''))
Derek Jonesb41032d2010-03-05 11:22:45 -060059 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020060 $$key = $val;
Derek Jonesb41032d2010-03-05 11:22:45 -060061 }
62 else
Barry Mienydd671972010-10-04 16:33:58 +020063 {
Derek Jonesb41032d2010-03-05 11:22:45 -060064 $$key = ( ! isset($data[$key])) ? $val : $data[$key];
65 }
66 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Andrey Andreev4f2933d2012-01-08 06:49:49 +020068 if ($img_path == '' OR $img_url == ''
69 OR ! @is_dir($img_path) OR ! is_writeable($img_path)
70 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
Michiel Vugteveenc2659b82012-03-07 21:34:52 +010079 $now = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Jonesb41032d2010-03-05 11:22:45 -060081 $current_dir = @opendir($img_path);
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050082 while ($filename = @readdir($current_dir))
Derek Jonesb41032d2010-03-05 11:22:45 -060083 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020084 if ($filename !== '.' && $filename !== '..' && $filename !== 'index.html'
85 && (str_replace('.jpg', '', $filename) + $expiration) < $now)
Derek Jonesb41032d2010-03-05 11:22:45 -060086 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020087 @unlink($img_path.$filename);
Derek Jonesb41032d2010-03-05 11:22:45 -060088 }
89 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Jonesb41032d2010-03-05 11:22:45 -060091 @closedir($current_dir);
92
93 // -----------------------------------
94 // Do we have a "word" yet?
95 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020096
Andrey Andreevf1b43d42012-01-06 14:41:50 +020097 if ($word == '')
98 {
Derek Jonesb41032d2010-03-05 11:22:45 -060099 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200100 $word = '';
101 for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600102 {
Andrey Andreev4f2933d2012-01-08 06:49:49 +0200103 $word .= $pool[mt_rand(0, $mt_rand_max)];
Derek Jonesb41032d2010-03-05 11:22:45 -0600104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Jonesb41032d2010-03-05 11:22:45 -0600108 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200109 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600110 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600111 $length = strlen($word);
112 $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200113 $x_axis = rand(6, (360/$length)-16);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200114 $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Jonesb41032d2010-03-05 11:22:45 -0600116 // Create image
Derek Jonesb41032d2010-03-05 11:22:45 -0600117 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
Andrey Andreev4f2933d2012-01-08 06:49:49 +0200118 $im = function_exists('imagecreatetruecolor')
119 ? imagecreatetruecolor($img_width, $img_height)
120 : imagecreate($img_width, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Jonesb41032d2010-03-05 11:22:45 -0600122 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500123 // Assign colors
Derek Jonesb41032d2010-03-05 11:22:45 -0600124 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600125 $bg_color = imagecolorallocate ($im, 255, 255, 255);
126 $border_color = imagecolorallocate ($im, 153, 102, 102);
127 $text_color = imagecolorallocate ($im, 204, 153, 153);
128 $grid_color = imagecolorallocate($im, 255, 182, 182);
129 $shadow_color = imagecolorallocate($im, 255, 240, 240);
130
Derek Jones37f4b9c2011-07-01 17:56:50 -0500131 // Create the rectangle
Derek Jonesb41032d2010-03-05 11:22:45 -0600132 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Jonesb41032d2010-03-05 11:22:45 -0600134 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500135 // Create the spiral pattern
Derek Jonesb41032d2010-03-05 11:22:45 -0600136 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600137 $theta = 1;
138 $thetac = 7;
139 $radius = 16;
140 $circles = 20;
141 $points = 32;
142
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200143 for ($i = 0, $cp = $circles * $points; $i < $cp - 1; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600144 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200145 $theta += $thetac;
146 $rad = $radius * ($i / $points);
Derek Jonesb41032d2010-03-05 11:22:45 -0600147 $x = ($rad * cos($theta)) + $x_axis;
148 $y = ($rad * sin($theta)) + $y_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200149 $theta += $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600150 $rad1 = $radius * (($i + 1) / $points);
151 $x1 = ($rad1 * cos($theta)) + $x_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200152 $y1 = ($rad1 * sin($theta)) + $y_axis;
Derek Jonesb41032d2010-03-05 11:22:45 -0600153 imageline($im, $x, $y, $x1, $y1, $grid_color);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200154 $theta -= $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600155 }
156
157 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500158 // Write the text
Derek Jonesb41032d2010-03-05 11:22:45 -0600159 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreev7203d542012-03-08 01:42:59 +0200161 $use_font = ($font_path != '' && file_exists($font_path) && function_exists('imagettftext'));
162 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600163 {
164 $font_size = 5;
Andrey Andreev7203d542012-03-08 01:42:59 +0200165 $x = rand(0, $img_width / ($length / 3));
Derek Jonesb41032d2010-03-05 11:22:45 -0600166 $y = 0;
167 }
168 else
169 {
Andrey Andreev63a21002012-01-19 15:13:32 +0200170 $font_size = 16;
Andrey Andreev7203d542012-03-08 01:42:59 +0200171 $x = rand(0, $img_width / ($length / 1.5));
172 $y = $font_size + 2;
Derek Jonesb41032d2010-03-05 11:22:45 -0600173 }
174
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200175 for ($i = 0; $i < $length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600176 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200177 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600178 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200179 $y = rand(0 , $img_height / 2);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200180 imagestring($im, $font_size, $x, $y, $word[$i], $text_color);
Andrey Andreev7203d542012-03-08 01:42:59 +0200181 $x += ($font_size * 2);
Derek Jonesb41032d2010-03-05 11:22:45 -0600182 }
183 else
Barry Mienydd671972010-10-04 16:33:58 +0200184 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200185 $y = rand($img_height / 2, $img_height - 3);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200186 imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, $word[$i]);
Derek Jonesb41032d2010-03-05 11:22:45 -0600187 $x += $font_size;
188 }
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Jonesb41032d2010-03-05 11:22:45 -0600191
Andrey Andreev63a21002012-01-19 15:13:32 +0200192 // Create the border
Andrey Andreev7203d542012-03-08 01:42:59 +0200193 imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color);
Derek Jonesb41032d2010-03-05 11:22:45 -0600194
195 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500196 // Generate the image
Derek Jonesb41032d2010-03-05 11:22:45 -0600197 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600198 $img_name = $now.'.jpg';
Derek Jonesb41032d2010-03-05 11:22:45 -0600199 ImageJPEG($im, $img_path.$img_name);
Andrey Andreev63a21002012-01-19 15:13:32 +0200200 $img = '<img src="'.$img_url.$img_name.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
Derek Jonesb41032d2010-03-05 11:22:45 -0600201 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Jonesb41032d2010-03-05 11:22:45 -0600203 return array('word' => $word, 'time' => $now, 'image' => $img);
204 }
205}
206
Derek Jones1c73f4e2010-03-05 11:27:41 -0600207/* End of file captcha_helper.php */
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200208/* Location: ./system/helpers/captcha_helper.php */