You are here

function google_appliance_admin_settings_validate in Google Search Appliance 7

Same name and namespace in other branches
  1. 5 google_appliance.module \google_appliance_admin_settings_validate()
  2. 6.2 google_appliance.module \google_appliance_admin_settings_validate()

Implements hook_admin_settings_validate(). form validation beyond FAPI '#required'

File

./google_appliance.admin.inc, line 191
admin callbacks for the Google Appliance module

Code

function google_appliance_admin_settings_validate($form, &$form_state) {

  // host name should be a valid-format URL or IPv4 address including 'http(s)://'
  if (filter_var($form_state['values']['google_appliance_hostname'], FILTER_VALIDATE_URL) === FALSE) {
    form_set_error('hostname', t('GSA Host name must be a valid-format URL or IPv4 address, including <em>http(s)://</em>. Example: http://my.googlebox.net.'));
  }

  // timeout should be a reasonable number seconds
  $timeout_validate_options = array(
    'options' => array(
      'max_range' => 30,
      'min_range' => 3,
    ),
  );
  if (filter_var($form_state['values']['google_appliance_timeout'], FILTER_VALIDATE_INT, $timeout_validate_options) === FALSE) {
    form_set_error('timeout', t('Search Timeout should be an integer from @min - @max, indicating the number of seconds to wait before the search request times out.', array(
      '@min' => $timeout_validate_options['options']['min_range'],
      '@max' => $timeout_validate_options['options']['max_range'],
    )));
  }

  // Trim slashes and whitespace from drupal_path to save headaches.
  $drupal_path = trim($form_state['values']['google_appliance_drupal_path'], ' /');
  form_set_value($form['display_settings']['google_appliance_drupal_path'], $drupal_path, $form_state);

  // results per page must be on a range that will be accepted by the device upon querying
  $results_per_page_validate_options = array(
    'options' => array(
      'max_range' => 1000,
      'min_range' => 1,
    ),
  );
  if (filter_var($form_state['values']['google_appliance_results_per_page'], FILTER_VALIDATE_INT, $results_per_page_validate_options) === FALSE) {
    form_set_error('results_per_page', t('Results per page should be an integer from @min - @max.', array(
      '@min' => $results_per_page_validate_options['options']['min_range'],
      '@max' => $results_per_page_validate_options['options']['max_range'],
    )));
  }

  // For security, we check that the user has access to use these filters.
  $field_text_format_keys = array(
    'google_appliance_error_gsa_no_results',
    'google_appliance_error_curl_error',
    'google_appliance_error_lib_xml_parse_error',
  );
  $formats = filter_formats();
  foreach ($field_text_format_keys as $field) {
    if (!filter_access($formats[$form_state['values'][$field]['format']])) {
      form_set_error($field . '][format', t('An illegal choice has been detected. Please contact the site administrator.'));
    }
    else {

      // Alter the formatted text area settings to our expectations.
      $form_state['values'][$field . '_format'] = $form_state['values'][$field]['format'];
      $form_state['values'][$field] = trim($form_state['values'][$field]['value']);
    }
  }
}