You are here

spambot.install in Spambot 8

Same filename and directory in other branches
  1. 6.3 spambot.install
  2. 7 spambot.install

Install and update hooks for Spambot module.

File

spambot.install
View source
<?php

/**
 * @file
 * Install and update hooks for Spambot module.
 */

/**
 * Implements hook_schema().
 */
function spambot_schema() {
  $schema['node_spambot'] = [
    'description' => 'Node table to track author IP addresses. For use by spambot only.',
    'fields' => [
      'nid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'uid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'hostname' => [
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ],
    ],
    'primary key' => [
      'nid',
    ],
    'indexes' => [
      'uid' => [
        'uid',
      ],
    ],
  ];
  return $schema;
}

/**
 * Clear all used storage.
 */
function spambot_uninstall() {
  \Drupal::state()
    ->delete('spambot_last_checked_uid');
}

/**
 * Update variables, create new table 'node_spambot'.
 */
function spambot_update_7101() {
  $messages = [];

  // Create new table node_spambot.
  if (!\Drupal::database()
    ->schema()
    ->tableExists('node_spambot')) {
    $node_spambot = [
      'description' => t('Node table to track author IP addresses. For use by spambot only.'),
      'fields' => [
        'nid' => [
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ],
        'uid' => [
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ],
        'hostname' => [
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
        ],
      ],
      'primary key' => [
        'nid',
      ],
      'indexes' => [
        'uid' => [
          'uid',
        ],
      ],
    ];
    \Drupal::database()
      ->schema()
      ->createTable('node_spambot', $node_spambot);
    $messages[] = t('Created new table <em>node_spambot</em>.');
  }
  return implode('<br />', $messages);
}

/**
 * Move spambot_last_checked_uid from config to state (it protects cex, cim).
 */
function spambot_update_8002() {

  /* @var \Drupal\Core\Config\Config $configuration */
  $configuration = \Drupal::service('config.factory')
    ->getEditable('spambot.settings');

  // Move from configuration to state.
  $uid = $configuration
    ->get('spambot_last_checked_uid');
  \Drupal::state()
    ->set('spambot_last_checked_uid', $uid);

  // Drop from configuration.
  $configuration
    ->clear('spambot_last_checked_uid');
  $configuration
    ->save();
}

/**
 * Set default value for new config settings.
 */
function spambot_update_8003() {
  $variables = [
    'spambot_cron_user_limit' => 0,
    'spambot_spam_account_action' => 0,
    'spambot_criteria_email' => 1,
    'spambot_criteria_username' => 0,
    'spambot_criteria_ip' => 20,
    'spambot_blacklisted_delay' => 20,
    'spambot_blocked_message_email' => 'Your email address or username or IP address is blacklisted.',
    'spambot_blocked_message_username' => 'Your email address or username or IP address is blacklisted.',
    'spambot_blocked_message_ip' => 'Your email address or username or IP address is blacklisted.',
  ];

  /* @var \Drupal\Core\Config\Config $configuration */
  $configuration = \Drupal::service('config.factory')
    ->getEditable('spambot.settings');
  $is_updated = FALSE;
  foreach ($variables as $key => $default) {
    $conf = $configuration
      ->get($key);
    if (!isset($conf)) {
      $configuration
        ->set($key, $default);
      $is_updated = TRUE;
    }
  }

  // Save new config.
  if ($is_updated) {
    $configuration
      ->save();
  }
}

/**
 * Update existing whitelists.
 */
function spambot_update_8004() {
  $config = \Drupal::configFactory()
    ->getEditable('spambot.settings');
  $whitelist_email = explode(PHP_EOL, $config
    ->get('spambot_whitelist_email'));
  $whitelist_username = explode(PHP_EOL, $config
    ->get('spambot_whitelist_username'));
  $whitelist_ip = explode(PHP_EOL, $config
    ->get('spambot_whitelist_ip'));
  $config
    ->set('spambot_whitelist_email_list', array_map('trim', array_filter($whitelist_email)))
    ->set('spambot_whitelist_username_list', array_map('trim', array_filter($whitelist_username)))
    ->set('spambot_whitelist_ip_list', array_map('trim', array_filter($whitelist_ip)))
    ->save();
}

Functions

Namesort descending Description
spambot_schema Implements hook_schema().
spambot_uninstall Clear all used storage.
spambot_update_7101 Update variables, create new table 'node_spambot'.
spambot_update_8002 Move spambot_last_checked_uid from config to state (it protects cex, cim).
spambot_update_8003 Set default value for new config settings.
spambot_update_8004 Update existing whitelists.