2012-11-14 04:33:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Controller;
|
|
|
|
|
2019-01-23 11:01:17 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2015-02-04 09:22:47 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2014-07-21 14:33:31 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2012-11-14 04:33:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a captcha via a URL
|
|
|
|
*
|
|
|
|
* @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
|
|
|
|
*/
|
2019-01-23 11:01:17 +01:00
|
|
|
class CaptchaController extends AbstractController
|
2012-11-14 04:33:36 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Action that is used to generate the captcha, save its code, and stream the image
|
|
|
|
*
|
2012-12-03 20:49:17 +01:00
|
|
|
* @param string $key
|
|
|
|
*
|
2014-07-21 14:33:31 +02:00
|
|
|
* @return Response
|
|
|
|
*
|
|
|
|
* @throws NotFoundHttpException
|
2012-11-14 04:33:36 +01:00
|
|
|
*/
|
2014-07-21 14:33:31 +02:00
|
|
|
public function generateCaptchaAction($key)
|
2012-11-14 04:33:36 +01:00
|
|
|
{
|
|
|
|
$options = $this->container->getParameter('gregwar_captcha.config');
|
2012-12-04 11:33:54 +01:00
|
|
|
$session = $this->get('session');
|
|
|
|
$whitelistKey = $options['whitelist_key'];
|
|
|
|
$isOk = false;
|
|
|
|
|
2013-01-23 19:58:45 +01:00
|
|
|
if ($session->has($whitelistKey)) {
|
2012-12-04 11:33:54 +01:00
|
|
|
$keys = $session->get($whitelistKey);
|
|
|
|
if (is_array($keys) && in_array($key, $keys)) {
|
|
|
|
$isOk = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$isOk) {
|
2015-02-04 09:22:47 +01:00
|
|
|
return $this->error($options);
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
|
|
|
|
$generator = $this->container->get('gregwar_captcha.generator');
|
|
|
|
|
2013-01-21 15:18:36 +01:00
|
|
|
$persistedOptions = $session->get($key, array());
|
|
|
|
$options = array_merge($options, $persistedOptions);
|
|
|
|
|
|
|
|
$phrase = $generator->getPhrase($options);
|
|
|
|
$generator->setPhrase($phrase);
|
|
|
|
$persistedOptions['phrase'] = $phrase;
|
|
|
|
$session->set($key, $persistedOptions);
|
|
|
|
|
|
|
|
$response = new Response($generator->generate($options));
|
2012-12-04 10:57:52 +01:00
|
|
|
$response->headers->set('Content-type', 'image/jpeg');
|
2014-08-27 12:29:12 +02:00
|
|
|
$response->headers->set('Pragma', 'no-cache');
|
2015-02-04 09:22:47 +01:00
|
|
|
$response->headers->set('Cache-Control', 'no-cache');
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an empty image with status code 428 Precondition Required
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
protected function error($options)
|
|
|
|
{
|
|
|
|
/* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
|
|
|
|
$generator = $this->container->get('gregwar_captcha.generator');
|
|
|
|
$generator->setPhrase('');
|
|
|
|
|
|
|
|
$response = new Response($generator->generate($options));
|
|
|
|
$response->setStatusCode(428);
|
|
|
|
$response->headers->set('Content-type', 'image/jpeg');
|
|
|
|
$response->headers->set('Pragma', 'no-cache');
|
|
|
|
$response->headers->set('Cache-Control', 'no-cache');
|
2012-12-04 10:57:52 +01:00
|
|
|
|
|
|
|
return $response;
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
}
|