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 |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 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 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter CAPTCHA Helper |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Helpers |
| 35 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/helpers/xml_helper.html |
| 38 | */ |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | /** |
| 43 | * Create CAPTCHA |
| 44 | * |
| 45 | * @access public |
| 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 | */ |
| 52 | if ( ! function_exists('create_captcha')) |
| 53 | { |
| 54 | function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | { |
| 56 | $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200); |
| 57 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 58 | foreach ($defaults as $key => $val) |
| 59 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 60 | if ( ! is_array($data) && ( ! isset($$key) OR $$key == '')) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 61 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 62 | $$key = $val; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 63 | } |
| 64 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 66 | $$key = ( ! isset($data[$key])) ? $val : $data[$key]; |
| 67 | } |
| 68 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame^] | 70 | if ($img_path == '' OR $img_url == '' |
| 71 | OR ! @is_dir($img_path) OR ! is_writeable($img_path) |
| 72 | OR ! extension_loaded('gd')) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 73 | { |
| 74 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | } |
| 76 | |
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 | // Remove old images |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 79 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 81 | list($usec, $sec) = explode(' ', microtime()); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 82 | $now = ((float)$usec + (float)$sec); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 83 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 84 | $current_dir = @opendir($img_path); |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 85 | while ($filename = @readdir($current_dir)) |
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 | if ($filename !== '.' && $filename !== '..' && $filename !== 'index.html' |
| 88 | && (str_replace('.jpg', '', $filename) + $expiration) < $now) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 89 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 90 | @unlink($img_path.$filename); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 91 | } |
| 92 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 94 | @closedir($current_dir); |
| 95 | |
| 96 | // ----------------------------------- |
| 97 | // Do we have a "word" yet? |
| 98 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 99 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 100 | if ($word == '') |
| 101 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 102 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 103 | $word = ''; |
| 104 | for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < 8; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 105 | { |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame^] | 106 | $word .= $pool[mt_rand(0, $mt_rand_max)]; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 107 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 108 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 109 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 111 | // ----------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | // Determine angle and position |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 113 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 114 | $length = strlen($word); |
| 115 | $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | $x_axis = rand(6, (360/$length)-16); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 117 | $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] | 118 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 119 | // Create image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 120 | // PHP.net recommends imagecreatetruecolor(), but it isn't always available |
Andrey Andreev | 4f2933d | 2012-01-08 06:49:49 +0200 | [diff] [blame^] | 121 | $im = function_exists('imagecreatetruecolor') |
| 122 | ? imagecreatetruecolor($img_width, $img_height) |
| 123 | : imagecreate($img_width, $img_height); |
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 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 126 | // Assign colors |
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 | $bg_color = imagecolorallocate ($im, 255, 255, 255); |
| 129 | $border_color = imagecolorallocate ($im, 153, 102, 102); |
| 130 | $text_color = imagecolorallocate ($im, 204, 153, 153); |
| 131 | $grid_color = imagecolorallocate($im, 255, 182, 182); |
| 132 | $shadow_color = imagecolorallocate($im, 255, 240, 240); |
| 133 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 134 | // Create the rectangle |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 135 | ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 137 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 138 | // Create the spiral pattern |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 139 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 140 | $theta = 1; |
| 141 | $thetac = 7; |
| 142 | $radius = 16; |
| 143 | $circles = 20; |
| 144 | $points = 32; |
| 145 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 146 | for ($i = 0, $cp = $circles * $points; $i < $cp - 1; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 147 | { |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 148 | $theta += $thetac; |
| 149 | $rad = $radius * ($i / $points); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 150 | $x = ($rad * cos($theta)) + $x_axis; |
| 151 | $y = ($rad * sin($theta)) + $y_axis; |
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 | $rad1 = $radius * (($i + 1) / $points); |
| 154 | $x1 = ($rad1 * cos($theta)) + $x_axis; |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 155 | $y1 = ($rad1 * sin($theta)) + $y_axis; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 156 | imageline($im, $x, $y, $x1, $y1, $grid_color); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 157 | $theta -= $thetac; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 161 | // Write the text |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 162 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 163 | $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 165 | if ($use_font == FALSE) |
| 166 | { |
| 167 | $font_size = 5; |
| 168 | $x = rand(0, $img_width/($length/3)); |
| 169 | $y = 0; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | $font_size = 16; |
| 174 | $x = rand(0, $img_width/($length/1.5)); |
| 175 | $y = $font_size+2; |
| 176 | } |
| 177 | |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 178 | for ($i = 0; $i < $length; $i++) |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 179 | { |
| 180 | if ($use_font == FALSE) |
| 181 | { |
| 182 | $y = rand(0 , $img_height/2); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 183 | imagestring($im, $font_size, $x, $y, $word[$i], $text_color); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 184 | $x += ($font_size*2); |
| 185 | } |
| 186 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | { |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 188 | $y = rand($img_height/2, $img_height-3); |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 189 | 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] | 190 | $x += $font_size; |
| 191 | } |
| 192 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 194 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 195 | // Create the border |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 197 | |
| 198 | // ----------------------------------- |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 199 | // Generate the image |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 200 | // ----------------------------------- |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 201 | $img_name = $now.'.jpg'; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 202 | ImageJPEG($im, $img_path.$img_name); |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 203 | $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />"; |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 204 | ImageDestroy($im); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | |
Derek Jones | b41032d | 2010-03-05 11:22:45 -0600 | [diff] [blame] | 206 | return array('word' => $word, 'time' => $now, 'image' => $img); |
| 207 | } |
| 208 | } |
| 209 | |
Derek Jones | 1c73f4e | 2010-03-05 11:27:41 -0600 | [diff] [blame] | 210 | /* End of file captcha_helper.php */ |
Andrey Andreev | f1b43d4 | 2012-01-06 14:41:50 +0200 | [diff] [blame] | 211 | /* Location: ./system/helpers/captcha_helper.php */ |