2012-11-14 04:33:36 +01:00
|
|
|
<?php
|
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2012-11-14 04:33:36 +01:00
|
|
|
namespace Gregwar\CaptchaBundle\Controller;
|
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
|
2019-01-23 11:01:17 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2019-12-30 00:27:20 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2015-02-04 09:22:47 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
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
|
|
|
{
|
2019-12-30 00:27:20 +01:00
|
|
|
/** @var CaptchaGenerator */
|
|
|
|
private $captchaGenerator;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
public function __construct(CaptchaGenerator $captchaGenerator, array $config)
|
2012-11-14 04:33:36 +01:00
|
|
|
{
|
2019-12-30 00:27:20 +01:00
|
|
|
$this->captchaGenerator = $captchaGenerator;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateCaptchaAction(Request $request, string $key): Response
|
|
|
|
{
|
|
|
|
$session = $request->getSession();
|
|
|
|
$whitelistKey = $this->config['whitelist_key'];
|
2012-12-04 11:33:54 +01:00
|
|
|
$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) {
|
2019-12-30 00:27:20 +01:00
|
|
|
return $this->error($this->config);
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-21 15:18:36 +01:00
|
|
|
$persistedOptions = $session->get($key, array());
|
2019-12-30 00:27:20 +01:00
|
|
|
$options = array_merge($this->config, $persistedOptions);
|
2013-01-21 15:18:36 +01:00
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
$phrase = $this->captchaGenerator->getPhrase($options);
|
|
|
|
$this->captchaGenerator->setPhrase($phrase);
|
2013-01-21 15:18:36 +01:00
|
|
|
$persistedOptions['phrase'] = $phrase;
|
|
|
|
$session->set($key, $persistedOptions);
|
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
$response = new Response($this->captchaGenerator->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;
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
private function error(array $options): Response
|
2015-02-04 09:22:47 +01:00
|
|
|
{
|
2019-12-30 00:27:20 +01:00
|
|
|
$this->captchaGenerator->setPhrase('');
|
2015-02-04 09:22:47 +01:00
|
|
|
|
2019-12-30 00:27:20 +01:00
|
|
|
$response = new Response($this->captchaGenerator->generate($options));
|
2015-02-04 09:22:47 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|