honeypot.admin.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if (!variable_get('honeypot_use_js_for_cached_pages', FALSE)) {
  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. }
  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 link, 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. if (!variable_get('honeypot_use_js_for_cached_pages', FALSE)) {
  50. $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>');
  51. }
  52. $form['configuration']['honeypot_use_js_for_cached_pages'] = array(
  53. '#type' => 'checkbox',
  54. '#title' => t('Use Javascript protection for cacheable pages. (experimental)'),
  55. '#description' => t('Uses Javascript to preserve Page caching.'),
  56. '#default_value' => variable_get('honeypot_use_js_for_cached_pages', FALSE),
  57. '#states' => array(
  58. // Hide this when time limit is disabled.
  59. 'invisible' => array(
  60. 'input[name="honeypot_time_limit"]' => array('value' => 0),
  61. ),
  62. ),
  63. );
  64. $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>');
  65. // Honeypot Enabled forms.
  66. $form['enabled_forms'] = array(
  67. '#type' => 'fieldset',
  68. '#title' => t('Honeypot Enabled Forms'),
  69. '#description' => t("Check the boxes next to individual forms on which you'd like Honeypot protection enabled."),
  70. '#collapsible' => TRUE,
  71. '#collapsed' => FALSE,
  72. '#states' => array(
  73. // Hide this fieldset when all forms are protected.
  74. 'invisible' => array(
  75. 'input[name="honeypot_protect_all_forms"]' => array('checked' => TRUE),
  76. ),
  77. ),
  78. );
  79. // Generic forms.
  80. $form['enabled_forms']['general_forms'] = array('#markup' => '<h5>' . t('General Forms') . '</h5>');
  81. // User register form.
  82. $form['enabled_forms']['honeypot_form_user_register_form'] = array(
  83. '#type' => 'checkbox',
  84. '#title' => t('User Registration form'),
  85. '#default_value' => variable_get('honeypot_form_user_register_form', 0),
  86. );
  87. // User password form.
  88. $form['enabled_forms']['honeypot_form_user_pass'] = array(
  89. '#type' => 'checkbox',
  90. '#title' => t('User Password Reset form'),
  91. '#default_value' => variable_get('honeypot_form_user_pass', 0),
  92. );
  93. // If webform.module enabled, add webforms.
  94. if (module_exists('webform')) {
  95. $form['enabled_forms']['honeypot_form_webforms'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Webforms (all)'),
  98. '#default_value' => variable_get('honeypot_form_webforms', 0),
  99. );
  100. }
  101. // If contact.module enabled, add contact forms.
  102. if (module_exists('contact')) {
  103. $form['enabled_forms']['contact_forms'] = array('#markup' => '<h5>' . t('Contact Forms') . '</h5>');
  104. // Sitewide contact form.
  105. $form['enabled_forms']['honeypot_form_contact_site_form'] = array(
  106. '#type' => 'checkbox',
  107. '#title' => t('Sitewide Contact form'),
  108. '#default_value' => variable_get('honeypot_form_contact_site_form', 0),
  109. );
  110. // Sitewide personal form.
  111. $form['enabled_forms']['honeypot_form_contact_personal_form'] = array(
  112. '#type' => 'checkbox',
  113. '#title' => t('Personal Contact forms'),
  114. '#default_value' => variable_get('honeypot_form_contact_personal_form', 0),
  115. );
  116. }
  117. // If profile.module enabled, add profile forms.
  118. if (module_exists('profile')) {
  119. $form['enabled_forms']['profile_forms'] = array('#value' => '<h5>' . t('Profile Forms') . '</h5>');
  120. $form['enabled_forms']['honeypot_form_user_profile_form'] = array(
  121. '#type' => 'checkbox',
  122. '#title' => t('Profile forms (all)'),
  123. '#default_value' => variable_get('honeypot_form_user_profile_form', 0),
  124. );
  125. }
  126. // Get node types for node forms and node comment forms.
  127. $types = node_type_get_types();
  128. if (!empty($types)) {
  129. // Node forms.
  130. $form['enabled_forms']['node_forms'] = array('#markup' => '<h5>' . t('Node Forms') . '</h5>');
  131. foreach ($types as $type) {
  132. $id = 'honeypot_form_' . $type->type . '_node_form';
  133. $form['enabled_forms'][$id] = array(
  134. '#type' => 'checkbox',
  135. '#title' => t('@name node form', array('@name' => $type->name)),
  136. '#default_value' => variable_get($id, 0),
  137. );
  138. }
  139. // Comment forms.
  140. if (module_exists('comment')) {
  141. $form['enabled_forms']['comment_forms'] = array('#markup' => '<h5>' . t('Comment Forms') . '</h5>');
  142. foreach ($types as $type) {
  143. $id = 'honeypot_form_comment_node_' . $type->type . '_form';
  144. $form['enabled_forms'][$id] = array(
  145. '#type' => 'checkbox',
  146. '#title' => t('@name comment form', array('@name' => $type->name)),
  147. '#default_value' => variable_get($id, 0),
  148. );
  149. }
  150. }
  151. }
  152. // Add our own submit handler to clear honeypot's form cache on save.
  153. $form['#submit'][] = 'honeypot_admin_form_submit';
  154. return system_settings_form($form);
  155. }
  156. /**
  157. * Validate the admin form.
  158. */
  159. function honeypot_admin_form_validate($form, &$form_state) {
  160. // Make sure the time limit is a positive integer or 0.
  161. $time_limit = $form_state['values']['honeypot_time_limit'];
  162. if ((is_numeric($time_limit) && $time_limit > 0) || $time_limit === '0') {
  163. if (ctype_digit($time_limit)) {
  164. // Good to go.
  165. }
  166. else {
  167. form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
  168. }
  169. }
  170. else {
  171. form_set_error('honeypot_time_limit', t("The time limit must be a positive integer or 0."));
  172. }
  173. // Make sure Honeypot element name only contains A-Z, 0-9.
  174. if (!preg_match("/^[-_a-zA-Z0-9]+$/", $form_state['values']['honeypot_element_name'])) {
  175. form_set_error('honeypot_element_name', t("The element name cannot contain spaces or other special characters."));
  176. }
  177. // Make sure Honeypot element name starts with a letter.
  178. if (!preg_match("/^[a-zA-Z].+$/", $form_state['values']['honeypot_element_name'])) {
  179. form_set_error('honeypot_element_name', t("The element name must start with a letter."));
  180. }
  181. // Make sure Honeypot element name isn't one of the reserved names.
  182. $reserved_element_names = array(
  183. 'name',
  184. 'pass',
  185. 'website',
  186. );
  187. if (in_array($form_state['values']['honeypot_element_name'], $reserved_element_names)) {
  188. 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))));
  189. }
  190. }
  191. /**
  192. * Honeypot admin form submit callback.
  193. */
  194. function honeypot_admin_form_submit($form, &$form_state) {
  195. // Create CSS file for honeypot.
  196. honeypot_create_css($form_state['values']['honeypot_element_name']);
  197. // Clear the Honeypot form cache on submit.
  198. cache_clear_all('honeypot_protected_forms', 'cache');
  199. }