lxsphp /Framework/Component/Captcha.php

Language PHP Lines 196
MD5 Hash bf183bdbd0d6c829bdc83d6303ac12b9 Estimated Cost $2,611 (why?)
Repository http://lxsphp.googlecode.com/svn/trunk/ View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
 * ????
 * 
 * [Code]
 * $c = App::C('Captcha');
 * $c->display(1, array('bordercolor'=>'#C5C5C5', 'width'=>148, 'height'=>26));
 * [/Code]
 * 
 * @version $Id: Captcha.php 312 2012-03-06 09:00:15Z linsir123 $
 * @package Component
 */
class comCaptcha
{
	/**
	 * @var ??,??????????
	 */
	public $options = array('bordercolor'=>null, 'width'=>70, 'height'=>21);
	
	/**
	 * @var ???
	 */
	public $code;
	
	/**
	 * @var ????
	 */
	public $expired;
	
	/**
	 * @var ????
	 */
	private $_im;
	
	/**
	 * @var ????
	 */
	private $_width;
	
	/**
	 * @var ????
	 */
	private $_height;
	
	/**
     * ????
     */
	public function __construct()
	{
		@session_start();
		$this->code = isset($_SESSION['CaptchaCode']) ? $_SESSION['CaptchaCode'] : '';
		$this->expired = isset($_SESSION['CaptchaExpired']) ? $_SESSION['CaptchaExpired'] : 0;
	}
	
	/**
	 * ????
	 * 
	 * @param int $type ???????
	 * @param array $options ????
	 */
	public function display($type = 0, $options = array())
	{
		$this->_code($type);
		
		///
		if (is_array($options)) 
			$this->options = array_merge($this->options, $options);
			
		$this->_width = intval($this->options['width']);
		$this->_height = intval($this->options['height']);
		$this->_background();
		$this->_ttfont();
		
		///
		header('Content-type:image/png');
		imagepng($this->_im);
		imagedestroy($this->_im);
	}
    
	/**
	 * ???????????
	 * 
	 * @param string $code ????????
	 * @return bool
	 */
	public function check($code)
	{
		return (time() < $this->expired && strtoupper($code) == strtoupper($this->code));
	}
	
	
	/// ??????? ///
	
	
	/**
	 * ????
	 */
	private function _background()
	{
		$this->_im = imagecreate($this->_width, $this->_height);
		
		///
		for($i = 0; $i < 3; $i++) {
			$start[$i] = mt_rand(200, 255);
			$end[$i] = mt_rand(100, 150);
			$step[$i] = ($end[$i] - $start[$i]) / $this->_width;
			$c[$i] = $start[$i];
		}
		for($i = 0; $i < $this->_width; $i++) {
			$color = imagecolorallocate($this->_im, $c[0], $c[1], $c[2]);
			imageline($this->_im, $i, 0, $i, $this->_height, $color);
			$c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];
		}
		
		///
		$linenums = $this->_height / 10;
		for($i=0; $i <= $linenums; $i++) {
			$color = imagecolorallocate($this->_im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
			$x = mt_rand(0, $this->_width);
			$y = mt_rand(0, $this->_height);
			if (mt_rand(0, 1))
				imagearc($this->_im, $x, $y, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, 360), mt_rand(0, 360), $color);
			else
				imageline($this->_im, $x, $y, $linex + mt_rand(0, $linemaxlong), $liney + mt_rand(0, mt_rand($this->_height, $this->_width)), $color);
		}
		
		///
		if ($this->options['bordercolor']) {
			list($r, $g, $b) = lpString::hex2rgb($this->options['bordercolor']);
			$color = imagecolorallocate($this->_im, $r, $g, $b);
			imagerectangle($this->_im, 0, 0, $this->_width - 1, $this->_height - 1, $color);
		}
	}
	
	/**
	 * ????
	 */
	private function _ttfont()
	{
		$widthtotal = 0;
		$seccode = $this->code;
		$seccodelength = strlen($seccode);
		for ($i = 0; $i < $seccodelength; $i++) {
			$font[$i]['font'] = dirname(__FILE__).DS.'fonts/verdanab.ttf';
			$font[$i]['size'] = 12;
			$box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);
			$font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);
			$font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this->_width / 8);
			$font[$i]['width'] = $font[$i]['width'] > $this->_width / $seccodelength ? $this->_width / $seccodelength : $font[$i]['width'];
			$widthtotal += $font[$i]['width'];
		}
		
		///
		$x = mt_rand(1, $this->_width - $widthtotal);
		for ($i = 0; $i < $seccodelength; $i++) {
			$fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
			$text_shadowcolor = imagecolorallocate($this->_im, 255 - $fontcolor[0], 255 - $fontcolor[1], 255 - $fontcolor[2]);
			$text_color = imagecolorallocate($this->_im, $fontcolor[0], $fontcolor[1], $fontcolor[2]);
			$y = mt_rand($font[$i]['height'] + 2, $this->_height - 5);
			imagettftext($this->_im, $font[$i]['size'], 0, $x + 1, $y + 1, $text_shadowcolor, $font[$i]['font'], $seccode[$i]);
			imagettftext($this->_im, $font[$i]['size'], 0, $x, $y, $text_color, $font[$i]['font'], $seccode[$i]);
			$x += $font[$i]['width'];
		}
	}
	
	/**
	 * ?????
	 * 
	 * @param int $type ???????
	 */
	private function _code($type)
	{
		switch ($type) {
			case 1:
				$seed = '0123456789';
				break;
			case 2:
				$seed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
				break;
			default:
				$seed = '012346789ABCDEFGHJKLMNPQRTUVWXYZabcdefghijklmnopqrstuvwxyz';
				break;
		}
		
		///
		$this->code = '';
		list($usec, $sec) = explode(' ', microtime());
		srand($sec + $usec * 100000);
		for ($i = 0; $i < 4; $i++) 
			$this->code .= substr($seed, rand(0, strlen($seed) - 1), 1);
			
		$_SESSION['CaptchaCode'] = $this->code;
		$_SESSION['CaptchaExpired'] = $this->expired = time() + 900;
	}
}
Back to Top