blob: 24cd53568f9d149b3847450ab99fdb74ef7786c3 [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 *
Andrey Andreev8963f402013-07-18 16:02:47 +030046 * @param array $data data for the CAPTCHA
47 * @param string $img_path path to create the image in
48 * @param string $img_url URL to the CAPTCHA image folder
49 * @param string $font_path server path to font
Timothy Warren01b129a2012-04-27 11:36:50 -040050 * @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 {
Andrey Andreev8963f402013-07-18 16:02:47 +030054 $defaults = array(
55 'word' => '',
56 'img_path' => '',
57 'img_url' => '',
58 'img_width' => '150',
59 'img_height' => '30',
60 'font_path' => '',
61 'expiration' => 7200,
62 'word_length' => 8,
63 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
64 'colors' => array(
65 'background' => array(255,255,255),
66 'border' => array(153,102,102),
67 'text' => array(204,153,153),
68 'grid' => array(255,182,182)
69 )
70 );
Barry Mienydd671972010-10-04 16:33:58 +020071
Derek Jonesb41032d2010-03-05 11:22:45 -060072 foreach ($defaults as $key => $val)
73 {
Andrey Andreeva7209612012-03-26 21:55:43 +030074 if ( ! is_array($data) && empty($$key))
Derek Jonesb41032d2010-03-05 11:22:45 -060075 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020076 $$key = $val;
Derek Jonesb41032d2010-03-05 11:22:45 -060077 }
78 else
Barry Mienydd671972010-10-04 16:33:58 +020079 {
Andrey Andreeva7209612012-03-26 21:55:43 +030080 $$key = isset($data[$key]) ? $data[$key] : $val;
Derek Jonesb41032d2010-03-05 11:22:45 -060081 }
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Alex Bilbie773ccc32012-06-02 11:11:08 +010084 if ($img_path === '' OR $img_url === ''
Andrey Andreev4f2933d2012-01-08 06:49:49 +020085 OR ! @is_dir($img_path) OR ! is_writeable($img_path)
86 OR ! extension_loaded('gd'))
Derek Jonesb41032d2010-03-05 11:22:45 -060087 {
88 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020089 }
90
Derek Jonesb41032d2010-03-05 11:22:45 -060091 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020092 // Remove old images
Derek Jonesb41032d2010-03-05 11:22:45 -060093 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020094
Michiel Vugteveenc2659b82012-03-07 21:34:52 +010095 $now = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Jonesb41032d2010-03-05 11:22:45 -060097 $current_dir = @opendir($img_path);
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050098 while ($filename = @readdir($current_dir))
Derek Jonesb41032d2010-03-05 11:22:45 -060099 {
vlakoff5f385d02012-09-02 23:00:25 +0200100 if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now)
Derek Jonesb41032d2010-03-05 11:22:45 -0600101 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200102 @unlink($img_path.$filename);
Derek Jonesb41032d2010-03-05 11:22:45 -0600103 }
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Jonesb41032d2010-03-05 11:22:45 -0600106 @closedir($current_dir);
107
108 // -----------------------------------
109 // Do we have a "word" yet?
110 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200111
Andrey Andreev97088552013-02-08 21:53:20 +0200112 if (empty($word))
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200113 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200114 $word = '';
ash29ae72d2013-04-10 13:59:42 +0100115 for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600116 {
ash29ae72d2013-04-10 13:59:42 +0100117 $word .= $pool[mt_rand(0, $mt_rand_max)];
Derek Jonesb41032d2010-03-05 11:22:45 -0600118 }
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200119 }
Andrey Andreev97088552013-02-08 21:53:20 +0200120 elseif ( ! is_string($word))
121 {
122 $word = (string) $word;
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Jonesb41032d2010-03-05 11:22:45 -0600125 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200126 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600127 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600128 $length = strlen($word);
vlakoff3a3d5f62013-10-17 22:22:16 +0200129 $angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
130 $x_axis = mt_rand(6, (360/$length)-16);
131 $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Jonesb41032d2010-03-05 11:22:45 -0600133 // Create image
Derek Jonesb41032d2010-03-05 11:22:45 -0600134 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
Andrey Andreev4f2933d2012-01-08 06:49:49 +0200135 $im = function_exists('imagecreatetruecolor')
136 ? imagecreatetruecolor($img_width, $img_height)
137 : imagecreate($img_width, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Jonesb41032d2010-03-05 11:22:45 -0600139 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500140 // Assign colors
Andrey Andreev8963f402013-07-18 16:02:47 +0300141 // ----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600142
Andrey Andreev8963f402013-07-18 16:02:47 +0300143 is_array($colors) OR $colors = $defaults['colors'];
144
Andrey Andreev53fd6882013-07-26 02:14:09 +0300145 foreach (array_keys($defaults['colors']) as $key)
Andrey Andreev8963f402013-07-18 16:02:47 +0300146 {
147 // Check for a possible missing value
148 is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
149 $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
150 }
151
152 // Create the rectangle
153 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Jonesb41032d2010-03-05 11:22:45 -0600155 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500156 // Create the spiral pattern
Derek Jonesb41032d2010-03-05 11:22:45 -0600157 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600158 $theta = 1;
159 $thetac = 7;
160 $radius = 16;
161 $circles = 20;
162 $points = 32;
163
Andrey Andreev3419db12012-03-26 22:00:07 +0300164 for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600165 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200166 $theta += $thetac;
167 $rad = $radius * ($i / $points);
Derek Jonesb41032d2010-03-05 11:22:45 -0600168 $x = ($rad * cos($theta)) + $x_axis;
169 $y = ($rad * sin($theta)) + $y_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200170 $theta += $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600171 $rad1 = $radius * (($i + 1) / $points);
172 $x1 = ($rad1 * cos($theta)) + $x_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200173 $y1 = ($rad1 * sin($theta)) + $y_axis;
Andrey Andreev8963f402013-07-18 16:02:47 +0300174 imageline($im, $x, $y, $x1, $y1, $colors['grid']);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200175 $theta -= $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600176 }
177
178 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500179 // Write the text
Derek Jonesb41032d2010-03-05 11:22:45 -0600180 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Alex Bilbie773ccc32012-06-02 11:11:08 +0100182 $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
Andrey Andreev7203d542012-03-08 01:42:59 +0200183 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600184 {
185 $font_size = 5;
vlakoff3a3d5f62013-10-17 22:22:16 +0200186 $x = mt_rand(0, $img_width / ($length / 3));
Derek Jonesb41032d2010-03-05 11:22:45 -0600187 $y = 0;
188 }
189 else
190 {
Andrey Andreev63a21002012-01-19 15:13:32 +0200191 $font_size = 16;
vlakoff3a3d5f62013-10-17 22:22:16 +0200192 $x = mt_rand(0, $img_width / ($length / 1.5));
Andrey Andreev7203d542012-03-08 01:42:59 +0200193 $y = $font_size + 2;
Derek Jonesb41032d2010-03-05 11:22:45 -0600194 }
195
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200196 for ($i = 0; $i < $length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600197 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200198 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600199 {
vlakoff3a3d5f62013-10-17 22:22:16 +0200200 $y = mt_rand(0 , $img_height / 2);
Andrey Andreev8963f402013-07-18 16:02:47 +0300201 imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
Andrey Andreev7203d542012-03-08 01:42:59 +0200202 $x += ($font_size * 2);
Derek Jonesb41032d2010-03-05 11:22:45 -0600203 }
204 else
Barry Mienydd671972010-10-04 16:33:58 +0200205 {
vlakoff3a3d5f62013-10-17 22:22:16 +0200206 $y = mt_rand($img_height / 2, $img_height - 3);
Andrey Andreev8963f402013-07-18 16:02:47 +0300207 imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
Derek Jonesb41032d2010-03-05 11:22:45 -0600208 $x += $font_size;
209 }
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Andrey Andreev63a21002012-01-19 15:13:32 +0200212 // Create the border
Andrey Andreev8963f402013-07-18 16:02:47 +0300213 imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
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 // -----------------------------------
Andrey Andreev72b4b3c2013-10-21 14:44:57 +0300218 $img_filename = $now.'.jpg';
219 ImageJPEG($im, $img_path.$img_filename);
220 $img = '<img src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
Derek Jonesb41032d2010-03-05 11:22:45 -0600221 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreev72b4b3c2013-10-21 14:44:57 +0300223 return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
Derek Jonesb41032d2010-03-05 11:22:45 -0600224 }
225}
226
Derek Jones1c73f4e2010-03-05 11:27:41 -0600227/* End of file captcha_helper.php */
ashc3dcf9f2013-04-14 14:36:22 +0100228/* Location: ./system/helpers/captcha_helper.php */