Projet

Général

Profil

Révision 211d6cee

Ajouté par Assos Assos il y a environ 6 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/hidden_captcha/hidden_captcha.module
2 2

  
3 3
/**
4 4
 * @file
5
 * All code for the Foo Bar module, except installation-related code.
5
 * All code for the Hidden CAPTCHA module, except installation-related code.
6 6
 */
7 7

  
8 8
/**
......
26 26
    'page arguments' => array('hidden_captcha_settings_form'),
27 27
    'access arguments' => array('administer CAPTCHA settings'),
28 28
    'type' => MENU_LOCAL_TASK,
29
    'weight' => -10
29
    'weight' => -10,
30 30
  );
31 31
  return $items;
32 32
}
......
39 39
  $form['hidden_captcha_label'] = array(
40 40
    '#type' => 'textfield',
41 41
    '#title' => t('Hidden text field label'),
42
    '#description' => t(
43
      "This is the hidden captcha form field's label, describing the expected input.<br />" .
44
      "<strong>It is highly recommended to change it!</strong><br />" .
45
      "The label should be as \"machine-readable\" as possible, encouraging spambots to fill in the field. An example might be a simple math challenge.<br />" .
46
      "The label will only be visible to people who do not have CSS enabled and to robots."
47
    ),
42
    '#description' => t("This is the hidden captcha form field's label, describing the expected input.<br /><strong>It is highly recommended to change it!</strong><br />The label should be as \"machine-readable\" as possible, encouraging spambots to fill in the field. An example might be a simple math challenge.<br />The label will only be visible to people who do not have CSS enabled and to robots."),
48 43
    '#default_value' => variable_get('hidden_captcha_label', 'Website URL'),
49 44
  );
50 45
  return system_settings_form($form);
......
55 50
 */
56 51
function hidden_captcha_captcha($op, $captcha_type = '') {
57 52
  switch ($op) {
58
    case 'list': return array('Hidden CAPTCHA');
53
    case 'list':
54
      return array('Hidden CAPTCHA');
55

  
59 56
    case 'generate':
60 57
      if ($captcha_type == 'Hidden CAPTCHA') {
61 58
        $captcha = array();
62 59
        $captcha['solution'] = '';
60

  
61
        // Ensure this captcha is cacheable.
62
        // @see https://www.drupal.org/project/captcha/issues/2449209
63
        $captcha['cacheable'] = TRUE;
64

  
65
        // The CAPTCHA solution provided by hook_captcha() is updated in the
66
        // 'captcha_session' table on pre-render phase of element process.
67
        // @see captcha_pre_render_process()
68
        // @see _captcha_update_captcha_session()
69
        // However, if the cache is enabled, solution would be empty for the
70
        // first attempt of the cached form. For the next attempt, there will
71
        // not be any 'sid' in form_state
72
        // (i.e. $form_state['captcha_info']['captcha_sid'] = null).
73
        // So a new solution would get generated at captcha_element_process().
74
        // The default 'captcha_validate' function,
75
        // captcha_validate_strict_equality(), would fail in this case.
76
        // @see captcha_validate()
77
        // Adding custom validation function to check solution is always empty.
78
        $captcha['captcha_validate'] = 'hidden_captcha_captcha_validate';
79

  
63 80
        $captcha['form']['captcha_response'] = array(
64 81
          '#type' => 'textfield',
65 82
          '#title' => variable_get('hidden_captcha_label', 'Website URL'),
......
72 89
  }
73 90
}
74 91

  
92
/**
93
 * CAPTCHA validation callback; checks that the response is empty.
94
 */
95
function hidden_captcha_captcha_validate($solution, $captcha_response, $element, $form_state) {
96
  return $captcha_response === '';
97
}
98

  
75 99
/**
76 100
 * Implements hook_theme().
77 101
 */
......
79 103
  return array(
80 104
    'captcha' => array(
81 105
      'arguments' => array('element' => NULL),
82
      'function' => 'theme_hidden_captcha_captcha'
83
    )
106
      'function' => 'theme_hidden_captcha_captcha',
107
    ),
84 108
  );
85 109
}
86 110

  
......
90 114
function theme_hidden_captcha_captcha($element) {
91 115
  $captcha = theme_captcha($element);
92 116
  if (strncmp($element["element"]["#captcha_type"], "hidden_captcha/", 15) == 0) {
93
    //generate a random class name
117
    // Generate a random class name.
94 118
    $chars = "abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
95 119
    $class = "";
96
    for ($i = 0; $i < 64; ++$i) $class .= substr($chars, rand(0, strlen($chars)-1), 1);
97
    //hide the random class via css
98
    drupal_add_css(".$class{width:0;height:0;overflow:hidden;}", "inline"); // TODO: move the random class to an external file
99
    //html for the captcha
120
    for ($i = 0; $i < 64; ++$i) {
121
      $class .= substr($chars, rand(0, strlen($chars) - 1), 1);
122
    }
123
    // Hide the random class via CSS.
124
    // @todo Move the random class to an external file.
125
    drupal_add_css(".$class{width:0;height:0;overflow:hidden;}", "inline");
126
    // HTML for the captcha.
100 127
    $captcha = "<div class=\"$class\">" . $captcha . "</div>";
101 128
  }
102 129
  return $captcha;

Formats disponibles : Unified diff