Code formatting

This commit is contained in:
Anton Terekhov
2011-10-13 14:55:06 +04:00
parent 642c5909aa
commit 3ebfac9db4
12 changed files with 92 additions and 86 deletions

View File

@ -11,47 +11,49 @@
class Captcha
{
protected $font_size = 38;
protected $width = 200;
protected $height = 70;
protected $width = 200;
protected $height = 70;
public function getImage($token)
{
$font = dirname(__FILE__) . '/poh.ttf';
$image = imagecreatetruecolor($this->width, $this->height);
// белый фон
$background = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $this->width, $this->height, $background);
// основная магия тут
if (Session::get('_ctoken') == $token && Session::get('_ccode')) {
$code = Session::get('_ccode');
$color = imagecolorallocate($image, 81, 81, 81);
for ($j = 0; $j < 5; $j++) {
imageellipse($image, rand(-$this->width, $this->width * 2),
rand(-$this->height, $this->height * 2),
imageellipse($image, rand(-$this->width, $this->width * 2),
rand(-$this->height, $this->height * 2),
$this->width, $this->width, $color);
}
$letters = preg_split("//u", strtoupper($code));
$length = count($letters);
$step = floor($this->width / $length);
$i = 2;
foreach ($letters as $key => $letter) {
imagettftext($image, $this->font_size, 0,
rand($i + 2, $i + 4), ($this->font_size + $this->height) / 2,
imagettftext($image, $this->font_size, 0,
rand($i + 2, $i + 4), ($this->font_size + $this->height) / 2,
$color, $font, $letter);
$i = $i + $step ;
$i = $i + $step;
}
}
ob_start();
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Content-Type: image/jpg");
@ -59,7 +61,7 @@ class Captcha
imagedestroy($image);
return ob_get_clean();
}
public function getToken()
{
$token = md5(microtime() . uniqid());
@ -68,10 +70,10 @@ class Captcha
Session::set('_ccode', $code);
return $token;
}
public function checkCode($token, $code)
{
if (Session::get('_ctoken') == $token && Session::get('_ccode') == strtoupper($code)){
if (Session::get('_ctoken') == $token && Session::get('_ccode') == strtoupper($code)) {
// Housekeeping
Session::del('_ccode');
Session::del('_ctoken');

View File

@ -11,11 +11,11 @@
class CaptchaImageAction
{
public function __construct()
{
$captcha = new Captcha();
echo $captcha->getImage(Env::Get('ctoken'));
exit();
exit();
}
}

View File

@ -13,9 +13,9 @@ class CaptchaValidator extends Validator
{
const WRONG_CODE = 'is_empty';
protected $templates = array(self::WRONG_CODE => 'Entered code wrong');
public function isValid($value, $context = null)
{
if (!$context || !isset($context['ctoken']) || !isset($context['ccode'])) {