Subversion Repositories SmartDukaan

Rev

Rev 11126 | Rev 11272 | 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
	{
44
		$defaults = array('word'      => '',    'word_length' => 5,
45
                  'img_path'  => '',    'img_url'     => '',
46
                  'img_width' => '150', 'img_height'  => '30',
47
                  'font_path' => '',    'expiration'  => 7200);
48
 
49
		foreach ($defaults as $key => $val)
50
		{
51
			if ( ! is_array($data))
52
			{
53
				if ( ! isset($$key) OR $$key == '')
54
				{
55
					$$key = $val;
56
				}
57
			}
58
			else
59
			{
60
				$$key = ( ! isset($data[$key])) ? $val : $data[$key];
61
			}
62
		}
63
 
64
		if ($img_path == '' OR $img_url == '')
65
		{
66
			return FALSE;
67
		}
68
 
69
		if ( ! @is_dir($img_path))
70
		{
71
			return FALSE;
72
		}
73
 
74
		if ( ! is_writable($img_path))
75
		{
76
			return FALSE;
77
		}
78
 
79
		if ( ! extension_loaded('gd'))
80
		{
81
			return FALSE;
82
		}
83
 
84
		// -----------------------------------
85
		// Remove old images
86
		// -----------------------------------
87
 
88
		list($usec, $sec) = explode(" ", microtime());
89
		$now = ((float)$usec + (float)$sec);
90
 
91
		$current_dir = @opendir($img_path);
92
 
93
		while ($filename = @readdir($current_dir))
94
		{
10621 lgm 95
			if ($filename != "." and $filename != ".." and $filename != "index.html")
10582 lgm 96
			{
11123 lgm 97
				$name = str_replace(".png", "", $filename);
10582 lgm 98
 
99
				if (($name + $expiration) < $now)
100
				{
101
					@unlink($img_path.$filename);
102
				}
103
			}
104
		}
105
 
106
		@closedir($current_dir);
107
 
108
		// -----------------------------------
109
		// Do we have a "word" yet?
110
		// -----------------------------------
111
 
112
	   if ($word == '')
113
	   {
114
			$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
115
 
116
			$str = '';
117
			for ($i = 0; $i < $word_length; $i++)
118
			{
119
				$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
120
			}
121
 
122
			$word = $str;
123
	   }
124
 
125
		// -----------------------------------
126
		// Determine angle and position
127
		// -----------------------------------
128
 
129
		$length	= strlen($word);
130
		$angle	= ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
131
		$x_axis	= rand(6, (360/$length)-16);
132
		$y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
133
 
134
		// -----------------------------------
135
		// Create image
136
		// -----------------------------------
137
 
138
		// PHP.net recommends imagecreatetruecolor(), but it isn't always available
139
		if (function_exists('imagecreatetruecolor'))
140
		{
141
			$im = imagecreatetruecolor($img_width, $img_height);
142
		}
143
		else
144
		{
145
			$im = imagecreate($img_width, $img_height);
146
		}
147
 
148
		// -----------------------------------
149
		//  Assign colors
150
		// -----------------------------------
151
 
152
		$bg_color		= imagecolorallocate ($im, 255, 255, 255);
153
		$border_color	= imagecolorallocate ($im, 153, 102, 102);
10621 lgm 154
		$text_color		= imagecolorallocate ($im, 0, 0, 0);
10582 lgm 155
		$grid_color		= imagecolorallocate($im, 255, 182, 182);
156
		$shadow_color	= imagecolorallocate($im, 255, 240, 240);
157
 
158
		// -----------------------------------
159
		//  Create the rectangle
160
		// -----------------------------------
161
 
10621 lgm 162
		ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
10582 lgm 163
 
164
		// -----------------------------------
165
		//  Create the spiral pattern
166
		// -----------------------------------
167
 
168
		$theta		= 1;
169
		$thetac		= 7;
170
		$radius		= 16;
171
		$circles	= 20;
172
		$points		= 32;
173
 
174
		for ($i = 0; $i < ($circles * $points) - 1; $i++)
175
		{
176
			$theta = $theta + $thetac;
177
			$rad = $radius * ($i / $points );
178
			$x = ($rad * cos($theta)) + $x_axis;
179
			$y = ($rad * sin($theta)) + $y_axis;
180
			$theta = $theta + $thetac;
181
			$rad1 = $radius * (($i + 1) / $points);
182
			$x1 = ($rad1 * cos($theta)) + $x_axis;
183
			$y1 = ($rad1 * sin($theta )) + $y_axis;
184
			imageline($im, $x, $y, $x1, $y1, $grid_color);
185
			$theta = $theta - $thetac;
186
		}
187
 
188
		// -----------------------------------
189
		//  Write the text
190
		// -----------------------------------
191
 
192
		$use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
193
 
194
		if ($use_font == FALSE)
10631 lgm 195
		{
11128 lgm 196
			$font_size = 14;
10582 lgm 197
			$x = rand(0, $img_width/($length/3));
198
			$y = 0;
199
		}
200
		else
201
		{
11123 lgm 202
			$font_size	= 30;
10582 lgm 203
			$x = rand(0, $img_width/($length/1.5));
204
			$y = $font_size+2;
205
		}
206
 
207
		for ($i = 0; $i < strlen($word); $i++)
208
		{
209
			if ($use_font == FALSE)
210
			{
211
				$y = rand(0 , $img_height/2);
11126 lgm 212
				imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
10582 lgm 213
				$x += ($font_size*2);
214
			}
215
			else
216
			{
217
				$y = rand($img_height/2, $img_height-3);
218
				imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
219
				$x += $font_size;
220
			}
221
		}
222
 
223
 
224
		// -----------------------------------
225
		//  Create the border
226
		// -----------------------------------
227
 
228
		imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
229
 
230
		// -----------------------------------
231
		//  Generate the image
232
		// -----------------------------------
233
 
10655 lgm 234
		$img_name = $now.'.png';
10582 lgm 235
 
10655 lgm 236
		imagepng($im, $img_path.$img_name);
10621 lgm 237
 
10582 lgm 238
		$img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
239
 
10621 lgm 240
		ImageDestroy($im);
241
 
10582 lgm 242
		return array('word' => $word, 'time' => $now, 'image' => $img, 'name' => $img_name);
243
	}
244
}
245
 
246
// ------------------------------------------------------------------------
247
 
248
/* End of file captcha_helper.php */
10621 lgm 249
/* Location: ./system/heleprs/captcha_helper.php */