You are here

class Ip2CountrySettingsForm in IP-based Determination of a Visitor's Country 8

Configure ip2country settings for this site.

Hierarchy

Expanded class hierarchy of Ip2CountrySettingsForm

1 string reference to 'Ip2CountrySettingsForm'
ip2country.routing.yml in ./ip2country.routing.yml
ip2country.routing.yml

File

src/Form/Ip2CountrySettingsForm.php, line 21

Namespace

Drupal\ip2country\Form
View source
class Ip2CountrySettingsForm extends ConfigFormBase {

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The current user's data.
   *
   * @var \Drupal\user\UserDataInterface
   */
  protected $userData;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $stateService;

  /**
   * The date.formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The country_manager service.
   *
   * @var \Drupal\Core\Locale\CountryManagerInterface
   */
  protected $countryManager;

  /**
   * The ip2country.manager service.
   *
   * @var \Drupal\ip2country\Ip2CountryLookupInterface
   */
  protected $ip2countryLookup;

  /**
   * Constructs an Ip2CountryController.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack.
   * @param \Drupal\Core\Session\AccountInterface $currentUser
   *   The current user.
   * @param \Drupal\user\UserDataInterface $userData
   *   The current user's data.
   * @param \Drupal\Core\State\StateInterface $stateService
   *   The state service.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
   *   The date formatter service.
   * @param \Drupal\Core\Locale\CountryManagerInterface $countryManager
   *   The country_manager service.
   * @param \Drupal\ip2country\Ip2CountryLookupInterface $ip2countryLookup
   *   The ip2country.manager service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ip2country_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'ip2country.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $ip2country_config = $this
      ->config('ip2country.settings');
    $form['#attached']['library'][] = 'ip2country/ip2country.settings';

    // Container for database update preference forms.
    $form['ip2country_database_update'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Database updates'),
      '#open' => TRUE,
    ];

    // Form to enable watchdog logging of updates.
    $form['ip2country_database_update']['ip2country_watchdog'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Log database updates to watchdog'),
      '#default_value' => $ip2country_config
        ->get('watchdog'),
    ];

    // Form to choose RIR.
    $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 to enable MD5 checksum of downloaded databases.
    $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 to set automatic update interval.
    $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 to customize database insertion batch size.
    $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 to initiate manual updating of the IP-Country database.
    $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>',
    ];

    // Container for manual lookup.
    $form['ip2country_manual_lookup'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Manual lookup'),
      '#description' => $this
        ->t('Examine database values'),
      '#open' => TRUE,
    ];

    // Form for IP address for manual lookup.
    $form['ip2country_manual_lookup']['ip2country_lookup'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Manual lookup'),
      '#description' => $this
        ->t('Enter IP address'),
    ];

    // Form to initiate manual lookup.
    $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>',
    ];

    // Container for debugging preference forms.
    $form['ip2country_debug_preferences'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Debug preferences'),
      '#description' => $this
        ->t('Set debugging values'),
      '#open' => TRUE,
    ];

    // Form to turn on debugging.
    $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 to select Dummy Country or Dummy IP Address for testing.
    $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();

    // Form to enter Country to spoof.
    $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(),
    ];

    // Form to enter IP address to spoof.
    $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);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    return parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  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();

    // Check to see if debug set.
    if ($values['ip2country_debug']) {

      // Debug on.
      if ($values['ip2country_test_type']) {

        // Dummy IP Address.
        $ip = $values['ip2country_test_ip_address'];
        $country_code = $this->ip2countryLookup
          ->getCountry($ip);
      }
      else {

        // Dummy Country.
        $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 {

      // Debug off - make sure we set/reset IP/Country to their real values.
      $ip = $this->currentRequest
        ->getClientIp();
      $country_code = $this->ip2countryLookup
        ->getCountry($ip);
      $this
        ->messenger()
        ->addMessage($this
        ->t('Using ACTUAL value for Country - @country', [
        '@country' => $country_code,
      ]));
    }

    // Finally, save country, if it has been determined.
    if ($country_code) {

      // Store the ISO country code in the $user object.
      $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);
  }

  /**
   * Element validation handler for IP address input.
   */
  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.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
Ip2CountrySettingsForm::$countryManager protected property The country_manager service.
Ip2CountrySettingsForm::$currentRequest protected property The current request.
Ip2CountrySettingsForm::$currentUser protected property The current user.
Ip2CountrySettingsForm::$dateFormatter protected property The date.formatter service.
Ip2CountrySettingsForm::$ip2countryLookup protected property The ip2country.manager service.
Ip2CountrySettingsForm::$stateService protected property The state service.
Ip2CountrySettingsForm::$userData protected property The current user's data.
Ip2CountrySettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
Ip2CountrySettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
Ip2CountrySettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
Ip2CountrySettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
Ip2CountrySettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
Ip2CountrySettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
Ip2CountrySettingsForm::validateIp public function Element validation handler for IP address input.
Ip2CountrySettingsForm::__construct public function Constructs an Ip2CountryController. Overrides ConfigFormBase::__construct
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.