blob: 5d023b28c58152fabb03c4d5dbf205bf6f4d4b5b [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevf1b43d42012-01-06 14:41:50 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreevf1b43d42012-01-06 14:41:50 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Jonesb41032d2010-03-05 11:22:45 -060036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jonesb41032d2010-03-05 11:22:45 -060039
Derek Jonesb41032d2010-03-05 11:22:45 -060040/**
41 * CodeIgniter CAPTCHA Helper
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
vkeranov63326642012-07-12 10:51:34 +030047 * @link http://codeigniter.com/user_guide/helpers/captcha_helper.html
Derek Jonesb41032d2010-03-05 11:22:45 -060048 */
49
50// ------------------------------------------------------------------------
51
Derek Jonesb41032d2010-03-05 11:22:45 -060052if ( ! function_exists('create_captcha'))
53{
Timothy Warren01b129a2012-04-27 11:36:50 -040054 /**
55 * Create CAPTCHA
56 *
Andrey Andreev8963f402013-07-18 16:02:47 +030057 * @param array $data data for the CAPTCHA
58 * @param string $img_path path to create the image in
59 * @param string $img_url URL to the CAPTCHA image folder
60 * @param string $font_path server path to font
Timothy Warren01b129a2012-04-27 11:36:50 -040061 * @return string
62 */
Derek Jonesb41032d2010-03-05 11:22:45 -060063 function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
Barry Mienydd671972010-10-04 16:33:58 +020064 {
Andrey Andreev8963f402013-07-18 16:02:47 +030065 $defaults = array(
66 'word' => '',
67 'img_path' => '',
68 'img_url' => '',
69 'img_width' => '150',
70 'img_height' => '30',
71 'font_path' => '',
72 'expiration' => 7200,
73 'word_length' => 8,
74 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
75 'colors' => array(
76 'background' => array(255,255,255),
77 'border' => array(153,102,102),
78 'text' => array(204,153,153),
79 'grid' => array(255,182,182)
80 )
81 );
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Jonesb41032d2010-03-05 11:22:45 -060083 foreach ($defaults as $key => $val)
84 {
Andrey Andreeva7209612012-03-26 21:55:43 +030085 if ( ! is_array($data) && empty($$key))
Derek Jonesb41032d2010-03-05 11:22:45 -060086 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +020087 $$key = $val;
Derek Jonesb41032d2010-03-05 11:22:45 -060088 }
89 else
Barry Mienydd671972010-10-04 16:33:58 +020090 {
Andrey Andreeva7209612012-03-26 21:55:43 +030091 $$key = isset($data[$key]) ? $data[$key] : $val;
Derek Jonesb41032d2010-03-05 11:22:45 -060092 }
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Alex Bilbie773ccc32012-06-02 11:11:08 +010095 if ($img_path === '' OR $img_url === ''
Andrey Andreev382b5132014-02-26 18:41:59 +020096 OR ! is_dir($img_path) OR ! is_really_writable($img_path)
Andrey Andreev4f2933d2012-01-08 06:49:49 +020097 OR ! extension_loaded('gd'))
Derek Jonesb41032d2010-03-05 11:22:45 -060098 {
99 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200100 }
101
Derek Jonesb41032d2010-03-05 11:22:45 -0600102 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200103 // Remove old images
Derek Jonesb41032d2010-03-05 11:22:45 -0600104 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200105
Michiel Vugteveenc2659b82012-03-07 21:34:52 +0100106 $now = microtime(TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Jonesb41032d2010-03-05 11:22:45 -0600108 $current_dir = @opendir($img_path);
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500109 while ($filename = @readdir($current_dir))
Derek Jonesb41032d2010-03-05 11:22:45 -0600110 {
vlakoff5f385d02012-09-02 23:00:25 +0200111 if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now)
Derek Jonesb41032d2010-03-05 11:22:45 -0600112 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200113 @unlink($img_path.$filename);
Derek Jonesb41032d2010-03-05 11:22:45 -0600114 }
115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Jonesb41032d2010-03-05 11:22:45 -0600117 @closedir($current_dir);
118
119 // -----------------------------------
120 // Do we have a "word" yet?
121 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Andrey Andreev97088552013-02-08 21:53:20 +0200123 if (empty($word))
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200124 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200125 $word = '';
ash29ae72d2013-04-10 13:59:42 +0100126 for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600127 {
ash29ae72d2013-04-10 13:59:42 +0100128 $word .= $pool[mt_rand(0, $mt_rand_max)];
Derek Jonesb41032d2010-03-05 11:22:45 -0600129 }
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200130 }
Andrey Andreev97088552013-02-08 21:53:20 +0200131 elseif ( ! is_string($word))
132 {
133 $word = (string) $word;
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Jonesb41032d2010-03-05 11:22:45 -0600136 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200137 // Determine angle and position
Derek Jonesb41032d2010-03-05 11:22:45 -0600138 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600139 $length = strlen($word);
vlakoff3a3d5f62013-10-17 22:22:16 +0200140 $angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
141 $x_axis = mt_rand(6, (360/$length)-16);
142 $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Jonesb41032d2010-03-05 11:22:45 -0600144 // Create image
Derek Jonesb41032d2010-03-05 11:22:45 -0600145 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
Andrey Andreev4f2933d2012-01-08 06:49:49 +0200146 $im = function_exists('imagecreatetruecolor')
147 ? imagecreatetruecolor($img_width, $img_height)
148 : imagecreate($img_width, $img_height);
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jonesb41032d2010-03-05 11:22:45 -0600150 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500151 // Assign colors
Andrey Andreev8963f402013-07-18 16:02:47 +0300152 // ----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600153
Andrey Andreev8963f402013-07-18 16:02:47 +0300154 is_array($colors) OR $colors = $defaults['colors'];
155
Andrey Andreev53fd6882013-07-26 02:14:09 +0300156 foreach (array_keys($defaults['colors']) as $key)
Andrey Andreev8963f402013-07-18 16:02:47 +0300157 {
158 // Check for a possible missing value
159 is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
160 $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
161 }
162
163 // Create the rectangle
164 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Jonesb41032d2010-03-05 11:22:45 -0600166 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500167 // Create the spiral pattern
Derek Jonesb41032d2010-03-05 11:22:45 -0600168 // -----------------------------------
Derek Jonesb41032d2010-03-05 11:22:45 -0600169 $theta = 1;
170 $thetac = 7;
171 $radius = 16;
172 $circles = 20;
173 $points = 32;
174
Andrey Andreev3419db12012-03-26 22:00:07 +0300175 for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600176 {
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200177 $theta += $thetac;
178 $rad = $radius * ($i / $points);
Derek Jonesb41032d2010-03-05 11:22:45 -0600179 $x = ($rad * cos($theta)) + $x_axis;
180 $y = ($rad * sin($theta)) + $y_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200181 $theta += $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600182 $rad1 = $radius * (($i + 1) / $points);
183 $x1 = ($rad1 * cos($theta)) + $x_axis;
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200184 $y1 = ($rad1 * sin($theta)) + $y_axis;
Andrey Andreev8963f402013-07-18 16:02:47 +0300185 imageline($im, $x, $y, $x1, $y1, $colors['grid']);
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200186 $theta -= $thetac;
Derek Jonesb41032d2010-03-05 11:22:45 -0600187 }
188
189 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500190 // Write the text
Derek Jonesb41032d2010-03-05 11:22:45 -0600191 // -----------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200192
Alex Bilbie773ccc32012-06-02 11:11:08 +0100193 $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
Andrey Andreev7203d542012-03-08 01:42:59 +0200194 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600195 {
196 $font_size = 5;
vlakoff3a3d5f62013-10-17 22:22:16 +0200197 $x = mt_rand(0, $img_width / ($length / 3));
Derek Jonesb41032d2010-03-05 11:22:45 -0600198 $y = 0;
199 }
200 else
201 {
Andrey Andreev63a21002012-01-19 15:13:32 +0200202 $font_size = 16;
vlakoff3a3d5f62013-10-17 22:22:16 +0200203 $x = mt_rand(0, $img_width / ($length / 1.5));
Andrey Andreev7203d542012-03-08 01:42:59 +0200204 $y = $font_size + 2;
Derek Jonesb41032d2010-03-05 11:22:45 -0600205 }
206
Andrey Andreevf1b43d42012-01-06 14:41:50 +0200207 for ($i = 0; $i < $length; $i++)
Derek Jonesb41032d2010-03-05 11:22:45 -0600208 {
Andrey Andreev7203d542012-03-08 01:42:59 +0200209 if ($use_font === FALSE)
Derek Jonesb41032d2010-03-05 11:22:45 -0600210 {
vlakoff3a3d5f62013-10-17 22:22:16 +0200211 $y = mt_rand(0 , $img_height / 2);
Andrey Andreev8963f402013-07-18 16:02:47 +0300212 imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
Andrey Andreev7203d542012-03-08 01:42:59 +0200213 $x += ($font_size * 2);
Derek Jonesb41032d2010-03-05 11:22:45 -0600214 }
215 else
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
vlakoff3a3d5f62013-10-17 22:22:16 +0200217 $y = mt_rand($img_height / 2, $img_height - 3);
Andrey Andreev8963f402013-07-18 16:02:47 +0300218 imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
Derek Jonesb41032d2010-03-05 11:22:45 -0600219 $x += $font_size;
220 }
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreev63a21002012-01-19 15:13:32 +0200223 // Create the border
Andrey Andreev8963f402013-07-18 16:02:47 +0300224 imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
Derek Jonesb41032d2010-03-05 11:22:45 -0600225
226 // -----------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500227 // Generate the image
Derek Jonesb41032d2010-03-05 11:22:45 -0600228 // -----------------------------------
egig225f53b2014-05-08 10:34:35 +0700229 $img_url = rtrim($img_url, '/').'/';
ET-NiK6854f872014-08-08 18:43:02 +0400230
Andrey Andreev09546ed2014-08-11 00:11:36 +0300231 if (function_exists('imagejpeg'))
ET-NiK6854f872014-08-08 18:43:02 +0400232 {
233 $img_filename = $now.'.jpg';
Andrey Andreev09546ed2014-08-11 00:11:36 +0300234 imagejpeg($im, $img_path.$img_filename);
ET-NiK6854f872014-08-08 18:43:02 +0400235 }
Andrey Andreev09546ed2014-08-11 00:11:36 +0300236 elseif (function_exists('imagepng'))
ET-NiK6854f872014-08-08 18:43:02 +0400237 {
238 $img_filename = $now.'.png';
Andrey Andreev09546ed2014-08-11 00:11:36 +0300239 imagepng($im, $img_path.$img_filename);
ET-NiK6854f872014-08-08 18:43:02 +0400240 }
241 else
242 {
243 return FALSE;
244 }
245
Andrey Andreev72b4b3c2013-10-21 14:44:57 +0300246 $img = '<img src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
Derek Jonesb41032d2010-03-05 11:22:45 -0600247 ImageDestroy($im);
Barry Mienydd671972010-10-04 16:33:58 +0200248
Andrey Andreev72b4b3c2013-10-21 14:44:57 +0300249 return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
Derek Jonesb41032d2010-03-05 11:22:45 -0600250 }
251}
252
Derek Jones1c73f4e2010-03-05 11:27:41 -0600253/* End of file captcha_helper.php */
egigf039c9f2014-05-09 03:10:05 +0700254/* Location: ./system/helpers/captcha_helper.php */