Вход Регистрация
Файл: captcha.php
Строк: 190
<?php

//файл защиты от спама авторегистрации captcha
define('VAPHP', 1);
$CONF = array();
$FORM = array();
$TMPL = array();

$CONF['path'] = '.';

// Можено изменить большинство этих настроек, если хотите исправить captchi
$TTF_folder = "{$CONF['path']}/fonts/";
$chars = array(3, 4); // Сколько случайных сгенерированных символов должен иметь текст (default: 6 или 7)
$minsize = 15; // Минимальный размер случайных символов (default: 30-50)
$maxsize = 30; // Максимальный размер случайных символов (default: 50-70)
$maxrotation = 30; // Максимальные степени случайных символов. Установка 30 означает случайный перебор между -30 и 30.
$noise = 1; // Фоновый шум, Включения - выключения (1/0); если будет Выключено, то сетка будет создана
$debug = 0; // Outputs configuration values for testing
$jpegquality = 80; // качество изображения
$noisefactor = 4; // этот будет multiplyed с числом случайных символов

$ckey = array_rand($chars);
$chars = $chars[$ckey];

// Connect to the database
require_once("{$CONF['path']}/settings.php");
require_once(
"{$CONF['path']}/sources/mysql.php");
$DB = new sql_mysql;
$DB->connect($CONF['sql_host'], $CONF['sql_username'], $CONF['sql_password'], $CONF['sql_database']);

$string = mt_rand();
$string = substr(sha1($string), 0, $chars);
$string = str_replace(array('0', 'O', 'o', '9', 'g', 'G', 'b', 'B', '6'), rand(1, 5), $string);

$TTF_RANGE = array();
$dir = opendir($TTF_folder);
while (
false !== ($file = readdir($dir))) {
  if (
is_file($TTF_folder.$file)) {
    
array_push($TTF_RANGE, $file);
  }
}

// check TrueTypeFonts
if($debug) echo "n<br>-Captcha-Debug: Check given TrueType-Array! (".count($TTF_RANGE).")";
$temp = array();
foreach(
$TTF_RANGE as $key => $value) {
  if (
is_readable($TTF_folder.$value)) {
    
$temp[] = $value;
  }
}
$TTF_RANGE = $temp;
if(
$debug) echo "n<br>-Captcha-Debug: Valid TrueType-files: (".count($TTF_RANGE).")";
if(
count($TTF_RANGE) < 1) die('No Truetypefont available for the CaptchaClass.');

// select first TrueTypeFont
$TTF_file = change_TTF();
if(
$debug) echo "n<br>-Captcha-Debug: Set current TrueType-File: (".$TTF_file.")";

// get number of noise-chars for background if is enabled
$nb_noise = $noise ? ($chars * $noisefactor) : 0;
if(
$debug) echo "n<br>-Captcha-Debug: Set number of noise characters to: (".$nb_noise.")";

// set dimension of image
$lx = ($chars + 1) * (int)(($maxsize + $minsize) / 1.5);
$ly = (int)(2.4 * $maxsize);
if(
$debug) echo "n<br>-Captcha-Debug: Set image dimension to: (".$lx." x ".$ly.")";
$image = imagecreatetruecolor($lx, $ly);

// Set Backgroundcolor
list($r, $g, $b) = random_color(224, 256);
$back =  imagecolorallocate($image, $r, $g, $b);
imagefilledrectangle($image, 0, 0, $lx, $ly, $back);
if(
$debug) echo "n<br>-Captcha-Debug: We allocate one color for Background: (".$r."-".$g."-".$b.")";

// fill with noise or grid
if($nb_noise > 0) {
  
// random characters in background with random position, angle, color
  
if($debug) echo "n<br>-Captcha-Debug: Fill background with noise: (".$nb_noise.")";
  for(
$i=0; $i < $nb_noise; $i++) {
    
$size = intval(rand((int)($minsize / 2.3), (int)($maxsize / 1.7)));
    
$angle = intval(rand(0, 360));
    
$x = intval(rand(0, $lx));
    
$y = intval(rand(0, (int)($ly - ($size / 5))));
    list(
$r, $g, $b) = random_color(160, 224);
    
$color = imagecolorallocate($image, $r, $g, $b);
    
$text = chr(intval(rand(45,250)));
    
imagettftext($image, $size, $angle, $x, $y, $color, change_TTF(), $text);
  }
}
else {
  
// generate grid
  
if($debug) echo "n<br>-Captcha-Debug: Fill background with x-gridlines: (".(int)($lx / (int)($minsize / 1.5)).")";
  for(
$i=0; $i < $lx; $i += (int)($minsize / 1.5)) {
    list(
$r, $g, $b) = random_color(160, 224);
    
$color = imagecolorallocate($image, $r, $g, $b);
    
imageline($image, $i, 0, $i, $ly, $color);
  }
  if(
$debug) echo "n<br>-Captcha-Debug: Fill background with y-gridlines: (".(int)($ly / (int)(($minsize / 1.8))).")";
  for(
$i=0 ; $i < $ly; $i += (int)($minsize / 1.8)) {
    list(
$r, $g, $b) = random_color(160, 224);
    
$color = imagecolorallocate($image, $r, $g, $b);
    
imageline($image, 0, $i, $lx, $i, $color);
  }
}

// generate Text
if($debug) echo "n<br>-Captcha-Debug: Fill forground with chars and shadows: (".$chars.")";
for(
$i=0, $x = intval(rand($minsize,$maxsize)); $i < $chars; $i++) {
  
$text = $string{$i};
  
$angle = intval(rand(($maxrotation * -1), $maxrotation));
  
$size = intval(rand($minsize, $maxsize));
  
$y = intval(rand((int)($size * 1.5), (int)($ly - ($size / 7))));
  list(
$r, $g, $b) = random_color(0, 127);
  
$color = imagecolorallocate($image, $r, $g, $b);
  list(
$r, $g, $b) = random_color(0, 127);
  
$shadow = imagecolorallocate($image, $r + 127, $g + 127, $b + 127);
  
$TTF_file = change_TTF();
  
imagettftext($image, $size, $angle, $x + (int)($size / 15), $y, $shadow, $TTF_file, $text);
  
imagettftext($image, $size, $angle, $x, $y - (int)($size / 15), $color, $TTF_file, $text);
  
$x += (int)($size + ($minsize / 5));
}

$ip = $DB->escape($_SERVER['REMOTE_ADDR'], 1);

$DB->query("DELETE FROM {$CONF['sql_prefix']}_sessions WHERE data LIKE '{$ip}|%'", __FILE__, __LINE__);
$data = "{$ip}|" . sha1(')F*RJ@FHR^%X'.$string.'(*Ht3h7f9&^F'.$ip);

require_once(
"{$CONF['path']}/sources/session.php");
$session = new session;
$session->create('captcha', $data);

header('Pragma: no-cache');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

// Select a new random font
function change_TTF() 
{
  global 
$TTF_RANGE, $TTF_folder;
  
$key = array_rand($TTF_RANGE);
  
$TTF_file = $TTF_folder.$TTF_RANGE[$key];

  return 
$TTF_file;
}

// Select a random color within a range
function random_color($min, $max) 
{
  
$r = intval(mt_rand($min, $max));
  
$g = intval(mt_rand($min, $max));
  
$b = intval(mt_rand($min, $max));

  return array(
$r, $g, $b);
}
?>
Онлайн: 2
Реклама