Skip to content
Snippets Groups Projects

Issue #3332326: Drupal Coding Standards Issues | phpcs

Open Charchil Khandelwal requested to merge issue/spamaway-3332326:1.0.x into 1.0.x
2 files
+ 84
41
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -3,6 +3,7 @@
namespace Drupal\spamaway\Plugin\WebformHandler;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -10,9 +11,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Checks Anti SPAM of a submission.
*
* Checks if the current submission has been done before based on these criteria:
* Checks if the current submission has been done before based on these
* criteria:
* - within a specific period of time (configuratble)
* - similar data has been posted for specific fields (based on similar_text percentage threshold)
* - similar data has been posted for specific fields (based on similar_text
* percentage threshold)
* - x amount of similar posts have been made.
*
* @WebformHandler(
@@ -26,10 +29,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class AntiSpamHandler extends WebformHandlerBase {
use StringTranslationTrait;
const SPAMAWAY_SUBMISSION_TABLE = 'spamaway_webform_submission';
/**
* @var \Drupal\Core\Database\Connection $connection
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
@@ -71,9 +78,9 @@ class AntiSpamHandler extends WebformHandlerBase {
$form['spamaway_anti_spam_field_names'] = [
'#type' => 'textfield',
'#title' => $this->t('Field names'),
'#description' => $this->t('A comma seperated list of field names to take into consideration for similarity. You can also add \'ip\' to check on IP address and combine fields using a + seperator. Ex: field_a,field_b+field_c,field_d+ip'),
'#description' => $this->t("A comma seperated list of field names to take into consideration for similarity. You can also add 'ip' to check on IP address and combine fields using a + seperator. Ex: field_a,field_b+field_c,field_d+ip"),
'#default_value' => $this->configuration['spamaway_anti_spam_field_names'] ??
$this->defaultConfiguration['spamaway_anti_spam_field_names'],
$this->defaultConfiguration['spamaway_anti_spam_field_names'],
];
$form['spamaway_anti_spam_hash'] = [
@@ -81,7 +88,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'#title' => $this->t('Hash algorithm'),
'#description' => $this->t('The hash algorithm used for storing field name values to check against. See php hash for supported algoritms. This is only used if the webform is not storing data itself.'),
'#default_value' => $this->configuration['spamaway_anti_spam_hash'] ??
$this->defaultConfiguration['spamaway_anti_spam_hash'],
$this->defaultConfiguration['spamaway_anti_spam_hash'],
];
$form['spamaway_anti_spam_threshold_percentage'] = [
@@ -90,7 +97,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'#suffix' => '%',
'#description' => $this->t('A comma seperated list of threshold percentages for each field name (or one single value used for all field names). This is only use if the webform stores data itself.'),
'#default_value' => $this->configuration['spamaway_anti_spam_threshold_percentage'] ??
$this->defaultConfiguration['spamaway_anti_spam_threshold_percentage'],
$this->defaultConfiguration['spamaway_anti_spam_threshold_percentage'],
];
$form['spamaway_anti_spam_period'] = [
@@ -99,7 +106,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'#description' => $this->t('The period for within similar submissions must have been submitted in seconds. Use 0(default) to disable this condition.'),
'#suffix' => 'seconds',
'#default_value' => $this->configuration['spamaway_anti_spam_period'] ??
$this->defaultConfiguration['spamaway_anti_spam_period'],
$this->defaultConfiguration['spamaway_anti_spam_period'],
];
$form['spamaway_anti_spam_allowed_count'] = [
@@ -107,7 +114,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'#title' => $this->t('Allowed count'),
'#description' => $this->t('The number of similar submissions allowed before we consider it spam. You can also use a comma seperated list of counts for each field name you specified.'),
'#default_value' => $this->configuration['spamaway_anti_spam_allowed_count'] ??
$this->defaultConfiguration['spamaway_anti_spam_allowed_count'],
$this->defaultConfiguration['spamaway_anti_spam_allowed_count'],
];
$form['spamaway_anti_spam_ip_period'] = [
@@ -116,7 +123,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'#description' => $this->t('The period for within submissions with the same IP address are considered spam in seconds. Defaults to 10 minutes'),
'#suffix' => 'seconds',
'#default_value' => $this->configuration['spamaway_anti_spam_ip_period'] ??
$this->defaultConfiguration['spamaway_anti_spam_ip_period'],
$this->defaultConfiguration['spamaway_anti_spam_ip_period'],
];
$form['spamaway_anti_spam_allowed_ip_count'] = [
@@ -124,7 +131,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'#title' => $this->t('Allowed IP count'),
'#description' => $this->t('The number of similar submissions allowed by the same IP address before we consider it spam. Defaults to 4'),
'#default_value' => $this->configuration['spamaway_anti_spam_allowed_ip_count'] ??
$this->defaultConfiguration['spamaway_anti_spam_allowed_ip_count'],
$this->defaultConfiguration['spamaway_anti_spam_allowed_ip_count'],
];
$form['spamaway_ip_check_enabled'] = [
@@ -132,14 +139,14 @@ class AntiSpamHandler extends WebformHandlerBase {
'#title' => $this->t('IP check enabled'),
'#description' => $this->t('Enable IP check validation.'),
'#default_value' => $this->configuration['spamaway_ip_check_enabled'] ??
$this->defaultConfiguration['spamaway_ip_check_enabled'],
$this->defaultConfiguration['spamaway_ip_check_enabled'],
];
$form['spamaway_anti_spam_logging'] = [
'#type' => 'textfield',
'#title' => $this->t('Enable logging'),
'#default_value' => $this->configuration['spamaway_anti_spam_logging'] ??
$this->defaultConfiguration['spamaway_anti_spam_logging'],
$this->defaultConfiguration['spamaway_anti_spam_logging'],
];
$form['spamaway_query_limit'] = [
@@ -147,7 +154,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'#title' => $this->t('Query limit'),
'#description' => $this->t('Cannot be higher than 200'),
'#default_value' => $this->configuration['spamaway_query_limit'] ??
$this->defaultConfiguration['spamaway_query_limit'],
$this->defaultConfiguration['spamaway_query_limit'],
];
return $form;
@@ -181,7 +188,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'created' => $webform_submission->getCreatedTime(),
'submission' => $webform_submission->serial(),
'field_name' => 'ip',
'value' => $webform_submission->getRemoteAddr()
'value' => $webform_submission->getRemoteAddr(),
])->execute();
}
@@ -196,7 +203,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'created' => $webform_submission->getCreatedTime(),
'submission' => $webform_submission->serial(),
'field_name' => $field_name,
'value' => $value
'value' => $value,
])->execute();
}
}
@@ -219,9 +226,12 @@ class AntiSpamHandler extends WebformHandlerBase {
* Helper to get an array of clean values from a string.
*
* @param string $seperator
* @param string $array
* String's seperator.
* @param string $string
* Fields array.
*
* @return array
* Clean array values.
*/
protected function explodeTrimmed($seperator, $string) {
return array_filter(array_map('trim', explode($seperator, $string)));
@@ -231,6 +241,7 @@ class AntiSpamHandler extends WebformHandlerBase {
* Get the field names from the config settings.
*
* @return array
* Clean field names.
*/
protected function getFieldNames() {
return $this->explodeTrimmed(',', $this->configuration['spamaway_anti_spam_field_names']);
@@ -242,7 +253,7 @@ class AntiSpamHandler extends WebformHandlerBase {
protected function getFieldValue($field_name, $data) {
$value = '';
// A field name can be a combination of fields using a +
// A field name can be a combination of fields using a +.
$keys = $this->explodeTrimmed('+', $field_name);
foreach ($keys as $key) {
if (!empty($data[$key])) {
@@ -253,20 +264,32 @@ class AntiSpamHandler extends WebformHandlerBase {
return $value;
}
/**
* Check is Save Submissions enabled.
*/
protected function isSaveSubmissionsEnabled() {
return ($this->getWebform()->getSetting('results_disabled') === FALSE);
}
/**
* Check is Logging enabled.
*/
protected function isLoggingEnabled() {
return ($this->configuration['spamaway_anti_spam_logging'] ?? 0);
}
/**
* Check is IP enabled.
*/
protected function isIpCheckEnabled() {
return ($this->configuration['spamaway_ip_check_enabled'] ?? 0);
}
/**
* Check value exists ot not.
*/
protected function hashValue($value) {
$hashed = hash($this->configuration['spamaway_anti_spam_hash'], $value, false);
$hashed = hash($this->configuration['spamaway_anti_spam_hash'], $value, FALSE);
return $hashed;
}
@@ -286,15 +309,15 @@ class AntiSpamHandler extends WebformHandlerBase {
if ($this->isLoggingEnabled()) {
$this->getLogger('spamaway_spam')->debug($this->t('Spam detection was bypassed for @user', ['@user' => \Drupal::currentUser()->getAccountName()]));
}
//return;
// return;.
}
// Set hard limit
// Set hard limit.
if ($this->configuration['spamaway_query_limit'] > 200) {
$this->configuration['spamaway_query_limit'] = 200;
}
////////////////////////////////////////////////////////////////////////////
//
// Make sure the last x seconds no submission was done by the same IP
// address. Otherwise consider it spam.
if ($this->isIpCheckEnabled()) {
@@ -303,12 +326,16 @@ class AntiSpamHandler extends WebformHandlerBase {
if ($this->isSaveSubmissionsEnabled()) {
$this->validateFormWithSavedSubmissions($form, $form_state, $webform_submission);
} else {
}
else {
$this->validateFormCustomSubmissions($form, $form_state, $webform_submission);
}
}
/**
* The baseIpCheck function.
*/
private function baseIpCheck(WebformSubmissionInterface $webform_submission, FormStateInterface $form_state) {
$query_ip = $this->connection->select(self::SPAMAWAY_SUBMISSION_TABLE, 'w');
$query_ip->condition('webform_id', $webform_submission->getWebform()->id());
@@ -322,7 +349,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'@ip' => $webform_submission->getRemoteAddr(),
'@period' => $this->configuration['spamaway_anti_spam_ip_period'],
'@count' => $count_ip,
'@allowed' => $this->configuration['spamaway_anti_spam_allowed_ip_count']
'@allowed' => $this->configuration['spamaway_anti_spam_allowed_ip_count'],
]));
}
}
@@ -339,9 +366,8 @@ class AntiSpamHandler extends WebformHandlerBase {
*/
protected function validateFormWithSavedSubmissions(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
////////////////////////////////////////////////////////////////////////////
//
// Check if the submitted message is similar to previous submitted messages
// Lookup X last submissions.
$query = $this->connection->select('webform_submission', 's');
$query->addField('d', 'value', 'value');
@@ -400,7 +426,7 @@ class AntiSpamHandler extends WebformHandlerBase {
'@webform' => $webform_submission->getWebform()->id(),
'@ip' => $webform_submission->getRemoteAddr(),
'@period' => $this->configuration['spamaway_anti_spam_ip_period'],
'@percentage' => $precentage
'@percentage' => $precentage,
]));
break;
@@ -420,9 +446,8 @@ class AntiSpamHandler extends WebformHandlerBase {
*/
public function validateFormCustomSubmissions(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
////////////////////////////////////////////////////////////////////////////
// Check if the submitted message is similar to previous submitted messages
//
// Check if the submitted message is similar to previous submitted messages.
$field_names = $this->getFieldNames();
if (empty($field_names)) {
return;
@@ -434,19 +459,19 @@ class AntiSpamHandler extends WebformHandlerBase {
$query->addField('s', 'field_name', 'field_name');
$query->orderBy('s.created', 'DESC');
// for this form ...
// For this form ...
$query->condition('s.webform_id', $webform_submission->getWebform()->id());
// limited to X last submissions ...
// Limited to X last submissions ...
$query->range(0, $this->configuration['spamaway_query_limit']);
// withing a period of time ...
// Withing a period of time ...
$period = time() - $this->configuration['spamaway_anti_spam_period'];
if ($this->configuration['spamaway_anti_spam_period'] && $period > 0) {
$query->condition('s.created', $period, '>');
}
// limited by the fields we need ...
// Limited by the fields we need ...
$query->condition('s.field_name', $field_names, 'IN');
$rows = $query->execute()->fetchAll();
@@ -455,7 +480,7 @@ class AntiSpamHandler extends WebformHandlerBase {
// How many times are we allowed to have the same field.
$allowed_count = $this->explodeTrimmed(',', $this->configuration['spamaway_anti_spam_allowed_count']);
if (count($allowed_count) == 1 || count($allowed_count)!=count($field_names)) {
if (count($allowed_count) == 1 || count($allowed_count) != count($field_names)) {
// If we have only one threshold or the numbers don't match the number
// of fields we need take the first threshold for everything.
$allowed_count = array_fill(0, count($field_names), $allowed_count[0]);
@@ -476,7 +501,7 @@ class AntiSpamHandler extends WebformHandlerBase {
$this->spamDetected($form_state, $this->t('Spam detected by hash check on @webform due to similar post within @period', [
'@webform' => $webform_submission->getWebform()->id(),
'@ip' => $webform_submission->getRemoteAddr(),
'@period' => $this->configuration['spamaway_anti_spam_ip_period']
'@period' => $this->configuration['spamaway_anti_spam_ip_period'],
]));
break;
@@ -484,8 +509,12 @@ class AntiSpamHandler extends WebformHandlerBase {
}
}
/**
* The spamDetected function.
*/
protected function spamDetected(FormStateInterface $form_state, $message) {
// We had (a) very similar submission(s) before so we ignore the new submission.
// We had (a) very similar submission(s) before so we ignore the new
// submission.
$form_state->setErrorByName('', $this->t('Spam detected. Please contact the site administrator if the issue persists.'));
if ($this->isLoggingEnabled()) {
$this->getLogger('spamaway_spam')->debug($message);
Loading