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',
'contact_mail_page',
'contact_mail_user',
'comment_form',
'forum_node_form',
);
return $form_ids;
}
function botcha_install() {
$t = get_t();
drupal_install_schema('botcha');
$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);
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');
}
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() {
drupal_uninstall_schema('botcha');
db_query("DELETE FROM {variable} WHERE name LIKE 'botcha_%'");
$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_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) {
$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')");
}
cache_clear_all();
}
return $items;
}