2011-08-25 23:10:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Generator;
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2012-11-14 04:33:36 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2012-12-03 20:49:17 +01:00
|
|
|
use Symfony\Component\Routing\RouterInterface;
|
2011-11-09 14:43:25 +01:00
|
|
|
|
2011-08-25 23:10:24 +02:00
|
|
|
/**
|
|
|
|
* Generates a CAPTCHA image
|
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
class CaptchaGenerator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
|
|
|
|
*/
|
|
|
|
protected $session;
|
2011-08-25 23:10:24 +02:00
|
|
|
|
2012-12-04 11:33:54 +01:00
|
|
|
/**
|
|
|
|
* Name of the whitelist key
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $whitelistKey;
|
|
|
|
|
2012-12-03 20:49:17 +01:00
|
|
|
/**
|
|
|
|
* @var \Symfony\Component\Routing\RouterInterface
|
|
|
|
*/
|
|
|
|
protected $router;
|
|
|
|
|
2011-11-09 17:58:53 +01:00
|
|
|
/**
|
|
|
|
* Name of folder for captcha images
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected $imageFolder;
|
2011-11-09 17:58:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Absolute path to public web folder
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected $webPath;
|
2011-11-09 17:58:53 +01:00
|
|
|
|
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* Frequency of garbage collection in fractions of 1
|
2011-11-09 17:58:53 +01:00
|
|
|
* @var int
|
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected $gcFreq;
|
2011-12-02 19:07:28 +01:00
|
|
|
|
2011-11-09 17:58:53 +01:00
|
|
|
/**
|
|
|
|
* Maximum age of images in minutes
|
|
|
|
* @var int
|
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected $expiration;
|
2011-11-09 17:58:53 +01:00
|
|
|
|
2011-12-02 19:26:31 +01:00
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* The fingerprint used to generate the image details across requests
|
|
|
|
* @var array|null
|
2011-12-02 19:26:31 +01:00
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected $fingerprint;
|
2011-12-02 19:26:31 +01:00
|
|
|
|
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* Whether this instance should use the fingerprint
|
|
|
|
* @var bool
|
2011-12-02 19:26:31 +01:00
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
protected $useFingerprint;
|
2011-12-02 19:26:31 +01:00
|
|
|
|
2011-12-02 19:07:28 +01:00
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
|
2012-12-03 20:49:17 +01:00
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $router
|
2012-11-14 04:33:36 +01:00
|
|
|
* @param string $imageFolder
|
|
|
|
* @param string $webPath
|
|
|
|
* @param int $gcFreq
|
|
|
|
* @param int $expiration
|
2011-12-02 19:07:28 +01:00
|
|
|
*/
|
2012-12-04 11:33:54 +01:00
|
|
|
public function __construct(SessionInterface $session, RouterInterface $router, $imageFolder, $webPath, $gcFreq, $expiration, $whitelistKey)
|
2011-08-25 23:10:24 +02:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
$this->session = $session;
|
2012-12-03 20:49:17 +01:00
|
|
|
$this->router = $router;
|
2012-11-14 04:33:36 +01:00
|
|
|
$this->imageFolder = $imageFolder;
|
|
|
|
$this->webPath = $webPath;
|
|
|
|
$this->gcFreq = $gcFreq;
|
|
|
|
$this->expiration = $expiration;
|
2012-12-04 11:33:54 +01:00
|
|
|
$this->whitelistKey = $whitelistKey;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
2011-11-09 14:43:25 +01:00
|
|
|
|
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* Get the captcha URL, stream, or filename that will go in the image's src attribute
|
|
|
|
*
|
|
|
|
* @param $key
|
|
|
|
* @param array $options
|
2011-11-09 14:43:25 +01:00
|
|
|
*
|
2012-11-14 04:33:36 +01:00
|
|
|
* @return array
|
2011-11-09 14:43:25 +01:00
|
|
|
*/
|
2012-11-14 04:33:36 +01:00
|
|
|
public function getCaptchaCode($key, array $options)
|
2011-11-09 14:43:25 +01:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
// Randomly execute garbage collection and returns the image filename
|
|
|
|
if ($options['as_file']) {
|
|
|
|
if (mt_rand(1, $this->gcFreq) == 1) {
|
|
|
|
$this->garbageCollection();
|
|
|
|
}
|
2011-11-09 14:43:25 +01:00
|
|
|
|
2012-12-03 20:49:17 +01:00
|
|
|
return $this->generate($key, $options);
|
2011-12-02 19:26:31 +01:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
// Returns the configured URL for image generation
|
|
|
|
if ($options['as_url']) {
|
2012-12-04 11:33:54 +01:00
|
|
|
$keys = $this->session->get($this->whitelistKey, array());
|
|
|
|
if (!in_array($key, $keys)) {
|
|
|
|
$keys[] = $key;
|
|
|
|
}
|
|
|
|
$this->session->set($this->whitelistKey, $keys);
|
2012-12-03 20:49:17 +01:00
|
|
|
return $this->router->generate('gregwar_captcha.generate_captcha', array('key' => $key));
|
2011-12-02 19:26:31 +01:00
|
|
|
}
|
|
|
|
|
2012-12-03 20:49:17 +01:00
|
|
|
return 'data:image/jpeg;base64,' . base64_encode($this->generate($key, $options));
|
2011-12-02 19:26:31 +01:00
|
|
|
}
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
/**
|
2012-11-14 04:33:36 +01:00
|
|
|
* Generate the image
|
2011-11-09 14:43:25 +01:00
|
|
|
*/
|
2012-12-03 20:49:17 +01:00
|
|
|
public function generate($key, array $options)
|
2011-11-09 14:43:25 +01:00
|
|
|
{
|
2012-11-14 04:33:36 +01:00
|
|
|
$width = $options['width'];
|
|
|
|
$height = $options['height'];
|
2011-11-09 14:43:25 +01:00
|
|
|
|
2012-12-03 20:49:17 +01:00
|
|
|
if ($options['keep_value'] && $this->session->has($key . '_fingerprint')) {
|
|
|
|
$this->fingerprint = $this->session->get($key . '_fingerprint');
|
2012-11-14 04:33:36 +01:00
|
|
|
$this->useFingerprint = true;
|
|
|
|
} else {
|
|
|
|
$this->fingerprint = null;
|
|
|
|
$this->useFingerprint = false;
|
2011-11-09 14:43:25 +01:00
|
|
|
}
|
|
|
|
|
2012-12-03 20:49:17 +01:00
|
|
|
$captchaValue = $this->getCaptchaValue($key, $options['keep_value'], $options['charset'], $options['length']);
|
2011-08-26 08:59:29 +02:00
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
$i = imagecreatetruecolor($width,$height);
|
2011-12-02 19:26:31 +01:00
|
|
|
$col = imagecolorallocate($i, $this->rand(0,110), $this->rand(0,110), $this->rand(0,110));
|
2011-08-25 23:10:24 +02:00
|
|
|
|
|
|
|
imagefill($i, 0, 0, 0xFFFFFF);
|
|
|
|
|
|
|
|
// Draw random lines
|
|
|
|
for ($t=0; $t<10; $t++) {
|
2011-12-02 19:26:31 +01:00
|
|
|
$tcol = imagecolorallocate($i, 100+$this->rand(0,150), 100+$this->rand(0,150), 100+$this->rand(0,150));
|
|
|
|
$Xa = $this->rand(0, $width);
|
|
|
|
$Ya = $this->rand(0, $height);
|
|
|
|
$Xb = $this->rand(0, $width);
|
|
|
|
$Yb = $this->rand(0, $height);
|
2011-08-25 23:10:24 +02:00
|
|
|
imageline($i, $Xa, $Ya, $Xb, $Yb, $tcol);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write CAPTCHA text
|
2012-12-03 20:55:11 +01:00
|
|
|
$size = $width / strlen($captchaValue);
|
|
|
|
$font = $options['font'];
|
|
|
|
$box = imagettfbbox($size, 0, $font, $captchaValue);
|
|
|
|
$textWidth = $box[2] - $box[0];
|
|
|
|
$textHeight = $box[1] - $box[7];
|
2011-09-09 19:51:07 +02:00
|
|
|
|
2012-12-03 20:55:11 +01:00
|
|
|
imagettftext($i, $size, 0, ($width - $textWidth) / 2, ($height - $textHeight) / 2 + $size, $col, $font, $captchaValue);
|
2011-08-25 23:10:24 +02:00
|
|
|
|
|
|
|
// Distort the image
|
2012-12-03 20:55:11 +01:00
|
|
|
$X = $this->rand(0, $width);
|
|
|
|
$Y = $this->rand(0, $height);
|
|
|
|
$phase = $this->rand(0, 10);
|
|
|
|
$scale = 1.3 + $this->rand(0, 10000) / 30000;
|
|
|
|
$out = imagecreatetruecolor($width, $height);
|
|
|
|
|
|
|
|
for ($x = 0; $x < $width; $x++) {
|
|
|
|
for ($y = 0; $y < $height; $y++) {
|
|
|
|
$Vx = $x - $X;
|
|
|
|
$Vy = $y - $Y;
|
|
|
|
$Vn = sqrt($Vx * $Vx + $Vy * $Vy);
|
|
|
|
|
|
|
|
if ($Vn != 0) {
|
|
|
|
$Vn2 = $Vn + 4 * sin($Vn / 8);
|
|
|
|
$nX = $X + ($Vx * $Vn2 / $Vn);
|
|
|
|
$nY = $Y + ($Vy * $Vn2 / $Vn);
|
2011-08-25 23:10:24 +02:00
|
|
|
} else {
|
2012-12-03 20:55:11 +01:00
|
|
|
$nX = $X;
|
|
|
|
$nY = $Y;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
2012-12-03 20:55:11 +01:00
|
|
|
$nY = $nY + $scale * sin($phase + $nX * 0.2);
|
2011-08-25 23:10:24 +02:00
|
|
|
|
2012-12-03 20:55:11 +01:00
|
|
|
$p = $this->bilinearInterpolate($nX - floor($nX), $nY - floor($nY),
|
|
|
|
$this->getCol($i, floor($nX), floor($nY)),
|
|
|
|
$this->getCol($i, ceil($nX), floor($nY)),
|
|
|
|
$this->getCol($i, floor($nX), ceil($nY)),
|
|
|
|
$this->getCol($i, ceil($nX), ceil($nY)));
|
2011-08-25 23:10:24 +02:00
|
|
|
|
2012-12-03 20:55:11 +01:00
|
|
|
if ($p == 0) {
|
|
|
|
$p = 0xFFFFFF;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
imagesetpixel($out, $x, $y, $p);
|
|
|
|
}
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['keep_value']) {
|
2012-12-03 20:49:17 +01:00
|
|
|
$this->session->set($key . '_fingerprint', $this->fingerprint);
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
2011-08-25 23:10:24 +02:00
|
|
|
|
|
|
|
// Renders it
|
2012-11-14 04:33:36 +01:00
|
|
|
if (!$options['as_file']) {
|
2011-11-09 14:43:25 +01:00
|
|
|
ob_start();
|
2012-11-14 04:33:36 +01:00
|
|
|
imagejpeg($out, null, $options['quality']);
|
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
return ob_get_clean();
|
|
|
|
}
|
2012-11-14 04:33:36 +01:00
|
|
|
|
|
|
|
// Check if folder exists and create it if not
|
|
|
|
if (!file_exists($this->webPath . '/' . $this->imageFolder)) {
|
|
|
|
mkdir($this->webPath . '/' . $this->imageFolder, 0755);
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = md5(uniqid()) . '.jpg';
|
|
|
|
$filepath = $this->webPath . '/' . $this->imageFolder . '/' . $filename;
|
|
|
|
imagejpeg($out, $filepath, 15);
|
|
|
|
|
|
|
|
return '/' . $this->imageFolder . '/' . $filename;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* Generate a new captcha value or pull the existing one from the session
|
|
|
|
*
|
2012-12-03 20:49:17 +01:00
|
|
|
* @param string $key
|
2012-11-14 04:33:36 +01:00
|
|
|
* @param bool $keepValue
|
|
|
|
* @param string $charset
|
|
|
|
* @param int $length
|
|
|
|
*
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
2012-12-03 20:49:17 +01:00
|
|
|
protected function getCaptchaValue($key, $keepValue, $charset, $length)
|
2011-08-25 23:10:24 +02:00
|
|
|
{
|
2012-12-03 20:49:17 +01:00
|
|
|
if ($keepValue && $this->session->has($key)) {
|
|
|
|
return $this->session->get($key);
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$value = '';
|
|
|
|
$chars = str_split($charset);
|
|
|
|
|
|
|
|
for ($i=0; $i < $length; $i++) {
|
|
|
|
$value .= $chars[array_rand($chars)];
|
|
|
|
}
|
|
|
|
|
2012-12-03 20:49:17 +01:00
|
|
|
$this->session->set($key, $value);
|
2012-11-14 04:33:36 +01:00
|
|
|
|
|
|
|
return $value;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
|
|
|
* Deletes all images in the configured folder
|
|
|
|
* that are older than the configured number of minutes
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function garbageCollection()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$criteria = sprintf('<= now - %s minutes', $this->expiration);
|
|
|
|
$finder->in($this->webPath . '/' . $this->imageFolder)
|
|
|
|
->date($criteria);
|
|
|
|
|
|
|
|
foreach($finder->files() as $file) {
|
|
|
|
unlink($file->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a random number or the next number in the
|
|
|
|
* fingerprint
|
|
|
|
*/
|
|
|
|
protected function rand($min, $max)
|
|
|
|
{
|
|
|
|
if (!is_array($this->fingerprint)) {
|
|
|
|
$this->fingerprint = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->useFingerprint) {
|
|
|
|
$value = current($this->fingerprint);
|
|
|
|
next($this->fingerprint);
|
|
|
|
} else {
|
|
|
|
$value = mt_rand($min, $max);
|
|
|
|
$this->fingerprint[] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
protected function bilinearInterpolate($x, $y, $nw, $ne, $sw, $se)
|
2011-08-25 23:10:24 +02:00
|
|
|
{
|
|
|
|
list($r0, $g0, $b0) = $this->getRGB($nw);
|
|
|
|
list($r1, $g1, $b1) = $this->getRGB($ne);
|
|
|
|
list($r2, $g2, $b2) = $this->getRGB($sw);
|
|
|
|
list($r3, $g3, $b3) = $this->getRGB($se);
|
|
|
|
|
|
|
|
$cx = 1.0 - $x;
|
|
|
|
$cy = 1.0 - $y;
|
|
|
|
|
|
|
|
$m0 = $cx * $r0 + $x * $r1;
|
|
|
|
$m1 = $cx * $r2 + $x * $r3;
|
|
|
|
$r = (int)($cy * $m0 + $y * $m1);
|
|
|
|
|
|
|
|
$m0 = $cx * $g0 + $x * $g1;
|
|
|
|
$m1 = $cx * $g2 + $x * $g3;
|
|
|
|
$g = (int)($cy * $m0 + $y * $m1);
|
|
|
|
|
|
|
|
$m0 = $cx * $b0 + $x * $b1;
|
|
|
|
$m1 = $cx * $b2 + $x * $b3;
|
|
|
|
$b = (int)($cy * $m0 + $y * $m1);
|
|
|
|
|
|
|
|
return ($r << 16) | ($g << 8) | $b;
|
|
|
|
}
|
2012-11-14 04:33:36 +01:00
|
|
|
|
|
|
|
protected function getCol($image, $x, $y)
|
|
|
|
{
|
|
|
|
$L = imagesx($image);
|
|
|
|
$H = imagesy($image);
|
2012-12-03 20:55:11 +01:00
|
|
|
if ($x < 0 || $x >= $L || $y < 0 || $y >= $H) {
|
2012-11-14 04:33:36 +01:00
|
|
|
return 0xFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return imagecolorat($image, $x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRGB($col) {
|
|
|
|
return array(
|
|
|
|
(int)($col >> 16) & 0xff,
|
|
|
|
(int)($col >> 8) & 0xff,
|
|
|
|
(int)($col) & 0xff,
|
|
|
|
);
|
|
|
|
}
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|