You are here

function smart_ip_admin_settings_validate in Smart IP 7.2

Same name and namespace in other branches
  1. 6.2 includes/smart_ip.admin.inc \smart_ip_admin_settings_validate()
  2. 6 includes/smart_ip.admin.inc \smart_ip_admin_settings_validate()
  3. 7 includes/smart_ip.admin.inc \smart_ip_admin_settings_validate()
1 string reference to 'smart_ip_admin_settings_validate'
smart_ip_admin_settings in includes/smart_ip.admin.inc
Smart IP administration settings.

File

includes/smart_ip.admin.inc, line 767
Admin interface callbacks/handlers to configure Smart IP.

Code

function smart_ip_admin_settings_validate($form, &$form_state) {
  $list = check_plain($form_state['values']['smart_ip_allowed_pages']);
  $form_state['values']['smart_ip_allowed_pages'] = str_replace('&lt;front&gt;', '<front>', $list);
  if ($form_state['values']['smart_ip_source'] == 'local_db') {
    $db_path = drupal_realpath(file_stream_wrapper_uri_normalize('private://smart_ip'));
    if (empty($db_path)) {
      form_set_error('smart_ip_source', t('Please define the "Private file system path" !here.', array(
        '!here' => l(t('here'), 'admin/config/media/file-system'),
      )));
    }
  }
  if ($form_state['values']['smart_ip_source'] == 'mod_geoip' && !isset($_SERVER['GEOIP_COUNTRY_NAME'])) {
    form_set_error('smart_ip_source', t("Maxmind's Apache module mod_geoip is not installed in your server."));
  }
  if ($form_state['values']['smart_ip_source'] == 'x_header' && !isset($_SERVER['HTTP_X_GEOIP_COUNTRY'])) {
    form_set_error('smart_ip_source', t('Your server does not support X-header GeoIP functionality.'));
  }
  if ($form_state['values']['smart_ip_source'] == 'es_header' && !isset($_SERVER['HTTP_X_AKAMAI_EDGESCAPE'])) {
    form_set_error('smart_ip_source', t('Your site is not using Akamai CDN or your Akamai "EdgeScape Geolocation" is disabled.'));
  }
  if ($form_state['values']['smart_ip_source'] == 'cf_header' && !isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
    form_set_error('smart_ip_source', t('Your site is not using Cloudflare CDN or your Cloudflare "IP Geolocation" is disabled.'));
  }
  if ($form_state['values']['smart_ip_source'] == 'ipinfodb_service' && empty($form_state['values']['smart_ip_ipinfodb_key'])) {
    form_set_error('smart_ip_ipinfodb_key', t('Please provide IPInfoDB API key.'));
  }
  $ipinfodb_key = variable_get('smart_ip_ipinfodb_key', '');
  if ($form_state['values']['smart_ip_ipinfodb_key'] != $ipinfodb_key) {
    variable_set('smart_ip_correct_ipinfodb_key', FALSE);
  }
  if ($form_state['values']['smart_ip_source'] == 'ip2location_bin') {
    if (empty($form_state['values']['smart_ip_ip2location_bin_path'])) {
      form_set_error('smart_ip_ip2location_bin_path', t('lease provide the IP2Location binary database IPV4 file full path.'));
    }
    if (empty($form_state['values']['smart_ip_ip2location_ipv6_bin_path'])) {
      form_set_error('smart_ip_ip2location_ipv6_bin_path', t('lease provide the IP2Location binary database IPV6 file full path.'));
    }
    if (!empty($form_state['values']['smart_ip_ip2location_bin_path'])) {

      // Check IP2Location binary database IPV4 file path if valid
      if (!file_exists($form_state['values']['smart_ip_ip2location_bin_path'])) {
        form_set_error('smart_ip_ip2location_bin_path', t('The IP2Location binary database IPV4 file path is not valid.'));
      }
      else {

        // Check IP2Location binary database IPV4 file if valid
        try {
          module_load_include('php', 'smart_ip', 'includes/vendor/autoload');
          $location_data = new IP2Location\Database($form_state['values']['smart_ip_ip2location_bin_path'], IP2Location\Database::FILE_IO);
          $record = $location_data
            ->lookup('8.8.8.8', IP2Location\Database::COUNTRY);
          if (strtotime($location_data
            ->getDate()) <= 0 || empty($record['countryCode'])) {
            form_set_error('smart_ip_ip2location_bin_path', t('The IP2Location binary database IPV4 file is not valid or corrupted.'));
          }
        } catch (Exception $e) {
          form_set_error('smart_ip_ip2location_bin_path', t('Loading IP2Location binary database IPV4 file failed: @error', array(
            '@error' => $e
              ->getMessage(),
          )));
        }
      }
    }
    if (!empty($form_state['values']['smart_ip_ip2location_ipv6_bin_path'])) {

      // Check IP2Location binary database IPV6 file path if valid
      if (!file_exists($form_state['values']['smart_ip_ip2location_ipv6_bin_path'])) {
        form_set_error('smart_ip_ip2location_ipv6_bin_path', t('The IP2Location binary database IPV6 file path is not valid.'));
      }
      else {

        // Check IP2Location binary database IPV6 file if valid
        try {
          module_load_include('php', 'smart_ip', 'includes/vendor/autoload');
          $location_data = new IP2Location\Database($form_state['values']['smart_ip_ip2location_ipv6_bin_path'], IP2Location\Database::FILE_IO);
          $record = $location_data
            ->lookup('2001:4860:4860::8888', IP2Location\Database::COUNTRY);
          if (strtotime($location_data
            ->getDate()) <= 0 || empty($record['countryCode'])) {
            form_set_error('smart_ip_ip2location_ipv6_bin_path', t('The IP2Location binary database IPV6 file is not valid or corrupted.'));
          }
        } catch (Exception $e) {
          form_set_error('smart_ip_ip2location_ipv6_bin_path', t('Loading IP2Location binary database IPV6 file failed: @error', array(
            '@error' => $e
              ->getMessage(),
          )));
        }
      }
    }
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmindgeoip_service' && empty($form_state['values']['smart_ip_maxmind_key'])) {
    form_set_error('smart_ip_maxmind_key', t('Please provide Maxmind GeoIP Legacy Web Services developer key.'));
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmindgeoip2_service' && empty($form_state['values']['smart_ip_maxmind_geoip_uid'])) {
    form_set_error('smart_ip_maxmind_geoip_uid', t('Please enter your Maxmind GeoIP2 Precision Web Services user ID.'));
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmindgeoip2_service' && empty($form_state['values']['smart_ip_maxmind_geoip_license'])) {
    form_set_error('smart_ip_maxmind_geoip_license', t('Please enter your Maxmind GeoIP2 Precision Web Services license key.'));
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmind_bin' && empty($form_state['values']['smart_ip_maxmind_license']) && $form_state['values']['smart_ip_maxmind_bin_version'] == SMART_IP_MAXMIND_BIN_LICENSED_VERSION) {
    form_set_error('smart_ip_maxmind_license', t('Please provide MaxMind GeoIP Legacy license key'));
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmind_bin') {
    $file = smart_ip_get_bin_source_filename($form_state['values']['smart_ip_maxmind_bin_version'], $form_state['values']['smart_ip_maxmind_bin_edition']) . '.dat';
    if (empty($form_state['values']['smart_ip_maxmind_bin_custom_path'])) {
      $db_path = drupal_realpath(file_stream_wrapper_uri_normalize('private://smart_ip'));
      if (empty($db_path)) {
        form_set_error('smart_ip_maxmind_bin_version', t('Please define the "Private file system path" !here.', array(
          '!here' => l(t('here'), 'admin/config/media/file-system'),
        )));
      }
      elseif (!file_exists("{$db_path}/{$file}") && !$form_state['values']['smart_ip_maxmind_bin_db_auto_update']) {
        form_set_error('smart_ip_maxmind_bin_db_auto_update', t('Please upload the @file at !path.', array(
          '@file' => $file,
          '!path' => $db_path,
        )));
      }
    }
    elseif ($form_state['values']['smart_ip_maxmind_bin_db_auto_update']) {
      $db_path = drupal_realpath(file_stream_wrapper_uri_normalize('private://smart_ip'));
      if (!file_exists("{$db_path}/{$file}")) {
        form_set_error('smart_ip_maxmind_bin_db_auto_update', t('Initially you need to manually download the @file and upload it to your server at !path. The next succeeding updates should be automatic.', array(
          '@file' => $file,
          '!path' => $db_path,
        )));
      }
    }
    else {
      $db_path = $form_state['values']['smart_ip_maxmind_bin_custom_path'];
      if (!file_exists("{$db_path}/{$file}")) {
        form_set_error('smart_ip_maxmind_bin_custom_path', t('Please upload the @file at !path.', array(
          '@file' => $file,
          '!path' => $db_path,
        )));
      }
    }
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmindgeoip2_bin' && empty($form_state['values']['smart_ip_maxmind_geoip2_bin_user_account']) && $form_state['values']['smart_ip_maxmind_geoip2_bin_version'] == SMART_IP_MAXMIND_BIN_LITE_VERSION && $form_state['values']['smart_ip_maxmind_bin_db_auto_update']) {
    form_set_error('smart_ip_maxmind_geoip2_bin_user_account', t('Please provide MaxMind user account'));
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmindgeoip2_bin' && empty($form_state['values']['smart_ip_maxmind_geoip2_license']) && $form_state['values']['smart_ip_maxmind_bin_db_auto_update']) {
    form_set_error('smart_ip_maxmind_geoip2_license', t('Please provide MaxMind GeoIP2 license key'));
  }
  if ($form_state['values']['smart_ip_source'] == 'maxmindgeoip2_bin') {
    $file = smart_ip_get_geoip2_bin_source_filename($form_state['values']['smart_ip_maxmind_geoip2_bin_version'], $form_state['values']['smart_ip_maxmind_geoip2_bin_edition']);
    if (empty($form_state['values']['smart_ip_maxmind_geoip2_bin_custom_path'])) {
      $db_path = drupal_realpath(file_stream_wrapper_uri_normalize('private://smart_ip'));
      if (empty($db_path)) {
        form_set_error('smart_ip_maxmind_geoip2_bin_version', t('Please define the "Private file system path" !here.', array(
          '!here' => l(t('here'), 'admin/config/media/file-system'),
        )));
      }
      elseif (!file_exists("{$db_path}/{$file}") && !$form_state['values']['smart_ip_maxmind_geoip2_bin_db_auto_update']) {
        form_set_error('smart_ip_maxmind_geoip2_bin_db_auto_update', t('Please upload the @file at !path.', array(
          '@file' => $file,
          '!path' => $db_path,
        )));
      }
    }
    else {
      $db_path = $form_state['values']['smart_ip_maxmind_geoip2_bin_custom_path'];
      if (!file_exists("{$db_path}/{$file}")) {
        form_set_error('smart_ip_maxmind_geoip2_bin_custom_path', t('Please upload the @file at !path.', array(
          '@file' => $file,
          '!path' => $db_path,
        )));
      }
    }
  }
  $roles = user_roles();
  foreach ($roles as $role_id => $role) {
    if ($form_state['values']["smart_ip_debug_{$role_id}"] == TRUE && empty($form_state['values']["smart_ip_test_ip_address_{$role_id}"])) {
      form_set_error("smart_ip_test_ip_address_{$role_id}", t('Please enter the IP address to use for @role testing.', array(
        '@role' => $role,
      )));
    }
  }
}