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-03 20:49:17 +01:00
|
|
|
if (!$options['as_url'] || !in_array($key, $options['valid_keys'])) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|