Skip to content
Snippets Groups Projects
Commit 497d17f6 authored by Jeff Geerling's avatar Jeff Geerling
Browse files

Issue #2138797 by geerlingguy: fix some coding standards violations.

parent 1a90e03e
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@
/**
* @file
*
* API Functionality for Honeypot module.
*/
......@@ -14,10 +13,10 @@
/**
* Alter the honeypot protections added to a particular form.
*
* @param (array) $options
* @param array $options
* Protections that will be applied to the form. May be empty, or may include
* 'honeypot' and/or 'time_restriction'.
* @param (array) $form
* @param array $form
* The Form API form to which protections will be added.
*/
function hook_honeypot_form_protections_alter(&$options, $form) {
......@@ -34,10 +33,10 @@ function hook_honeypot_form_protections_alter(&$options, $form) {
* You can use this hook to track when and how many times certain protected
* forms are displayed to certain users, or for other tracking purposes.
*
* @param (array) $options
* @param array $options
* Protections that were applied to the form. Includes 'honeypot' and/or
* 'time_restriction'.
* @param (array) $form
* @param array $form
* The Form API form to which protections were added.
*/
function hook_honeypot_add_form_protection($options, $form) {
......@@ -53,11 +52,11 @@ function hook_honeypot_add_form_protection($options, $form) {
* the user ID (0 if anonymous) of the user that was disallowed from submitting
* the form, and the reason (type) for the rejection of the form submission.
*
* @param (string) $form_id
* @param string $form_id
* Form ID of the form the user was disallowed from submitting.
* @param (int) $uid
* @param int $uid
* 0 for anonymous users, otherwise the user ID of the user.
* @param (string) $type
* @param string $type
* String indicating the reason the submission was blocked. Allowed values:
* - honeypot: If honeypot field was filled in.
* - honeypot_time: If form was completed before the configured time limit.
......@@ -76,15 +75,15 @@ function hook_honeypot_reject($form_id, $uid, $type) {
* hook to return additional time (in seconds) to honeypot when it is calculates
* the time limit for a particular form.
*
* @param (int) $honeypot_time_limit
* @param int $honeypot_time_limit
* The current honeypot time limit (in seconds), to which any additions you
* return will be added.
* @param (array) $form_values
* @param array $form_values
* Array of form values (may be empty).
* @param (int) $number
* @param int $number
* Number of times the current user has already fallen into the honeypot trap.
*
* @return (int) $additions
* @return int
* Additional time to add to the honeypot_time_limit, in seconds (integer).
*/
function hook_honeypot_time_limit($honeypot_time_limit, $form_values, $number) {
......
......@@ -4,4 +4,4 @@ description: 'Mitigates spam form submissions using the honeypot method.'
package: "Spam control"
core: 8.x
configure: admin/config/content/honeypot
hidden: false
\ No newline at end of file
hidden: false
......@@ -6,7 +6,7 @@
*/
/**
* Implements of hook_schema().
* Implements hook_schema().
*/
function honeypot_schema() {
$schema['honeypot_user'] = array(
......@@ -38,7 +38,7 @@ function honeypot_schema() {
*/
function honeypot_install() {
drupal_set_message(t("Honeypot installed successfully. Please !link to protect your forms from spam bots.", array(
'!link' => l(t('configure Honeypot'), 'admin/config/content/honeypot')
'!link' => l(t('configure Honeypot'), 'admin/config/content/honeypot'),
)));
}
......
......@@ -129,12 +129,9 @@ function honeypot_get_protected_forms() {
/**
* Form builder function to add different types of protection to forms.
*
* @param $options (array)
* @param array $options
* Array of options to be added to form. Currently accepts 'honeypot' and
* 'time_restriction'.
*
* @return $form_elements
* Returns elements to be placed in a form's elements array to prevent spam.
*/
function honeypot_add_form_protection(&$form, &$form_state, $options = array()) {
$account = \Drupal::request()->attributes->get('_account');
......@@ -163,9 +160,10 @@ function honeypot_add_form_protection(&$form, &$form_state, $options = array())
'#element_validate' => array('_honeypot_honeypot_validate'),
'#prefix' => '<div class="' . $honeypot_class . '">',
'#suffix' => '</div>',
// Hide honeypot.
'#attached' => array(
'css' => array(
'.' . $honeypot_class . ' { display: none !important; }' => array('type' => 'inline'), // Hide honeypot.
'.' . $honeypot_class . ' { display: none !important; }' => array('type' => 'inline'),
),
),
);
......@@ -232,9 +230,9 @@ function _honeypot_time_restriction_validate($form, &$form_state) {
/**
* Log blocked form submissions.
*
* @param $form_id
* @param string $form_id
* Form ID for the form on which submission was blocked.
* @param $type
* @param string $type
* String indicating the reason the submission was blocked. Allowed values:
* - honeypot: If honeypot field was filled in.
* - honeypot_time: If form was completed before the configured time limit.
......@@ -243,18 +241,17 @@ function _honeypot_log($form_id, $type) {
honeypot_log_failure($form_id, $type);
if (variable_get('honeypot_log', 0)) {
$variables = array(
'%form' => $form_id,
'@cause' => ($type == 'honeypot') ? t('submission of a value in the honeypot field') : t('submission of the form in less than minimum required time'),
'%form' => $form_id,
'@cause' => ($type == 'honeypot') ? t('submission of a value in the honeypot field') : t('submission of the form in less than minimum required time'),
);
watchdog('honeypot', 'Blocked submission of %form due to @cause.', $variables);
}
return;
}
/**
* Look up the time limit for the current user.
*
* @param $form_values
* @param array $form_values
* Array of form values (optional).
*/
function honeypot_get_time_limit($form_values = array()) {
......@@ -289,9 +286,9 @@ function honeypot_get_time_limit($form_values = array()) {
/**
* Log the failed submision with timestamp.
*
* @param $form_id
* @param string $form_id
* Form ID for the rejected form submission.
* @param $type
* @param string $type
* String indicating the reason the submission was blocked. Allowed values:
* - honeypot: If honeypot field was filled in.
* - honeypot_time: If form was completed before the configured time limit.
......
......@@ -47,37 +47,37 @@ class HoneypotSettingsController implements FormInterface {
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['configuration']['protect_all_forms'] = array(
'#type' => 'checkbox',
'#title' => t('Protect all forms with Honeypot'),
'#description' => t('Enable Honeypot protection for ALL forms on this site (it is best to only enable Honeypot for the forms you need below).'),
'#default_value' => config('honeypot.settings')->get('protect_all_forms'),
);
$form['configuration']['protect_all_forms']['#description'] .= '<br />' . t('<strong>Page caching will be disabled on any page where a form is present if the Honeypot time limit is not set to 0.</strong>');
$form['configuration']['log'] = array(
'#type' => 'checkbox',
'#title' => t('Log blocked form submissions'),
'#description' => t('Log submissions that are blocked due to Honeypot protection.'),
'#default_value' => config('honeypot.settings')->get('log'),
);
$form['configuration']['element_name'] = array(
'#type' => 'textfield',
'#title' => t('Honeypot element name'),
'#description' => t("The name of the Honeypot form field. It's usually most effective to use a generic name like email, homepage, or name, but this should be changed if it interferes with fields that are already in your forms. Must not contain spaces or special characters."),
'#default_value' => config('honeypot.settings')->get('element_name'),
'#required' => TRUE,
'#size' => 30,
);
$form['configuration']['time_limit'] = array(
'#type' => 'textfield',
'#title' => t('Honeypot time limit'),
'#description' => t('Minimum time required before form should be considered entered by a human instead of a bot. Set to 0 to disable.'),
'#default_value' => config('honeypot.settings')->get('time_limit'),
'#required' => TRUE,
'#size' => 5,
'#field_suffix' => t('seconds'),
);
$form['configuration']['time_limit']['#description'] .= '<br />' . t('<strong>Page caching will be disabled if there is a form protected by time limit on the page.</strong>');
$form['configuration']['protect_all_forms'] = array(
'#type' => 'checkbox',
'#title' => t('Protect all forms with Honeypot'),
'#description' => t('Enable Honeypot protection for ALL forms on this site (it is best to only enable Honeypot for the forms you need below).'),
'#default_value' => config('honeypot.settings')->get('protect_all_forms'),
);
$form['configuration']['protect_all_forms']['#description'] .= '<br />' . t('<strong>Page caching will be disabled on any page where a form is present if the Honeypot time limit is not set to 0.</strong>');
$form['configuration']['log'] = array(
'#type' => 'checkbox',
'#title' => t('Log blocked form submissions'),
'#description' => t('Log submissions that are blocked due to Honeypot protection.'),
'#default_value' => config('honeypot.settings')->get('log'),
);
$form['configuration']['element_name'] = array(
'#type' => 'textfield',
'#title' => t('Honeypot element name'),
'#description' => t("The name of the Honeypot form field. It's usually most effective to use a generic name like email, homepage, or name, but this should be changed if it interferes with fields that are already in your forms. Must not contain spaces or special characters."),
'#default_value' => config('honeypot.settings')->get('element_name'),
'#required' => TRUE,
'#size' => 30,
);
$form['configuration']['time_limit'] = array(
'#type' => 'textfield',
'#title' => t('Honeypot time limit'),
'#description' => t('Minimum time required before form should be considered entered by a human instead of a bot. Set to 0 to disable.'),
'#default_value' => config('honeypot.settings')->get('time_limit'),
'#required' => TRUE,
'#size' => 5,
'#field_suffix' => t('seconds'),
);
$form['configuration']['time_limit']['#description'] .= '<br />' . t('<strong>Page caching will be disabled if there is a form protected by time limit on the page.</strong>');
// Honeypot Enabled forms.
$form_settings = config('honeypot.settings')->get('form_settings');
......@@ -96,76 +96,76 @@ class HoneypotSettingsController implements FormInterface {
),
);
// Generic forms.
$form['form_settings']['general_forms'] = array('#markup' => '<h5>' . t('General Forms') . '</h5>');
// User register form.
$form['form_settings']['user_register_form'] = array(
// Generic forms.
$form['form_settings']['general_forms'] = array('#markup' => '<h5>' . t('General Forms') . '</h5>');
// User register form.
$form['form_settings']['user_register_form'] = array(
'#type' => 'checkbox',
'#title' => t('User Registration form'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'user_register_form'),
);
// User password form.
$form['form_settings']['user_pass'] = array(
'#type' => 'checkbox',
'#title' => t('User Password Reset form'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'user_pass'),
);
// If webform.module enabled, add webforms.
// TODO D8 - See if D8 version of Webform.module still uses this form ID.
if (module_exists('webform')) {
$form['form_settings']['webforms'] = array(
'#type' => 'checkbox',
'#title' => t('User Registration form'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'user_register_form'),
'#title' => t('Webforms (all)'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'webforms'),
);
// User password form.
$form['form_settings']['user_pass'] = array(
}
// If contact.module enabled, add contact forms.
if (module_exists('contact')) {
// TODO D8 - Sitewide contact forms are now dynamically-named.
$form['form_settings']['contact_forms'] = array('#markup' => '<h5>' . t('Contact Forms') . '</h5>');
// Sitewide contact form.
$form['form_settings']['contact_site_form'] = array(
'#type' => 'checkbox',
'#title' => t('User Password Reset form'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'user_pass'),
'#title' => t('Sitewide Contact form'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'contact_site_form'),
);
// Sitewide personal form.
$form['form_settings']['contact_personal_form'] = array(
'#type' => 'checkbox',
'#title' => t('Personal Contact forms'),
'#default_value' => $this->getFormSettingsValue($form_settings, '_contact_message_form'),
);
}
// If webform.module enabled, add webforms.
// TODO D8 - See if D8 version of Webform.module still uses this form ID.
if (module_exists('webform')) {
$form['form_settings']['webforms'] = array(
'#type' => 'checkbox',
'#title' => t('Webforms (all)'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'webforms'),
);
}
// If contact.module enabled, add contact forms.
if (module_exists('contact')) {
// TODO D8 - Sitewide contact forms are now dynamically-named.
$form['form_settings']['contact_forms'] = array('#markup' => '<h5>' . t('Contact Forms') . '</h5>');
// Sitewide contact form.
$form['form_settings']['contact_site_form'] = array(
'#type' => 'checkbox',
'#title' => t('Sitewide Contact form'),
'#default_value' => $this->getFormSettingsValue($form_settings, 'contact_site_form'),
);
// Sitewide personal form.
$form['form_settings']['contact_personal_form'] = array(
// Get node types for node forms and node comment forms.
$types = node_type_get_types();
if (!empty($types)) {
// Node forms.
$form['form_settings']['node_forms'] = array('#markup' => '<h5>' . t('Node Forms') . '</h5>');
foreach ($types as $type) {
$id = $type->type . '_node_form';
$form['form_settings'][$id] = array(
'#type' => 'checkbox',
'#title' => t('Personal Contact forms'),
'#default_value' => $this->getFormSettingsValue($form_settings, '_contact_message_form'),
'#title' => t('@name node form', array('@name' => $type->name)),
'#default_value' => $this->getFormSettingsValue($form_settings, $id),
);
}
// Get node types for node forms and node comment forms.
$types = node_type_get_types();
if (!empty($types)) {
// Node forms.
$form['form_settings']['node_forms'] = array('#markup' => '<h5>' . t('Node Forms') . '</h5>');
// Comment forms.
if (module_exists('comment')) {
$form['form_settings']['comment_forms'] = array('#markup' => '<h5>' . t('Comment Forms') . '</h5>');
foreach ($types as $type) {
$id = $type->type . '_node_form';
$id = 'comment_node_' . $type->type . '_comment_form';
$form['form_settings'][$id] = array(
'#type' => 'checkbox',
'#title' => t('@name node form', array('@name' => $type->name)),
'#title' => t('@name comment form', array('@name' => $type->name)),
'#default_value' => $this->getFormSettingsValue($form_settings, $id),
);
}
// Comment forms.
if (module_exists('comment')) {
$form['form_settings']['comment_forms'] = array('#markup' => '<h5>' . t('Comment Forms') . '</h5>');
foreach ($types as $type) {
$id = 'comment_node_' . $type->type . '_comment_form';
$form['form_settings'][$id] = array(
'#type' => 'checkbox',
'#title' => t('@name comment form', array('@name' => $type->name)),
'#default_value' => $this->getFormSettingsValue($form_settings, $id),
);
}
}
}
}
// For now, manually add submit button. Hopefully, by the time D8 is
// released, there will be something like system_settings_form() in D7.
......@@ -227,4 +227,4 @@ class HoneypotSettingsController implements FormInterface {
drupal_set_message(t('The configuration options have been saved.'));
}
}
\ No newline at end of file
}
......@@ -14,8 +14,8 @@ use Drupal\Core\Database\Database;
* Test the functionality of the Honeypot module for an admin user.
*/
class HoneypotFormTest extends WebTestBase {
protected $admin_user;
protected $web_user;
protected $adminUser;
protected $webUser;
protected $node;
/**
......@@ -40,8 +40,10 @@ class HoneypotFormTest extends WebTestBase {
// Set up required Honeypot configuration.
$honeypot_config = config('honeypot.settings');
$honeypot_config->set('element_name', 'url');
$honeypot_config->set('time_limit', 0); // Disable time_limit protection.
$honeypot_config->set('protect_all_forms', TRUE); // Test protecting all forms.
// Disable time_limit protection.
$honeypot_config->set('time_limit', 0);
// Test protecting all forms.
$honeypot_config->set('protect_all_forms', TRUE);
$honeypot_config->set('log', FALSE);
$honeypot_config->save();
......@@ -59,7 +61,7 @@ class HoneypotFormTest extends WebTestBase {
}
// Set up admin user.
$this->admin_user = $this->drupalCreateUser(array(
$this->adminUser = $this->drupalCreateUser(array(
'administer honeypot',
'bypass honeypot protection',
'administer content types',
......@@ -71,7 +73,7 @@ class HoneypotFormTest extends WebTestBase {
));
// Set up web user.
$this->web_user = $this->drupalCreateUser(array(
$this->webUser = $this->drupalCreateUser(array(
'access comments',
'post comments',
'create article content',
......@@ -128,7 +130,7 @@ class HoneypotFormTest extends WebTestBase {
$honeypot_config = config('honeypot.settings')->set('time_limit', 0)->save();
// Log in the web user.
$this->drupalLogin($this->web_user);
$this->drupalLogin($this->webUser);
// Set up form and submit it.
$edit["comment_body[0][value]"] = $comment;
......@@ -140,7 +142,7 @@ class HoneypotFormTest extends WebTestBase {
$comment = 'Test comment.';
// Log in the web user.
$this->drupalLogin($this->web_user);
$this->drupalLogin($this->webUser);
// Set up form and submit it.
$edit["comment_body[0][value]"] = $comment;
......@@ -151,7 +153,7 @@ class HoneypotFormTest extends WebTestBase {
public function testProtectCommentFormHoneypotBypass() {
// Log in the admin user.
$this->drupalLogin($this->admin_user);
$this->drupalLogin($this->adminUser);
// Get the comment reply form and ensure there's no 'url' field.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment