Файл: sys/secure/captcha.php
Строк: 75
<?php
session_start();
class captcha {
public function generate($length, $case){
$symbols = '1234567890';
$value;
for($i = 0;$i < $length;$i++){
$to = $symbols[rand(0, strlen($symbols))];
$value .= $case ? ($case === 1 ? mb_strtoupper($to) : ($case === 2 ? (rand(0, 1) == 1 ? mb_strtoupper($to) : $to) : $to)) : $to;
};
return $value;
}
public function draw($path, $backgrounds, $font, $size, $length = 6, $case = 0, $red = null, $green = null, $blue = null) {
header('Content-Type: image/png');
$session;
$code = captcha::generate($length, $case);
$background = $path . $backgrounds[rand(0, sizeof($backgrounds) - 1)];
$image = imagecreatefrompng($background);
$sizes = getimagesize($background);
$i = rand((-($size * $length - $sizes[0])) / 2,(-($size * $length - $sizes[0])) / 2 + $size / 1.5);
for($n = 0; $n < strlen($code); $n++){
$value = $code[$n];
$session .= $value;
$color = imagecolorallocate($image, $red ? $red : rand(0, 200), $green ? $green : rand(0, 200), $blue ? $blue : rand(0, 200));
imagettftext($image, $size, rand( -4, 4), $i, rand(($sizes[1] + $size) / 2,($sizes[1] + $size) / 3), $color, $path . $font, $value);
$i = $i +(rand($size - 4, $size - 2));
};
$_SESSION['seckey' . md5(date('d.m.H'))] = md5(md5(strtoupper($session)));
imagepng($image);
imagedestroy($image);
}
public function session() {
return 'seckey' . md5(date('d.m.H'));
}
public function check($code, $session = null) {
if(!$session) $session = 'seckey' . md5(date('d.m.H'));
if($_SESSION[$session] == md5(md5(strtoupper($code)))) return true;
else return false;
}
public function destroy($session = null) {
if(!$session) $session = md5(date('d.m.H'));
session_unset($_SESSION['seckey' . $session]);
}
};
?>