honeypot.admin.inc 7.2 KB

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