CaptchaBundle/Controller/CaptchaController.php

77 lines
2.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Gregwar\CaptchaBundle\Controller;
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator;
2019-01-23 11:01:17 +01:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
2015-02-04 09:22:47 +01:00
use Symfony\Component\HttpFoundation\Response;
/**
* 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
{
/** @var CaptchaGenerator */
private $captchaGenerator;
/** @var array */
private $config;
public function __construct(CaptchaGenerator $captchaGenerator, array $config)
{
$this->captchaGenerator = $captchaGenerator;
$this->config = $config;
}
public function generateCaptchaAction(Request $request, string $key): Response
{
$session = $request->getSession();
$whitelistKey = $this->config['whitelist_key'];
$isOk = false;
if ($session->has($whitelistKey)) {
$keys = $session->get($whitelistKey);
if (is_array($keys) && in_array($key, $keys)) {
$isOk = true;
}
}
if (!$isOk) {
return $this->error($this->config);
}
$persistedOptions = $session->get($key, array());
$options = array_merge($this->config, $persistedOptions);
$phrase = $this->captchaGenerator->getPhrase($options);
$this->captchaGenerator->setPhrase($phrase);
$persistedOptions['phrase'] = $phrase;
$session->set($key, $persistedOptions);
$response = new Response($this->captchaGenerator->generate($options));
$response->headers->set('Content-type', 'image/jpeg');
$response->headers->set('Pragma', 'no-cache');
2015-02-04 09:22:47 +01:00
$response->headers->set('Cache-Control', 'no-cache');
return $response;
}
private function error(array $options): Response
2015-02-04 09:22:47 +01:00
{
$this->captchaGenerator->setPhrase('');
2015-02-04 09:22:47 +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');
return $response;
}
}