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 | * |
| 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 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 | { |
| 54 | $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200); |
| 55 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 56 | foreach ($defaults as $key => $val) |
| 57 | { |
Andrey Andreev | a720961 | 2012-03-26 21:55:43 +0300 | [diff] [blame] | 58 | if ( ! is_array($data) && empty($$key)) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 59 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 60 | $$key = $val; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 61 | } |
| 62 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | { |
Andrey Andreev | a720961 | 2012-03-26 21:55:43 +0300 | [diff] [blame] | 64 | $$key = isset($data[$key]) ? $data[$key] : $val; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 65 | } |
| 66 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 68 | if ($img_path === '' OR $img_url === '' |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 69 | OR ! @is_dir($img_path) OR ! is_writeable($img_path) |
| 70 | OR ! extension_loaded('gd')) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 71 | { |
| 72 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 75 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 76 | // Remove old images |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 77 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | |
Michiel Vugteveen | c2659b8 | 2012-03-07 21:34:52 +0100 | [diff] [blame] | 79 | $now = microtime(TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 81 | $current_dir = @opendir($img_path); |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 82 | while ($filename = @readdir($current_dir)) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 83 | { |
vlakoff | 5f385d0 | 2012-09-02 23:00:25 +0200 | [diff] [blame] | 84 | if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 85 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 86 | @unlink($img_path.$filename); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 87 | } |
| 88 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 90 | @closedir($current_dir); |
| 91 | |
| 92 | // ----------------------------------- |
| 93 | // Do we have a "word" yet? |
| 94 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Andrey Andreev | 9708855 | 2013-02-08 21:53:20 +0200 | [diff] [blame^] | 96 | if (empty($word)) |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 97 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 98 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 99 | $word = ''; |
| 100 | for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 101 | { |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 102 | $word .= $pool[mt_rand(0, $mt_rand_max)]; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 103 | } |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 104 | } |
Andrey Andreev | 9708855 | 2013-02-08 21:53:20 +0200 | [diff] [blame^] | 105 | elseif ( ! is_string($word)) |
| 106 | { |
| 107 | $word = (string) $word; |
| 108 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 109 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 110 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | // Determine angle and position |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 112 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 113 | $length = strlen($word); |
| 114 | $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | $x_axis = rand(6, (360/$length)-16); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 116 | $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] | 117 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 118 | // Create image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 119 | // PHP.net recommends imagecreatetruecolor(), but it isn't always available |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 120 | $im = function_exists('imagecreatetruecolor') |
| 121 | ? imagecreatetruecolor($img_width, $img_height) |
| 122 | : imagecreate($img_width, $img_height); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 124 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 125 | // Assign colors |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 126 | // ----------------------------------- |
Andrey Andreev | 33101f4 | 2012-03-26 21:53:37 +0300 | [diff] [blame] | 127 | $bg_color = imagecolorallocate($im, 255, 255, 255); |
| 128 | $border_color = imagecolorallocate($im, 153, 102, 102); |
| 129 | $text_color = imagecolorallocate($im, 204, 153, 153); |
| 130 | $grid_color = imagecolorallocate($im, 255, 182, 182); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 131 | $shadow_color = imagecolorallocate($im, 255, 240, 240); |
| 132 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 133 | // Create the rectangle |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 134 | ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 136 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 137 | // Create the spiral pattern |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 138 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 139 | $theta = 1; |
| 140 | $thetac = 7; |
| 141 | $radius = 16; |
| 142 | $circles = 20; |
| 143 | $points = 32; |
| 144 | |
Andrey Andreev | 3419db1 | 2012-03-26 22:00:07 +0300 | [diff] [blame] | 145 | for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 146 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 147 | $theta += $thetac; |
| 148 | $rad = $radius * ($i / $points); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 149 | $x = ($rad * cos($theta)) + $x_axis; |
| 150 | $y = ($rad * sin($theta)) + $y_axis; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 151 | $theta += $thetac; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 152 | $rad1 = $radius * (($i + 1) / $points); |
| 153 | $x1 = ($rad1 * cos($theta)) + $x_axis; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 154 | $y1 = ($rad1 * sin($theta)) + $y_axis; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 155 | imageline($im, $x, $y, $x1, $y1, $grid_color); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 156 | $theta -= $thetac; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 160 | // Write the text |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 161 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 163 | $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext')); |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 164 | if ($use_font === FALSE) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 165 | { |
| 166 | $font_size = 5; |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 167 | $x = rand(0, $img_width / ($length / 3)); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 168 | $y = 0; |
| 169 | } |
| 170 | else |
| 171 | { |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 172 | $font_size = 16; |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 173 | $x = rand(0, $img_width / ($length / 1.5)); |
| 174 | $y = $font_size + 2; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 175 | } |
| 176 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 177 | for ($i = 0; $i < $length; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 178 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 179 | if ($use_font === FALSE) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 180 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 181 | $y = rand(0 , $img_height / 2); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 182 | imagestring($im, $font_size, $x, $y, $word[$i], $text_color); |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 183 | $x += ($font_size * 2); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 184 | } |
| 185 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 187 | $y = rand($img_height / 2, $img_height - 3); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 188 | imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, $word[$i]); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 189 | $x += $font_size; |
| 190 | } |
| 191 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 193 | // Create the border |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame] | 194 | imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 195 | |
| 196 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 197 | // Generate the image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 198 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 199 | $img_name = $now.'.jpg'; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 200 | ImageJPEG($im, $img_path.$img_name); |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 201 | $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] | 202 | ImageDestroy($im); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 204 | return array('word' => $word, 'time' => $now, 'image' => $img); |
| 205 | } |
| 206 | } |
| 207 | |
Derek Jones | 1c73f4e | 2010-03-05 11:27:41 -0600 | [diff] [blame] | 208 | /* End of file captcha_helper.php */ |
Andrey Andreev | e6f7d61 | 2012-03-20 15:39:22 +0200 | [diff] [blame] | 209 | /* Location: ./system/helpers/captcha_helper.php */ |