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-03 20:49:17 +01:00
|
|
|
/**
|
|
|
|
* @var \Symfony\Component\Routing\RouterInterface
|
|
|
|
*/
|
|
|
|
protected $router;
|
|
|
|
|
2011-11-09 17:58:53 +01:00
|
|
|
/**
|
2012-12-04 00:48:32 +01:00
|
|
|
* @var ImageBuilder
|
2011-11-09 17:58:53 +01:00
|
|
|
*/
|
2012-12-04 00:48:32 +01:00
|
|
|
protected $builder;
|
2011-11-09 17:58:53 +01:00
|
|
|
|
|
|
|
/**
|
2012-12-04 00:48:32 +01:00
|
|
|
* @var ImageFileHandler
|
2011-11-09 17:58:53 +01:00
|
|
|
*/
|
2012-12-04 00:48:32 +01:00
|
|
|
protected $imageFileHandler;
|
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-12-04 00:48:32 +01:00
|
|
|
* @param ImageBuilder $builder
|
|
|
|
* @param ImageFileHandler $imageFileHandler
|
2011-12-02 19:07:28 +01:00
|
|
|
*/
|
2012-12-04 00:48:32 +01:00
|
|
|
public function __construct(SessionInterface $session, RouterInterface $router, ImageBuilder $builder, ImageFileHandler $imageFileHandler)
|
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-12-04 00:48:32 +01:00
|
|
|
$this->builder = $builder;
|
|
|
|
$this->imageFileHandler = $imageFileHandler;
|
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']) {
|
2012-12-04 00:48:32 +01:00
|
|
|
$this->imageFileHandler->collectGarbage();
|
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-12-04 00:48:32 +01:00
|
|
|
// Returns the image generation URL
|
2012-11-14 04:33:36 +01:00
|
|
|
if ($options['as_url']) {
|
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-12-04 00:48:32 +01:00
|
|
|
* @param string $key
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return string
|
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-12-04 00:48:32 +01:00
|
|
|
$fingerprint = $this->getFingerprint($key, $options);
|
|
|
|
|
|
|
|
$content = $this->builder->build(
|
|
|
|
$options['width'],
|
|
|
|
$options['height'],
|
|
|
|
$options['font'],
|
|
|
|
$this->getPhrase($key, $options),
|
|
|
|
$fingerprint
|
|
|
|
);
|
2012-11-14 04:33:36 +01:00
|
|
|
|
|
|
|
if ($options['keep_value']) {
|
2012-12-04 00:48:32 +01:00
|
|
|
$this->session->set($key . '_fingerprint', $this->builder->getFingerprint());
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
2011-08-25 23:10:24 +02:00
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
if (!$options['as_file']) {
|
2011-11-09 14:43:25 +01:00
|
|
|
ob_start();
|
2012-12-04 00:48:32 +01:00
|
|
|
imagejpeg($content, null, $options['quality']);
|
2012-11-14 04:33:36 +01:00
|
|
|
|
2011-11-09 14:43:25 +01:00
|
|
|
return ob_get_clean();
|
|
|
|
}
|
2012-11-14 04:33:36 +01:00
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
return $this->imageFileHandler->saveAsFile($content);
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2012-12-03 20:49:17 +01:00
|
|
|
* @param string $key
|
2012-12-04 00:48:32 +01:00
|
|
|
* @param array $options
|
2012-11-14 04:33:36 +01:00
|
|
|
*
|
2012-12-04 00:48:32 +01:00
|
|
|
* @return string
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2012-12-04 00:48:32 +01:00
|
|
|
protected function getPhrase($key, array $options)
|
2011-08-25 23:10:24 +02:00
|
|
|
{
|
2012-12-04 00:48:32 +01:00
|
|
|
// Get the phrase that we'll use for this image
|
|
|
|
if ($options['keep_value'] && $this->session->has($key)) {
|
2012-12-03 20:49:17 +01:00
|
|
|
return $this->session->get($key);
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
$phrase = '';
|
|
|
|
$chars = str_split($options['charset']);
|
2012-11-14 04:33:36 +01:00
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
for ($i = 0; $i < $options['length']; $i++) {
|
|
|
|
$phrase .= $chars[array_rand($chars)];
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
$this->session->set($key, $phrase);
|
2012-11-14 04:33:36 +01:00
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
return $phrase;
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
/**
|
2012-12-04 00:48:32 +01:00
|
|
|
* @param string $key
|
|
|
|
* @param array $options
|
2012-11-14 04:33:36 +01:00
|
|
|
*
|
2012-12-04 00:48:32 +01:00
|
|
|
* @return array|null
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2012-12-04 00:48:32 +01:00
|
|
|
protected function getFingerprint($key, array $options)
|
2012-11-14 04:33:36 +01:00
|
|
|
{
|
2012-12-04 00:48:32 +01:00
|
|
|
if ($options['keep_value'] && $this->session->has($key . '_fingerprint')) {
|
|
|
|
return $this->session->get($key . '_fingerprint');
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
2012-12-04 00:48:32 +01:00
|
|
|
return null;
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
2011-08-25 23:10:24 +02:00
|
|
|
}
|
|
|
|
|