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