View source
<?php
namespace Drupal\ip_ranges;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\EntityManagerInterface;
class IPRangesFormController extends ContentEntityForm implements ContentEntityFormInterface {
protected $ownIp;
public function __construct(EntityManagerInterface $entity_manager) {
$this->ownIp = \Drupal::request()
->getClientIp();
parent::__construct($entity_manager);
}
public function form(array $form, array &$form_state) {
$entity = $this->entity;
if ($this->operation == 'add') {
$form['own_ip_warning'] = array(
'#type' => 'value',
'#markup' => t('<strong>Note that your own IP Address is currently @ip. Be careful not to lock yourself out!</strong>', array(
'@ip' => $this->ownIp,
)) . '<br />' . l(t('Click here to whitelist your own IP-address.'), '', array(
'fragment' => '',
'external' => TRUE,
'attributes' => array(
'id' => 'add-my-own-ip',
'data-my-ip' => $this->ownIp,
),
)),
);
}
$form['ip_lower'] = array(
'#title' => t('IP range start / Single IP-address'),
'#type' => 'textfield',
'#size' => 48,
'#required' => TRUE,
'#maxlength' => 15,
'#description' => t('Enter IP-address (100.100.100.100). If range end is specified, it will be used as start of the range, otherwise as a single IP-address.'),
);
$form['ip_higher'] = array(
'#title' => t('IP range end (optional)'),
'#type' => 'textfield',
'#size' => 48,
'#required' => FALSE,
'#maxlength' => 15,
'#description' => t('If entered, the banned ip will be treated as a range.'),
);
$form['type'] = array(
'#title' => t('List type'),
'#description' => t('Add the IP to a whitelist to ensure access or the blacklist to deny access'),
'#type' => 'select',
'#options' => array(
0 => t('Blacklist'),
1 => t('Whitelist'),
),
'#multiple' => FALSE,
'#default_value' => 0,
'#required' => TRUE,
);
$form['description'] = array(
'#title' => t('IP Range description (optional)'),
'#type' => 'textfield',
'#size' => 68,
'#required' => FALSE,
'#maxlength' => 254,
'#description' => t('Description can be used to better identify added IP Ranges'),
);
if ($this->operation == 'edit') {
$ip_lower = $entity
->getIpLower();
$ip_higher = $entity
->getIpHigher();
$form['type']['#default_value'] = $entity
->getType();
$form['ip_lower']['#default_value'] = long2ip($ip_lower);
if ($ip_lower != $ip_higher) {
$form['ip_higher']['#default_value'] = long2ip($ip_higher);
}
$form['description']['#default_value'] = $entity
->getDescription();
}
$form['#attached']['js'] = array(
drupal_get_path('module', 'ip_ranges') . '/js/ip_ranges_form.js',
);
return parent::form($form, $form_state, $entity);
}
public function validate(array $form, array &$form_state) {
$ip_lower = trim($form_state['values']['ip_lower']);
$ip_higher = isset($form_state['values']['ip_higher']) ? trim($form_state['values']['ip_higher']) : NULL;
$type = $form_state['values']['type'];
if (filter_var($ip_lower, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
\Drupal::formBuilder()
->setErrorByName('ip_lower', $form_state, t("IP(/range start) is not a valid IP address."));
}
elseif ($ip_higher && filter_var($ip_higher, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
\Drupal::formBuilder()
->setErrorByName('ip_higher', $form_state, t("IP range end is not a valid IP address."));
}
elseif (ip2long($ip_lower) <= ip2long($this->ownIp) && ip2long($ip_higher) >= ip2long($this->ownIp) || ip2long($ip_lower) == ip2long($this->ownIp) || ip2long($ip_higher) == ip2long($this->ownIp)) {
if ($type == 0) {
\Drupal::formBuilder()
->setErrorByName('', $form_state, t("You may not block your own IP address"));
}
}
parent::validate($form, $form_state);
}
public function submit(array $form, array &$form_state) {
$ip_lower =& $form_state['values']['ip_lower'];
$ip_higher =& $form_state['values']['ip_higher'];
$ip_lower = trim($ip_lower);
$ip_higher = trim($ip_higher);
if (empty($form_state['values']['ip_higher'])) {
$ip_higher = $ip_lower;
}
$ip_lower = sprintf("%u", ip2long($ip_lower));
$ip_higher = sprintf("%u", ip2long($ip_higher));
if ($ip_lower > $ip_higher) {
$temp = $ip_lower;
$ip_lower = $ip_higher;
$ip_higher = $temp;
}
parent::submit($form, $form_state);
}
public function save(array $form, array &$form_state) {
$entity = $this->entity;
$entity
->save();
$form_state['redirect_route']['route_name'] = 'ip_ranges.admin_list';
}
}