<?php

/**
 * @file
 */

/**
 * Implements hook_install().
 */
function scratchpads_security_install() {
  // Set the Image CAPTCHA options.
  variable_set('image_captcha_code_length', 7);
  variable_set('image_captcha_bilinear_interpolation', TRUE);
  variable_set('image_captcha_distortion_amplitude', 3);
  variable_set('image_captcha_line_noise', 1);
  variable_set('image_captcha_noise_level', 1);
  // Set the CAPTCHA options.
  variable_set('captcha_default_challenge', 'image_captcha/Image');
  // Set the forms that require a CAPTCHA.
  scratchpads_security_flush_caches();
  // Set the Spambot settings.
  variable_set('spambot_blocked_message_email', 'Your email address is blacklisted.');
  variable_set('spambot_blocked_message_ip', 'Your IP address is blacklisted.');
  variable_set('spambot_cron_user_limit', 10);
  variable_set('spambot_check_blocked_accounts', 1);
  variable_set('spambot_spam_account_action', 1);
  variable_set('spambot_criteria_ip', 5);
  variable_set('spambot_criteria_username', 0);
  variable_set('spambot_blacklisted_delay', 5);
  // Set the Honeypot settings.
  variable_set('honeypot_form_user_register_form', 1);
}

/**
 * Implements hook_disable()
 */
function scratchpads_security_disable() {
  // We need to disable the modules once everything else has done. So we register
  // a shutdown function with arguments.
  drupal_register_shutdown_function('module_disable', array(
    'captcha',
  ));
}

/**
 * Implements hook_uninstall()
 */
function scratchpads_security_uninstall() {
  // We need to disable the modules once everything else has done. So we register
  // a shutdown function with arguments.
  drupal_register_shutdown_function('drupal_uninstall_modules', array(
    'captcha',
  ));
}

/**
 * Enable the spambot module.
 */
function scratchpads_security_update_7001() {
  module_enable(array(
    'spambot',
  ));
  // Set the Spambot settings.
  variable_set('spambot_blocked_message_email', 'Your email address is blacklisted.');
  variable_set('spambot_blocked_message_ip', 'Your IP address is blacklisted.');
  variable_set('spambot_cron_user_limit', 1);
  variable_set('spambot_check_blocked_accounts', 1);
  variable_set('spambot_spam_account_action', 1);
}

/**
 * Disable the captcha_after module, and also prevent captcha from being added
 * to the login block form.  This is to enable caching to work.
 */
function scratchpads_security_update_7002() {
  module_disable(array(
    'captcha_after',
  ));
  db_delete('captcha_points')->condition('form_id', array(
    'user_login',
    'user_login_block',
  ))->execute();
}

/**
 * Enable honeypot and make spambot a little more sensitive.
 */
function scratchpads_security_update_7003() {
  if (!module_exists('honeypot')) {
    module_enable(array(
      'honeypot',
    ));
  }
  variable_set('honeypot_form_user_register_form', 1);
  variable_set('spambot_criteria_ip', 5);
  variable_set('spambot_criteria_username', 0);
  variable_set('spambot_blacklisted_delay', 5);
  variable_set('spambot_cron_user_limit', 10);
}

/**
 * Enable spamicide module.
 */
function scratchpads_security_update_7004() {
  if (!module_exists('spamicide')) {
    $path = 'public://' . variable_get('spamicide_dir', 'spamicide');
    file_prepare_directory($path, FILE_CREATE_DIRECTORY);
    module_enable(array(
      'spamicide',
    ));
  }
}
