Commit d57b1c2f authored by elachlan's avatar elachlan

Merge branch '8.x-1.x' of git.drupal.org:project/captcha into 8.x-1.x

parents 9eea1822 d33ec9e4
......@@ -5,8 +5,6 @@
* Install, update and uninstall functions for the CAPTCHA module.
*/
use Drupal\Core\Url;
/**
* Implements hook_schema().
*/
......
......@@ -52,20 +52,20 @@ function captcha_help($route_name, RouteMatchInterface $route_match) {
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('"CAPTCHA" is an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart". It is typically a challenge-response test to determine whether the user is human. The CAPTCHA module is a tool to fight automated submission by malicious users (spamming) of for example comments forms, user registration forms, guestbook forms, etc. You can extend the desired forms with an additional challenge, which should be easy for a human to solve correctly, but hard enough to keep automated scripts and spam bots out.') . '</p>';
$output .= '<p>' . t('Note that the CAPTCHA module interacts with page caching (see <a href=":performancesettings">performance settings</a>). Because the challenge should be unique for each generated form, the caching of the page it appears on is prevented. Make sure that these forms do not appear on too many pages or you will lose much caching efficiency. For example, if you put a CAPTCHA on the user login block, which typically appears on each page for anonymous visitors, caching will practically be disabled. The comment submission forms are another example. In this case you should set the <em>Location of comment submission form</em> to <em>Display on separate page</em> in the comment settings of the relevant <a href=":contenttypes">content types</a> for better caching efficiency.', [
':performancesettings' => \Drupal::url('system.performance_settings'),
':contenttypes' => \Drupal::url('entity.node_type.collection'),
]) . '</p>';
':performancesettings' => \Drupal::url('system.performance_settings'),
':contenttypes' => \Drupal::url('entity.node_type.collection'),
]) . '</p>';
$output .= '<p>' . t('CAPTCHA is a trademark of Carnegie Mellon University.') . '</p>';
return ['#markup' => $output];
case 'captcha_settings':
$output = '<p>' . t('A CAPTCHA can be added to virtually each Drupal form. Some default forms are already provided in the form list, but arbitrary forms can be easily added and managed when the option <em>Add CAPTCHA administration links to forms</em> is enabled.') . '</p>';
$output .= '<p>' . t('Users with the <em>Skip CAPTCHA</em> <a href=":perm">permission</a> won\'t be offered a challenge. Be sure to grant this permission to the trusted users (e.g. site administrators). If you want to test a protected form, be sure to do it as a user without the <em>Skip CAPTCHA</em> permission (e.g. as anonymous user).', [
':perm' => \Drupal::url('user.admin_permissions'),
]) . '</p>';
':perm' => \Drupal::url('user.admin_permissions'),
]) . '</p>';
$output .= '<p><b>' . t('Note that the CAPTCHA module disables <a href=":performancesettings">page caching</a> of pages that include a CAPTCHA challenge.', [
':performancesettings' => \Drupal::url('system.performance_settings'),
]) . '</b></p>';
':performancesettings' => \Drupal::url('system.performance_settings'),
]) . '</b></p>';
return ['#markup' => $output];
}
}
......@@ -172,7 +172,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
}
elseif ($config->get('administration_mode') && $account->hasPermission('administer CAPTCHA settings')
&& (!\Drupal::service('router.admin_context')
->isAdminRoute() || $config->get('allow_on_admin_pages'))
->isAdminRoute() || $config->get('allow_on_admin_pages'))
) {
// Add CAPTCHA administration tools.
/* @var \Drupal\captcha\Entity\CaptchaPoint $captcha_point */
......@@ -215,7 +215,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
t('Place a CAPTCHA here for untrusted users.'),
Url::fromRoute('captcha_point.add', [], [
'query' => Drupal::destination()
->getAsArray() + ['form_id' => $form_id],
->getAsArray() + ['form_id' => $form_id],
])
)->toString(),
];
......@@ -448,7 +448,7 @@ function captcha_validate($element, FormStateInterface &$form_state) {
// Update wrong response counter.
if (\Drupal::config('captcha.settings')->get('enable_stats', FALSE)) {
Drupal::state()->set('captcha.wrong_response_counter', Drupal::state()
->get('captcha.wrong_response_counter', 0) + 1);
->get('captcha.wrong_response_counter', 0) + 1);
}
if (\Drupal::config('captcha.settings')
......
......@@ -18,7 +18,8 @@ function image_captcha_font_preview($font_token) {
}
else {
// Get the mapping of font tokens to font file objects.
$fonts = \Drupal::config('image_captcha.settings')->get('image_captcha_fonts_preview_map_cache');
$fonts = \Drupal::config('image_captcha.settings')
->get('image_captcha_fonts_preview_map_cache');
if (!isset($fonts[$font_token])) {
print 'Bad token';
exit();
......
......@@ -17,11 +17,13 @@ function image_captcha_requirements($phase) {
// Check if the GD library is available and raise an error when not.
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'),
'title' => \Drupal::translation()
->translate('Image CAPTCHA requires GD library'),
'description' =>
\Drupal::translation()->translate('The Image CAPTCHA module can not be installed because your PHP setup does not provide the <a href="!gddoc">GD library</a>, which is required to generate images.',
['!gddoc' => 'http://www.php.net/manual/en/book.image.php']
),
\Drupal::translation()
->translate('The Image CAPTCHA module can not be installed because your PHP setup does not provide the <a href="!gddoc">GD library</a>, which is required to generate images.',
['!gddoc' => 'http://www.php.net/manual/en/book.image.php']
),
'severity' => REQUIREMENT_ERROR,
];
}
......
......@@ -5,7 +5,8 @@
* Implements image CAPTCHA for use with the CAPTCHA module.
*/
use \Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
define('IMAGE_CAPTCHA_ALLOWED_CHARACTERS', 'aAbBCdEeFfGHhijKLMmNPQRrSTtWXYZ23456789');
......@@ -18,8 +19,6 @@ define('IMAGE_CAPTCHA_FILE_FORMAT_JPG', 1);
define('IMAGE_CAPTCHA_FILE_FORMAT_PNG', 2);
define('IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG', 3);
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
......@@ -42,7 +41,8 @@ function _image_captcha_get_enabled_fonts() {
return ['BUILTIN'];
}
else {
return \Drupal::config('image_captcha.settings')->get('image_captcha_fonts');
return \Drupal::config('image_captcha.settings')
->get('image_captcha_fonts');
}
}
......@@ -107,7 +107,8 @@ function _image_captcha_utf8_split($str) {
$width = 4;
}
else {
\Drupal::logger('CAPTCHA')->error('Encountered an illegal byte while splitting an utf8 string in characters.');
\Drupal::logger('CAPTCHA')
->error('Encountered an illegal byte while splitting an utf8 string in characters.');
return $characters;
}
}
......@@ -151,7 +152,7 @@ function _image_captcha_check_setup($check_fonts = TRUE) {
if ($check_fonts) {
// Check availability of enabled fonts.
$fonts = _image_captcha_get_enabled_fonts();
$readable_fonts = array();
$readable_fonts = [];
list($readable_fonts, $problem_fonts) = _image_captcha_check_fonts($fonts);
if (count($problem_fonts) != 0) {
$status = $status | IMAGE_CAPTCHA_ERROR_TTF_FILE_READ_PROBLEM;
......@@ -210,7 +211,9 @@ function image_captcha_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
// the request for the image itself won't succeed (only ?q=user
// is permitted for unauthenticated users). We fall back to the
// Math CAPTCHA in that case.
if (defined('MAINTENANCE_MODE') && \Drupal::currentUser()->isAnonymous()) {
if (defined('MAINTENANCE_MODE') && \Drupal::currentUser()
->isAnonymous()
) {
return captcha_captcha('generate', 'Math');
}
// Generate a CAPTCHA code.
......@@ -232,7 +235,10 @@ function image_captcha_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
list($width, $height) = _image_captcha_image_size($code);
$result['form']['captcha_image'] = [
'#theme' => 'image',
'#uri' => Url::fromRoute('image_captcha.generator', ['session_id' => $captcha_sid, 'timestamp' => REQUEST_TIME])->toString(),
'#uri' => Url::fromRoute('image_captcha.generator', [
'session_id' => $captcha_sid,
'timestamp' => REQUEST_TIME,
])->toString(),
'#width' => $width,
'#height' => $height,
'#alt' => t('Image CAPTCHA'),
......@@ -253,7 +259,8 @@ function image_captcha_captcha($op, $captcha_type = '', $captcha_sid = NULL) {
// Handle the case insensitive validation option combined with
// ignoring spaces.
switch (\Drupal::config('captcha.settings')->get('default_validation')) {
switch (\Drupal::config('captcha.settings')
->get('default_validation')) {
case CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE:
$result['captcha_validate'] = 'captcha_validate_ignore_spaces';
break;
......
......@@ -2,12 +2,12 @@
namespace Drupal\image_captcha\Controller;
use Drupal\Core\Config\Config;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\image_captcha\Response\CaptchaImageResponse;
use Drupal\Core\Config\Config;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller which generates the image from defined settings.
......
......@@ -2,10 +2,10 @@
namespace Drupal\image_captcha\Response;
use Drupal\Core\Config\Config;
use Drupal\Core\Logger\LoggerChannelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Config\Config;
/**
* Response which is returned as the captcha for image_captcha.
......@@ -230,8 +230,8 @@ class CaptchaImageResponse extends Response {
$color_11_g = ($color_11 >> 8) & 0xFF;
$color_11_b = $color_11 & 0xFF;
// Interpolation factors.
$u = $sx - $sxf;
$v = $sy - $syf;
$u = $sx - $sxf;
$v = $sy - $syf;
$r = (int) ((1 - $v) * ((1 - $u) * $color_00_r + $u * $color_10_r) + $v * ((1 - $u) * $color_01_r + $u * $color_11_r));
$g = (int) ((1 - $v) * ((1 - $u) * $color_00_g + $u * $color_10_g) + $v * ((1 - $u) * $color_01_g + $u * $color_11_g));
$b = (int) ((1 - $v) * ((1 - $u) * $color_00_b + $u * $color_10_b) + $v * ((1 - $u) * $color_01_b + $u * $color_11_b));
......
......@@ -33,7 +33,7 @@ class Captcha extends FormElement {
// insensitive validation.
// TODO: shouldn't this be done somewhere else, e.g. in form_alter?
if (CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE == \Drupal::config('captcha.settings')
->get('default_validation')
->get('default_validation')
) {
$captcha_element['#captcha_validate'] = 'captcha_validate_case_insensitive_equality';
}
......
......@@ -101,20 +101,20 @@ class CaptchaSettingsForm extends ConfigFormBase {
// Based on Drupal core's "Clear all caches" (performance settings page).
$form['form_protection']['placement_caching'] = [
'#type' => 'item',
'#title' => t('CAPTCHA placement caching'),
'#description' => t('For efficiency, the positions of the CAPTCHA elements in each of the configured forms are cached. Most of the time, the structure of a form does not change and it would be a waste to recalculate the positions every time. Occasionally however, the form structure can change (e.g. during site building) and clearing the CAPTCHA placement cache can be required to fix the CAPTCHA placement.'),
'#title' => $this->t('CAPTCHA placement caching'),
'#description' => $this->t('For efficiency, the positions of the CAPTCHA elements in each of the configured forms are cached. Most of the time, the structure of a form does not change and it would be a waste to recalculate the positions every time. Occasionally however, the form structure can change (e.g. during site building) and clearing the CAPTCHA placement cache can be required to fix the CAPTCHA placement.'),
];
$form['form_protection']['placement_caching']['placement_cache_clear'] = [
'#type' => 'submit',
'#value' => t('Clear the CAPTCHA placement cache'),
'#value' => $this->t('Clear the CAPTCHA placement cache'),
'#submit' => ['::clearCaptchaPlacementCacheSubmit'],
];
// Configuration option for adding a CAPTCHA description.
$form['add_captcha_description'] = [
'#type' => 'checkbox',
'#title' => t('Add a description to the CAPTCHA'),
'#description' => t('Add a configurable description to explain the purpose of the CAPTCHA to the visitor.'),
'#title' => $this->t('Add a description to the CAPTCHA'),
'#description' => $this->t('Add a configurable description to explain the purpose of the CAPTCHA to the visitor.'),
'#default_value' => $config->get('add_captcha_description'),
];
$form['description'] = [
......@@ -136,8 +136,8 @@ class CaptchaSettingsForm extends ConfigFormBase {
// Option for case sensitive/insensitive validation of the responses.
$form['default_validation'] = [
'#type' => 'radios',
'#title' => t('Default CAPTCHA validation'),
'#description' => t('Define how the response should be processed by default. Note that the modules that provide the actual challenges can override or ignore this.'),
'#title' => $this->t('Default CAPTCHA validation'),
'#description' => $this->t('Define how the response should be processed by default. Note that the modules that provide the actual challenges can override or ignore this.'),
'#options' => [
CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE => $this->t('Case sensitive validation: the response has to exactly match the solution.'),
CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE => $this->t('Case insensitive validation: lowercase/uppercase errors are ignored.'),
......@@ -149,7 +149,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
// TODO for D7: Rethink/simplify the explanation and UI strings.
$form['persistence'] = [
'#type' => 'radios',
'#title' => t('Persistence'),
'#title' => $this->t('Persistence'),
'#default_value' => $config->get('persistence'),
'#options' => [
CAPTCHA_PERSISTENCE_SHOW_ALWAYS => $this->t('Always add a challenge.'),
......@@ -157,7 +157,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE => $this->t('Omit challenges on a form type once the user successfully responds to a challenge on a form of that type.'),
CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL => $this->t('Omit challenges on all forms once the user successfully responds to any challenge on the site.'),
],
'#description' => t('Define if challenges should be omitted during the rest of a session once the user successfully responds to a challenge.'),
'#description' => $this->t('Define if challenges should be omitted during the rest of a session once the user successfully responds to a challenge.'),
];
// Enable wrong response counter.
......@@ -189,7 +189,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save configuration'),
'#value' => $this->t('Save configuration'),
];
return parent::buildForm($form, $form_state);
......@@ -214,7 +214,7 @@ class CaptchaSettingsForm extends ConfigFormBase {
$config->set('enable_stats', $form_state->getValue('enable_stats'));
$config->set('log_wrong_responses', $form_state->getValue('log_wrong_responses'));
$config->save();
drupal_set_message(t('The CAPTCHA settings have been saved.'), 'status');
drupal_set_message($this->t('The CAPTCHA settings have been saved.'), 'status');
parent::submitForm($form, $form_state);
}
......
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