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