Subversion Repositories SmartDukaan

Rev

Rev 11128 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP 5.1.6 or newer
6
 *
7
 * @package		CodeIgniter
8
 * @author		ExpressionEngine Dev Team
9
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
10
 * @license		http://codeigniter.com/user_guide/license.html
11
 * @link		http://codeigniter.com
12
 * @since		Version 1.0
13
 * @filesource
14
 */
15
 
16
// ------------------------------------------------------------------------
17
 
18
/**
19
 * CodeIgniter CAPTCHA Helper
20
 *
21
 * @package		CodeIgniter
22
 * @subpackage	Helpers
23
 * @category	Helpers
24
 * @author		ExpressionEngine Dev Team
25
 * @link		http://codeigniter.com/user_guide/helpers/xml_helper.html
26
 */
27
 
28
// ------------------------------------------------------------------------
29
 
30
/**
31
 * Create CAPTCHA
32
 *
33
 * @access	public
34
 * @param	array	array of data for the CAPTCHA
35
 * @param	string	path to create the image in
36
 * @param	string	URL to the CAPTCHA image folder
37
 * @param	string	server path to font
38
 * @return	string
39
 */
40
if ( ! function_exists('create_captcha'))
41
{
42
	function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
43
	{
11272 lgm 44
		ob_start();
10582 lgm 45
		$defaults = array('word'      => '',    'word_length' => 5,
46
                  'img_path'  => '',    'img_url'     => '',
47
                  'img_width' => '150', 'img_height'  => '30',
48
                  'font_path' => '',    'expiration'  => 7200);
49
 
50
		foreach ($defaults as $key => $val)
51
		{
52
			if ( ! is_array($data))
53
			{
54
				if ( ! isset($$key) OR $$key == '')
55
				{
56
					$$key = $val;
57
				}
58
			}
59
			else
60
			{
61
				$$key = ( ! isset($data[$key])) ? $val : $data[$key];
62
			}
63
		}
64
 
65
		if ($img_path == '' OR $img_url == '')
66
		{
67
			return FALSE;
68
		}
69
 
70
		if ( ! @is_dir($img_path))
71
		{
72
			return FALSE;
73
		}
74
 
75
		if ( ! is_writable($img_path))
76
		{
77
			return FALSE;
78
		}
79
 
80
		if ( ! extension_loaded('gd'))
81
		{
82
			return FALSE;
83
		}
84
 
85
		// -----------------------------------
86
		// Remove old images
87
		// -----------------------------------
88
 
89
		list($usec, $sec) = explode(" ", microtime());
90
		$now = ((float)$usec + (float)$sec);
91
 
92
		$current_dir = @opendir($img_path);
93
 
94
		while ($filename = @readdir($current_dir))
95
		{
10621 lgm 96
			if ($filename != "." and $filename != ".." and $filename != "index.html")
10582 lgm 97
			{
11123 lgm 98
				$name = str_replace(".png", "", $filename);
10582 lgm 99
 
100
				if (($name + $expiration) < $now)
101
				{
102
					@unlink($img_path.$filename);
103
				}
104
			}
105
		}
106
 
107
		@closedir($current_dir);
108
 
109
		// -----------------------------------
110
		// Do we have a "word" yet?
111
		// -----------------------------------
112
 
113
	   if ($word == '')
114
	   {
115
			$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
116
 
117
			$str = '';
118
			for ($i = 0; $i < $word_length; $i++)
119
			{
120
				$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
121
			}
122
 
123
			$word = $str;
124
	   }
125
 
126
		// -----------------------------------
127
		// Determine angle and position
128
		// -----------------------------------
129
 
130
		$length	= strlen($word);
131
		$angle	= ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
132
		$x_axis	= rand(6, (360/$length)-16);
133
		$y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
134
 
135
		// -----------------------------------
136
		// Create image
137
		// -----------------------------------
138
 
139
		// PHP.net recommends imagecreatetruecolor(), but it isn't always available
140
		if (function_exists('imagecreatetruecolor'))
141
		{
142
			$im = imagecreatetruecolor($img_width, $img_height);
143
		}
144
		else
145
		{
146
			$im = imagecreate($img_width, $img_height);
147
		}
148
 
149
		// -----------------------------------
150
		//  Assign colors
151
		// -----------------------------------
152
 
153
		$bg_color		= imagecolorallocate ($im, 255, 255, 255);
154
		$border_color	= imagecolorallocate ($im, 153, 102, 102);
10621 lgm 155
		$text_color		= imagecolorallocate ($im, 0, 0, 0);
10582 lgm 156
		$grid_color		= imagecolorallocate($im, 255, 182, 182);
157
		$shadow_color	= imagecolorallocate($im, 255, 240, 240);
158
 
159
		// -----------------------------------
160
		//  Create the rectangle
161
		// -----------------------------------
162
 
10621 lgm 163
		ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
10582 lgm 164
 
165
		// -----------------------------------
166
		//  Create the spiral pattern
167
		// -----------------------------------
168
 
169
		$theta		= 1;
170
		$thetac		= 7;
171
		$radius		= 16;
172
		$circles	= 20;
173
		$points		= 32;
174
 
175
		for ($i = 0; $i < ($circles * $points) - 1; $i++)
176
		{
177
			$theta = $theta + $thetac;
178
			$rad = $radius * ($i / $points );
179
			$x = ($rad * cos($theta)) + $x_axis;
180
			$y = ($rad * sin($theta)) + $y_axis;
181
			$theta = $theta + $thetac;
182
			$rad1 = $radius * (($i + 1) / $points);
183
			$x1 = ($rad1 * cos($theta)) + $x_axis;
184
			$y1 = ($rad1 * sin($theta )) + $y_axis;
185
			imageline($im, $x, $y, $x1, $y1, $grid_color);
186
			$theta = $theta - $thetac;
187
		}
188
 
189
		// -----------------------------------
190
		//  Write the text
191
		// -----------------------------------
192
 
193
		$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
194
 
195
		if ($use_font == FALSE)
10631 lgm 196
		{
11272 lgm 197
			$font_size = 30;
10582 lgm 198
			$x = rand(0, $img_width/($length/3));
199
			$y = 0;
200
		}
201
		else
202
		{
11123 lgm 203
			$font_size	= 30;
10582 lgm 204
			$x = rand(0, $img_width/($length/1.5));
205
			$y = $font_size+2;
206
		}
207
 
208
		for ($i = 0; $i < strlen($word); $i++)
209
		{
210
			if ($use_font == FALSE)
211
			{
212
				$y = rand(0 , $img_height/2);
11272 lgm 213
				imagettftext($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
10582 lgm 214
				$x += ($font_size*2);
215
			}
216
			else
217
			{
218
				$y = rand($img_height/2, $img_height-3);
219
				imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
220
				$x += $font_size;
221
			}
222
		}
223
 
224
 
225
		// -----------------------------------
226
		//  Create the border
227
		// -----------------------------------
228
 
229
		imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
230
 
231
		// -----------------------------------
232
		//  Generate the image
233
		// -----------------------------------
234
 
10655 lgm 235
		$img_name = $now.'.png';
11272 lgm 236
 
237
	//var_dump(imagepng($im, $img_path.$img_name));
238
 
239
		//var_dump(($im));
240
		ob_start();
241
		imagepng($im);
242
		// Capture the output
243
		$imagedata = ob_get_contents();
244
		// Clear the output buffer
245
		ob_end_clean();
10582 lgm 246
		$img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
10621 lgm 247
		ImageDestroy($im);
11272 lgm 248
		ob_end_clean();
249
		//echo '<img src="data:image/png;base64,'.base64_encode($image).'" />';
250
		return array('word' => $word, 'time' => $now, 'name' => $img_name, 'imagedata' => base64_encode($imagedata));
10582 lgm 251
	}
252
}
253
 
254
// ------------------------------------------------------------------------
255
 
256
/* End of file captcha_helper.php */
10621 lgm 257
/* Location: ./system/heleprs/captcha_helper.php */