/Framework/Component/Captcha.php
PHP | 195 lines | 109 code | 23 blank | 63 comment | 12 complexity | bf183bdbd0d6c829bdc83d6303ac12b9 MD5 | raw file
Possible License(s): LGPL-2.1
- <?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;
- }
- }