You are here

botcha.install in BOTCHA Spam Prevention 6

File

botcha.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the BOTCHA module.
 */
include_once "botcha.module";

// for _botcha_variables()

/**
 * Implementation of hook_schema().
 */
function botcha_schema() {

  // Table for assigning recipe cookbooks to the forms.
  $schema['botcha_points'] = array(
    'description' => 'This table describes which recipe cookbooks should be used to modify which forms.',
    'fields' => array(
      'form_id' => array(
        'description' => 'The form_id of the form to add a BOTCHA to.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'botcha_type' => array(
        'description' => 'The recipe cookbook to use.',
        'type' => 'varchar',
        'length' => 64,
      ),
    ),
    'primary key' => array(
      'form_id',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_requirements().
 */
function botcha_requirements($phase) {
  $requirements = array();
  $t = get_t();
  if ($phase == 'runtime') {

    // Just clean up -dev variables that were renamed

    //FIXME: decrement this for release: 1

    //FIXME: remove the below for release when the above is 0
    if (variable_get('botcha_form_pass_counter', 0) > 0) {
      variable_set('botcha_form_passed_counter', variable_get('botcha_form_passed_counter', 0) + variable_get('botcha_form_pass_counter', 0));
      variable_del('botcha_form_pass_counter');
    }
    if (variable_get('botcha_wrong_response_counter', 0) > 0) {
      variable_set('botcha_form_blocked_counter', variable_get('botcha_form_blocked_counter', 0) + variable_get('botcha_wrong_response_counter', 0));
      variable_del('botcha_wrong_response_counter');
    }
    $block_cnt = variable_get('botcha_form_blocked_counter', 0);
    $build_cnt = variable_get('botcha_form_passed_counter', 0) + $block_cnt;

    // Show statistic counters in the status report.
    $requirements['botcha_statistics'] = array(
      'title' => $t('BOTCHA'),
      'value' => format_plural($block_cnt, 'Already 1 blocked form submission', 'Already @count blocked form submissions') . ($build_cnt > 0 ? ' ' . $t('(!percent% of total !build_cnt processed)', array(
        '!percent' => sprintf("%0.3f", 100 * $block_cnt / $build_cnt),
        '!build_cnt' => $build_cnt,
      )) : ''),
      'severity' => REQUIREMENT_INFO,
    );
  }
  return $requirements;
}
function _botcha_default_form_ids() {

  // Some default BOTCHA points.
  $form_ids = array(
    'user_pass',
    'user_login',
    'user_login_block',
    'user_register',
    'contact_mail_page',
    'contact_mail_user',
    'comment_form',
    'forum_node_form',
  );
  return $form_ids;
}

/**
 * Implementation of hook_install().
 */
function botcha_install() {
  $t = get_t();
  drupal_install_schema('botcha');

  // Insert some default BOTCHA points.
  $form_ids = _botcha_default_form_ids();
  foreach ($form_ids as $form_id) {
    db_query("INSERT INTO {botcha_points} (form_id, botcha_type) VALUES ('%s', 'default')", $form_id);
  }
  $i18n_variables = variable_get('i18n_variables', '');
  if (!is_array($i18n_variables)) {
    $i18n_variables = array();
  }
  $i18n_variables = array_merge($i18n_variables, _botcha_variables(TRUE));
  variable_set('i18n_variables', $i18n_variables);

  // Explain to users that page caching may be disabled.
  if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
    drupal_set_message($t('Note that BOTCHA module disables <a href="!performance_admin">page caching</a> of pages that include forms processed by BOTCHA. ', array(
      '!performance_admin' => url('admin/settings/performance'),
    )), 'warning');
  }

  // Generate unique secret for this site
  variable_set('botcha_secret', md5(uniqid(mt_rand(), TRUE)));

  // Ensure statistics variables exist
  variable_set('botcha_form_passed_counter', variable_get('botcha_form_passed_counter', 0));
  variable_set('botcha_form_blocked_counter', variable_get('botcha_form_blocked_counter', 0));
}

/**
 * Implementation of hook_uninstall().
 */
function botcha_uninstall() {
  drupal_uninstall_schema('botcha');
  db_query("DELETE FROM {variable} WHERE name LIKE 'botcha_%'");

  //  foreach (_botcha_variables() as $var) { variable_del($var); }
  $i18n_variables = variable_get('i18n_variables', '');
  if (is_array($i18n_variables)) {
    $i18n_variables = array_diff($i18n_variables, _botcha_variables());
    variable_set('i18n_variables', $i18n_variables);
  }
  cache_clear_all('variables', 'cache');
}

/**
 * Implementation of hook_update_N()
 * Create new 'botcha_points' table and fill it in with defaults
 */
function botcha_update_6001() {
  $items = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $items[] = update_sql("CREATE TABLE {botcha_points} (\n        form_id varchar(128) NOT NULL,\n        botcha_type varchar(64) default NULL,\n        PRIMARY KEY (form_id)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $succes = TRUE;
      break;
    case 'pgsql':
      $items[] = update_sql("CREATE TABLE {botcha_points} (\n        form_id varchar(128) NOT NULL,\n        botcha_type varchar(64) default NULL,\n        PRIMARY KEY (form_id)\n        );");
      $succes = TRUE;
      break;
    default:
      drupal_set_message(t('Unsupported database.'), 'error');
      $succes = FALSE;
      break;
  }
  if ($succes) {

    // insert some defaults
    $form_ids = _botcha_default_form_ids();
    foreach ($form_ids as $form_id) {
      $items[] = update_sql("INSERT INTO {botcha_points} (form_id, botcha_type) VALUES ('{$form_id}', 'default')");
    }

    // Clear the cache to get updates to menu router and themes.
    cache_clear_all();
  }
  return $items;
}

Functions

Namesort descending Description
botcha_install Implementation of hook_install().
botcha_requirements Implementation of hook_requirements().
botcha_schema Implementation of hook_schema().
botcha_uninstall Implementation of hook_uninstall().
botcha_update_6001 Implementation of hook_update_N() Create new 'botcha_points' table and fill it in with defaults
_botcha_default_form_ids