You are here

captcha.install in CAPTCHA 8

Install, update and uninstall functions for the CAPTCHA module.

File

captcha.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the CAPTCHA module.
 */

/**
 * Implements hook_schema().
 */
function captcha_schema() {
  $schema['captcha_sessions'] = [
    'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
    'fields' => [
      'csid' => [
        'description' => 'CAPTCHA session ID.',
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'token' => [
        'description' => 'One time CAPTCHA token.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => FALSE,
      ],
      'uid' => [
        'description' => "User's {users}.uid.",
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'sid' => [
        'description' => "Session ID of the user.",
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ],
      'ip_address' => [
        'description' => 'IP address of the visitor.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ],
      'timestamp' => [
        'description' => 'A Unix timestamp indicating when the challenge was generated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'form_id' => [
        'description' => 'The form_id of the form where the CAPTCHA is added to.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'solution' => [
        'description' => 'Solution of the challenge.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ],
      'status' => [
        'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'attempts' => [
        'description' => 'The number of attempts.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'csid',
    ],
    'indexes' => [
      'csid_ip' => [
        'csid',
        'ip_address',
      ],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_requirements().
 */
function captcha_requirements($phase) {
  $requirements = [];
  $config = \Drupal::config('captcha.settings');
  if ($phase == 'runtime' && $config
    ->get('enable_stats')) {

    // Show the wrong response counter in the status report.
    $requirements['captcha_wrong_response_counter'] = [
      'title' => \Drupal::translation()
        ->translate('CAPTCHA'),
      'value' => \Drupal::translation()
        ->formatPlural(\Drupal::state()
        ->get('captcha.wrong_response_counter'), 'Already 1 blocked form submission', 'Already @count blocked form submissions'),
      'severity' => REQUIREMENT_INFO,
    ];
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function captcha_install() {
  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();
    }
  }
}

Functions

Namesort descending Description
captcha_install Implements hook_install().
captcha_requirements Implements hook_requirements().
captcha_schema Implements hook_schema().