Validator, captcha, form, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@141 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
pzinovkin
2010-04-26 19:56:01 +00:00
parent 64c3cace88
commit c214b75d60
19 changed files with 660 additions and 9 deletions

79
captcha/Captcha.php Normal file
View File

@ -0,0 +1,79 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage captcha
* @since 2010-04-24
* @version SVN: $Id$
* @filesource $URL$
*/
class Captcha
{
protected $font_size = 38;
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),
$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,
$color, $font, $letter);
$i = $i + $step ;
}
}
ob_start();
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Content-Type: image/jpg");
imagejpeg($image, '', 100);
imagedestroy($image);
return ob_get_clean();
}
public function getToken()
{
$token = md5(microtime() . uniqid());
$code = strtoupper(substr(str_ireplace(array('0', 'O'), '', md5(time() . 'captcha' . rand(0, 1000))), 1, 5));
Session::set('_ctoken', $token);
Session::set('_ccode', $code);
return $token;
}
public function checkCode($token, $code)
{
if (Session::get('_ctoken') == $token && Session::get('_ccode') == strtoupper($code)){
return true;
}
return false;
}
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage captcha
* @since 2010-04-24
* @version SVN: $Id$
* @filesource $URL$
*/
class CaptchaImageAction
{
public function __construct()
{
$captcha = new Captcha();
echo $captcha->getImage(Env::Get('ctoken'));
exit();
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage validator
* @since 2010-04-26
* @version SVN: $Id$
* @filesource $URL$
*/
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'])) {
$this->error();
return false;
}
$captcha = new Captcha();
if ($captcha->checkCode($context['ctoken'], $context['ccode'])) {
return true;
}
$this->error();
return false;
}
}

BIN
captcha/poh.ttf Normal file

Binary file not shown.