captcha.install 3.42 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Install, update and uninstall functions for the CAPTCHA module.
 */

8
/**
9
 * Implements hook_schema().
10 11
 */
function captcha_schema() {
12
  $schema['captcha_sessions'] = [
13
    'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
14 15
    'fields' => [
      'csid' => [
16
        'description' => 'CAPTCHA session ID.',
17 18
        'type' => 'serial',
        'not null' => TRUE,
19 20
      ],
      'token' => [
21 22 23 24
        'description' => 'One time CAPTCHA token.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => FALSE,
25 26
      ],
      'uid' => [
27
        'description' => "User's {users}.uid.",
28
        'type' => 'int',
29
        'not null' => TRUE,
30
        'default' => 0,
31 32
      ],
      'sid' => [
33
        'description' => "Session ID of the user.",
34 35 36 37
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
38 39
      ],
      'ip_address' => [
40
        'description' => 'IP address of the visitor.',
41 42 43
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
44 45
      ],
      'timestamp' => [
46
        'description' => 'A Unix timestamp indicating when the challenge was generated.',
47 48 49
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
50 51
      ],
      'form_id' => [
52
        'description' => 'The form_id of the form where the CAPTCHA is added to.',
53 54 55
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
56 57
      ],
      'solution' => [
58
        'description' => 'Solution of the challenge.',
59 60 61 62
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
63 64
      ],
      'status' => [
65
        'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
66 67 68
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
69 70
      ],
      'attempts' => [
71
        'description' => 'The number of attempts.',
72 73 74
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
75 76 77 78 79 80 81
      ],
    ],
    'primary key' => ['csid'],
    'indexes' => [
      'csid_ip' => ['csid', 'ip_address'],
    ],
  ];
82

83 84 85
  return $schema;
}

86
/**
87
 * Implements hook_requirements().
88 89
 */
function captcha_requirements($phase) {
90
  $requirements = [];
91 92
  $config = \Drupal::config('captcha.settings');

tonnosf's avatar
tonnosf committed
93
  if ($phase == 'runtime' && $config->get('enable_stats')) {
94
    // Show the wrong response counter in the status report.
95
    $requirements['captcha_wrong_response_counter'] = [
96
      'title' => \Drupal::translation()->translate('CAPTCHA'),
97
      'value' => \Drupal::translation()->formatPlural(
98
        \Drupal::state()->get('captcha.wrong_response_counter'),
99 100 101 102
        'Already 1 blocked form submission',
        'Already @count blocked form submissions'
      ),
      'severity' => REQUIREMENT_INFO,
103
    ];
104 105 106 107
  }
  return $requirements;
}

108
/**
109
 * Implements hook_install().
110 111
 */
function captcha_install() {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  if (!\Drupal::service('config.installer')->isSyncing()) {
    $form_ids = [];
    // Add form_ids of all currently known node types too.
    foreach (node_type_get_names() as $type => $name) {
      $form_ids[] = 'node_' . $type . '_form';
    }

    $captcha_storage = \Drupal::entityTypeManager()
      ->getStorage('captcha_point');
    foreach ($form_ids as $form_id) {
      $values = [
        'formId' => $form_id,
        'captchaType' => 'default',
        'status' => FALSE,
      ];
      $captcha_storage->create($values)->save();
    }
130
  }
131

132
}