honeypot.api.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * API Functionality for Honeypot module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Alter the honeypot protections added to a particular form.
  12. *
  13. * @param array $options
  14. * Protections that will be applied to the form. May be empty, or may include
  15. * 'honeypot' and/or 'time_restriction'.
  16. * @param array $form
  17. * The Form API form to which protections will be added.
  18. */
  19. function hook_honeypot_form_protections_alter(array &$options, array $form) {
  20. // Add 'time_restriction' protection to 'mymodule-form' if it's not set.
  21. if ($form['form_id']['#value'] == 'mymodule_form' && !in_array('time_restriction', $options)) {
  22. $options[] = 'time_restriction';
  23. }
  24. }
  25. /**
  26. * React to an addition of honeypot form protections for a given form_id.
  27. *
  28. * After honeypot has added its protections to a form, this hook will be called.
  29. * You can use this hook to track when and how many times certain protected
  30. * forms are displayed to certain users, or for other tracking purposes.
  31. *
  32. * @param array $options
  33. * Protections that were applied to the form. Includes 'honeypot' and/or
  34. * 'time_restriction'.
  35. * @param array $form
  36. * The Form API form to which protections were added.
  37. */
  38. function hook_honeypot_add_form_protection(array $options, array $form) {
  39. if ($form['form_id']['#value'] == 'mymodule_form') {
  40. // Do something...
  41. }
  42. }
  43. /**
  44. * React to the rejection of a form submission.
  45. *
  46. * When honeypot rejects a form submission, it calls this hook with the form ID,
  47. * the user ID (0 if anonymous) of the user that was disallowed from submitting
  48. * the form, and the reason (type) for the rejection of the form submission.
  49. *
  50. * @param string $form_id
  51. * Form ID of the form the user was disallowed from submitting.
  52. * @param int $uid
  53. * 0 for anonymous users, otherwise the user ID of the user.
  54. * @param string $type
  55. * String indicating the reason the submission was blocked. Allowed values:
  56. * - honeypot: If honeypot field was filled in.
  57. * - honeypot_time: If form was completed before the configured time limit.
  58. */
  59. function hook_honeypot_reject($form_id, $uid, $type) {
  60. if ($form_id == 'mymodule_form') {
  61. // Do something...
  62. }
  63. }
  64. /**
  65. * Add time to the Honeypot time limit.
  66. *
  67. * In certain circumstances (for example, on forms routinely targeted by
  68. * spammers), you may want to add an additional time delay. You can use this
  69. * hook to return additional time (in seconds) to honeypot when it is calculates
  70. * the time limit for a particular form.
  71. *
  72. * @param int $honeypot_time_limit
  73. * The current honeypot time limit (in seconds), to which any additions you
  74. * return will be added.
  75. * @param array $form_values
  76. * Array of form values (may be empty).
  77. * @param int $number
  78. * Number of times the current user has already fallen into the honeypot trap.
  79. *
  80. * @return int
  81. * Additional time to add to the honeypot_time_limit, in seconds (integer).
  82. */
  83. function hook_honeypot_time_limit($honeypot_time_limit, array $form_values, $number) {
  84. $additions = 0;
  85. // If 'some_interesting_value' is set in your form, add 10 seconds to limit.
  86. if (!empty($form_values['some_interesting_value']) && $form_values['some_interesting_value']) {
  87. $additions = 10;
  88. }
  89. return $additions;
  90. }
  91. /**
  92. * @} End of "addtogroup hooks".
  93. */