summaryrefslogtreecommitdiffstats
path: root/httprl.admin.inc
blob: 26a29a7ff50065e92c073f116b3ba6213da702cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

/**
 * @file
 * Administrative page callbacks for the httprl module.
 */

/**
 * Form definition; general settings.
 */
function httprl_admin_settings_form() {
  $form['httprl_server_addr'] = array(
    '#type'           => 'textfield',
    '#title'          => t('IP Address to send all self server requests to'),
    '#default_value'  => variable_get('httprl_server_addr', FALSE),
    '#description'    => t('If left blank it will use the same server as the request. If set to -1 it will use the host name instead of an IP address. This controls the output of httprl_build_url_self()'),
  );
  $form['timeous'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default Timeouts'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Set the default timeouts. These will be overridden if a different timeout is set in the options array.'),
  );
  $form['timeous']['httprl_dns_timeout'] = array(
    '#type'           => 'textfield',
    '#title'          => t('dns_timeout. Maximum number of seconds a DNS lookup request may take'),
    '#default_value'  => variable_get('httprl_dns_timeout', HTTPRL_DNS_TIMEOUT),
    '#description'    => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_DNS_TIMEOUT)),
  );
  $form['timeous']['httprl_connect_timeout'] = array(
    '#type'           => 'textfield',
    '#title'          => t('connect_timeout. Maximum number of seconds establishing the TCP connection may take'),
    '#default_value'  => variable_get('httprl_connect_timeout', HTTPRL_CONNECT_TIMEOUT),
    '#description'    => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_CONNECT_TIMEOUT)),
  );
  $form['timeous']['httprl_ttfb_timeout'] = array(
    '#type'           => 'textfield',
    '#title'          => t('ttfb_timeout. Maximum number of seconds a connection may take to download the first byte'),
    '#default_value'  => variable_get('httprl_ttfb_timeout', HTTPRL_TTFB_TIMEOUT),
    '#description'    => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_TTFB_TIMEOUT)),
  );
  $form['timeous']['httprl_timeout'] = array(
    '#type'           => 'textfield',
    '#title'          => t('timeout. Maximum number of seconds the request may take'),
    '#default_value'  => variable_get('httprl_timeout', HTTPRL_TIMEOUT),
    '#description'    => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_TIMEOUT)),
  );
  $form['timeous']['httprl_global_timeout'] = array(
    '#type'           => 'textfield',
    '#title'          => t('global_timeout. Maximum number of seconds httprl_send_request() may take'),
    '#default_value'  => variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT),
    '#description'    => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_GLOBAL_TIMEOUT)),
  );


  return system_settings_form($form);
}

/**
 * Validate form values.
 */
function httprl_admin_settings_form_validate($form, &$form_state) {
  // Skip validation if we are resting to defaults.
  if (isset($form_state['clicked_button']['#post']['op']) && $form_state['clicked_button']['#post']['op'] == 'Reset to defaults') {
    return;
  }

  // Get form values.
  $values = $form_state['values'];

  // If the IP field is not blank, check that it is a valid address.
  if (   !empty($values['httprl_server_addr'])
      && $values['httprl_server_addr'] != -1
      && ip2long($values['httprl_server_addr']) === FALSE
        ) {
    form_set_error('httprl_server_addr', t('Must be a valid IP address.'));
  }

  // Make sure the timeouts are positive numbers.
  $positive_values = array(
    'httprl_dns_timeout',
    'httprl_connect_timeout',
    'httprl_ttfb_timeout',
    'httprl_timeout',
    'httprl_global_timeout',
  );
  foreach ($positive_values as $name) {
    if (empty($values[$name])) {
      form_set_error($name, t('Must not be empty or zero.'));
      continue;
    }
    if (!is_numeric($values[$name])) {
      form_set_error($name, t('Must be numeric.'));
      continue;
    }
    if ($values[$name] <= 0) {
      form_set_error($name, t('Must be a postive number.'));
      continue;
    }
  }
}