2012-11-14 04:33:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gregwar\CaptchaBundle\Controller;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a captcha via a URL
|
|
|
|
*
|
|
|
|
* @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
|
|
|
|
*/
|
|
|
|
class CaptchaController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Action that is used to generate the captcha, save its code, and stream the image
|
|
|
|
*
|
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request
|
2012-12-03 20:49:17 +01:00
|
|
|
* @param string $key
|
|
|
|
*
|
2012-11-14 04:33:36 +01:00
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2012-12-03 20:49:17 +01:00
|
|
|
public function generateCaptchaAction(Request $request, $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;
|
|
|
|
|
|
|
|
if ($options['as_url'] && $session->has($whitelistKey)) {
|
|
|
|
$keys = $session->get($whitelistKey);
|
|
|
|
if (is_array($keys) && in_array($key, $keys)) {
|
|
|
|
$isOk = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$isOk) {
|
2012-11-14 04:33:36 +01:00
|
|
|
return $this->createNotFoundException('Unable to generate a captcha via a URL without the proper configuration.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @var \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator */
|
|
|
|
$generator = $this->container->get('gregwar_captcha.generator');
|
|
|
|
|
2012-12-04 10:57:52 +01:00
|
|
|
$response = new Response($generator->generate($key, $options));
|
|
|
|
$response->headers->set('Content-type', 'image/jpeg');
|
|
|
|
|
|
|
|
return $response;
|
2012-11-14 04:33:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|