/nova/lib/vcode.class.php
PHP | 119 lines | 88 code | 24 blank | 7 comment | 14 complexity | 97f91723e579f44eea8f92fb2e584c56 MD5 | raw file
Possible License(s): AGPL-1.0
- <?php
- !defined('IN_NOVA') && exit('Access Denied!');
-
- class Vcode {
- private $sessionvar = 'vdcode'; //Session????
-
- public function show( $code_name = 'vdcode', $code_type = 1, $width = 50, $height = 20 ) {
- $this->sessionvar = $code_name;
-
- if (function_exists('imagecreate')) {
-
- //??+??????
- if ( $code_type == 1 ) {
-
- //??4?????????
- $str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- $len = strlen($str);
- $code = array();
- for ($i=0; $i<4; $i++) {
- $code[] = $str{mt_rand(0,$len-1)};
- }
-
- $_SESSION[$this->sessionvar] = strtolower(implode('',$code));
-
- $img = ImageCreate($width,$height); //????
- ImageColorAllocate($img,255,255,255); //????
-
- //????
- for ($i=0; $i<100; $i++) {
- $pxcolor = ImageColorAllocate($img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
- ImagesetPixel($img,mt_rand(0,$width),mt_rand(0,$height),$pxcolor);
- }
-
- //????
- $bordercolor = ImageColorAllocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
- ImageRectangle($img,0,0,$width-1,$height-1,$bordercolor);
-
- //???????
- $offset = round($width/4/2);
- $h1 = 0;
- $h2 = round($height/3);
- foreach ($code as $char) {
- $textcolor = ImageColorAllocate($img,mt_rand(0,250),mt_rand(0,150),mt_rand(0,250));
- ImageChar($img,5,$offset + mt_rand(-5,round($width/4/3)), mt_rand($h1, $h2),$char,$textcolor);
- $offset += round($width/4)-3;
- }
-
- //?????????
- } elseif ( $code_type == 2 ) {
-
- $operator = '+-*'; //???
-
- $code = array();
- $code[] = mt_rand(1,9);
- $code[] = $operator{mt_rand(0,2)};
- $code[] = mt_rand(1,9);
- $code[] = $operator{mt_rand(0,2)};
- $code[] = mt_rand(1,9);
- $codestr = implode('',$code);
- eval("\$result = ".implode('',$code).";");
- $code[] = '=';
-
- $_SESSION[$this->sessionvar] = $result;
-
- $img = ImageCreate($width,$height);
- ImageColorAllocate($img, mt_rand(230,250), mt_rand(230,250), mt_rand(230,250));
- $color = ImageColorAllocate($img, 0, 0, 0);
-
- $offset = round($width/6/2);
- $h1 = 0;
- $h2 = round($height/6);
- foreach ($code as $char) {
-
- $txtcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,150), mt_rand(0,255));
- ImageChar($img, mt_rand(3,5), $offset + mt_rand(-5,round($width/6/3)), mt_rand($h1, $h2), $char, $txtcolor);
- $offset += round($width/6)-3;
-
- }
- for ($i=0; $i<100; $i++) {
-
- $pxcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
- ImagesetPixel($img, mt_rand(0,$width), mt_rand(0,$height), $pxcolor);
- }
-
- }
-
- header("pragma:no-cache\r\n");
- header("Cache-Control:no-cache\r\n");
- header("Expires:0\r\n");
- //????????????????PNG->JPEG->GIF
- if (Imagetypes() & IMG_PNG) {
- header('content-type:image/png');
- ImagePNG($img);
- } elseif (Imagetypes() & IMG_JPEG) {
- header('content-type:image/jpeg');
- ImageJPEG($img);
- } else {
- header('content-type:image/gif');
- ImageGif($img);
- }
- }
- }
- public function check( $code ) {
- if( !isset( $_SESSION[$this->sessionvar] ) ) {
- return false;
- }
- if( $code != $_SESSION[$this->sessionvar] ) {
- unset( $_SESSION[$this->sessionvar] );
- return false;
- } else {
- unset( $_SESSION[$this->sessionvar] );
- return true;
- }
- }
-
- }
- ?>