View source
<?php
namespace Drupal\ip2country\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserDataInterface;
use Drupal\ip2country\Ip2CountryLookupInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class Ip2CountrySettingsForm extends ConfigFormBase {
protected $currentRequest;
protected $currentUser;
protected $userData;
protected $stateService;
protected $dateFormatter;
protected $countryManager;
protected $ip2countryLookup;
public function __construct(ConfigFactoryInterface $config_factory, RequestStack $requestStack, AccountInterface $currentUser, UserDataInterface $userData, StateInterface $stateService, DateFormatterInterface $dateFormatter, CountryManagerInterface $countryManager, Ip2CountryLookupInterface $ip2countryLookup) {
parent::__construct($config_factory);
$this->currentRequest = $requestStack
->getCurrentRequest();
$this->currentUser = $currentUser;
$this->userData = $userData;
$this->stateService = $stateService;
$this->dateFormatter = $dateFormatter;
$this->countryManager = $countryManager;
$this->ip2countryLookup = $ip2countryLookup;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('request_stack'), $container
->get('current_user'), $container
->get('user.data'), $container
->get('state'), $container
->get('date.formatter'), $container
->get('country_manager'), $container
->get('ip2country.lookup'));
}
public function getFormId() {
return 'ip2country_admin_settings';
}
protected function getEditableConfigNames() {
return [
'ip2country.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$ip2country_config = $this
->config('ip2country.settings');
$form['#attached']['library'][] = 'ip2country/ip2country.settings';
$form['ip2country_database_update'] = [
'#type' => 'details',
'#title' => $this
->t('Database updates'),
'#open' => TRUE,
];
$form['ip2country_database_update']['ip2country_watchdog'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Log database updates to watchdog'),
'#default_value' => $ip2country_config
->get('watchdog'),
];
$form['ip2country_database_update']['ip2country_rir'] = [
'#type' => 'select',
'#title' => $this
->t('Regional Internet Registry'),
'#options' => [
'afrinic' => 'AFRINIC',
'apnic' => 'APNIC',
'arin' => 'ARIN',
'lacnic' => 'LACNIC',
'ripe' => 'RIPE',
],
'#default_value' => $ip2country_config
->get('rir'),
'#description' => $this
->t('Database will be downloaded from the selected RIR. You may find that the regional server nearest you has the best response time, but note that AFRINIC provides only its own subset of registration data.'),
];
$form['ip2country_database_update']['ip2country_md5_checksum'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Perform MD5 checksum comparison'),
'#description' => $this
->t("Compare MD5 checksum downloaded from the RIR with MD5 checksum calculated locally to ensure the data has not been corrupted. RIRs don't always store current checksums, so if this option is checked your database updates may sometimes fail."),
'#default_value' => $ip2country_config
->get('md5_checksum'),
];
$intervals = [
86400,
302400,
604800,
1209600,
2419200,
];
$period = array_map([
$this->dateFormatter,
'formatInterval',
], array_combine($intervals, $intervals));
$period[0] = $this
->t('Never');
$form['ip2country_database_update']['ip2country_update_interval'] = [
'#type' => 'select',
'#title' => $this
->t('Database update frequency'),
'#default_value' => $ip2country_config
->get('update_interval'),
'#options' => $period,
'#description' => $this
->t('Database will be automatically updated via cron.php. Cron must be enabled for this to work. Default period is 1 week (604800 seconds).'),
];
$update_time = $this->stateService
->get('ip2country_last_update');
if (!empty($update_time)) {
$message = $this
->t('Database last updated on @date at @time from @registry server.', [
'@date' => $this->dateFormatter
->format($update_time, 'ip2country_date'),
'@time' => $this->dateFormatter
->format($update_time, 'ip2country_time'),
'@registry' => mb_strtoupper($this->stateService
->get('ip2country_last_update_rir')),
]);
}
else {
$message = $this
->t('Database is empty. You may fill the database by pressing the @update button.', [
'@update' => $this
->t('Update'),
]);
}
$form['ip2country_database_update']['ip2country_update_batch_size'] = [
'#type' => 'number',
'#title' => $this
->t('Database update batch size'),
'#default_value' => $ip2country_config
->get('batch_size'),
'#min' => 1,
'#size' => 8,
'#required' => TRUE,
'#description' => $this
->t('The number of rows to insert simultaneously. A larger number means faster filling of the database, but more system resources (memory usage).'),
];
$form['ip2country_database_update']['ip2country_update_database'] = [
'#type' => 'button',
'#value' => $this
->t('Update'),
'#executes_submit_callback' => FALSE,
'#prefix' => '<p>' . $this
->t('The IP to Country Database may be updated manually by pressing the "Update" button below. Note, this may take several minutes. Changes to the above settings will not be permanently saved unless you press the "Save configuration" button at the bottom of this page.') . '</p>',
'#suffix' => '<span id="dbthrobber" class="message">' . $message . '</span>',
];
$form['ip2country_manual_lookup'] = [
'#type' => 'details',
'#title' => $this
->t('Manual lookup'),
'#description' => $this
->t('Examine database values'),
'#open' => TRUE,
];
$form['ip2country_manual_lookup']['ip2country_lookup'] = [
'#type' => 'textfield',
'#title' => $this
->t('Manual lookup'),
'#description' => $this
->t('Enter IP address'),
];
$form['ip2country_manual_lookup']['ip2country_lookup_button'] = [
'#type' => 'button',
'#value' => $this
->t('Lookup'),
'#executes_submit_callback' => FALSE,
'#prefix' => '<div>' . $this
->t('An IP address may be looked up in the database by entering the address above then pressing the Lookup button below.') . '</div>',
'#suffix' => '<span id="lookup-message" class="message"></span>',
];
$form['ip2country_debug_preferences'] = [
'#type' => 'details',
'#title' => $this
->t('Debug preferences'),
'#description' => $this
->t('Set debugging values'),
'#open' => TRUE,
];
$form['ip2country_debug_preferences']['ip2country_debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Admin debug'),
'#default_value' => $ip2country_config
->get('debug'),
'#description' => $this
->t('Enables administrator to spoof an IP Address or Country for debugging purposes.'),
];
$form['ip2country_debug_preferences']['ip2country_test_type'] = [
'#type' => 'radios',
'#title' => $this
->t('Select which parameter to spoof'),
'#default_value' => $ip2country_config
->get('test_type'),
'#options' => [
$this
->t('Country'),
$this
->t('IP Address'),
],
];
$ip_current = $this->currentRequest
->getClientIp();
$default_country = $ip2country_config
->get('test_country');
$default_country = empty($default_country) ? $this->ip2countryLookup
->getCountry($ip_current) : $default_country;
$form['ip2country_debug_preferences']['ip2country_test_country'] = [
'#type' => 'select',
'#title' => $this
->t('Country to use for testing'),
'#default_value' => $default_country,
'#options' => $this->countryManager
->getList(),
];
$test_ip_address = $ip2country_config
->get('test_ip_address');
$test_ip_address = empty($test_ip_address) ? $ip_current : $test_ip_address;
$form['ip2country_debug_preferences']['ip2country_test_ip_address'] = [
'#type' => 'textfield',
'#title' => $this
->t('IP address to use for testing'),
'#default_value' => $test_ip_address,
'#element_validate' => [
[
$this,
'validateIp',
],
],
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
return parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$ip2country_config = $this
->config('ip2country.settings');
$ip2country_config
->setData([
'watchdog' => (bool) $values['ip2country_watchdog'],
'rir' => (string) $values['ip2country_rir'],
'md5_checksum' => (bool) $values['ip2country_md5_checksum'],
'update_interval' => (int) $values['ip2country_update_interval'],
'batch_size' => (int) $values['ip2country_update_batch_size'],
'debug' => (bool) $values['ip2country_debug'],
'test_type' => (int) $values['ip2country_test_type'],
'test_country' => (string) $values['ip2country_test_country'],
'test_ip_address' => (string) $values['ip2country_test_ip_address'],
])
->save();
if ($values['ip2country_debug']) {
if ($values['ip2country_test_type']) {
$ip = $values['ip2country_test_ip_address'];
$country_code = $this->ip2countryLookup
->getCountry($ip);
}
else {
$country_code = $values['ip2country_test_country'];
}
$country_list = $this->countryManager
->getList();
$country_name = $country_list[$country_code];
$this
->messenger()
->addMessage($this
->t('Using DEBUG value for Country - @country (@code)', [
'@country' => $country_name,
'@code' => $country_code,
]));
}
else {
$ip = $this->currentRequest
->getClientIp();
$country_code = $this->ip2countryLookup
->getCountry($ip);
$this
->messenger()
->addMessage($this
->t('Using ACTUAL value for Country - @country', [
'@country' => $country_code,
]));
}
if ($country_code) {
$account_id = $this->currentUser
->id();
$account = User::load($account_id);
$account->country_iso_code_2 = $country_code;
$this->userData
->set('ip2country', $account_id, 'country_iso_code_2', $country_code);
}
parent::submitForm($form, $form_state);
}
public function validateIp($element, FormStateInterface $form_state) {
$ip_address = $element['#value'];
if (!filter_var($ip_address, FILTER_VALIDATE_IP)) {
$form_state
->setError($element, $this
->t('The IP address you entered is invalid. Please enter an address in the form xxx.xxx.xxx.xxx where xxx is between 0 and 255 inclusive.'));
}
}
}