Projet

Général

Profil

Révision ac1bc5de

Ajouté par Assos Assos il y a plus de 9 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/captcha/captcha.module
46 46
      ) . '</p>';
47 47
      $output .= '<p>' . t('CAPTCHA is a trademark of Carnegie Mellon University.') . '</p>';
48 48
      return $output;
49

  
49 50
    case 'admin/config/people/captcha':
50 51
    case 'admin/config/people/captcha/captcha':
51 52
    case 'admin/config/people/captcha/captcha/settings':
......
56 57
}
57 58

  
58 59
/**
59
 * Implementation of hook_menu().
60
 * Implements of hook_menu().
60 61
 */
61 62
function captcha_menu() {
62 63
  $items = array();
63
  // main configuration page of the basic CAPTCHA module
64
  // Main configuration page of the basic CAPTCHA module.
64 65
  $items['admin/config/people/captcha'] = array(
65 66
    'title' => 'CAPTCHA',
66 67
    'description' => 'Administer how and where CAPTCHAs are used.',
......
70 71
    'access arguments' => array('administer CAPTCHA settings'),
71 72
    'type' => MENU_NORMAL_ITEM,
72 73
  );
73
  // the default local task (needed when other modules want to offer
74
  // alternative CAPTCHA types and their own configuration page as local task)
74
  // The default local task (needed when other modules want to offer,
75
  // alternative CAPTCHA types and their own configuration page as local task).
75 76
  $items['admin/config/people/captcha/captcha'] = array(
76 77
    'title' => 'CAPTCHA',
77 78
    'access arguments' => array('administer CAPTCHA settings'),
......
106 107
}
107 108

  
108 109
/**
109
 * Implementation of hook_permission().
110
 * Implements of hook_permission().
110 111
 */
111 112
function captcha_permission() {
112 113
  return array(
......
121 122
}
122 123

  
123 124
/**
124
 * Implementation of hook_theme().
125
 * Implements of hook_theme().
125 126
 */
126 127
function captcha_theme() {
127 128
  return array(
......
135 136
}
136 137

  
137 138
/**
138
 * Implementation of hook_cron().
139
 * Implements of hook_cron().
139 140
 *
140 141
 * Remove old entries from captcha_sessions table.
141 142
 */
142 143
function captcha_cron() {
143 144
  // Remove challenges older than 1 day.
144 145
  db_delete('captcha_sessions')
145
    ->condition('timestamp', REQUEST_TIME - 60*60*24, '<')
146
    ->condition('timestamp', REQUEST_TIME - 60 * 60 * 24, '<')
146 147
    ->execute();
147 148
}
148 149

  
149

  
150 150
/**
151
 * Implementation of hook_element_info().
151
 * Implements of hook_element_info().
152 152
 */
153 153
function captcha_element_info() {
154 154
  // Define the CAPTCHA form element with default properties.
155 155
  $captcha_element = array(
156 156
    '#input' => TRUE,
157 157
    '#process' => array('captcha_element_process'),
158
    // The type of challenge: e.g. 'default', 'none', 'captcha/Math', 'image_captcha/Image', ...
158
    // The type of challenge: e.g. 'default', 'none', 'captcha/Math', 'image_captcha/Image'.
159 159
    '#captcha_type' => 'default',
160 160
    '#default_value' => '',
161 161
    // CAPTCHA in admin mode: presolve the CAPTCHA and always show it (despite previous successful responses).
......
254 254
    'captcha_sid' => $captcha_sid,
255 255
  );
256 256

  
257

  
258 257
  if (_captcha_required_for_user($captcha_sid, $this_form_id) || $element['#captcha_admin_mode']) {
259 258
    // Generate a CAPTCHA and its solution
260 259
    // (note that the CAPTCHA session ID is given as third argument).
......
263 262
      // The selected module did not return what we expected: log about it and quit.
264 263
      watchdog('CAPTCHA',
265 264
        'CAPTCHA problem: unexpected result from hook_captcha() of module %module when trying to retrieve challenge type %type for form %form_id.',
266
        array('%type' => $captcha_type_challenge, '%module' => $captcha_type_module, '%form_id' => $this_form_id),
265
        array(
266
          '%type' => $captcha_type_challenge,
267
          '%module' => $captcha_type_module,
268
          '%form_id' => $this_form_id,
269
        ),
267 270
        WATCHDOG_ERROR);
268 271
      return $element;
269 272
    }
......
300 303
  return $element;
301 304
}
302 305

  
303

  
304 306
/**
305 307
 * Theme function for a CAPTCHA element.
306 308
 *
......
324 326
  }
325 327
}
326 328

  
327

  
328 329
/**
329
 * Implementation of hook_form_alter().
330
 * Implements of hook_form_alter().
330 331
 *
331 332
 * This function adds a CAPTCHA to forms for untrusted users if needed and adds
332 333
 * CAPTCHA administration links for site administrators if this option is enabled.
......
339 340

  
340 341
    // Get CAPTCHA type and module for given form_id.
341 342
    $captcha_point = captcha_get_form_id_setting($form_id);
342
    if ($captcha_point && $captcha_point->captcha_type) {
343
    if ($captcha_point && !empty($captcha_point->captcha_type)) {
343 344
      module_load_include('inc', 'captcha');
344 345
      // Build CAPTCHA form element.
345 346
      $captcha_element = array(
......
365 366
    module_load_include('inc', 'captcha');
366 367

  
367 368
    $captcha_point = captcha_get_form_id_setting($form_id);
368
    // For administrators: show CAPTCHA info and offer link to configure it
369
    // For administrators: show CAPTCHA info and offer link to configure it.
369 370
    $captcha_element = array(
370 371
      '#type' => 'fieldset',
371 372
      '#title' => t('CAPTCHA'),
......
424 425

  
425 426
  // Add a warning about caching on the Perfomance settings page.
426 427
  if ($form_id == 'system_performance_settings') {
427
    $icon = theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('warning'), 'title' => t('warning')));
428
    $icon = theme(
429
      'image',
430
      array(
431
        'path' => 'misc/watchdog-warning.png',
432
        'width' => 18,
433
        'height' => 18,
434
        'alt' => t('warning'),
435
        'title' => t('warning'),
436
      )
437
    );
428 438
    $form['caching']['captcha'] = array(
429 439
      '#type' => 'item',
430 440
      '#title' => t('CAPTCHA'),
......
439 449

  
440 450
/**
441 451
 * CAPTCHA validation function to tests strict equality.
442
 * @param $solution the solution of the test.
443
 * @param $response the response to the test.
444
 * @return TRUE when strictly equal, FALSE otherwise.
452
 *
453
 * @param string $solution
454
 *   the solution of the test.
455
 *
456
 * @param string $response
457
 *   the response to the test.
458
 *
459
 * @return bool
460
 *   TRUE when equal (ignoring spaces), FALSE otherwise.
445 461
 */
446 462
function captcha_validate_strict_equality($solution, $response) {
447 463
  return $solution === $response;
......
449 465

  
450 466
/**
451 467
 * CAPTCHA validation function to tests case insensitive equality.
452
 * @param $solution the solution of the test.
453
 * @param $response the response to the test.
454
 * @return TRUE when case insensitive equal, FALSE otherwise.
468
 *
469
 * @param string $solution
470
 *   the solution of the test.
471
 *
472
 * @param string $response
473
 *   the response to the test.
474
 *
475
 * @return bool
476
 *   TRUE when equal (ignoring spaces), FALSE otherwise.
455 477
 */
456 478
function captcha_validate_case_insensitive_equality($solution, $response) {
457 479
  return drupal_strtolower($solution) === drupal_strtolower($response);
......
459 481

  
460 482
/**
461 483
 * CAPTCHA validation function to tests equality while ignoring spaces.
462
 * @param $solution the solution of the test.
463
 * @param $response the response to the test.
464
 * @return TRUE when equal (ignoring spaces), FALSE otherwise.
484
 *
485
 * @param string $solution
486
 *   the solution of the test.
487
 *
488
 * @param string $response
489
 *   the response to the test.
490
 *
491
 * @return bool
492
 *   TRUE when equal (ignoring spaces), FALSE otherwise.
465 493
 */
466 494
function captcha_validate_ignore_spaces($solution, $response) {
467 495
  return preg_replace('/\s/', '', $solution) === preg_replace('/\s/', '', $response);
......
469 497

  
470 498
/**
471 499
 * CAPTCHA validation function to tests case insensitive equality while ignoring spaces.
472
 * @param $solution the solution of the test.
473
 * @param $response the response to the test.
474
 * @return TRUE when equal (ignoring spaces), FALSE otherwise.
500
 *
501
 * @param string $solution
502
 *   the solution of the test.
503
 *
504
 * @param string $response
505
 *   the response to the test.
506
 *
507
 * @return bool
508
 *   TRUE when equal (ignoring spaces), FALSE otherwise.
475 509
 */
476 510
function captcha_validate_case_insensitive_ignore_spaces($solution, $response) {
477 511
  return preg_replace('/\s/', '', drupal_strtolower($solution)) === preg_replace('/\s/', '', drupal_strtolower($response));
478 512
}
479 513

  
480 514
/**
481
 * Helper function for getting the posted CAPTCHA info (posted form_id and
482
 * CAPTCHA sessions ID) from a form in case it is posted.
515
 * Helper function for getting the posted CAPTCHA info (posted form_id and CAPTCHA sessions ID) from a form in case it is posted.
483 516
 *
484 517
 * This function hides the form processing mess for several use cases an
485 518
 * browser bug workarounds.
......
494 527
 *
495 528
 * @todo for Drupal 7 version: is this IE7 workaround still needed?
496 529
 *
497
 * @param $element the CAPTCHA element.
498
 * @param $form_state the form state structure to extract the info from.
499
 * @param $this_form_id the form ID of the form we are currently processing
530
 * @param array $element
531
 *   the CAPTCHA element.
532
 *
533
 * @param array $form_state
534
 *   the form state structure to extract the info from.
535
 *
536
 * @param string $this_form_id
537
 *   the form ID of the form we are currently processing
500 538
 *     (which is not necessarily the form that was posted).
501 539
 *
502
 * @return an array with $posted_form_id and $post_captcha_sid (with NULL values
540
 * @return array
541
 *   an array with $posted_form_id and $post_captcha_sid (with NULL values
503 542
 *     if the values could not be found, e.g. for a fresh form).
504 543
 */
505 544
function _captcha_get_posted_captcha_info($element, $form_state, $this_form_id) {
......
515 554
    // from the post data.
516 555
    // Because we possibly use raw post data here,
517 556
    // we should be extra cautious and filter this data.
518
    $posted_form_id = isset($form_state['input']['form_id']) ?
519
      preg_replace("/[^a-z0-9_]/", "", (string) $form_state['input']['form_id'])
520
      : NULL;
521
    $posted_captcha_sid = isset($form_state['input']['captcha_sid']) ?
522
      (int) $form_state['input']['captcha_sid']
523
      : NULL;
524
    $posted_captcha_token = isset($form_state['input']['captcha_token']) ?
525
      preg_replace("/[^a-zA-Z0-9]/", "", (string) $form_state['input']['captcha_token'])
526
      : NULL;
557
    $posted_form_id = isset($form_state['input']['form_id']) ? preg_replace("/[^a-z0-9_]/", "", (string) $form_state['input']['form_id']) : NULL;
558
    $posted_captcha_sid = isset($form_state['input']['captcha_sid']) ? (int) $form_state['input']['captcha_sid'] : NULL;
559
    $posted_captcha_token = isset($form_state['input']['captcha_token']) ? preg_replace("/[^a-zA-Z0-9]/", "", (string) $form_state['input']['captcha_token']) : NULL;
527 560

  
528 561
    if ($posted_form_id == $this_form_id) {
529 562
      // Check if the posted CAPTCHA token is valid for the posted CAPTCHA
......
599 632
    // Get CAPTCHA validate function or fall back on strict equality.
600 633
    $captcha_validate = $element['#captcha_validate'];
601 634
    if (!function_exists($captcha_validate)) {
602
       $captcha_validate = 'captcha_validate_strict_equality';
635
      $captcha_validate = 'captcha_validate_strict_equality';
603 636
    }
604 637
    // Check the response with the CAPTCHA validation function.
605 638
    // Apart from the traditional expected $solution and received $response,
......
626 659
        ->condition('csid', $csid)
627 660
        ->expression('attempts', 'attempts + 1')
628 661
        ->execute();
629
      // set form error
662
      // Set form error.
630 663
      form_set_error('captcha_response', t('The answer you entered for the CAPTCHA was not correct.'));
631
      // update wrong response counter
664
      // Update wrong response counter.
632 665
      if (variable_get('captcha_enable_stats', FALSE)) {
633 666
        variable_set('captcha_wrong_response_counter', variable_get('captcha_wrong_response_counter', 0) + 1);
634 667
      }
635
      // log to watchdog if needed
668
      // Log to watchdog if needed.
636 669
      if (variable_get('captcha_log_wrong_responses', FALSE)) {
637 670
        watchdog('CAPTCHA',
638 671
          '%form_id post blocked by CAPTCHA module: challenge %challenge (by module %module), user answered "@response", but the solution was "@solution".',
639
          array('%form_id' => $form_id,
672
          array(
673
            '%form_id' => $form_id,
640 674
            '@response' => $captcha_response, '@solution' => $solution,
641 675
            '%challenge' => $captcha_info['captcha_type'], '%module' => $captcha_info['module'],
642 676
          ),
......
652 686
 * This encompasses tasks that should happen after the general FAPI processing
653 687
 * (building, submission and validation) but before rendering (e.g. storing the solution).
654 688
 *
655
 * @param $element the CAPTCHA form element
656
 * @return the manipulated element
689
 * @param array $element
690
 *   the CAPTCHA form element
691
 *
692
 * @return array
693
 *   the manipulated element
657 694
 */
658 695
function captcha_pre_render_process($element) {
659 696
  module_load_include('inc', 'captcha');
......
661 698
  // Get form and CAPTCHA information.
662 699
  $captcha_info = $element['#captcha_info'];
663 700
  $form_id = $captcha_info['form_id'];
664
  $captcha_sid = (int)($captcha_info['captcha_sid']);
701
  $captcha_sid = (int) ($captcha_info['captcha_sid']);
665 702
  // Check if CAPTCHA is still required.
666 703
  // This check is done in a first phase during the element processing
667 704
  // (@see captcha_process), but it is also done here for better support
......
695 732
  switch ($op) {
696 733
    case 'list':
697 734
      return array('Math');
698
      break;
699 735

  
700 736
    case 'generate':
701 737
      if ($captcha_type == 'Math') {
......
741 777
}
742 778

  
743 779
/**
744
 * Implements hook_modules_enabled.
780
 * Implements hook_modules_enabled().
745 781
 */
746 782
function captcha_modules_enabled() {
747 783
  // When new modules are enabled: clear the CAPTCHA placement cache, so that

Formats disponibles : Unified diff