Projet

Général

Profil

Paste
Télécharger (7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / honeypot / honeypot.admin.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Honeypot administration forms.
6
 */
7

    
8
/**
9
 * Honeypot administration page.
10
 */
11
function honeypot_admin_form($form, &$form_state) {
12
  // Honeypot Configuration.
13
  $form['configuration'] = array(
14
    '#type' => 'fieldset',
15
    '#title' => t('Honeypot Configuration'),
16
    '#collapsible' => TRUE,
17
    '#collapsed' => FALSE,
18
  );
19
  $form['configuration']['honeypot_protect_all_forms'] = array(
20
    '#type' => 'checkbox',
21
    '#title' => t('Protect all forms with Honeypot'),
22
    '#description' => t('Enable Honeypot protection for ALL forms on this site (it is best to only enable Honeypot for the forms you need below).'),
23
    '#default_value' => variable_get('honeypot_protect_all_forms', 0),
24
  );
25
  $form['configuration']['honeypot_protect_all_forms']['#description'] .= '<br />' . t('<strong>Page caching will be disabled on any page where a form is present if the Honeypot time limit is not set to 0.</strong>');
26
  $form['configuration']['honeypot_log'] = array(
27
    '#type' => 'checkbox',
28
    '#title' => t('Log blocked form submissions'),
29
    '#description' => t('Log submissions that are blocked due to Honeypot protection.'),
30
    '#default_value' => variable_get('honeypot_log', 0),
31
  );
32
  $form['configuration']['honeypot_element_name'] = array(
33
    '#type' => 'textfield',
34
    '#title' => t('Honeypot element name'),
35
    '#description' => t("The name of the Honeypot form field. It's usually most effective to use a generic name like email, homepage, or name, but this should be changed if it interferes with fields that are already in your forms. Must not contain spaces or special characters."),
36
    '#default_value' => variable_get('honeypot_element_name', 'url'),
37
    '#required' => TRUE,
38
    '#size' => 30,
39
  );
40
  $form['configuration']['honeypot_time_limit'] = array(
41
    '#type' => 'textfield',
42
    '#title' => t('Honeypot time limit'),
43
    '#description' => t('Minimum time required before form should be considered entered by a human instead of a bot. Set to 0 to disable.'),
44
    '#default_value' => variable_get('honeypot_time_limit', 5),
45
    '#required' => TRUE,
46
    '#size' => 5,
47
    '#field_suffix' => t('seconds'),
48
  );
49
  $form['configuration']['honeypot_time_limit']['#description'] .= '<br />' . t('<strong>Page caching will be disabled if there is a form protected by time limit on the page.</strong>');
50

    
51
  // Honeypot Enabled forms.
52
  $form['enabled_forms'] = array(
53
    '#type' => 'fieldset',
54
    '#title' => t('Honeypot Enabled Forms'),
55
    '#description' => t("Check the boxes next to individual forms on which you'd like Honeypot protection enabled."),
56
    '#collapsible' => TRUE,
57
    '#collapsed' => FALSE,
58
    '#states' => array(
59
      // Hide this fieldset when all forms are protected.
60
      'invisible' => array(
61
        'input[name="honeypot_protect_all_forms"]' => array('checked' => TRUE),
62
      ),
63
    ),
64
  );
65

    
66
  // Generic forms.
67
  $form['enabled_forms']['general_forms'] = array('#markup' => '<h5>' . t('General Forms') . '</h5>');
68
  // User register form.
69
  $form['enabled_forms']['honeypot_form_user_register_form'] = array(
70
    '#type' => 'checkbox',
71
    '#title' => t('User Registration form'),
72
    '#default_value' => variable_get('honeypot_form_user_register_form', 0),
73
  );
74
  // User password form.
75
  $form['enabled_forms']['honeypot_form_user_pass'] = array(
76
    '#type' => 'checkbox',
77
    '#title' => t('User Password Reset form'),
78
    '#default_value' => variable_get('honeypot_form_user_pass', 0),
79
  );
80

    
81
  // If webform.module enabled, add webforms.
82
  if (module_exists('webform')) {
83
    $form['enabled_forms']['honeypot_form_webforms'] = array(
84
      '#type' => 'checkbox',
85
      '#title' => t('Webforms (all)'),
86
      '#default_value' => variable_get('honeypot_form_webforms', 0),
87
    );
88
  }
89

    
90
  // If contact.module enabled, add contact forms.
91
  if (module_exists('contact')) {
92
    $form['enabled_forms']['contact_forms'] = array('#markup' => '<h5>' . t('Contact Forms') . '</h5>');
93
    // Sitewide contact form.
94
    $form['enabled_forms']['honeypot_form_contact_site_form'] = array(
95
      '#type' => 'checkbox',
96
      '#title' => t('Sitewide Contact form'),
97
      '#default_value' => variable_get('honeypot_form_contact_site_form', 0),
98
    );
99
    // Sitewide personal form.
100
    $form['enabled_forms']['honeypot_form_contact_personal_form'] = array(
101
      '#type' => 'checkbox',
102
      '#title' => t('Personal Contact forms'),
103
      '#default_value' => variable_get('honeypot_form_contact_personal_form', 0),
104
    );
105
  }
106

    
107
  // If profile.module enabled, add profile forms.
108
  if (module_exists('profile')) {
109
    $form['enabled_forms']['profile_forms'] = array('#value' => '<h5>' . t('Profile Forms') . '</h5>');
110
    $form['enabled_forms']['honeypot_form_user_profile_form'] = array(
111
      '#type' => 'checkbox',
112
      '#title' => t('Profile forms (all)'),
113
      '#default_value' => variable_get('honeypot_form_user_profile_form', 0),
114
    );
115
  }
116

    
117
  // Get node types for node forms and node comment forms.
118
  $types = node_type_get_types();
119
  if (!empty($types)) {
120
    // Node forms.
121
    $form['enabled_forms']['node_forms'] = array('#markup' => '<h5>' . t('Node Forms') . '</h5>');
122
    foreach ($types as $type) {
123
      $id = 'honeypot_form_' . $type->type . '_node_form';
124
      $form['enabled_forms'][$id] = array(
125
        '#type' => 'checkbox',
126
        '#title' => t('@name node form', array('@name' => $type->name)),
127
        '#default_value' => variable_get($id, 0),
128
      );
129
    }
130

    
131
    // Comment forms.
132
    if (module_exists('comment')) {
133
      $form['enabled_forms']['comment_forms'] = array('#markup' => '<h5>' . t('Comment Forms') . '</h5>');
134
      foreach ($types as $type) {
135
        $id = 'honeypot_form_comment_node_' . $type->type . '_form';
136
        $form['enabled_forms'][$id] = array(
137
          '#type' => 'checkbox',
138
          '#title' => t('@name comment form', array('@name' => $type->name)),
139
          '#default_value' => variable_get($id, 0),
140
        );
141
      }
142
    }
143
  }
144

    
145
  // Add our own submit handler to clear honeypot's form cache on save.
146
  $form['#submit'][] = 'honeypot_admin_form_submit';
147

    
148
  return system_settings_form($form);
149
}
150

    
151
/**
152
 * Validate the admin form.
153
 */
154
function honeypot_admin_form_validate($form, &$form_state) {
155
  // Make sure the time limit is a positive integer or 0.
156
  $time_limit = $form_state['values']['honeypot_time_limit'];
157
  if ((is_numeric($time_limit) && $time_limit > 0) || $time_limit === '0') {
158
    if (ctype_digit($time_limit)) {
159
      // Good to go.
160
    }
161
    else {
162
      form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
163
    }
164
  }
165
  else {
166
    form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
167
  }
168

    
169
  // Make sure Honeypot element name only contains A-Z, 0-9.
170
  if (!preg_match("/^[-_a-zA-Z0-9]+$/", $form_state['values']['honeypot_element_name'])) {
171
    form_set_error('honeypot_element_name', t("The element name cannot contain spaces or other special characters."));
172
  }
173
}
174

    
175
/**
176
 * Clear the honeypot form cache on submit.
177
 */
178
function honeypot_admin_form_submit($form, &$form_state) {
179
  cache_clear_all('honeypot_protected_forms', 'cache');
180
}