summaryrefslogtreecommitdiffstats
path: root/honeypot.api.php
blob: dd9fdcd3b151134b961861abeeebeca2369e92fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

/**
 * @file
 * API Functionality for Honeypot module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Alter the honeypot protections added to a particular form.
 *
 * @param array $options
 *   Protections that will be applied to the form. May be empty, or may include
 *   'honeypot' and/or 'time_restriction'.
 * @param array $form
 *   The Form API form to which protections will be added.
 */
function hook_honeypot_form_protections_alter(&$options, $form) {
  // Add 'time_restriction' protection to 'mymodule-form' if it's not set.
  if ($form['form_id']['#value'] == 'mymodule_form' && !in_array('time_restriction', $options)) {
    $options[] = 'time_restriction';
  }
}

/**
 * React to an addition of honeypot form protections for a given form_id.
 *
 * After honeypot has added its protections to a form, this hook will be called.
 * You can use this hook to track when and how many times certain protected
 * forms are displayed to certain users, or for other tracking purposes.
 *
 * @param array $options
 *   Protections that were applied to the form. Includes 'honeypot' and/or
 *   'time_restriction'.
 * @param array $form
 *   The Form API form to which protections were added.
 */
function hook_honeypot_add_form_protection($options, $form) {
  if ($form['form_id']['#value'] == 'mymodule_form') {
    // Do something...
  }
}

/**
 * React to the rejection of a form submission.
 *
 * When honeypot rejects a form submission, it calls this hook with the form ID,
 * the user ID (0 if anonymous) of the user that was disallowed from submitting
 * the form, and the reason (type) for the rejection of the form submission.
 *
 * @param string $form_id
 *   Form ID of the form the user was disallowed from submitting.
 * @param int $uid
 *   0 for anonymous users, otherwise the user ID of the user.
 * @param string $type
 *   String indicating the reason the submission was blocked. Allowed values:
 *     - honeypot: If honeypot field was filled in.
 *     - honeypot_time: If form was completed before the configured time limit.
 */
function hook_honeypot_reject($form_id, $uid, $type) {
  if ($form_id == 'mymodule_form') {
    // Do something...
  }
}

/**
 * Add time to the Honeypot time limit.
 *
 * In certain circumstances (for example, on forms routinely targeted by
 * spammers), you may want to add an additional time delay. You can use this
 * hook to return additional time (in seconds) to honeypot when it is calculates
 * the time limit for a particular form.
 *
 * @param int $honeypot_time_limit
 *   The current honeypot time limit (in seconds), to which any additions you
 *   return will be added.
 * @param array $form_values
 *   Array of form values (may be empty).
 * @param int $number
 *   Number of times the current user has already fallen into the honeypot trap.
 *
 * @return int
 *   Additional time to add to the honeypot_time_limit, in seconds (integer).
 */
function hook_honeypot_time_limit($honeypot_time_limit, $form_values, $number) {
  $additions = 0;
  // If 'some_interesting_value' is set in your form, add 10 seconds to limit.
  if (!empty($form_values['some_interesting_value']) && $form_values['some_interesting_value']) {
    $additions = 10;
  }
  return $additions;
}

/**
 * @} End of "addtogroup hooks".
 */