Move all files to 2017/

This commit is contained in:
Oliver Davies 2025-09-29 22:25:17 +01:00
parent ac7370f67f
commit 2875863330
15717 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,169 @@
<?php
/**
* @file
* Integrates third party settings on the Antibot module's behalf.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Flag to indicate that antibot can be set.
*/
define('WEBFORM_ANTIBOT_NEUTRAL', -1);
/**
* Flag to indicate that antibot is disabled for all webforms.
*/
define('WEBFORM_ANTIBOT_DISABLED_WEBFORM', 2);
/**
* Flag to indicate that antibot is enabled for all webforms.
*/
define('WEBFORM_ANTIBOT_ENABLED_WEBFORM', 3);
/**
* Alter webform third party settings webforms to include Antibot configuration.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param bool $antibot
* TRUE if antibot protection is enabled.
* @param int $antibot_state
* Flag that determines if antibot protection is enabled, disabled, or can be
* set.
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
* The label to displayed within the checkbox titles.
*/
function _webform_antibot_form(array &$form, FormStateInterface $form_state, $antibot, $antibot_state, $label) {
$t_args = [
'%label' => $label,
':href_antibot' => Url::fromRoute('antibot.settings')->toString(),
':href_webform' => Url::fromRoute('webform.config')->toString(),
];
// Antibot.
$form['third_party_settings']['antibot'] = [
'#type' => 'details',
'#title' => t('Antibot'),
'#open' => TRUE,
'#description' => t('Prevent SPAM webform submissions from being submitted without JavaScript enabled using the <a href=":href_antibot">antibot</a> method.', $t_args),
];
$form['third_party_settings']['antibot']['antibot'] = [
'#type' => 'checkbox',
'#title' => t('Protect %label with Antibot', $t_args),
'#default_value' => $antibot,
'#return_value' => TRUE,
];
if ($antibot_state != WEBFORM_ANTIBOT_NEUTRAL) {
$form['third_party_settings']['antibot']['antibot']['#attributes']['disabled'] = 'disabled';
$form_state->set('antibot_disabled', TRUE);
if ($antibot_state == WEBFORM_ANTIBOT_ENABLED_WEBFORM) {
$form['third_party_settings']['antibot']['antibot']['#default_value'] = 1;
$form['third_party_settings']['antibot']['antibot']['#description'] = t('<a href=":href_webform">Antibot protection</a> is enabled for all webforms.', $t_args);
}
}
$form['#validate'][] = '_webform_antibot_form_validate';
}
/**
* Validate callback; Checks if antibot is disabled and remove it from the third party settings values.
*/
function _webform_antibot_form_validate(&$form, FormStateInterface $form_state) {
$third_party_settings = $form_state->getValue('third_party_settings');
if ($form_state->get('antibot_disabled') || empty($third_party_settings['antibot']['antibot'])) {
unset($third_party_settings['antibot']['antibot']);
}
if (empty($third_party_settings['antibot'])) {
unset($third_party_settings['antibot']);
}
$form_state->setValue('third_party_settings', $third_party_settings);
}
/**
* Implements hook_webform_admin_third_party_settings_form_alter().
*/
function antibot_webform_admin_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$antibot = $third_party_settings_manager->getThirdPartySetting('antibot', 'antibot');
$antibot_state = WEBFORM_ANTIBOT_NEUTRAL;
_webform_antibot_form(
$form,
$form_state,
$antibot,
$antibot_state,
t('all webforms')
);
}
/**
* Implements hook_webform_third_party_settings_form_alter().
*/
function antibot_webform_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $form_state->getFormObject()->getEntity();
$antibot = $webform->getThirdPartySetting('antibot', 'antibot');
if ($third_party_settings_manager->getThirdPartySetting('antibot', 'antibot')) {
$antibot_state = WEBFORM_ANTIBOT_ENABLED_WEBFORM;
}
else {
$antibot_state = WEBFORM_ANTIBOT_NEUTRAL;
}
_webform_antibot_form(
$form,
$form_state,
$antibot,
$antibot_state,
t('@label webform', ['@label' => $webform->label()])
);
}
/**
* Implements hook_webform_submission_form_alter().
*/
function antibot_webform_submission_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only add an Antibot when a webform is initially load.
// After a webform is submitted, via a multi-step webform and/or saving a draft,
// we can skip adding an Antibot.
if ($form_state->isSubmitted()) {
return;
}
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $form_state->getFormObject()->getEntity();
$webform = $webform_submission->getWebform();
$antibot = $third_party_settings_manager->getThirdPartySetting('antibot', 'antibot') ?:
$webform->getThirdPartySetting('antibot', 'antibot');
if ($antibot) {
if (function_exists('antibot_protect_form')) {
// Applies to antibot-8.x-1.2+
// Set #form_id which is needed by antibot_protect_form().
$form['#form_id'] = $form_id;
antibot_protect_form($form);
}
else {
// Applies to antibot-8.x-1.1 and below.
// @todo Remove backward compatibility for antibot-8.x-1.1.
$form['#pre_render'][] = 'antibot_form_pre_render';
}
}
}

View file

@ -0,0 +1,234 @@
<?php
/**
* @file
* Integrates third party settings on the Honeypot module's behalf.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Flag to indicate that a honeypot setting can be set.
*/
define('WEBFORM_HONEYPOT_NEUTRAL', -1);
/**
* Flag to indicate that a honeypot setting is disabled for all webforms.
*/
define('WEBFORM_HONEYPOT_DISABLED_ALL', 0);
/**
* Flag to indicate that a honeypot setting is enabled for all webforms.
*/
define('WEBFORM_HONEYPOT_ENABLED_ALL', 1);
/**
* Flag to indicate that a honeypot setting is disabled for all webforms.
*/
define('WEBFORM_HONEYPOT_DISABLED_WEBFORM', 2);
/**
* Flag to indicate that a honeypot setting is enabled for all webforms.
*/
define('WEBFORM_HONEYPOT_ENABLED_WEBFORM', 3);
/**
* Alter webform third party settings webforms to include Honeypot configuration.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param bool $honeypot
* TRUE if honeypot protection is enabled.
* @param int $honeypot_state
* Flag that determines if honeypot protection is enabled, disabled, or can be
* set.
* @param bool $time_restriction
* TRUE if honeypot time restriction is enabled.
* @param int $time_restriction_state
* Flag that determines if honeypot time restriction is enabled, disabled,
* or can be set.
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
* The label to displayed within the checkbox titles.
*/
function _webform_honeypot_form(array &$form, FormStateInterface $form_state, $honeypot, $honeypot_state, $time_restriction, $time_restriction_state, $label) {
$t_args = [
'%label' => $label,
':href_honeypot' => Url::fromRoute('honeypot.config')->toString(),
':href_webform' => Url::fromRoute('webform.config')->toString(),
];
// Honeypot.
$form['third_party_settings']['honeypot'] = [
'#type' => 'details',
'#title' => t('Honeypot'),
'#open' => TRUE,
'#description' => t('Mitigate SPAM webform submissions using the <a href=":href_honeypot">honeypot</a> method.', $t_args),
];
$form['third_party_settings']['honeypot']['honeypot'] = [
'#type' => 'checkbox',
'#title' => t('Protect %label with Honeypot', $t_args),
'#default_value' => $honeypot,
'#return_value' => TRUE,
];
if ($honeypot_state != WEBFORM_HONEYPOT_NEUTRAL) {
$form['third_party_settings']['honeypot']['honeypot']['#attributes']['disabled'] = 'disabled';
$form_state->set('honeypot_disabled', TRUE);
if ($honeypot_state == WEBFORM_HONEYPOT_ENABLED_ALL) {
$form['third_party_settings']['honeypot']['honeypot']['#default_value'] = 1;
$form['third_party_settings']['honeypot']['honeypot']['#description'] = t('<a href=":href_honeypot">Honeypot protection</a> is enabled for all webforms.', $t_args);
}
elseif ($honeypot_state == WEBFORM_HONEYPOT_ENABLED_WEBFORM) {
$form['third_party_settings']['honeypot']['honeypot']['#default_value'] = 1;
$form['third_party_settings']['honeypot']['honeypot']['#description'] = t('<a href=":href_webform">Honeypot protection</a> is enabled for all webforms.', $t_args);
}
}
// Time limit.
$form['third_party_settings']['honeypot']['time_restriction'] = [
'#type' => 'checkbox',
'#title' => t('Add time restriction to %label', $t_args),
'#default_value' => $time_restriction,
'#return_value' => TRUE,
];
if ($time_restriction_state != WEBFORM_HONEYPOT_NEUTRAL) {
$form['third_party_settings']['honeypot']['time_restriction']['#attributes']['disabled'] = 'disabled';
$form_state->set('time_restriction_disabled', TRUE);
if ($time_restriction_state == WEBFORM_HONEYPOT_DISABLED_ALL) {
$form['third_party_settings']['honeypot']['time_restriction']['#default_value'] = 0;
$form['third_party_settings']['honeypot']['time_restriction']['#description'] = t('<a href=":href_honeypot">Time limit</a> is disabled for all webforms.', $t_args);
}
elseif ($time_restriction_state == WEBFORM_HONEYPOT_ENABLED_WEBFORM) {
$form['third_party_settings']['honeypot']['time_restriction']['#default_value'] = 1;
$form['third_party_settings']['honeypot']['time_restriction']['#description'] = t('<a href=":href_webform">Time limit</a> is enabled for all webforms.', $t_args);
}
}
$form['#validate'][] = '_webform_honeypot_form_validate';
}
/**
* Validate callback; Checks if honeypot or time_restriction is disabled and removes them from the third party settings values.
*/
function _webform_honeypot_form_validate(&$form, FormStateInterface $form_state) {
$third_party_settings = $form_state->getValue('third_party_settings');
if ($form_state->get('honeypot_disabled') || empty($third_party_settings['honeypot']['honeypot'])) {
unset($third_party_settings['honeypot']['honeypot']);
}
if ($form_state->get('time_restriction_disabled') || empty($third_party_settings['honeypot']['time_restriction'])) {
unset($third_party_settings['honeypot']['time_restriction']);
}
if (empty($third_party_settings['honeypot'])) {
unset($third_party_settings['honeypot']);
}
$form_state->setValue('third_party_settings', $third_party_settings);
}
/**
* Implements hook_webform_admin_third_party_settings_form_alter().
*/
function honeypot_webform_admin_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$honeypot = $third_party_settings_manager->getThirdPartySetting('honeypot', 'honeypot');
$honeypot_state = \Drupal::config('honeypot.settings')->get('protect_all_forms') ? WEBFORM_HONEYPOT_ENABLED_ALL : WEBFORM_HONEYPOT_NEUTRAL;
$time_restriction = $third_party_settings_manager->getThirdPartySetting('honeypot', 'time_restriction');
$time_restriction_state = (\Drupal::config('honeypot.settings')->get('time_limit') == 0) ? WEBFORM_HONEYPOT_DISABLED_ALL : WEBFORM_HONEYPOT_NEUTRAL;
_webform_honeypot_form(
$form,
$form_state,
$honeypot,
$honeypot_state,
$time_restriction,
$time_restriction_state,
t('all webforms')
);
}
/**
* Implements hook_webform_third_party_settings_form_alter().
*/
function honeypot_webform_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $form_state->getFormObject()->getEntity();
$honeypot = $webform->getThirdPartySetting('honeypot', 'honeypot');
if (\Drupal::config('honeypot.settings')->get('protect_all_forms')) {
$honeypot_state = WEBFORM_HONEYPOT_ENABLED_ALL;
}
elseif ($third_party_settings_manager->getThirdPartySetting('honeypot', 'honeypot')) {
$honeypot_state = WEBFORM_HONEYPOT_ENABLED_WEBFORM;
}
else {
$honeypot_state = WEBFORM_HONEYPOT_NEUTRAL;
}
$time_restriction = $webform->getThirdPartySetting('honeypot', 'time_restriction');
if (\Drupal::config('honeypot.settings')->get('time_limit') == 0) {
$time_restriction_state = WEBFORM_HONEYPOT_DISABLED_ALL;
}
elseif ($third_party_settings_manager->getThirdPartySetting('honeypot', 'time_restriction')) {
$time_restriction_state = WEBFORM_HONEYPOT_ENABLED_WEBFORM;
}
else {
$time_restriction_state = WEBFORM_HONEYPOT_NEUTRAL;
}
_webform_honeypot_form(
$form,
$form_state,
$honeypot,
$honeypot_state,
$time_restriction,
$time_restriction_state,
t('@label webform', ['@label' => $webform->label()])
);
}
/**
* Implements hook_webform_submission_form_alter().
*/
function honeypot_webform_submission_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only add a Honeypot when a webform is initially load.
// After a webform is submitted, via a multi-step webform and/or saving a draft,
// we can skip adding a Honeypot.
if ($form_state->isSubmitted()) {
return;
}
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $form_state->getFormObject()->getEntity();
$webform = $webform_submission->getWebform();
$options = [];
$honeypot = $third_party_settings_manager->getThirdPartySetting('honeypot', 'honeypot') ?:
$webform->getThirdPartySetting('honeypot', 'honeypot');
if ($honeypot) {
$options[] = 'honeypot';
}
$time_restriction = $third_party_settings_manager->getThirdPartySetting('honeypot', 'time_restriction') ?:
$webform->getThirdPartySetting('honeypot', 'time_restriction');
if ($time_restriction) {
$options[] = 'time_restriction';
}
if ($options) {
honeypot_add_form_protection($form, $form_state, $options);
}
}

View file

@ -0,0 +1,79 @@
<?php
/**
* @file
* Integrates third party settings on the Maillog module's behalf.
*/
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Plugin\WebformHandler\EmailWebformHandler;
/**
* Implements hook_webform_submission_form_alter().
*/
function maillog_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
// Never display Maillog message via CLI.
if (PHP_SAPI == 'cli') {
return;
}
// Never display Maillog message via dialogs or Ajax requests.
$wrapper_format = \Drupal::request()->get(MainContentViewSubscriber::WRAPPER_FORMAT);
if ($wrapper_format) {
return;
}
$config = \Drupal::config('maillog.settings');
// Only display warning if emails are not being sent.
if ($config->get('send')) {
return;
}
// If this webform does not send any email do not display any warning.
$webform = Webform::load($form['#webform_id']);
$sends_email = FALSE;
$handlers = $webform->getHandlers();
foreach ($handlers as $handler) {
if ($handler instanceof EmailWebformHandler) {
$sends_email = TRUE;
}
}
if (!$sends_email) {
return;
}
// Build warning message base on maillog settings and permissions.
$build = [];
// Display warning that all emails will be logged to admins only.
// Display warning that all emails will be logged to admins only.
if (\Drupal::currentUser()->hasPermission('administer maillog')) {
$t_args = [':href' => Url::fromRoute('maillog.settings')->toString()];
if ($config->get('log')) {
$build[] = ['#markup' => t('The <a href=":href">Maillog</a> module is logging all emails.', $t_args)];
}
else {
$build[] = ['#markup' => t('The <a href=":href">Maillog</a> module is installed.', $t_args)];
}
}
// Display warning if the user can view email on page.
if (\Drupal::currentUser()->hasPermission('view maillog') && $config->get('verbose')) {
$build[] = ['#prefix' => ' ', '#markup' => t('Emails will displayed on this page.')];
}
// Display warning if no emails are being sent.
$build[] = [
'#markup' => t('No emails will be sent.'),
'#prefix' => ' <b>',
'#suffix' => '</b>',
];
if ($build) {
\Drupal::messenger()->addWarning(\Drupal::service('renderer')->renderPlain($build));
}
}