Issue #1874764 by mikeytown2: displayed default values come from the defined constants.
[project/httprl.git] / httprl.admin.inc
1 <?php
2
3 /**
4 * @file
5 * Administrative page callbacks for the httprl module.
6 */
7
8 /**
9 * Form definition; general settings.
10 */
11 function httprl_admin_settings_form() {
12 $form['httprl_server_addr'] = array(
13 '#type' => 'textfield',
14 '#title' => t('IP Address to send all self server requests to'),
15 '#default_value' => variable_get('httprl_server_addr', FALSE),
16 '#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()'),
17 );
18 $form['timeous'] = array(
19 '#type' => 'fieldset',
20 '#title' => t('Default Timeouts'),
21 '#collapsible' => TRUE,
22 '#collapsed' => TRUE,
23 '#description' => t('Set the default timeouts. These will be overridden if a different timeout is set in the options array.'),
24 );
25 $form['timeous']['httprl_dns_timeout'] = array(
26 '#type' => 'textfield',
27 '#title' => t('dns_timeout. Maximum number of seconds a DNS lookup request may take'),
28 '#default_value' => variable_get('httprl_dns_timeout', HTTPRL_DNS_TIMEOUT),
29 '#description' => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_DNS_TIMEOUT)),
30 );
31 $form['timeous']['httprl_connect_timeout'] = array(
32 '#type' => 'textfield',
33 '#title' => t('connect_timeout. Maximum number of seconds establishing the TCP connection may take'),
34 '#default_value' => variable_get('httprl_connect_timeout', HTTPRL_CONNECT_TIMEOUT),
35 '#description' => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_CONNECT_TIMEOUT)),
36 );
37 $form['timeous']['httprl_ttfb_timeout'] = array(
38 '#type' => 'textfield',
39 '#title' => t('ttfb_timeout. Maximum number of seconds a connection may take to download the first byte'),
40 '#default_value' => variable_get('httprl_ttfb_timeout', HTTPRL_TTFB_TIMEOUT),
41 '#description' => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_TTFB_TIMEOUT)),
42 );
43 $form['timeous']['httprl_timeout'] = array(
44 '#type' => 'textfield',
45 '#title' => t('timeout. Maximum number of seconds the request may take'),
46 '#default_value' => variable_get('httprl_timeout', HTTPRL_TIMEOUT),
47 '#description' => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_TIMEOUT)),
48 );
49 $form['timeous']['httprl_global_timeout'] = array(
50 '#type' => 'textfield',
51 '#title' => t('global_timeout. Maximum number of seconds httprl_send_request() may take'),
52 '#default_value' => variable_get('httprl_global_timeout', HTTPRL_GLOBAL_TIMEOUT),
53 '#description' => t('Value can be a float. The default is %value seconds.', array('%value' => HTTPRL_GLOBAL_TIMEOUT)),
54 );
55
56
57 return system_settings_form($form);
58 }
59
60 /**
61 * Validate form values.
62 */
63 function httprl_admin_settings_form_validate($form, &$form_state) {
64 // Skip validation if we are resting to defaults.
65 if (isset($form_state['clicked_button']['#post']['op']) && $form_state['clicked_button']['#post']['op'] == 'Reset to defaults') {
66 return;
67 }
68
69 // Get form values.
70 $values = $form_state['values'];
71
72 // If the IP field is not blank, check that it is a valid address.
73 if ( !empty($values['httprl_server_addr'])
74 && $values['httprl_server_addr'] != -1
75 && ip2long($values['httprl_server_addr']) === FALSE
76 ) {
77 form_set_error('httprl_server_addr', t('Must be a valid IP address.'));
78 }
79
80 // Make sure the timeouts are positive numbers.
81 $positive_values = array(
82 'httprl_dns_timeout',
83 'httprl_connect_timeout',
84 'httprl_ttfb_timeout',
85 'httprl_timeout',
86 'httprl_global_timeout',
87 );
88 foreach ($positive_values as $name) {
89 if (empty($values[$name])) {
90 form_set_error($name, t('Must not be empty or zero.'));
91 continue;
92 }
93 if (!is_numeric($values[$name])) {
94 form_set_error($name, t('Must be numeric.'));
95 continue;
96 }
97 if ($values[$name] <= 0) {
98 form_set_error($name, t('Must be a postive number.'));
99 continue;
100 }
101 }
102 }