Commit 451e3b69 authored by Joshua Sedler's avatar Joshua Sedler 🤸🏼
Browse files

Issue #3321215: "ImageCaptchaConstants" won't get recognized inside image_captcha.install

parent 75bf3398
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@
 * Installation/uninstallation related functions for the image_captcha module.
 */

use Drupal\captcha\Constants\CaptchaConstants;

/**
 * Implements hook_requirements().
@@ -13,11 +12,19 @@ use Drupal\captcha\Constants\CaptchaConstants;
function image_captcha_requirements($phase) {
  $requirements = [];
  if ($phase == 'install') {
    // We can not use the IMAGE_CAPTCHA_ERROR_NO_GDLIB constant in this
    // as it isn't loaded yet on installation. Define a variable here instead,
    // With the same value:
    $image_captcha_error_no_gdlib = 1;
    // _image_captcha_check_setup() is defined in image_captcha.module.
    // Using 'module_load_include' returns FALSE so 'include_once' used instead.
    include_once __DIR__ . '/image_captcha.module';
    // Check if the GD library is available and raise an error when not.
    if (_image_captcha_check_setup(FALSE) & CaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB) {
    // @todo Move this bitwise operation inside _image_captcha_check_setup if
    // possible:
    // Visit https://www.drupal.org/project/captcha/issues/3324321 for more
    // information.
    if (_image_captcha_check_setup(FALSE) & $image_captcha_error_no_gdlib) {
      $requirements['image_captcha_requires_gd'] = [
        'title' => \Drupal::translation()
          ->translate('Image CAPTCHA requires GD library'),
+7 −6
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\DrupalKernel;
use Drupal\captcha\Constants\CaptchaConstants;
use Drupal\image_captcha\Constants\ImageCaptchaConstants;

/**
 * Implements hook_help().
@@ -29,7 +30,7 @@ function image_captcha_help($route_name, RouteMatchInterface $route_match) {
 *   List of font paths.
 */
function _image_captcha_get_enabled_fonts() {
  if (CaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT & _image_captcha_check_setup(FALSE)) {
  if (ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT & _image_captcha_check_setup(FALSE)) {
    return ['BUILTIN'];
  }
  else {
@@ -185,11 +186,11 @@ function _image_captcha_check_setup($check_fonts = TRUE) {
  // We need at least the imagepng function.
  // Note that the imagejpg function is optionally also used, but not required.
  if (!function_exists('imagepng')) {
    $status = $status | CaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB;
    $status = $status | ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB;
  }

  if (!function_exists('imagettftext')) {
    $status = $status | CaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT;
    $status = $status | ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT;
  }

  if ($check_fonts) {
@@ -203,13 +204,13 @@ function _image_captcha_check_setup($check_fonts = TRUE) {
      // Try again now.
      $fonts = _image_captcha_get_enabled_fonts();
      if (empty($fonts)) {
        $status = $status | CaptchaConstants::IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM;
        $status = $status | ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM;
      }
    }
    if (!empty($fonts)) {
      $problem_fonts = _image_captcha_check_fonts($fonts);
      if (count($problem_fonts) != 0) {
        $status = $status | CaptchaConstants::IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM;
        $status = $status | ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM;
      }
    }
  }
@@ -252,7 +253,7 @@ function image_captcha_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
    case 'list':
      // Only offer the image CAPTCHA if it is possible to generate an image
      // on this setup.
      if (!(_image_captcha_check_setup() & CaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB)) {
      if (!(_image_captcha_check_setup() & ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB)) {
        return ['Image'];
      }
      else {
+21 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\image_captcha\Constants;

/**
 * Constants for the image_captcha module.
 */
class ImageCaptchaConstants {

  const IMAGE_CAPTCHA_ALLOWED_CHARACTERS = 'aAbBCdEeFfGHhijKLMmNPQRrSTtWXYZ23456789';

  // Setup status flags:
  const IMAGE_CAPTCHA_ERROR_NO_GDLIB = 1;
  const IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT = 2;
  const IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM = 4;

  const IMAGE_CAPTCHA_FILE_FORMAT_JPG = 1;
  const IMAGE_CAPTCHA_FILE_FORMAT_PNG = 2;
  const IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG = 3;

}
+2 −2
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Component\Utility\Crypt;
use Drupal\captcha\Constants\CaptchaConstants;
use Drupal\image_captcha\Constants\ImageCaptchaConstants;

/**
 * Description of CaptchaImageRefresh.
@@ -59,7 +59,7 @@ class CaptchaImageRefresh extends ControllerBase {
      $config = $this->config('image_captcha.settings');
      $captcha_sid = _captcha_generate_captcha_session($form_id);
      $captcha_token = Crypt::randomBytesBase64();
      $allowed_char = $config->get('image_captcha_image_allowed_chars') ? $config->get('image_captcha_image_allowed_chars') : CaptchaConstants::IMAGE_CAPTCHA_ALLOWED_CHARACTERS;
      $allowed_char = $config->get('image_captcha_image_allowed_chars') ? $config->get('image_captcha_image_allowed_chars') : ImageCaptchaConstants::IMAGE_CAPTCHA_ALLOWED_CHARACTERS;
      $allowed_chars = _image_captcha_utf8_split($allowed_char);
      $code_length = (int) $config->get('image_captcha_code_length');
      $code = '';
+7 −7
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\captcha\Constants\CaptchaConstants;
use Drupal\image_captcha\Constants\ImageCaptchaConstants;

/**
 * Displays the pants settings form.
@@ -84,7 +84,7 @@ class ImageCaptchaSettingsForm extends ConfigFormBase {

    // First some error checking.
    $setup_status = _image_captcha_check_setup(FALSE);
    if ($setup_status & CaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB) {
    if ($setup_status & ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB) {
      $this->messenger()->addError($this->t(
        'The Image CAPTCHA module can not generate images because your PHP setup does not support it (no <a href="!gdlib" target="_blank">GD library</a> with JPEG support).',
        ['!gdlib' => 'http://php.net/manual/en/book.image.php']
@@ -114,7 +114,7 @@ class ImageCaptchaSettingsForm extends ConfigFormBase {
    $form['image_captcha_code_settings']['image_captcha_image_allowed_chars'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Characters to use in the code'),
      '#default_value' => $config->get('image_captcha_image_allowed_chars') ? $config->get('image_captcha_image_allowed_chars') : CaptchaConstants::IMAGE_CAPTCHA_ALLOWED_CHARACTERS,
      '#default_value' => $config->get('image_captcha_image_allowed_chars') ? $config->get('image_captcha_image_allowed_chars') : ImageCaptchaConstants::IMAGE_CAPTCHA_ALLOWED_CHARACTERS,
    ];
    $form['image_captcha_code_settings']['image_captcha_code_length'] = [
      '#type' => 'select',
@@ -179,9 +179,9 @@ class ImageCaptchaSettingsForm extends ConfigFormBase {
      '#description' => $this->t('Select the file format for the image. JPEG usually results in smaller files, PNG allows tranparency.'),
      '#default_value' => $config->get('image_captcha_file_format'),
      '#options' => [
        CaptchaConstants::IMAGE_CAPTCHA_FILE_FORMAT_JPG => $this->t('JPEG'),
        CaptchaConstants::IMAGE_CAPTCHA_FILE_FORMAT_PNG => $this->t('PNG'),
        CaptchaConstants::IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG => $this->t('PNG with transparent background'),
        ImageCaptchaConstants::IMAGE_CAPTCHA_FILE_FORMAT_JPG => $this->t('JPEG'),
        ImageCaptchaConstants::IMAGE_CAPTCHA_FILE_FORMAT_PNG => $this->t('PNG'),
        ImageCaptchaConstants::IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG => $this->t('PNG with transparent background'),
      ],
    ];

@@ -350,7 +350,7 @@ class ImageCaptchaSettingsForm extends ConfigFormBase {

    // First check if there is TrueType support.
    $setup_status = _image_captcha_check_setup(FALSE);
    if ($setup_status & CaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) {
    if ($setup_status & ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) {
      // Show a warning that there is no TrueType support.
      $form['no_ttf_support'] = [
        '#type' => 'item',
Loading