Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | 51a2281 | 2012-01-24 15:27:34 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 28 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 29 | /** |
| 30 | * CodeIgniter CAPTCHA Helper |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Helpers |
| 34 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
vkeranov | 6332664 | 2012-07-12 10:51:34 +0300 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/helpers/captcha_helper.html |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 41 | if ( ! function_exists('create_captcha')) |
| 42 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 43 | /** |
| 44 | * Create CAPTCHA |
| 45 | * |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 46 | * @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 Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 50 | * @return string |
| 51 | */ |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 52 | function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 53 | { |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 54 | $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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 71 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 72 | foreach ($defaults as $key => $val) |
| 73 | { |
Andrey Andreev | a720961 | 2012-03-26 21:55:43 +0300 | [diff] [blame] | 74 | if ( ! is_array($data) && empty($$key)) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 75 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 76 | $$key = $val; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 77 | } |
| 78 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | { |
Andrey Andreev | a720961 | 2012-03-26 21:55:43 +0300 | [diff] [blame] | 80 | $$key = isset($data[$key]) ? $data[$key] : $val; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 81 | } |
| 82 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 83 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 84 | if ($img_path === '' OR $img_url === '' |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 85 | OR ! @is_dir($img_path) OR ! is_writeable($img_path) |
| 86 | OR ! extension_loaded('gd')) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 87 | { |
| 88 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 91 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | // Remove old images |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 93 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame] | 95 | $now = microtime(TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 97 | $current_dir = @opendir($img_path); |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 98 | while ($filename = @readdir($current_dir)) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 99 | { |
vlakoff | 5f385d0 | 2012-09-02 23:00:25 +0200 | [diff] [blame] | 100 | if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 101 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 102 | @unlink($img_path.$filename); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 103 | } |
| 104 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 106 | @closedir($current_dir); |
| 107 | |
| 108 | // ----------------------------------- |
| 109 | // Do we have a "word" yet? |
| 110 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Andrey Andreev | 9708855 | 2013-02-08 21:53:20 +0200 | [diff] [blame] | 112 | if (empty($word)) |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 113 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 114 | $word = ''; |
ash | 29ae72d | 2013-04-10 13:59:42 +0100 | [diff] [blame] | 115 | for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 116 | { |
ash | 29ae72d | 2013-04-10 13:59:42 +0100 | [diff] [blame] | 117 | $word .= $pool[mt_rand(0, $mt_rand_max)]; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 118 | } |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 119 | } |
Andrey Andreev | 9708855 | 2013-02-08 21:53:20 +0200 | [diff] [blame] | 120 | elseif ( ! is_string($word)) |
| 121 | { |
| 122 | $word = (string) $word; |
| 123 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 124 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 125 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | // Determine angle and position |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 127 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 128 | $length = strlen($word); |
| 129 | $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | $x_axis = rand(6, (360/$length)-16); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 131 | $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 133 | // Create image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 134 | // PHP.net recommends imagecreatetruecolor(), but it isn't always available |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 135 | $im = function_exists('imagecreatetruecolor') |
| 136 | ? imagecreatetruecolor($img_width, $img_height) |
| 137 | : imagecreate($img_width, $img_height); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 139 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 140 | // Assign colors |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 141 | // ---------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 142 | |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 143 | is_array($colors) OR $colors = $defaults['colors']; |
| 144 | |
| 145 | foreach (array_keys($default['colors']) as $key) |
| 146 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 155 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 156 | // Create the spiral pattern |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 157 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 158 | $theta = 1; |
| 159 | $thetac = 7; |
| 160 | $radius = 16; |
| 161 | $circles = 20; |
| 162 | $points = 32; |
| 163 | |
Andrey Andreev | 3419db1 | 2012-03-26 22:00:07 +0300 | [diff] [blame] | 164 | for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 165 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 166 | $theta += $thetac; |
| 167 | $rad = $radius * ($i / $points); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 168 | $x = ($rad * cos($theta)) + $x_axis; |
| 169 | $y = ($rad * sin($theta)) + $y_axis; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 170 | $theta += $thetac; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 171 | $rad1 = $radius * (($i + 1) / $points); |
| 172 | $x1 = ($rad1 * cos($theta)) + $x_axis; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 173 | $y1 = ($rad1 * sin($theta)) + $y_axis; |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 174 | imageline($im, $x, $y, $x1, $y1, $colors['grid']); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 175 | $theta -= $thetac; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 179 | // Write the text |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 180 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 182 | $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext')); |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 183 | if ($use_font === FALSE) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 184 | { |
| 185 | $font_size = 5; |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 186 | $x = rand(0, $img_width / ($length / 3)); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 187 | $y = 0; |
| 188 | } |
| 189 | else |
| 190 | { |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 191 | $font_size = 16; |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 192 | $x = rand(0, $img_width / ($length / 1.5)); |
| 193 | $y = $font_size + 2; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 196 | for ($i = 0; $i < $length; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 197 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 198 | if ($use_font === FALSE) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 199 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 200 | $y = rand(0 , $img_height / 2); |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 201 | imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']); |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 202 | $x += ($font_size * 2); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 203 | } |
| 204 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 206 | $y = rand($img_height / 2, $img_height - 3); |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 207 | imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 208 | $x += $font_size; |
| 209 | } |
| 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 212 | // Create the border |
Andrey Andreev | 8963f40 | 2013-07-18 16:02:47 +0300 | [diff] [blame^] | 213 | imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 214 | |
| 215 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 216 | // Generate the image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 217 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 218 | $img_name = $now.'.jpg'; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 219 | ImageJPEG($im, $img_path.$img_name); |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 220 | $img = '<img src="'.$img_url.$img_name.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />'; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 221 | ImageDestroy($im); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 222 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 223 | return array('word' => $word, 'time' => $now, 'image' => $img); |
| 224 | } |
| 225 | } |
| 226 | |
Derek Jones | 1c73f4e | 2010-03-05 11:27:41 -0600 | [diff] [blame] | 227 | /* End of file captcha_helper.php */ |
ash | c3dcf9f | 2013-04-14 14:36:22 +0100 | [diff] [blame] | 228 | /* Location: ./system/helpers/captcha_helper.php */ |