CaptchaBundle/Generator/CaptchaGenerator.php

146 lines
3.7 KiB
PHP
Raw Normal View History

2011-08-25 23:10:24 +02:00
<?php
namespace Gregwar\CaptchaBundle\Generator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
2011-08-25 23:10:24 +02:00
/**
* Generates a CAPTCHA image
*/
class CaptchaGenerator
{
/**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;
2011-08-25 23:10:24 +02:00
/**
* @var \Symfony\Component\Routing\RouterInterface
*/
protected $router;
/**
* @var ImageBuilder
*/
protected $builder;
/**
* @var ImageFileHandler
*/
protected $imageFileHandler;
2011-12-02 19:07:28 +01:00
/**
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Symfony\Component\Routing\RouterInterface $router
* @param ImageBuilder $builder
* @param ImageFileHandler $imageFileHandler
2011-12-02 19:07:28 +01:00
*/
public function __construct(SessionInterface $session, RouterInterface $router, ImageBuilder $builder, ImageFileHandler $imageFileHandler)
2011-08-25 23:10:24 +02:00
{
$this->session = $session;
$this->router = $router;
$this->builder = $builder;
$this->imageFileHandler = $imageFileHandler;
2011-08-25 23:10:24 +02:00
}
/**
* Get the captcha URL, stream, or filename that will go in the image's src attribute
*
* @param $key
* @param array $options
*
* @return array
*/
public function getCaptchaCode($key, array $options)
{
// Randomly execute garbage collection and returns the image filename
if ($options['as_file']) {
$this->imageFileHandler->collectGarbage();
return $this->generate($key, $options);
}
// Returns the image generation URL
if ($options['as_url']) {
return $this->router->generate('gregwar_captcha.generate_captcha', array('key' => $key));
}
return 'data:image/jpeg;base64,' . base64_encode($this->generate($key, $options));
}
/**
* @param string $key
* @param array $options
*
* @return string
*/
public function generate($key, array $options)
{
$fingerprint = $this->getFingerprint($key, $options);
$content = $this->builder->build(
$options['width'],
$options['height'],
$options['font'],
$this->getPhrase($key, $options),
$fingerprint
);
if ($options['keep_value']) {
$this->session->set($key . '_fingerprint', $this->builder->getFingerprint());
}
2011-08-25 23:10:24 +02:00
if (!$options['as_file']) {
ob_start();
imagejpeg($content, null, $options['quality']);
return ob_get_clean();
}
return $this->imageFileHandler->saveAsFile($content);
2011-08-25 23:10:24 +02:00
}
/**
* @param string $key
* @param array $options
*
* @return string
*/
protected function getPhrase($key, array $options)
2011-08-25 23:10:24 +02:00
{
// Get the phrase that we'll use for this image
if ($options['keep_value'] && $this->session->has($key)) {
return $this->session->get($key);
}
$phrase = '';
$chars = str_split($options['charset']);
for ($i = 0; $i < $options['length']; $i++) {
$phrase .= $chars[array_rand($chars)];
}
$this->session->set($key, $phrase);
return $phrase;
2011-08-25 23:10:24 +02:00
}
/**
* @param string $key
* @param array $options
*
* @return array|null
*/
protected function getFingerprint($key, array $options)
{
if ($options['keep_value'] && $this->session->has($key . '_fingerprint')) {
return $this->session->get($key . '_fingerprint');
}
return null;
}
2011-08-25 23:10:24 +02:00
}