You are here

captcha_api.txt in CAPTCHA 6

Same filename and directory in other branches
  1. 5.3 captcha_api.txt
  2. 6.2 captcha_api.txt
  3. 7 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.

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"'),
        );
        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/user/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/user/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/user/captcha/foo_captcha':
      return '<p>'. t('This is a very simple challenge, which requires users to enter "foo" in a textfield.') .'</p>';
  }
}
"""


=== Optional: preprocess the response ===
In some situations it could be necessary to preprocess the response before
letting the CAPTCHA module validate it. For example: if you want the validation
to be case insensitive, you could convert the reponse to lower case.
To enable response preprocessing:

* Add a non zero 'preprocess' entry to $captcha in the 'generate' part of
  hook_captcha(). E.g.:

"""
function foo_captcha_captcha($op, $captcha_type='') {
  switch($op) {
    ...
    case 'generate':
      if ($captcha_type == 'Foo CAPTCHA') {
        ...
        $captcha['preprocess'] = TRUE,
        ...
"""

* Add a 'preprocess' operation to hook_captcha() and add a $response argument
  to the function signature. E.g.:

"""
function foo_captcha_captcha($op, $captcha_type='', $response='') {
  switch($op) {
    ...
    case 'preprocess':
      if ($captcha_type == 'Foo CAPTCHA') {
        return strtolower($response);
      }
      break;
    ...
"""

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. Let's give a simple example to make this more clear.
  18. We create the challenge 'Foo CAPTCHA', which requires the user to
  19. enter "foo" in a textfield.
  20. """
  21. /**
  22. * Implementation of hook_captcha().
  23. */
  24. function foo_captcha_captcha($op, $captcha_type='') {
  25. switch ($op) {
  26. case 'list':
  27. return array('Foo CAPTCHA');
  28. case 'generate':
  29. if ($captcha_type == 'Foo CAPTCHA') {
  30. $captcha = array();
  31. $captcha['solution'] = 'foo';
  32. $captcha['form']['captcha_response'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Enter "foo"'),
  35. );
  36. return $captcha;
  37. }
  38. break;
  39. }
  40. }
  41. """
  42. Validation of the answer against the solution and other stuff is done by the
  43. base CAPTCHA module.
  44. === Required: the .info file ===
  45. You should specify that your module depends on the base CAPTCHA module.
  46. Optionally you could put your module in the "Spam control" package.
  47. For our simple foo CAPTCHA module this would mean the following lines in the
  48. file foo_captcha.info:
  49. """
  50. name = "Foo CAPTCHA"
  51. description = "The foo CAPTCHA requires the user to enter the word 'foo'."
  52. package = "Spam control"
  53. dependencies = captcha
  54. core = 6.x
  55. """
  56. === Recommended: hook_menu($may_cache) ===
  57. More advanced CAPTCHA modules probably want some configuration page.
  58. To integrate nicely with the base CAPTCHA module you should offer your
  59. configuration page as a MENU_LOCAL_TASK menu entry under 'admin/user/captcha/'.
  60. For our simple foo CAPTCHA module this would mean:
  61. """
  62. /**
  63. * Implementation of hook_menu().
  64. */
  65. function foo_captcha_menu($may_cache) {
  66. $items = array();
  67. if ($may_cache) {
  68. $items['admin/user/captcha/foo_captcha'] = array(
  69. 'title' => t('Foo CAPTCHA'),
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('foo_captcha_settings_form'),
  72. 'type' => MENU_LOCAL_TASK,
  73. );
  74. }
  75. return $items;
  76. }
  77. """
  78. You should of course implement a function foo_captcha_settings_form() which
  79. returns the form of your configuration page.
  80. === Optional: hook_help($section) ===
  81. To offer a description/explanation of your challenge, you can use the
  82. normal hook_help() system.
  83. For our simple foo CAPTCHA module this would mean:
  84. """
  85. /**
  86. * Implementation of hook_help().
  87. */
  88. function foo_captcha_help($path, $arg) {
  89. switch ($path) {
  90. case 'admin/user/captcha/foo_captcha':
  91. return '

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

    ';
  92. }
  93. }
  94. """
  95. === Optional: preprocess the response ===
  96. In some situations it could be necessary to preprocess the response before
  97. letting the CAPTCHA module validate it. For example: if you want the validation
  98. to be case insensitive, you could convert the reponse to lower case.
  99. To enable response preprocessing:
  100. * Add a non zero 'preprocess' entry to $captcha in the 'generate' part of
  101. hook_captcha(). E.g.:
  102. """
  103. function foo_captcha_captcha($op, $captcha_type='') {
  104. switch($op) {
  105. ...
  106. case 'generate':
  107. if ($captcha_type == 'Foo CAPTCHA') {
  108. ...
  109. $captcha['preprocess'] = TRUE,
  110. ...
  111. """
  112. * Add a 'preprocess' operation to hook_captcha() and add a $response argument
  113. to the function signature. E.g.:
  114. """
  115. function foo_captcha_captcha($op, $captcha_type='', $response='') {
  116. switch($op) {
  117. ...
  118. case 'preprocess':
  119. if ($captcha_type == 'Foo CAPTCHA') {
  120. return strtolower($response);
  121. }
  122. break;
  123. ...
  124. """