honeypot.api.php 3.2 KB

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