Projet

Général

Profil

Paste
Télécharger (8,73 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / honeypot / honeypot.admin.inc @ bc175c27

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
  if (!variable_get('honeypot_use_js_for_cached_pages', FALSE)) {
26
    $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>');
27
  }
28
  $form['configuration']['honeypot_log'] = array(
29
    '#type' => 'checkbox',
30
    '#title' => t('Log blocked form submissions'),
31
    '#description' => t('Log submissions that are blocked due to Honeypot protection.'),
32
    '#default_value' => variable_get('honeypot_log', 0),
33
  );
34
  $form['configuration']['honeypot_element_name'] = array(
35
    '#type' => 'textfield',
36
    '#title' => t('Honeypot element name'),
37
    '#description' => t("The name of the Honeypot form field. It's usually most effective to use a generic name like email, homepage, or link, but this should be changed if it interferes with fields that are already in your forms. Must not contain spaces or special characters."),
38
    '#default_value' => variable_get('honeypot_element_name', 'url'),
39
    '#required' => TRUE,
40
    '#size' => 30,
41
  );
42
  $form['configuration']['honeypot_time_limit'] = array(
43
    '#type' => 'textfield',
44
    '#title' => t('Honeypot time limit'),
45
    '#description' => t('Minimum time required before form should be considered entered by a human instead of a bot. Set to 0 to disable.'),
46
    '#default_value' => variable_get('honeypot_time_limit', 5),
47
    '#required' => TRUE,
48
    '#size' => 5,
49
    '#field_suffix' => t('seconds'),
50
  );
51
  if (!variable_get('honeypot_use_js_for_cached_pages', FALSE)) {
52
    $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>');
53
  }
54

    
55
  $form['configuration']['honeypot_use_js_for_cached_pages'] = array(
56
    '#type' => 'checkbox',
57
    '#title' => t('Use Javascript protection for cacheable pages. (experimental)'),
58
    '#description' => t('Uses Javascript to preserve Page caching.'),
59
    '#default_value' => variable_get('honeypot_use_js_for_cached_pages', FALSE),
60
    '#states' => array(
61
      // Hide this when time limit is disabled.
62
      'invisible' => array(
63
        'input[name="honeypot_time_limit"]' => array('value' => 0),
64
      ),
65
    ),
66
  );
67
  $form['configuration']['honeypot_use_js_for_cached_pages']['#description'] .= '<br />' . t('<strong>Warning: Users who have javascript disabled will need to confirm their form submission on the next page (if the Honeypot-enabled form is on a cacheable page).</strong>');
68

    
69
  // Honeypot Enabled forms.
70
  $form['enabled_forms'] = array(
71
    '#type' => 'fieldset',
72
    '#title' => t('Honeypot Enabled Forms'),
73
    '#description' => t("Check the boxes next to individual forms on which you'd like Honeypot protection enabled."),
74
    '#collapsible' => TRUE,
75
    '#collapsed' => FALSE,
76
    '#states' => array(
77
      // Hide this fieldset when all forms are protected.
78
      'invisible' => array(
79
        'input[name="honeypot_protect_all_forms"]' => array('checked' => TRUE),
80
      ),
81
    ),
82
  );
83

    
84
  // Generic forms.
85
  $form['enabled_forms']['general_forms'] = array('#markup' => '<h5>' . t('General Forms') . '</h5>');
86
  // User register form.
87
  $form['enabled_forms']['honeypot_form_user_register_form'] = array(
88
    '#type' => 'checkbox',
89
    '#title' => t('User Registration form'),
90
    '#default_value' => variable_get('honeypot_form_user_register_form', 0),
91
  );
92
  // User password form.
93
  $form['enabled_forms']['honeypot_form_user_pass'] = array(
94
    '#type' => 'checkbox',
95
    '#title' => t('User Password Reset form'),
96
    '#default_value' => variable_get('honeypot_form_user_pass', 0),
97
  );
98

    
99
  // If webform.module enabled, add webforms.
100
  if (module_exists('webform')) {
101
    $form['enabled_forms']['honeypot_form_webforms'] = array(
102
      '#type' => 'checkbox',
103
      '#title' => t('Webforms (all)'),
104
      '#default_value' => variable_get('honeypot_form_webforms', 0),
105
    );
106
  }
107

    
108
  // If contact.module enabled, add contact forms.
109
  if (module_exists('contact')) {
110
    $form['enabled_forms']['contact_forms'] = array('#markup' => '<h5>' . t('Contact Forms') . '</h5>');
111
    // Sitewide contact form.
112
    $form['enabled_forms']['honeypot_form_contact_site_form'] = array(
113
      '#type' => 'checkbox',
114
      '#title' => t('Sitewide Contact form'),
115
      '#default_value' => variable_get('honeypot_form_contact_site_form', 0),
116
    );
117
    // Sitewide personal form.
118
    $form['enabled_forms']['honeypot_form_contact_personal_form'] = array(
119
      '#type' => 'checkbox',
120
      '#title' => t('Personal Contact forms'),
121
      '#default_value' => variable_get('honeypot_form_contact_personal_form', 0),
122
    );
123
  }
124

    
125
  // If profile.module enabled, add profile forms.
126
  if (module_exists('profile')) {
127
    $form['enabled_forms']['profile_forms'] = array('#value' => '<h5>' . t('Profile Forms') . '</h5>');
128
    $form['enabled_forms']['honeypot_form_user_profile_form'] = array(
129
      '#type' => 'checkbox',
130
      '#title' => t('Profile forms (all)'),
131
      '#default_value' => variable_get('honeypot_form_user_profile_form', 0),
132
    );
133
  }
134

    
135
  // Get node types for node forms and node comment forms.
136
  $types = node_type_get_types();
137
  if (!empty($types)) {
138
    // Node forms.
139
    $form['enabled_forms']['node_forms'] = array('#markup' => '<h5>' . t('Node Forms') . '</h5>');
140
    foreach ($types as $type) {
141
      $id = 'honeypot_form_' . $type->type . '_node_form';
142
      $form['enabled_forms'][$id] = array(
143
        '#type' => 'checkbox',
144
        '#title' => t('@name node form', array('@name' => $type->name)),
145
        '#default_value' => variable_get($id, 0),
146
      );
147
    }
148

    
149
    // Comment forms.
150
    if (module_exists('comment')) {
151
      $form['enabled_forms']['comment_forms'] = array('#markup' => '<h5>' . t('Comment Forms') . '</h5>');
152
      foreach ($types as $type) {
153
        $id = 'honeypot_form_comment_node_' . $type->type . '_form';
154
        $form['enabled_forms'][$id] = array(
155
          '#type' => 'checkbox',
156
          '#title' => t('@name comment form', array('@name' => $type->name)),
157
          '#default_value' => variable_get($id, 0),
158
        );
159
      }
160
    }
161
  }
162

    
163
  // Add our own submit handler to clear honeypot's form cache on save.
164
  $form['#submit'][] = 'honeypot_admin_form_submit';
165

    
166
  return system_settings_form($form);
167
}
168

    
169
/**
170
 * Validate the admin form.
171
 */
172
function honeypot_admin_form_validate($form, &$form_state) {
173
  // Make sure the time limit is a positive integer or 0.
174
  $time_limit = $form_state['values']['honeypot_time_limit'];
175
  if ((is_numeric($time_limit) && $time_limit > 0) || $time_limit === '0') {
176
    if (ctype_digit($time_limit)) {
177
      // Good to go.
178
    }
179
    else {
180
      form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
181
    }
182
  }
183
  else {
184
    form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
185
  }
186

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

    
192
  // Make sure Honeypot element name starts with a letter.
193
  if (!preg_match("/^[a-zA-Z].+$/", $form_state['values']['honeypot_element_name'])) {
194
    form_set_error('honeypot_element_name', t("The element name must start with a letter."));
195
  }
196

    
197
  // Make sure Honeypot element name isn't one of the reserved names.
198
  $reserved_element_names = array(
199
    'name',
200
    'pass',
201
    'website',
202
  );
203
  if (in_array($form_state['values']['honeypot_element_name'], $reserved_element_names)) {
204
    form_set_error('honeypot_element_name', t("The element name cannot match one of the common Drupal form field names (e.g. @names).", array('@names' => implode(', ', $reserved_element_names))));
205
  }
206
}
207

    
208
/**
209
 * Honeypot admin form submit callback.
210
 */
211
function honeypot_admin_form_submit($form, &$form_state) {
212
  // Create CSS file for honeypot.
213
  honeypot_create_css($form_state['values']['honeypot_element_name']);
214

    
215
  // Clear the Honeypot form cache on submit.
216
  cache_clear_all('honeypot_protected_forms', 'cache');
217
}