You are here

honeypot.install in Honeypot 7

Same filename and directory in other branches
  1. 8 honeypot.install
  2. 6 honeypot.install
  3. 2.0.x honeypot.install

Install, update and uninstall functions for the Honeypot module.

File

honeypot.install
View source
<?php

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

/**
 * Implements hook_schema().
 */
function honeypot_schema() {
  $schema['honeypot_user'] = array(
    'description' => 'Table that stores failed attempts to submit a form.',
    'fields' => array(
      'uid' => array(
        'description' => 'Foreign key to {users}.uid; uniquely identifies a Drupal user to whom this ACL data applies.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'hostname' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'description' => 'Hostname of user that that triggered honeypot.',
      ),
      'timestamp' => array(
        'description' => 'Date/time when the form submission failed, as Unix timestamp.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
      'timestamp' => array(
        'timestamp',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function honeypot_install() {

  // Create CSS file.
  honeypot_create_css(variable_get('honeypot_element_name', 'url'));
  if (!drupal_is_cli()) {
    $t = get_t();
    drupal_set_message($t("Honeypot installed successfully. Please !link to protect your forms from spam bots.", array(
      '!link' => l($t('configure Honeypot'), 'admin/config/content/honeypot'),
    )));
  }
}

/**
 * Implements hook_uninstall().
 */
function honeypot_uninstall() {
  db_delete('variable')
    ->condition('name', db_like('honeypot_') . '%', 'LIKE')
    ->execute();
  $cache_tables = array(
    'variables',
    'cache_bootstrap',
  );
  foreach ($cache_tables as $table) {
    if (db_table_exists($table)) {
      cache_clear_all($table, 'cache');
    }
  }

  // Delete 'honeypot' directory from files directory.
  drupal_load('module', 'honeypot');
  file_unmanaged_delete_recursive(honeypot_file_default_scheme() . '://honeypot');
}

/**
 * Implements hook_update_N().
 */
function honeypot_update_7001() {
  $ret = array();

  // Leaving this in because I had it in version 1.3. Silly me.
  return $ret;
}

/**
 * Update form names after upgrade from 6.x version.
 */
function honeypot_update_7002() {
  $map = array(
    'user_register' => 'user_register_form',
    'contact_mail_page' => 'contact_site_form',
    'contact_mail_user' => 'contact_personal_form',
  );
  foreach ($map as $d6_name => $d7_name) {
    $value = variable_get('honeypot_form_' . $d6_name, 0);
    if ($value) {
      variable_set('honeypot_form_' . $d7_name, $value);
    }
    variable_del('honeypot_form_' . $d6_name);
  }
  $comment_form_value = variable_get('honeypot_form_comment_form', 0);
  if ($comment_form_value) {
    $types = node_type_get_types();
    if (!empty($types)) {
      foreach ($types as $type) {
        $d7_name = 'honeypot_form_comment_node_' . $type->type . '_form';
        variable_set($d7_name, $comment_form_value);
      }
    }
  }
  variable_del('honeypot_form_comment_form');
}

/**
 * Add {honeypot_users} database table if it doesn't exist.
 */
function honeypot_update_7003() {

  // Make sure the {honeypot_users} table doesn't already exist.
  if (!db_table_exists('honeypot_user')) {
    $table = array(
      'description' => 'Table that stores failed attempts to submit a form.',
      'fields' => array(
        'uid' => array(
          'description' => 'Foreign key to {users}.uid; uniquely identifies a Drupal user to whom this ACL data applies.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'timestamp' => array(
          'description' => 'Date/time when the form submission failed, as Unix timestamp.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
      ),
      'indexes' => array(
        'uid' => array(
          'uid',
        ),
        'timestamp' => array(
          'timestamp',
        ),
      ),
    );
    db_create_table('honeypot_user', $table);
  }
}

/**
 * Create Honeypot CSS file.
 */
function honeypot_update_7004() {
  drupal_load('module', 'honeypot');
  module_load_include('inc', 'honeypot', 'honeypot.admin');
  honeypot_create_css(variable_get('honeypot_element_name', 'url'));
}

/**
 * Adds the 'hostname' column to the {honeypot_user} table.
 */
function honeypot_update_7100() {
  $schema = honeypot_schema();
  $spec = $schema['honeypot_user']['fields']['hostname'];
  $spec['initial'] = '';
  db_add_field('honeypot_user', 'hostname', $spec);
}

/**
 * Transfer previous honeypot trigger info from {flood} to {honeypot_user}.
 */
function honeypot_update_7101() {

  // Move all 'honeypot' events, which are honeypot captures for anonymous
  // users, to the {honeypot_user}-table, since the latter now supports
  // tracking based on ip/hostname for anonymous users.
  $query = db_select('flood', 'f')
    ->condition('event', 'honeypot');
  $query
    ->addExpression('0', 'uid');
  $query
    ->addField('f', 'identifier', 'hostname');
  $query
    ->addField('f', 'timestamp');
  db_insert('honeypot_user')
    ->from($query)
    ->execute();

  // Clean up the flood table by removing our events, since we are no longer
  // relying on the flood mechanism to track anonymous honeypot captures.
  flood_clear_event('honeypot');
}

Functions

Namesort descending Description
honeypot_install Implements hook_install().
honeypot_schema Implements hook_schema().
honeypot_uninstall Implements hook_uninstall().
honeypot_update_7001 Implements hook_update_N().
honeypot_update_7002 Update form names after upgrade from 6.x version.
honeypot_update_7003 Add {honeypot_users} database table if it doesn't exist.
honeypot_update_7004 Create Honeypot CSS file.
honeypot_update_7100 Adds the 'hostname' column to the {honeypot_user} table.
honeypot_update_7101 Transfer previous honeypot trigger info from {flood} to {honeypot_user}.