You are here

function botcha_update_6200 in BOTCHA Spam Prevention 6.3

Same name and namespace in other branches
  1. 6.2 botcha.install \botcha_update_6200()

Implementation of hook_update_N(). Create flexible relationships between recipe books and recipes and between recipe books and forms.

1 call to botcha_update_6200()
botcha_install in ./botcha.install
Implementation of hook_install().

File

./botcha.install, line 279

Code

function botcha_update_6200() {

  // Create new tables.
  // Not to repeat ourselves we are doing it using schema definition.
  $schema_definition = botcha_schema();

  // Tables creation itself.
  // Checks added to let this to be safely called while installing also.
  // @see botcha_install()
  $ret = array();
  if (!db_table_exists('botcha_form')) {
    db_create_table($ret, 'botcha_form', $schema_definition['botcha_form']);
  }
  if (!db_table_exists('botcha_recipe')) {
    db_create_table($ret, 'botcha_recipe', $schema_definition['botcha_recipe']);
  }
  if (!db_table_exists('botcha_recipebook')) {
    db_create_table($ret, 'botcha_recipebook', $schema_definition['botcha_recipebook']);
  }
  if (!db_table_exists('botcha_recipebook_form')) {
    db_create_table($ret, 'botcha_recipebook_form', $schema_definition['botcha_recipebook_form']);
  }
  if (!db_table_exists('botcha_recipebook_recipe')) {
    db_create_table($ret, 'botcha_recipebook_recipe', $schema_definition['botcha_recipebook_recipe']);
  }

  // Fill in botcha_recipebook.
  $recipebooks = array(
    array(
      'id' => 'default',
      'title' => 'Default',
      'description' => 'Default recipe book provided by BOTCHA installer. You can customize it.',
    ),
    array(
      'id' => 'ajax_friendly',
      'title' => 'AJAX friendly',
      'description' => 'Recipe book which contains recipes that do not break AJAX form submissions.',
    ),
    array(
      'id' => 'forbidden_forms',
      'title' => 'Forbidden forms',
      'description' => 'Recipe book which contains no recipes at all and forms that must not be protected. This recipe book was created for informational purpose only and can not be edited or deleted.',
    ),
  );
  foreach ($recipebooks as $recipebook) {
    $ret[] = db_merge('botcha_recipebook')
      ->key(array(
      'id' => $recipebook['id'],
    ))
      ->fields(array(
      'title' => $recipebook['title'],
      'description' => $recipebook['description'],
    ))
      ->execute();
  }

  // Fill in botcha_form and botcha_recipebook_form.
  $forms = _botcha_default_form_ids();
  foreach ($forms as $form_id) {
    $ret[] = db_merge('botcha_form')
      ->key(array(
      'id' => $form_id,
    ))
      ->execute();

    // Leave login forms unprotected. It is very important, because if one of the
    // recipes is broken (ie always blocks), admin must have opportunity to login.
    $query = db_merge('botcha_recipebook_form')
      ->key(array(
      'form_id' => $form_id,
    ));
    if (in_array($form_id, array(
      'update_script_selection_form',
      'user_login',
      'user_login_block',
    ))) {
      $ret[] = $query
        ->fields(array(
        'rbid' => 'forbidden_forms',
      ))
        ->execute();
    }
    else {
      $ret[] = $query
        ->fields(array(
        'rbid' => 'default',
      ))
        ->execute();
    }
  }

  // Fill in botcha_recipe and botcha_recipebook_recipe.
  $recipes = array(
    array(
      'id' => 'honeypot',
      'classname' => 'BotchaRecipeHoneypot',
      'title' => 'Default Honeypot recipe',
      'description' => 'Recipe which implements Honeypot protection method with default configuration.',
    ),
    // @todo Turn it into just a customization of Honeypot recipe (by providing rich configuration UI).
    array(
      'id' => 'honeypot2',
      'classname' => 'BotchaRecipeHoneypot2',
      'title' => 'Default Honeypot2 recipe',
      'description' => 'Recipe which implements Honeypot2 protection method with default configuration.',
    ),
    array(
      'id' => 'no_resubmit',
      'classname' => 'BotchaRecipeNoResubmit',
      'title' => 'Default NoResubmit recipe',
      'description' => 'Recipe which implements NoResubmit protection method with default configuration.',
    ),
    array(
      'id' => 'obscure_url',
      'classname' => 'BotchaRecipeObscureUrl',
      'title' => 'Default ObscureUrl recipe',
      'description' => 'Recipe which implements ObscureUrl protection method with default configuration.',
    ),
    array(
      'id' => 'timegate',
      'classname' => 'BotchaRecipeTimegate',
      'title' => 'Default Timegate recipe',
      'description' => 'Recipe which implements Timegate protection method with default configuration.',
    ),
  );
  foreach ($recipes as $recipe) {
    $ret[] = db_merge('botcha_recipe')
      ->key(array(
      'id' => $recipe['id'],
    ))
      ->fields(array(
      'classname' => $recipe['classname'],
      'title' => $recipe['title'],
      'description' => $recipe['description'],
    ))
      ->execute();

    // It looks like db_merge does not work for complex primary key.
    $count = count(db_select('botcha_recipebook_recipe', 'brr')
      ->fields('brr')
      ->condition('rbid', 'default')
      ->condition('recipe_id', $recipe['id'])
      ->execute()
      ->fetchCol());
    if (!$count) {
      $ret[] = db_insert('botcha_recipebook_recipe')
        ->fields(array(
        'rbid' => 'default',
        'recipe_id' => $recipe['id'],
      ))
        ->execute();
    }
    if (in_array($recipe['id'], array(
      'no_resubmit',
      'timegate',
    ))) {

      // It looks loke db_merge does not work for complex primary key.
      $count = count(db_select('botcha_recipebook_recipe', 'brr')
        ->fields('brr')
        ->condition('rbid', 'ajax_friendly')
        ->condition('recipe_id', $recipe['id'])
        ->execute()
        ->fetchCol());
      if (!$count) {
        $ret[] = db_insert('botcha_recipebook_recipe')
          ->fields(array(
          'rbid' => 'ajax_friendly',
          'recipe_id' => $recipe['id'],
        ))
          ->execute();
      }
    }
  }

  // Remove botcha_points table.
  if (db_table_exists('botcha_points')) {
    db_drop_table($ret, 'botcha_points');
  }
  return $ret;
}