View source
<?php
namespace Drupal\httpbl\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\httpbl\HttpblResponseInterface;
use Drupal\httpbl\HttpblEvaluatorInterface;
use Drupal\httpbl\Logger\HttpblLogTrapperInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HttpblWhitelistForm extends FormBase {
protected $httpblEvaluator;
protected $httpblResponse;
protected $logTrapper;
public function __construct(HttpblEvaluatorInterface $httpblEvaluator, HttpblResponseInterface $httpblResponse, HttpblLogTrapperInterface $logTrapper) {
$this->httpblEvaluator = $httpblEvaluator;
$this->httpblResponse = $httpblResponse;
$this->logTrapper = $logTrapper;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('httpbl.evaluator'), $container
->get('httpbl.response'), $container
->get('httpbl.logtrapper'));
}
public function getFormId() {
return 'httpbl_whitelist_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['advise'] = array(
'#markup' => '<div class="httpbl-advice form-item">' . $this
->t('Please note: Session white-listing requires cookies to be enabled.') . '</div>',
);
$form['reason'] = array(
'#type' => 'textarea',
'#title' => t('Reason you were blocked. (It\'s okay to say you don\'t know if you don\'t)'),
'#size' => 60,
'#required' => TRUE,
);
$form['block'] = array(
'#type' => 'textfield',
'#title' => t('LEAVE THIS BLANK! (This is where robotic spammers fail, because they don\'t actually read!)'),
'#size' => 15,
);
$form['leave'] = array(
'#type' => 'textfield',
'#size' => 30,
'#attributes' => array(
'style' => 'display: none',
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('White-list request'),
);
$form['arrival'] = array(
'#type' => 'hidden',
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$ip = $this
->getRequest()
->getClientIP();
$project_link = $this->httpblEvaluator
->projectLink($ip);
$values = $form_state
->getValues();
if ($values['block'] || $values['leave']) {
if (isset($_SESSION['httpbl_status'])) {
unset($_SESSION['httpbl_status']);
}
if (\Drupal::state()
->get('httpbl.storage') == HTTPBL_DB_HH || \Drupal::state()
->get('httpbl.storage') == HTTPBL_DB_HH_DRUPAL) {
$this->httpblEvaluator
->updateIpLocalStatus($ip, HTTPBL_LIST_BLACK, $offset = \Drupal::state()
->get('httpbl.blacklist_offset'));
\Drupal::state()
->get('httpbl.blacklist_offset');
$offset = \Drupal::state()
->get('httpbl.blacklist_offset');
$return_date = \Drupal::service('date.formatter')
->formatInterval($offset, $granularity = 2, $langcode = NULL);
$this->logTrapper
->trapNotice('@ip blacklisted for @return_date for failing session white-list challenge. Source: @source.', [
'@ip' => $ip,
'@return_date' => $return_date,
'@source' => HTTPBL_CHALLENGE_FAILURE,
'link' => $project_link,
]);
$failureResponse = $this->httpblResponse
->challengeFailureBlacklisted($ip, $return_date);
print $failureResponse;
exit;
}
else {
$this->logTrapper
->trapWarning('@ip failed session white-list request. Source: @source.', [
'@ip' => $ip,
'@source' => HTTPBL_CHALLENGE_FAILURE,
'link' => $project_link,
]);
$failureResponse = $this->httpblResponse
->challengeFailurePurgatory();
print $failureResponse;
exit;
}
}
$this->logTrapper
->trapNotice('@ip success at white-list challenge. Source: @source.', [
'@ip' => $ip,
'@source' => HTTPBL_CHALLENGE_SUCCESS,
'link' => $project_link,
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (isset($_SESSION['httpbl_challenge'])) {
unset($_SESSION['httpbl_challenge']);
}
$_SESSION['httpbl_status'] = 'session_whitelisted';
drupal_set_message(t('Success! Your current session has been white-listed.'), 'status', FALSE);
$url = Url::fromRoute('<front>');
$form_state
->setRedirectUrl($url);
}
}