honeypot.admin.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @file
  4. * Honeypot administration forms.
  5. */
  6. /**
  7. * Honeypot administration page.
  8. */
  9. function honeypot_admin_form($form, &$form_state) {
  10. // Honeypot Configuration.
  11. $form['configuration'] = array(
  12. '#type' => 'fieldset',
  13. '#title' => t('Honeypot Configuration'),
  14. '#collapsible' => TRUE,
  15. '#collapsed' => FALSE,
  16. );
  17. $form['configuration']['honeypot_protect_all_forms'] = array(
  18. '#type' => 'checkbox',
  19. '#title' => t('Protect all forms with Honeypot'),
  20. '#description' => t('Enable Honeypot protection for ALL forms on this site (it is best to only enable Honeypot for the forms you need below).'),
  21. '#default_value' => variable_get('honeypot_protect_all_forms', 0),
  22. );
  23. $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>');
  24. $form['configuration']['honeypot_log'] = array(
  25. '#type' => 'checkbox',
  26. '#title' => t('Log blocked form submissions'),
  27. '#description' => t('Log submissions that are blocked due to Honeypot protection.'),
  28. '#default_value' => variable_get('honeypot_log', 0),
  29. );
  30. $form['configuration']['honeypot_element_name'] = array(
  31. '#type' => 'textfield',
  32. '#title' => t('Honeypot element name'),
  33. '#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."),
  34. '#default_value' => variable_get('honeypot_element_name', 'url'),
  35. '#required' => TRUE,
  36. '#size' => 30,
  37. );
  38. $form['configuration']['honeypot_time_limit'] = array(
  39. '#type' => 'textfield',
  40. '#title' => t('Honeypot time limit'),
  41. '#description' => t('Minimum time required before form should be considered entered by a human instead of a bot. Set to 0 to disable.'),
  42. '#default_value' => variable_get('honeypot_time_limit', 5),
  43. '#required' => TRUE,
  44. '#size' => 5,
  45. '#field_suffix' => t('seconds'),
  46. );
  47. $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>');
  48. // Honeypot Enabled forms.
  49. $form['enabled_forms'] = array(
  50. '#type' => 'fieldset',
  51. '#title' => t('Honeypot Enabled Forms'),
  52. '#description' => t("Check the boxes next to individual forms on which you'd like Honeypot protection enabled."),
  53. '#collapsible' => TRUE,
  54. '#collapsed' => FALSE,
  55. '#states' => array(
  56. // Hide this fieldset when all forms are protected.
  57. 'invisible' => array(
  58. 'input[name="honeypot_protect_all_forms"]' => array('checked' => TRUE),
  59. ),
  60. ),
  61. );
  62. // Generic forms.
  63. $form['enabled_forms']['general_forms'] = array('#markup' => '<h5>' . t('General Forms') . '</h5>');
  64. // User register form.
  65. $form['enabled_forms']['honeypot_form_user_register_form'] = array(
  66. '#type' => 'checkbox',
  67. '#title' => t('User Registration form'),
  68. '#default_value' => variable_get('honeypot_form_user_register_form', 0),
  69. );
  70. // User password form.
  71. $form['enabled_forms']['honeypot_form_user_pass'] = array(
  72. '#type' => 'checkbox',
  73. '#title' => t('User Password Reset form'),
  74. '#default_value' => variable_get('honeypot_form_user_pass', 0),
  75. );
  76. // If webform.module enabled, add webforms.
  77. if (module_exists('webform')) {
  78. $form['enabled_forms']['honeypot_form_webforms'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => t('Webforms (all)'),
  81. '#default_value' => variable_get('honeypot_form_webforms', 0),
  82. );
  83. }
  84. // If contact.module enabled, add contact forms.
  85. if (module_exists('contact')) {
  86. $form['enabled_forms']['contact_forms'] = array('#markup' => '<h5>' . t('Contact Forms') . '</h5>');
  87. // Sitewide contact form.
  88. $form['enabled_forms']['honeypot_form_contact_site_form'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('Sitewide Contact form'),
  91. '#default_value' => variable_get('honeypot_form_contact_site_form', 0),
  92. );
  93. // Sitewide personal form.
  94. $form['enabled_forms']['honeypot_form_contact_personal_form'] = array(
  95. '#type' => 'checkbox',
  96. '#title' => t('Personal Contact forms'),
  97. '#default_value' => variable_get('honeypot_form_contact_personal_form', 0),
  98. );
  99. }
  100. // If profile.module enabled, add profile forms.
  101. if (module_exists('profile')) {
  102. $form['enabled_forms']['profile_forms'] = array('#value' => '<h5>' . t('Profile Forms') . '</h5>');
  103. $form['enabled_forms']['honeypot_form_user_profile_form'] = array(
  104. '#type' => 'checkbox',
  105. '#title' => t('Profile forms (all)'),
  106. '#default_value' => variable_get('honeypot_form_user_profile_form', 0),
  107. );
  108. }
  109. // Get node types for node forms and node comment forms.
  110. $types = node_type_get_types();
  111. if (!empty($types)) {
  112. // Node forms.
  113. $form['enabled_forms']['node_forms'] = array('#markup' => '<h5>' . t('Node Forms') . '</h5>');
  114. foreach ($types as $type) {
  115. $id = 'honeypot_form_' . $type->type . '_node_form';
  116. $form['enabled_forms'][$id] = array(
  117. '#type' => 'checkbox',
  118. '#title' => t('@name node form', array('@name' => $type->name)),
  119. '#default_value' => variable_get($id, 0),
  120. );
  121. }
  122. // Comment forms.
  123. if (module_exists('comment')) {
  124. $form['enabled_forms']['comment_forms'] = array('#markup' => '<h5>' . t('Comment Forms') . '</h5>');
  125. foreach ($types as $type) {
  126. $id = 'honeypot_form_comment_node_' . $type->type . '_form';
  127. $form['enabled_forms'][$id] = array(
  128. '#type' => 'checkbox',
  129. '#title' => t('@name comment form', array('@name' => $type->name)),
  130. '#default_value' => variable_get($id, 0),
  131. );
  132. }
  133. }
  134. }
  135. // Add our own submit handler to clear honeypot's form cache on save.
  136. $form['#submit'][] = 'honeypot_admin_form_submit';
  137. return system_settings_form($form);
  138. }
  139. /**
  140. * Validate the admin form.
  141. */
  142. function honeypot_admin_form_validate($form, &$form_state) {
  143. // Make sure the time limit is a positive integer or 0.
  144. $time_limit = $form_state['values']['honeypot_time_limit'];
  145. if ((is_numeric($time_limit) && $time_limit > 0) || $time_limit === '0') {
  146. if (ctype_digit($time_limit)) {
  147. // Good to go.
  148. }
  149. else {
  150. form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
  151. }
  152. }
  153. else {
  154. form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
  155. }
  156. // Make sure Honeypot element name only contains A-Z, 0-9.
  157. if (!preg_match("/^[-_a-zA-Z0-9]+$/", $form_state['values']['honeypot_element_name'])) {
  158. form_set_error('honeypot_element_name', t("The element name cannot contain spaces or other special characters."));
  159. }
  160. // Make sure Honeypot element name starts with a letter.
  161. if (!preg_match("/^[a-zA-Z].+$/", $form_state['values']['honeypot_element_name'])) {
  162. form_set_error('honeypot_element_name', t("The element name must start with a letter."));
  163. }
  164. // Make sure Honeypot element name isn't one of the reserved names.
  165. $reserved_element_names = array(
  166. 'name',
  167. 'pass',
  168. 'website',
  169. );
  170. if (in_array($form_state['values']['honeypot_element_name'], $reserved_element_names)) {
  171. 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))));
  172. }
  173. }
  174. /**
  175. * Honeypot admin form submit callback.
  176. */
  177. function honeypot_admin_form_submit($form, &$form_state) {
  178. // Create CSS file for honeypot.
  179. honeypot_create_css($form_state['values']['honeypot_element_name']);
  180. // Clear the Honeypot form cache on submit.
  181. cache_clear_all('honeypot_protected_forms', 'cache');
  182. }