Commit 84f543a3 authored by Jakob Perry's avatar Jakob Perry
Browse files

Rewrite the Controllers to Drupal 8+ Standards.

parent b8010519
......@@ -8,7 +8,7 @@ image_captcha.settings:
image_captcha.font_preview:
path: '/admin/config/people/captcha/image_captcha/font_preview/{token}'
defaults:
_controller: '\Drupal\image_captcha\Controller\CaptchaFontPreview::content'
_controller: '\Drupal\image_captcha\Controller\CaptchaFontPreviewController::getFont'
requirements:
_permission: 'administer CAPTCHA settings'
......
<?php
namespace Drupal\image_captcha\Controller;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\image_captcha\StreamedResponse\CaptchaFontPreviewStreamedResponse;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller which generates the image from defined settings.
*/
class CaptchaFontPreviewController implements ContainerInjectionInterface {
/**
* Image Captcha config storage.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Kill Switch for page caching.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $killSwitch;
/**
* {@inheritdoc}
*/
public function __construct(ImmutableConfig $config, KillSwitch $kill_switch) {
$this->config = $config;
$this->killSwitch = $kill_switch;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')->get('image_captcha.settings'),
$container->get('page_cache_kill_switch')
);
}
/**
* Main method that throw ImageResponse object to generate image.
*
* @return \Drupal\image_captcha\StreamedResponse\CaptchaFontPreviewStreamedResponse
* Make a CaptchaImageResponse with the correct configuration and return it.
*/
public function getFont($token) {
$this->killSwitch->trigger();
return new CaptchaFontPreviewStreamedResponse($this->config, $token);
}
}
......@@ -3,6 +3,8 @@
namespace Drupal\image_captcha\Controller;
use Drupal\Core\Config\Config;
use Drupal\Core\Database\Connection;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Psr\Log\LoggerInterface;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
......@@ -14,6 +16,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class CaptchaImageGeneratorController implements ContainerInjectionInterface {
/**
* Connection container.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Image Captcha config storage.
*
......@@ -21,6 +30,13 @@ class CaptchaImageGeneratorController implements ContainerInjectionInterface {
*/
protected $config;
/**
* File System Service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Watchdog logger channel for captcha.
*
......@@ -38,10 +54,12 @@ class CaptchaImageGeneratorController implements ContainerInjectionInterface {
/**
* {@inheritdoc}
*/
public function __construct(Config $config, LoggerInterface $logger, KillSwitch $kill_switch) {
public function __construct(Config $config, LoggerInterface $logger, KillSwitch $kill_switch, Connection $connection, FileSystemInterface $file_system) {
$this->config = $config;
$this->logger = $logger;
$this->killSwitch = $kill_switch;
$this->connection = $connection;
$this->fileSystem = $file_system;
}
/**
......@@ -51,7 +69,9 @@ class CaptchaImageGeneratorController implements ContainerInjectionInterface {
return new static(
$container->get('config.factory')->get('image_captcha.settings'),
$container->get('logger.factory')->get('captcha'),
$container->get('page_cache_kill_switch')
$container->get('page_cache_kill_switch'),
$container->get('database'),
$container->get('file_system')
);
}
......@@ -63,7 +83,7 @@ class CaptchaImageGeneratorController implements ContainerInjectionInterface {
*/
public function image() {
$this->killSwitch->trigger();
return new CaptchaImageResponse($this->config, $this->logger);
return new CaptchaImageResponse($this->config, $this->logger, $this->connection, $this->fileSystem);
}
}
......@@ -3,6 +3,8 @@
namespace Drupal\image_captcha\Response;
use Drupal\Core\Config\Config;
use Drupal\Core\Database\Connection;
use Drupal\Core\File\FileSystemInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
......@@ -16,6 +18,13 @@ class CaptchaImageResponse extends Response {
const LOG_LEVEL = 'ERROR';
/**
* Database connection configuration container.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Image Captcha config storage.
*
......@@ -23,6 +32,13 @@ class CaptchaImageResponse extends Response {
*/
protected $config;
/**
* File System container.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Watchdog logger channel for captcha.
*
......@@ -40,11 +56,13 @@ class CaptchaImageResponse extends Response {
/**
* {@inheritdoc}
*/
public function __construct(Config $config, LoggerInterface $logger, $callback = NULL, $status = 200, $headers = []) {
public function __construct(Config $config, LoggerInterface $logger, Connection $connection, FileSystemInterface $fileSystem, $callback = NULL, $status = 200, $headers = []) {
parent::__construct(NULL, $status, $headers);
$this->config = $config;
$this->logger = $logger;
$this->connection = $connection;
$this->fileSystem = $fileSystem;
}
/**
......@@ -53,7 +71,7 @@ class CaptchaImageResponse extends Response {
public function prepare(Request $request) {
$session_id = $request->get('session_id');
$code = \Drupal::database()
$code = $this->connection
->select('captcha_sessions', 'cs')
->fields('cs', ['solution'])
->condition('csid', $session_id)
......@@ -376,7 +394,7 @@ class CaptchaImageResponse extends Response {
// Get character dimensions for TrueType fonts.
if ($font != 'BUILTIN') {
putenv('GDFONTPATH=' . realpath('.'));
$bbox = imagettfbbox($font_size, 0, \Drupal::service('file_system')->realpath($font), $character);
$bbox = imagettfbbox($font_size, 0, $this->fileSystem->realpath($font), $character);
// In very rare cases with some versions of the GD library, the x-value
// of the left side of the bounding box as returned by the first call of
// imagettfbbox is corrupt (value -2147483648 = 0x80000000).
......@@ -384,7 +402,7 @@ class CaptchaImageResponse extends Response {
// can be used as workaround.
// This issue is discussed at http://drupal.org/node/349218.
if ($bbox[2] < 0) {
$bbox = imagettfbbox($font_size, 0, \Drupal::service('file_system')->realpath($font), $character);
$bbox = imagettfbbox($font_size, 0, $this->fileSystem->realpath($font), $character);
}
}
else {
......@@ -403,7 +421,7 @@ class CaptchaImageResponse extends Response {
}
// Random (but small) rotation of the character.
// TODO: add a setting for this?
// @todo add a setting for this?
$angle = mt_rand(-10, 10);
// Determine print position: at what coordinate should the character be
......@@ -440,7 +458,7 @@ class CaptchaImageResponse extends Response {
imagestring($image, 5, $pos_x, $pos_y, $character, $color);
}
else {
imagettftext($image, $font_size, $angle, $pos_x, $pos_y, $color, \Drupal::service('file_system')->realpath($font), $character);
imagettftext($image, $font_size, $angle, $pos_x, $pos_y, $color, $this->fileSystem->realpath($font), $character);
}
}
......
<?php
namespace Drupal\image_captcha\Controller;
namespace Drupal\image_captcha\StreamedResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Config\ImmutableConfig;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* A Controller to preview the captcha font on the settings page.
*/
class CaptchaFontPreview extends StreamedResponse {
class CaptchaFontPreviewStreamedResponse extends StreamedResponse {
/**
* Config service.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Token font selector.
*
* @string
*/
protected $token;
/**
* {@inheritdoc}
*/
public function content(Request $request) {
$token = $request->get('token');
public function __construct(ImmutableConfig $config, $token, $callback = NULL, $status = 200, $headers = []) {
parent::__construct(NULL, $status, $headers);
$this->config = $config;
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public function sendContent() {
// Get the font from the given font token.
if ($token == 'BUILTIN') {
if ($this->token == 'BUILTIN') {
$font = 'BUILTIN';
}
else {
// Get the mapping of font tokens to font file objects.
$fonts = \Drupal::config('image_captcha.settings')
->get('image_captcha_fonts_preview_map_cache');
if (!isset($fonts[$token])) {
echo 'bad token';
exit();
$fonts = $this->config->get('image_captcha_fonts_preview_map_cache');
if (!isset($fonts[$this->token])) {
return 'bad token';
}
// Get the font path.
$font = $fonts[$token]['uri'];
$font = $fonts[$this->token]['uri'];
// Some sanity checks if the given font is valid.
if (!is_file($font) || !is_readable($font)) {
echo 'bad font';
exit();
return 'bad font';
}
}
......@@ -45,7 +65,7 @@ class CaptchaFontPreview extends StreamedResponse {
// Allocate image resource.
$image = imagecreatetruecolor($width, $height);
if (!$image) {
exit();
return NULL;
}
// White background and black foreground.
$background_color = imagecolorallocate($image, 255, 255, 255);
......@@ -65,9 +85,6 @@ class CaptchaFontPreview extends StreamedResponse {
imagepng($image);
// Release image memory.
imagedestroy($image);
// Close connection.
exit();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment