Skip to content
Snippets Groups Projects
Commit 5b78c8dc authored by Joshua Sedler's avatar Joshua Sedler :cartwheel_tone2:
Browse files

Partial fix for Issue #3316486

parent 6749a64d
No related branches found
No related tags found
No related merge requests found
Showing
with 1026 additions and 66 deletions
......@@ -8,6 +8,7 @@
use Drupal\captcha\Entity\CaptchaPoint;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Render\Element;
use Drupal\captcha\Constants\CaptchaConstants;
/**
* Helper function for adding/updating a CAPTCHA point.
......@@ -76,7 +77,7 @@ function captcha_get_form_id_setting($form_id, $symbolic = FALSE) {
* @return string
* The session ID of the new CAPTCHA session.
*/
function _captcha_generate_captcha_session($form_id = NULL, $status = CAPTCHA_STATUS_UNSOLVED) {
function _captcha_generate_captcha_session($form_id = NULL, $status = CaptchaConstants::CAPTCHA_STATUS_UNSOLVED) {
$user = \Drupal::currentUser();
// Initialize solution with random data.
......@@ -129,7 +130,7 @@ function _captcha_required_for_user($captcha_sid, $form_id) {
->get('persistence');
// First check: should we always add a CAPTCHA?
if ($captcha_persistence == CAPTCHA_PERSISTENCE_SHOW_ALWAYS) {
if ($captcha_persistence == CaptchaConstants::CAPTCHA_PERSISTENCE_SHOW_ALWAYS) {
return TRUE;
}
......@@ -143,22 +144,22 @@ function _captcha_required_for_user($captcha_sid, $form_id) {
// Second check: if the current session is already
// solved: omit further CAPTCHAs.
if ($captcha_session_status == CAPTCHA_STATUS_SOLVED) {
if ($captcha_session_status == CaptchaConstants::CAPTCHA_STATUS_SOLVED) {
return FALSE;
}
// Third check: look at the persistence level
// (per form instance, per form or per user).
if ($captcha_persistence == CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE) {
if ($captcha_persistence == CaptchaConstants::CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE) {
return TRUE;
}
else {
$captcha_success_form_ids = isset($_SESSION['captcha_success_form_ids']) ? (array) ($_SESSION['captcha_success_form_ids']) : [];
switch ($captcha_persistence) {
case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL:
case CaptchaConstants::CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL:
return (count($captcha_success_form_ids) == 0);
case CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE:
case CaptchaConstants::CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE:
return !isset($captcha_success_form_ids[$form_id]);
}
}
......
......@@ -20,38 +20,7 @@ use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Site\Settings;
/**
* Constants for CAPTCHA persistence.
*
* @todo change these integers to strings because the CAPTCHA settings
* form saves them as strings in the variables table anyway?
*/
// @todo move all constants to some class.
// Always add a CAPTCHA (even on every page of a multipage workflow).
define('CAPTCHA_PERSISTENCE_SHOW_ALWAYS', 0);
// Only one CAPTCHA has to be solved per form instance/multi-step workflow.
define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE', 1);
// Once the user answered correctly for a CAPTCHA on a certain form type,
// no more CAPTCHAs will be offered anymore for that form.
define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE', 2);
// Once the user answered correctly for a CAPTCHA on the site,
// no more CAPTCHAs will be offered anymore.
define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL', 3);
define('CAPTCHA_STATUS_UNSOLVED', 0);
define('CAPTCHA_STATUS_SOLVED', 1);
define('CAPTCHA_STATUS_EXAMPLE', 2);
define('CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE', 0);
define('CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE', 1);
define('CAPTCHA_WHITELIST_IP_ADDRESS', 'addresses');
define('CAPTCHA_WHITELIST_IP_RANGE', 'ranges');
// Default captcha field access.
define('CAPTCHA_FIELD_DEFAULT_ACCESS', 1);
use Drupal\captcha\Constants\CaptchaConstants;
/**
* Implements hook_help().
......@@ -569,8 +538,8 @@ function captcha_validate($element, FormStateInterface &$form_state) {
if (in_array($captcha_persistence,
[
CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL,
CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE,
CaptchaConstants::CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL,
CaptchaConstants::CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE,
])) {
// Only save the success in $_SESSION if it is actually needed for
// further validation in _captcha_required_for_user(). Setting
......@@ -581,7 +550,7 @@ function captcha_validate($element, FormStateInterface &$form_state) {
// Record success.
\Drupal::database()->update('captcha_sessions')
->condition('csid', $csid)
->fields(['status' => CAPTCHA_STATUS_SOLVED])
->fields(['status' => CaptchaConstants::CAPTCHA_STATUS_SOLVED])
->expression('attempts', 'attempts + 1')
->execute();
}
......@@ -719,8 +688,8 @@ function captcha_captcha($op, $captcha_type = '') {
*/
function captcha_whitelist_ips_parse_values($whitelist_ips_value) {
$whitelist_ips = [
CAPTCHA_WHITELIST_IP_RANGE => [],
CAPTCHA_WHITELIST_IP_ADDRESS => [],
CaptchaConstants::CAPTCHA_WHITELIST_IP_RANGE => [],
CaptchaConstants::CAPTCHA_WHITELIST_IP_ADDRESS => [],
];
// Ensure the IPs value is trimmed before moving onward.
......@@ -734,10 +703,10 @@ function captcha_whitelist_ips_parse_values($whitelist_ips_value) {
foreach ($value_rows as $value_row) {
$value_row = trim($value_row);
if (strpos($value_row, '-') !== FALSE) {
$whitelist_ips[CAPTCHA_WHITELIST_IP_RANGE][] = $value_row;
$whitelist_ips[CaptchaConstants::CAPTCHA_WHITELIST_IP_RANGE][] = $value_row;
}
else {
$whitelist_ips[CAPTCHA_WHITELIST_IP_ADDRESS][] = $value_row;
$whitelist_ips[CaptchaConstants::CAPTCHA_WHITELIST_IP_ADDRESS][] = $value_row;
}
}
......@@ -763,15 +732,15 @@ function captcha_whitelist_ip_whitelisted($ip_address = '') {
$whitelist_ips_value = $config->get('whitelist_ips');
$whitelist_ips = captcha_whitelist_ips_parse_values($whitelist_ips_value);
if (in_array($ip_address, $whitelist_ips[CAPTCHA_WHITELIST_IP_ADDRESS])) {
if (in_array($ip_address, $whitelist_ips[CaptchaConstants::CAPTCHA_WHITELIST_IP_ADDRESS])) {
return TRUE;
}
elseif (empty($whitelist_ips[CAPTCHA_WHITELIST_IP_RANGE])) {
elseif (empty($whitelist_ips[CaptchaConstants::CAPTCHA_WHITELIST_IP_RANGE])) {
return FALSE;
}
foreach ($whitelist_ips[CAPTCHA_WHITELIST_IP_RANGE] as $ip_range) {
list($ip_lower, $ip_upper) = explode('-', $ip_range, 2);
foreach ($whitelist_ips[CaptchaConstants::CAPTCHA_WHITELIST_IP_RANGE] as $ip_range) {
[$ip_lower, $ip_upper] = explode('-', $ip_range, 2);
$ip_lower_dec = (float) sprintf("%u", ip2long($ip_lower));
$ip_upper_dec = (float) sprintf("%u", ip2long($ip_upper));
$ip_address_dec = (float) sprintf("%u", ip2long($ip_address));
......
This diff is collapsed.
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
......@@ -5,6 +5,8 @@
* Installation/uninstallation related functions for the image_captcha module.
*/
use Drupal\image_captcha\Constants\ImageCaptchaConstants;
/**
* Implements hook_requirements().
*/
......@@ -15,7 +17,7 @@ function image_captcha_requirements($phase) {
// 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) & IMAGE_CAPTCHA_ERROR_NO_GDLIB) {
if (_image_captcha_check_setup(FALSE) & ImageCaptchaConstants::IMAGE_CAPTCHA_ERROR_NO_GDLIB) {
$requirements['image_captcha_requires_gd'] = [
'title' => \Drupal::translation()
->translate('Image CAPTCHA requires GD library'),
......
File moved
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment