summaryrefslogtreecommitdiffstats
path: root/math_captcha/math_captcha.module
blob: 52a7cfc5542210d05ced98e8be0a9d2f718543b4 (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
<?php

/**
 * Implementation of a math CAPTCHA, for use with the CAPTCHA module
 */

/**
 * Implementation of hook_menu().
 */
function math_captcha_menu() {
  $items = array();
  $items['admin/user/captcha/math_captcha'] = array(
    'title' => 'Math CAPTCHA',
    'file' => 'math_captcha.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('math_captcha_settings_form'),
    'access arguments' => array('administer CAPTCHA settings'),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Function for getting the enabled challenges (e.g. for use as checkboxes default_value)
 */
function _math_captcha_enabled_challenges() {
  $enabled = variable_get('math_captcha_enabled_challenges', array(
    'addition' => 'addition',
    'subtraction' => 'subtraction',
    'multiplication' => 'multiplication'
  ));
  return $enabled;
}

/**
 * Implementation of hook_captcha().
 */
function math_captcha_captcha($op, $captcha_type='') {
  switch ($op) {
    case 'list':
      return array('Math CAPTCHA');
    case 'generate':
      require_once('math_captcha.challenge.inc');
      if ($captcha_type == 'Math CAPTCHA') {
        // get the available challenges
        $challenges = array_filter(_math_captcha_enabled_challenges());
        $challenge = $challenges[array_rand($challenges)];
        $form_item = call_user_func("_math_captcha_{$challenge}_challenge");
        return $form_item;
      }
  }
}