View file captcha.php

File size: 3.52Kb
<?php
# Captcha v1.0 by Wapweb #
require_once ('inc/db.php');
// Соединение с базой
$dbcnx = mysql_connect($db_host,$db_user,$db_pass);
if (!$dbcnx) {exit ("Сервер базы данных не доступен");}
if (!mysql_select_db($db_name , $dbcnx)){exit ("База данных не доступна");}
mysql_query('SET NAMES "utf8"');
$CAPTCHA = mysql_fetch_array(mysql_query("SELECT `set` FROM `modules` WHERE `name` = 'captcha' LIMIT 1"));
$CAPTCHA_SET = unserialize($CAPTCHA['set']);
//Настройки по умолчанию
$width = 180;
$heigh = 90;
$letters = range('0','9');
$font_size = 10;
/////////////////////////////////
$width = $CAPTCHA_SET['width'];
$height = $CAPTCHA_SET['height'];
$font_size = $CAPTCHA_SET['size'];
$sign = rand(3,6);
$code = '';
ob_start();
session_start();
error_reporting(E_ALL & ~E_NOTICE);
if ($CAPTCHA_SET['type'] == 0) {
	$letters = range('0','9');
}elseif ($CAPTCHA_SET['type'] == 1) {
	$letters = range('a','z');
} else {
	$letters = array('a','b','c','d','f','g','h','i','j','k','m','n','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z','0','1','2','3','4','5','6','7','8','9');
}
$figures = array('10','20','30','50','70','90','110','130','150','170','190','210');
$img = imagecreatetruecolor($width,$height);
$f1 = rand(200,255);
$f2 = rand(200,255);
$f3 = rand(200,255);
$fon = imagecolorallocate($img , $f1,$f2,$f3);
imagefill($img,0,0,$fon);
	$figures1 = range(120,255);
	$color1 = imagecolorallocatealpha(
	         $img,
			 $figures1[rand(0,count($figures1)-1)],
			 $figures1[rand(0,count($figures1)-1)],
			 $figures1[rand(0,count($figures1)-1)],
			 rand(10,30));

for ($j=0; $j<$sign; $j++) {
 imageline($img, 0, rand(0,24), 130, rand(0,40),$color1);
}
 for ($s=0; $s<80; $s++) {
 imagesetpixel(
		$img,
		rand(0,$width),	
        rand(0,$height),
        $color);	
		}
		
	$fontsdir = 'images/captcha';
	$fonts = array ();
    $fontsdir_absolute = dirname(__FILE__) . '/' . $fontsdir;
		if (($handle = opendir($fontsdir_absolute)) !== false) {
            while (false !== ($file = readdir($handle))) {
                if (preg_match('/\.ttf$/i', $file)) {
                    $fonts[] = $fontsdir_absolute . '/' . $file;
                }
            }
            closedir($handle);
        }	
for ($i=0; $i<$sign; $i++) {
     $h =1;
	 $color =  imagecolorallocatealpha(
	         $img,
			 $figures[rand(0,count($figures)-1)],
			 $figures[rand(0,count($figures)-1)],
			 $figures[rand(0,count($figures)-1)],
			 rand(10,30));
	 
	 $letter = $letters[rand(0,sizeof($letters)-1)];
	 
	 if (empty($x)) $x = $width*0.08;
	 else $x = $x + ($width*0.8)/$sign + rand(0,$width*0.02);
	 if ($h == rand(1,2)) $y = (($height*1)/4) + rand (0,$height*0.1);
	 else $y = (($height*1)/4) - rand(0,$height*0.1);
	 
	$code.=$letter;
	if($h == rand(0,1)) $letter = strtoupper($letter);
	$sl = rand(8,20);
	$index = rand(0,count($fonts) - 1);

	$t = rand(3,10);
	imagettftext($img,$font_size,0,($x+$sl-$t),($y+$sl),$color,$fonts[$index],$letter);
}

        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Cache-Control: no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', FALSE);
		header('Pragma: no-cache');
		header("Content-type: image/jpeg");
		imagejpeg($img);
		header('Content-Disposition: inline; filename=' . ((rand(10, 9999))) . '.jpg');
		header('Content-Length: ' . ob_get_length());
        ob_end_flush();
		$_SESSION['code'] = $code;
?>