View source
<?php
include_once "botcha.module";
function botcha_schema() {
$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;
}
function botcha_requirements($phase) {
$requirements = array();
$t = get_t();
if ($phase == 'runtime') {
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;
$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() {
$form_ids = array(
'user_pass',
'user_login',
'user_login_block',
'user_register_form',
'contact_site_form',
'contact_personal_form',
'forum_node_form',
);
foreach (node_type_get_names() as $type => $name) {
$form_ids[] = 'comment_node_' . $type . '_form';
}
return $form_ids;
}
function botcha_install() {
$t = get_t();
$form_ids = _botcha_default_form_ids();
foreach ($form_ids as $form_id) {
$id = db_insert('botcha_points')
->fields(array(
'form_id' => $form_id,
'botcha_type' => 'default',
))
->execute();
}
$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);
drupal_set_message($t('You can now <a href="!botcha_admin">configure BOTCHA module</a> for your site.', array(
'!botcha_admin' => url('admin/config/people/botcha'),
)), 'status');
variable_set('botcha_secret', md5(uniqid(mt_rand(), TRUE)));
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));
}
function botcha_uninstall() {
db_delete('variable')
->condition('name', 'botcha_%', 'LIKE')
->execute();
$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');
}
function botcha_update_7000() {
db_update('botcha_points')
->fields(array(
'form_id' => 'user_register_form',
))
->condition('form_id', 'user_register')
->execute();
db_update('botcha_points')
->fields(array(
'form_id' => 'contact_site_form',
))
->condition('form_id', 'contact_mail_page')
->execute();
db_update('botcha_points')
->fields(array(
'form_id' => 'contact_personal_form',
))
->condition('form_id', 'contact_mail_user')
->execute();
$botcha_point = db_query("SELECT * FROM {botcha_points} WHERE form_id = :comment_form_id", array(
':comment_form_id' => 'comment_form',
))
->fetchObject();
if ($botcha_point !== FALSE) {
$botcha_type = $botcha_point->botcha_type;
foreach (node_type_get_names() as $type => $name) {
$form_id = 'comment_node_' . $type . '_form';
db_insert('botcha_points')
->fields(array(
'form_id' => $form_id,
'botcha_type' => $botcha_type,
))
->execute();
}
db_delete('botcha_points')
->condition('form_id', 'comment_form')
->execute();
}
}