blob: 731b59e1448ba4e20dddc50c91e5f5a4b32e7254 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Jonesb41032d2010-03-05 11:22:45 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jonesb41032d2010-03-05 11:22:45 -060028
Derek Jonesb41032d2010-03-05 11:22:45 -060029/**
30 * CodeIgniter CAPTCHA Helper
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
vkeranov63326642012-07-12 10:51:34 +030036 * @link http://codeigniter.com/user_guide/helpers/captcha_helper.html
Derek Jonesb41032d2010-03-05 11:22:45 -060037 */
38
39// ------------------------------------------------------------------------
40
Derek Jonesb41032d2010-03-05 11:22:45 -060041if ( ! function_exists('create_captcha'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Create CAPTCHA
45 *
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 */
Derek Jonesb41032d2010-03-05 11:22:45 -060052 function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
Barry Mienydd671972010-10-04 16:33:58 +020053 {
ash79dfac72013-04-10 12:36:49 +010054 $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200, 'captcha_word_length' => 8, 'character_pool_for_generated_word' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Jonesb41032d2010-03-05 11:22:45 -060056 foreach ($defaults as $key => $val)
57 {
Andrey Andreeva7209612012-03-26 21:55:43 +030058 if ( ! is_array($data) && empty($$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 {
Andrey Andreeva7209612012-03-26 21:55:43 +030064 $$key = isset($data[$key]) ? $data[$key] : $val;
Derek Jonesb41032d2010-03-05 11:22:45 -060065 }
66 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Alex Bilbie773ccc32012-06-02 11:11:08 +010068 if ($img_path === '' OR $img_url === ''
Andrey Andreev4f2933d2012-01-08 06:49:49 +020069 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
ash1aa336d2013-04-10 12:30:12 +010075
76
77 // -----------------------------------
78 // Make sure captcha max length is a valid/realistic value.
79 // -----------------------------------
80
81 $captcha_word_length = (int) $captcha_word_length;
82 if ($captcha_word_length < 4) { $captcha_word_length = 4;}
83 if ($captcha_word_length > 15) { $captcha_word_length = 15; }
84
85
Derek Jonesb41032d2010-03-05 11:22:45 -060086 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020087 // Remove old images
Derek Jonesb41032d2010-03-05 11:22:45 -060088 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020089
Michiel Vugteveenc2659b82012-03-07 21:34:52 +010090 $now = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Jonesb41032d2010-03-05 11:22:45 -060092 $current_dir = @opendir($img_path);
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050093 while ($filename = @readdir($current_dir))
Derek Jonesb41032d2010-03-05 11:22:45 -060094 {
vlakoff5f385d02012-09-02 23:00:25 +020095 if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now)
Derek Jonesb41032d2010-03-05 11:22:45 -060096 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020097 @unlink($img_path.$filename);
Derek Jonesb41032d2010-03-05 11:22:45 -060098 }
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Jonesb41032d2010-03-05 11:22:45 -0600101 @closedir($current_dir);
102
103 // -----------------------------------
104 // Do we have a "word" yet?
105 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200106
Andrey Andreev97088552013-02-08 21:53:20 +0200107 if (empty($word))
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200108 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200109 $word = '';
ash79dfac72013-04-10 12:36:49 +0100110 for ($i = 0, $mt_rand_max = strlen($character_pool_for_generated_word) - 1; $i < $captcha_word_length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600111 {
ash79dfac72013-04-10 12:36:49 +0100112 $word .= $character_pool_for_generated_word[mt_rand(0, $mt_rand_max)];
Derek Jonesb41032d2010-03-05 11:22:45 -0600113 }
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200114 }
Andrey Andreev97088552013-02-08 21:53:20 +0200115 elseif ( ! is_string($word))
116 {
117 $word = (string) $word;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Jonesb41032d2010-03-05 11:22:45 -0600120 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200121 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600122 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600123 $length = strlen($word);
124 $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
Barry Mienydd671972010-10-04 16:33:58 +0200125 $x_axis = rand(6, (360/$length)-16);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200126 $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Jonesb41032d2010-03-05 11:22:45 -0600128 // Create image
Derek Jonesb41032d2010-03-05 11:22:45 -0600129 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
Andrey Andreev4f2933d2012-01-08 06:49:49 +0200130 $im = function_exists('imagecreatetruecolor')
131 ? imagecreatetruecolor($img_width, $img_height)
132 : imagecreate($img_width, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Jonesb41032d2010-03-05 11:22:45 -0600134 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500135 // Assign colors
Derek Jonesb41032d2010-03-05 11:22:45 -0600136 // -----------------------------------
Andrey Andreev33101f42012-03-26 21:53:37 +0300137 $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);
Derek Jonesb41032d2010-03-05 11:22:45 -0600141 $shadow_color = imagecolorallocate($im, 255, 240, 240);
142
Derek Jones37f4b9c2011-07-01 17:56:50 -0500143 // Create the rectangle
Derek Jonesb41032d2010-03-05 11:22:45 -0600144 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Jonesb41032d2010-03-05 11:22:45 -0600146 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500147 // Create the spiral pattern
Derek Jonesb41032d2010-03-05 11:22:45 -0600148 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600149 $theta = 1;
150 $thetac = 7;
151 $radius = 16;
152 $circles = 20;
153 $points = 32;
154
Andrey Andreev3419db12012-03-26 22:00:07 +0300155 for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600156 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200157 $theta += $thetac;
158 $rad = $radius * ($i / $points);
Derek Jonesb41032d2010-03-05 11:22:45 -0600159 $x = ($rad * cos($theta)) + $x_axis;
160 $y = ($rad * sin($theta)) + $y_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200161 $theta += $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600162 $rad1 = $radius * (($i + 1) / $points);
163 $x1 = ($rad1 * cos($theta)) + $x_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200164 $y1 = ($rad1 * sin($theta)) + $y_axis;
Derek Jonesb41032d2010-03-05 11:22:45 -0600165 imageline($im, $x, $y, $x1, $y1, $grid_color);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200166 $theta -= $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600167 }
168
169 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500170 // Write the text
Derek Jonesb41032d2010-03-05 11:22:45 -0600171 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172
Alex Bilbie773ccc32012-06-02 11:11:08 +0100173 $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
Andrey Andreev7203d542012-03-08 01:42:59 +0200174 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600175 {
176 $font_size = 5;
Andrey Andreev7203d542012-03-08 01:42:59 +0200177 $x = rand(0, $img_width / ($length / 3));
Derek Jonesb41032d2010-03-05 11:22:45 -0600178 $y = 0;
179 }
180 else
181 {
Andrey Andreev63a21002012-01-19 15:13:32 +0200182 $font_size = 16;
Andrey Andreev7203d542012-03-08 01:42:59 +0200183 $x = rand(0, $img_width / ($length / 1.5));
184 $y = $font_size + 2;
Derek Jonesb41032d2010-03-05 11:22:45 -0600185 }
186
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200187 for ($i = 0; $i < $length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600188 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200189 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600190 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200191 $y = rand(0 , $img_height / 2);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200192 imagestring($im, $font_size, $x, $y, $word[$i], $text_color);
Andrey Andreev7203d542012-03-08 01:42:59 +0200193 $x += ($font_size * 2);
Derek Jonesb41032d2010-03-05 11:22:45 -0600194 }
195 else
Barry Mienydd671972010-10-04 16:33:58 +0200196 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200197 $y = rand($img_height / 2, $img_height - 3);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200198 imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, $word[$i]);
Derek Jonesb41032d2010-03-05 11:22:45 -0600199 $x += $font_size;
200 }
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Andrey Andreev63a21002012-01-19 15:13:32 +0200203 // Create the border
Andrey Andreev7203d542012-03-08 01:42:59 +0200204 imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color);
Derek Jonesb41032d2010-03-05 11:22:45 -0600205
206 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500207 // Generate the image
Derek Jonesb41032d2010-03-05 11:22:45 -0600208 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600209 $img_name = $now.'.jpg';
Derek Jonesb41032d2010-03-05 11:22:45 -0600210 ImageJPEG($im, $img_path.$img_name);
Andrey Andreev63a21002012-01-19 15:13:32 +0200211 $img = '<img src="'.$img_url.$img_name.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
Derek Jonesb41032d2010-03-05 11:22:45 -0600212 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Jonesb41032d2010-03-05 11:22:45 -0600214 return array('word' => $word, 'time' => $now, 'image' => $img);
215 }
216}
217
Derek Jones1c73f4e2010-03-05 11:27:41 -0600218/* End of file captcha_helper.php */
ash1aa336d2013-04-10 12:30:12 +0100219/* Location: ./system/helpers/captcha_helper.php */
220