Subversion Repositories SmartDukaan

Rev

Rev 11272 | 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
	   {
15836 anikendra 115
			// $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
116
	   		$pool = '23456789';
10582 lgm 117
 
118
			$str = '';
119
			for ($i = 0; $i < $word_length; $i++)
120
			{
121
				$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
122
			}
123
 
124
			$word = $str;
125
	   }
126
 
127
		// -----------------------------------
128
		// Determine angle and position
129
		// -----------------------------------
130
 
131
		$length	= strlen($word);
132
		$angle	= ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
133
		$x_axis	= rand(6, (360/$length)-16);
134
		$y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
135
 
136
		// -----------------------------------
137
		// Create image
138
		// -----------------------------------
139
 
140
		// PHP.net recommends imagecreatetruecolor(), but it isn't always available
141
		if (function_exists('imagecreatetruecolor'))
142
		{
143
			$im = imagecreatetruecolor($img_width, $img_height);
144
		}
145
		else
146
		{
147
			$im = imagecreate($img_width, $img_height);
148
		}
149
 
150
		// -----------------------------------
151
		//  Assign colors
152
		// -----------------------------------
153
 
154
		$bg_color		= imagecolorallocate ($im, 255, 255, 255);
155
		$border_color	= imagecolorallocate ($im, 153, 102, 102);
10621 lgm 156
		$text_color		= imagecolorallocate ($im, 0, 0, 0);
10582 lgm 157
		$grid_color		= imagecolorallocate($im, 255, 182, 182);
158
		$shadow_color	= imagecolorallocate($im, 255, 240, 240);
159
 
160
		// -----------------------------------
161
		//  Create the rectangle
162
		// -----------------------------------
163
 
10621 lgm 164
		ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
10582 lgm 165
 
166
		// -----------------------------------
167
		//  Create the spiral pattern
168
		// -----------------------------------
169
 
170
		$theta		= 1;
171
		$thetac		= 7;
172
		$radius		= 16;
173
		$circles	= 20;
174
		$points		= 32;
175
 
176
		for ($i = 0; $i < ($circles * $points) - 1; $i++)
177
		{
178
			$theta = $theta + $thetac;
179
			$rad = $radius * ($i / $points );
180
			$x = ($rad * cos($theta)) + $x_axis;
181
			$y = ($rad * sin($theta)) + $y_axis;
182
			$theta = $theta + $thetac;
183
			$rad1 = $radius * (($i + 1) / $points);
184
			$x1 = ($rad1 * cos($theta)) + $x_axis;
185
			$y1 = ($rad1 * sin($theta )) + $y_axis;
186
			imageline($im, $x, $y, $x1, $y1, $grid_color);
187
			$theta = $theta - $thetac;
188
		}
189
 
190
		// -----------------------------------
191
		//  Write the text
192
		// -----------------------------------
193
 
194
		$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
195
 
196
		if ($use_font == FALSE)
10631 lgm 197
		{
11272 lgm 198
			$font_size = 30;
10582 lgm 199
			$x = rand(0, $img_width/($length/3));
200
			$y = 0;
201
		}
202
		else
203
		{
11123 lgm 204
			$font_size	= 30;
10582 lgm 205
			$x = rand(0, $img_width/($length/1.5));
206
			$y = $font_size+2;
207
		}
208
 
209
		for ($i = 0; $i < strlen($word); $i++)
210
		{
211
			if ($use_font == FALSE)
212
			{
213
				$y = rand(0 , $img_height/2);
11272 lgm 214
				imagettftext($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
10582 lgm 215
				$x += ($font_size*2);
216
			}
217
			else
218
			{
219
				$y = rand($img_height/2, $img_height-3);
220
				imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
221
				$x += $font_size;
222
			}
223
		}
224
 
225
 
226
		// -----------------------------------
227
		//  Create the border
228
		// -----------------------------------
229
 
230
		imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
231
 
232
		// -----------------------------------
233
		//  Generate the image
234
		// -----------------------------------
235
 
10655 lgm 236
		$img_name = $now.'.png';
11272 lgm 237
 
238
	//var_dump(imagepng($im, $img_path.$img_name));
239
 
240
		//var_dump(($im));
241
		ob_start();
242
		imagepng($im);
243
		// Capture the output
244
		$imagedata = ob_get_contents();
245
		// Clear the output buffer
246
		ob_end_clean();
10582 lgm 247
		$img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
10621 lgm 248
		ImageDestroy($im);
11272 lgm 249
		ob_end_clean();
250
		//echo '<img src="data:image/png;base64,'.base64_encode($image).'" />';
251
		return array('word' => $word, 'time' => $now, 'name' => $img_name, 'imagedata' => base64_encode($imagedata));
10582 lgm 252
	}
253
}
254
 
255
// ------------------------------------------------------------------------
256
 
257
/* End of file captcha_helper.php */
10621 lgm 258
/* Location: ./system/heleprs/captcha_helper.php */