You are here

captcha_api.txt in CAPTCHA 7

Same filename and directory in other branches
  1. 5.3 captcha_api.txt
  2. 6.2 captcha_api.txt
  3. 6 captcha_api.txt
This documentation is for developers that want to implement their own
challenge type and integrate it with the base CAPTCHA module.


=== Required: hook_captcha($op, $captcha_type='') ===

The hook_captcha() hook is the only required function if you want to integrate
with the base CAPTCHA module.
Functionality depends on the first argument $op:
  * 'list': you should return an array of possible challenge types
    that your module implements.
  * 'generate': generate a challenge.
    You should return an array that offers form elements and the solution
    of your challenge, defined by the second argument $captcha_type.
    The returned array $captcha should have the following items:
    $captcha['solution']: this is the solution of your challenge
    $captcha['form']: an array of the form elements you want to add to the form.
      There should be a key 'captcha_response' in this array, which points to
      the form element where the user enters his answer.
    An optional additional argument $captcha_sid with the captcha session ID is
    available for more advanced challenges (e.g. the image CAPTCHA uses this
    argument, see image_captcha_captcha()).

Let's give a simple example to make this more clear.
We create the challenge 'Foo CAPTCHA', which requires the user to
enter "foo" in a textfield.

"""
/**
 * Implementation of hook_captcha().
 */
function foo_captcha_captcha($op, $captcha_type='') {
  switch ($op) {
    case 'list':
      return array('Foo CAPTCHA');
    case 'generate':
      if ($captcha_type == 'Foo CAPTCHA') {
        $captcha = array();
        $captcha['solution'] = 'foo';
        $captcha['form']['captcha_response'] = array(
          '#type' => 'textfield',
          '#title' => t('Enter "foo"'),
          '#required' => TRUE,
        );
        return $captcha;
      }
      break;
  }
}
"""

Validation of the answer against the solution and other stuff is done by the
base CAPTCHA module.




=== Required: the .info file ===

You should specify that your module depends on the base CAPTCHA module.
Optionally you could put your module in the "Spam control" package.

For our simple foo CAPTCHA module this would mean the following lines in the
file foo_captcha.info:

"""
name = "Foo CAPTCHA"
description = "The foo CAPTCHA requires the user to enter the word 'foo'."
package = "Spam control"
dependencies[] = captcha
core = 6.x
"""




=== Recommended: hook_menu($may_cache) ===

More advanced CAPTCHA modules probably want some configuration page.
To integrate nicely with the base CAPTCHA module you should offer your
configuration page as a MENU_LOCAL_TASK menu entry under 'admin/config/people/captcha/'.

For our simple foo CAPTCHA module this would mean:

"""
/**
 * Implementation of hook_menu().
 */
function foo_captcha_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items['admin/config/people/captcha/foo_captcha'] = array(
      'title' => t('Foo CAPTCHA'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('foo_captcha_settings_form'),
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}
"""

You should of course implement a function foo_captcha_settings_form() which
returns the form of your configuration page.




=== Optional: hook_help($section) ===
To offer a description/explanation of your challenge, you can use the
normal hook_help() system.

For our simple foo CAPTCHA module this would mean:

"""
/**
 * Implementation of hook_help().
 */
function foo_captcha_help($path, $arg) {
  switch ($path) {
    case 'admin/config/people/captcha/foo_captcha':
      return '<p>'. t('This is a very simple challenge, which requires users to enter "foo" in a textfield.') .'</p>';
  }
}
"""



=== Optional: custom response validation ===
The CAPTCHA module provides an option for case sensitive and case insensitve
validation of the responses. If this is not sufficient, you can provide
your own validation function with the 'captcha_validate' field, illustrated
by the following example:

"""
/**
 * Implementation of hook_captcha().
 */
function foo_captcha_captcha($op, $captcha_type='') {
  switch ($op) {
    ...
    case 'generate':
      if ($captcha_type == 'Foo CAPTCHA') {
        $captcha = array();
        $captcha['solution'] = ...
        $captcha['form'] = ...
        $captcha['captcha_validate'] = 'foo_captcha_custom_validation';
        return $captcha;
      }
      break;
  }
}

/**
 * Custom CAPTCHA validation function.
 *
 * @param solution the solution for the challenge as reported by hook_captcha('generate', ...).
 * @param response the answer given by the user.
 * @return TRUE on succes and FALSE on failure.
 */
function foo_captcha_custom_validation($solution, $response) {
  return $response == "foo" || $response == "bar";
}
"""

Previous example shows the basic usage for custom validation with only a $solution
and $response argument, which should be sufficient for most CAPTCHA modules.
More advanced CAPTCHA modules can also use extra provided arguments $element
and $form_state:
"""
function foo_captcha_custom_validation($solution, $response, $element, $form_state) {
  return $form_state['foo']['#bar'] = 'baz';
}
"""
These extra arguments are the $element and $form_state arguments of the validation function
of the #captcha element. See captcha_validate() in captcha.module for more info about this.



=== Hook into CAPTCHA placement ===
The CAPTCHA module attempts to place the CAPTCHA element in an appropriate spot
at the bottom of the targeted form, but this automatic detection may be insufficient
for complex forms.
The hook_captcha_placement_map hook allows to define the placement of the CAPTCHA element
as desired. The hook should return an array, mapping form IDs to placement arrays, which are
associative arrays with the following fields:
  - 'path': path (array of path items) of the form's container element in which the
    CAPTCHA element should be inserted.
  - 'key': the key of the element before which the CAPTCHA element
    should be inserted. If the field 'key' is undefined or NULL, the CAPTCHA will
    just be appended in the container.
  - 'weight': if 'key' is not NULL: should be the weight of the element defined by 'key'.
    If 'key' is NULL and weight is not NULL/unset: set the weight property of the CAPTCHA element
    to this value.

For example:
"""
/**
 * Implementation of hook_captcha_placement_map
 */
function hook_captcha_placement_map() {
  return array(
    'my_fancy_form' => array(
      'path' => array('items', 'buttons'),
      'key' => 'savebutton',
    ),
    'another_form' => array(
      'path' => array(),
      'weight' => 34,
    ),
  );
}
"""
This will place the CAPTCHA element
  - in the 'my_fancy_form' form inside the container $form['items']['buttons'],
    just before the element $form['items']['buttons']['sacebutton'].
  - in the 'another_form' form at the toplevel of the form, with a weight 34.

File

captcha_api.txt
View source
  1. This documentation is for developers that want to implement their own
  2. challenge type and integrate it with the base CAPTCHA module.
  3. === Required: hook_captcha($op, $captcha_type='') ===
  4. The hook_captcha() hook is the only required function if you want to integrate
  5. with the base CAPTCHA module.
  6. Functionality depends on the first argument $op:
  7. * 'list': you should return an array of possible challenge types
  8. that your module implements.
  9. * 'generate': generate a challenge.
  10. You should return an array that offers form elements and the solution
  11. of your challenge, defined by the second argument $captcha_type.
  12. The returned array $captcha should have the following items:
  13. $captcha['solution']: this is the solution of your challenge
  14. $captcha['form']: an array of the form elements you want to add to the form.
  15. There should be a key 'captcha_response' in this array, which points to
  16. the form element where the user enters his answer.
  17. An optional additional argument $captcha_sid with the captcha session ID is
  18. available for more advanced challenges (e.g. the image CAPTCHA uses this
  19. argument, see image_captcha_captcha()).
  20. Let's give a simple example to make this more clear.
  21. We create the challenge 'Foo CAPTCHA', which requires the user to
  22. enter "foo" in a textfield.
  23. """
  24. /**
  25. * Implementation of hook_captcha().
  26. */
  27. function foo_captcha_captcha($op, $captcha_type='') {
  28. switch ($op) {
  29. case 'list':
  30. return array('Foo CAPTCHA');
  31. case 'generate':
  32. if ($captcha_type == 'Foo CAPTCHA') {
  33. $captcha = array();
  34. $captcha['solution'] = 'foo';
  35. $captcha['form']['captcha_response'] = array(
  36. '#type' => 'textfield',
  37. '#title' => t('Enter "foo"'),
  38. '#required' => TRUE,
  39. );
  40. return $captcha;
  41. }
  42. break;
  43. }
  44. }
  45. """
  46. Validation of the answer against the solution and other stuff is done by the
  47. base CAPTCHA module.
  48. === Required: the .info file ===
  49. You should specify that your module depends on the base CAPTCHA module.
  50. Optionally you could put your module in the "Spam control" package.
  51. For our simple foo CAPTCHA module this would mean the following lines in the
  52. file foo_captcha.info:
  53. """
  54. name = "Foo CAPTCHA"
  55. description = "The foo CAPTCHA requires the user to enter the word 'foo'."
  56. package = "Spam control"
  57. dependencies[] = captcha
  58. core = 6.x
  59. """
  60. === Recommended: hook_menu($may_cache) ===
  61. More advanced CAPTCHA modules probably want some configuration page.
  62. To integrate nicely with the base CAPTCHA module you should offer your
  63. configuration page as a MENU_LOCAL_TASK menu entry under 'admin/config/people/captcha/'.
  64. For our simple foo CAPTCHA module this would mean:
  65. """
  66. /**
  67. * Implementation of hook_menu().
  68. */
  69. function foo_captcha_menu($may_cache) {
  70. $items = array();
  71. if ($may_cache) {
  72. $items['admin/config/people/captcha/foo_captcha'] = array(
  73. 'title' => t('Foo CAPTCHA'),
  74. 'page callback' => 'drupal_get_form',
  75. 'page arguments' => array('foo_captcha_settings_form'),
  76. 'type' => MENU_LOCAL_TASK,
  77. );
  78. }
  79. return $items;
  80. }
  81. """
  82. You should of course implement a function foo_captcha_settings_form() which
  83. returns the form of your configuration page.
  84. === Optional: hook_help($section) ===
  85. To offer a description/explanation of your challenge, you can use the
  86. normal hook_help() system.
  87. For our simple foo CAPTCHA module this would mean:
  88. """
  89. /**
  90. * Implementation of hook_help().
  91. */
  92. function foo_captcha_help($path, $arg) {
  93. switch ($path) {
  94. case 'admin/config/people/captcha/foo_captcha':
  95. return '

    '. t('This is a very simple challenge, which requires users to enter "foo" in a textfield.') .'

    ';
  96. }
  97. }
  98. """
  99. === Optional: custom response validation ===
  100. The CAPTCHA module provides an option for case sensitive and case insensitve
  101. validation of the responses. If this is not sufficient, you can provide
  102. your own validation function with the 'captcha_validate' field, illustrated
  103. by the following example:
  104. """
  105. /**
  106. * Implementation of hook_captcha().
  107. */
  108. function foo_captcha_captcha($op, $captcha_type='') {
  109. switch ($op) {
  110. ...
  111. case 'generate':
  112. if ($captcha_type == 'Foo CAPTCHA') {
  113. $captcha = array();
  114. $captcha['solution'] = ...
  115. $captcha['form'] = ...
  116. $captcha['captcha_validate'] = 'foo_captcha_custom_validation';
  117. return $captcha;
  118. }
  119. break;
  120. }
  121. }
  122. /**
  123. * Custom CAPTCHA validation function.
  124. *
  125. * @param solution the solution for the challenge as reported by hook_captcha('generate', ...).
  126. * @param response the answer given by the user.
  127. * @return TRUE on succes and FALSE on failure.
  128. */
  129. function foo_captcha_custom_validation($solution, $response) {
  130. return $response == "foo" || $response == "bar";
  131. }
  132. """
  133. Previous example shows the basic usage for custom validation with only a $solution
  134. and $response argument, which should be sufficient for most CAPTCHA modules.
  135. More advanced CAPTCHA modules can also use extra provided arguments $element
  136. and $form_state:
  137. """
  138. function foo_captcha_custom_validation($solution, $response, $element, $form_state) {
  139. return $form_state['foo']['#bar'] = 'baz';
  140. }
  141. """
  142. These extra arguments are the $element and $form_state arguments of the validation function
  143. of the #captcha element. See captcha_validate() in captcha.module for more info about this.
  144. === Hook into CAPTCHA placement ===
  145. The CAPTCHA module attempts to place the CAPTCHA element in an appropriate spot
  146. at the bottom of the targeted form, but this automatic detection may be insufficient
  147. for complex forms.
  148. The hook_captcha_placement_map hook allows to define the placement of the CAPTCHA element
  149. as desired. The hook should return an array, mapping form IDs to placement arrays, which are
  150. associative arrays with the following fields:
  151. - 'path': path (array of path items) of the form's container element in which the
  152. CAPTCHA element should be inserted.
  153. - 'key': the key of the element before which the CAPTCHA element
  154. should be inserted. If the field 'key' is undefined or NULL, the CAPTCHA will
  155. just be appended in the container.
  156. - 'weight': if 'key' is not NULL: should be the weight of the element defined by 'key'.
  157. If 'key' is NULL and weight is not NULL/unset: set the weight property of the CAPTCHA element
  158. to this value.
  159. For example:
  160. """
  161. /**
  162. * Implementation of hook_captcha_placement_map
  163. */
  164. function hook_captcha_placement_map() {
  165. return array(
  166. 'my_fancy_form' => array(
  167. 'path' => array('items', 'buttons'),
  168. 'key' => 'savebutton',
  169. ),
  170. 'another_form' => array(
  171. 'path' => array(),
  172. 'weight' => 34,
  173. ),
  174. );
  175. }
  176. """
  177. This will place the CAPTCHA element
  178. - in the 'my_fancy_form' form inside the container $form['items']['buttons'],
  179. just before the element $form['items']['buttons']['sacebutton'].
  180. - in the 'another_form' form at the toplevel of the form, with a weight 34.