Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, 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 | */ |
| 27 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter CAPTCHA Helper |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/xml_helper.html |
| 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | /** |
| 41 | * Create CAPTCHA |
| 42 | * |
| 43 | * @access public |
| 44 | * @param array array of data for the CAPTCHA |
| 45 | * @param string path to create the image in |
| 46 | * @param string URL to the CAPTCHA image folder |
| 47 | * @param string server path to font |
| 48 | * @return string |
| 49 | */ |
| 50 | if ( ! function_exists('create_captcha')) |
| 51 | { |
| 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 | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 58 | if ( ! is_array($data) && ( ! isset($$key) OR $$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 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 64 | $$key = ( ! isset($data[$key])) ? $val : $data[$key]; |
| 65 | } |
| 66 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 68 | if ($img_path == '' OR $img_url == '' |
| 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 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 84 | if ($filename !== '.' && $filename !== '..' && $filename !== 'index.html' |
| 85 | && (str_replace('.jpg', '', $filename) + $expiration) < $now) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 86 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 87 | @unlink($img_path.$filename); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 88 | } |
| 89 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 91 | @closedir($current_dir); |
| 92 | |
| 93 | // ----------------------------------- |
| 94 | // Do we have a "word" yet? |
| 95 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 97 | if ($word == '') |
| 98 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 99 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 100 | $word = ''; |
| 101 | for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 102 | { |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 103 | $word .= $pool[mt_rand(0, $mt_rand_max)]; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 104 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 106 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 108 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 109 | // Determine angle and position |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 110 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 111 | $length = strlen($word); |
| 112 | $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | $x_axis = rand(6, (360/$length)-16); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 114 | $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] | 115 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 116 | // Create image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 117 | // PHP.net recommends imagecreatetruecolor(), but it isn't always available |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame] | 118 | $im = function_exists('imagecreatetruecolor') |
| 119 | ? imagecreatetruecolor($img_width, $img_height) |
| 120 | : imagecreate($img_width, $img_height); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 122 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 123 | // Assign colors |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 124 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 125 | $bg_color = imagecolorallocate ($im, 255, 255, 255); |
| 126 | $border_color = imagecolorallocate ($im, 153, 102, 102); |
| 127 | $text_color = imagecolorallocate ($im, 204, 153, 153); |
| 128 | $grid_color = imagecolorallocate($im, 255, 182, 182); |
| 129 | $shadow_color = imagecolorallocate($im, 255, 240, 240); |
| 130 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 131 | // Create the rectangle |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 132 | ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 134 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 135 | // Create the spiral pattern |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 136 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 137 | $theta = 1; |
| 138 | $thetac = 7; |
| 139 | $radius = 16; |
| 140 | $circles = 20; |
| 141 | $points = 32; |
| 142 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 143 | for ($i = 0, $cp = $circles * $points; $i < $cp - 1; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 144 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 145 | $theta += $thetac; |
| 146 | $rad = $radius * ($i / $points); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 147 | $x = ($rad * cos($theta)) + $x_axis; |
| 148 | $y = ($rad * sin($theta)) + $y_axis; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 149 | $theta += $thetac; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 150 | $rad1 = $radius * (($i + 1) / $points); |
| 151 | $x1 = ($rad1 * cos($theta)) + $x_axis; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 152 | $y1 = ($rad1 * sin($theta)) + $y_axis; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 153 | imageline($im, $x, $y, $x1, $y1, $grid_color); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 154 | $theta -= $thetac; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 158 | // Write the text |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 159 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame^] | 161 | $use_font = ($font_path != '' && file_exists($font_path) && function_exists('imagettftext')); |
| 162 | if ($use_font === FALSE) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 163 | { |
| 164 | $font_size = 5; |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame^] | 165 | $x = rand(0, $img_width / ($length / 3)); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 166 | $y = 0; |
| 167 | } |
| 168 | else |
| 169 | { |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 170 | $font_size = 16; |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame^] | 171 | $x = rand(0, $img_width / ($length / 1.5)); |
| 172 | $y = $font_size + 2; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 173 | } |
| 174 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 175 | for ($i = 0; $i < $length; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 176 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame^] | 177 | if ($use_font === FALSE) |
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 | $y = rand(0 , $img_height / 2); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 180 | imagestring($im, $font_size, $x, $y, $word[$i], $text_color); |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame^] | 181 | $x += ($font_size * 2); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 182 | } |
| 183 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | { |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame^] | 185 | $y = rand($img_height / 2, $img_height - 3); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 186 | 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] | 187 | $x += $font_size; |
| 188 | } |
| 189 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 191 | |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 192 | // Create the border |
Andrey Andreev | 7203d54 | 2012-03-08 01:42:59 +0200 | [diff] [blame^] | 193 | imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 194 | |
| 195 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 196 | // Generate the image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 197 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 198 | $img_name = $now.'.jpg'; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 199 | ImageJPEG($im, $img_path.$img_name); |
Andrey Andreev | 63a2100 | 2012-01-19 15:13:32 +0200 | [diff] [blame] | 200 | $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] | 201 | ImageDestroy($im); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 203 | return array('word' => $word, 'time' => $now, 'image' => $img); |
| 204 | } |
| 205 | } |
| 206 | |
Derek Jones | 1c73f4e | 2010-03-05 11:27:41 -0600 | [diff] [blame] | 207 | /* End of file captcha_helper.php */ |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 208 | /* Location: ./system/helpers/captcha_helper.php */ |