Commit d2dfa333 authored by soxofaan's avatar soxofaan

working on 6.x-2.x version, using dedicated table instead of sessions for storage of solutions

parent a07fcbae
......@@ -5,6 +5,7 @@
* Implementation of hook_schema().
*/
function captcha_schema() {
// Table for positions and types of the challenges.
$schema['captcha_points'] = array(
'description' => t('This table describes which challenges should be added to which forms.'),
'fields' => array(
......@@ -27,20 +28,20 @@ function captcha_schema() {
),
'primary key' => array('form_id'),
);
$schema['captcha_challenges'] = array(
'description' => t('Stores the solutions of the generated challenges.'),
// Table for the CAPTCHA sessions.
$schema['captcha_sessions'] = array(
'description' => t('Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).'),
'fields' => array(
'challenge_id' => array(
'description' => t('Primary Key: Unique challenge ID.'),
'csid' => array(
'description' => t('CAPTCHA session ID.'),
'type' => 'serial',
'not null' => TRUE,
),
'solution' => array(
'description' => t('Solution of the challenge.'),
'type' => 'varchar',
'length' => 128,
'uid' => array(
'description' => t("User's {users}.uid."),
'type' => 'int',
'not null' => TRUE,
'default' => '',
'default' => 0,
),
'sid' => array(
'description' => t("Session ID of the user."),
......@@ -49,16 +50,47 @@ function captcha_schema() {
'not null' => TRUE,
'default' => '',
),
'ip_address' => array(
'description' => t('IP addres of visitor.'),
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
'timestamp' => array(
'description' => t('A Unix timestamp indicating when the challenge was generated.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'form_id' => array(
'description' => t('The form_id of the form where the CAPTCHA is added to.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'solution' => array(
'description' => t('Solution of the challenge.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'status' => array(
'description' => t('Status of the CAPTCHA session (unsolved, solved, ...)'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'attempts' => array(
'description' => t('The number of attempts.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
)
),
'primary key' => array('challenge_id'),
'primary key' => array('csid'),
'indexes' => array(
'sid' => array('sid')
'csid_ip' => array('csid', 'ip_address'),
),
);
......@@ -77,7 +109,7 @@ function captcha_install() {
foreach ($form_ids as $form_id) {
db_query("INSERT INTO {captcha_points} (form_id, module, type) VALUES ('%s', NULL, NULL)", $form_id);
}
// what to do after install?
drupal_set_message(t('You can now <a href="!captcha_admin">configure the CAPTCHA module</a> for your site.',
array('!captcha_admin' => url('admin/user/captcha'))), 'status');
......@@ -154,20 +186,20 @@ function captcha_update_2() {
function captcha_update_6200() {
$items = array();
$schema['captcha_challenges'] = array(
'description' => t('Stores the solutions of the generated challenges.'),
// Table for the CAPTCHA sessions.
$schema['captcha_sessions'] = array(
'description' => t('Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).'),
'fields' => array(
'challenge_id' => array(
'description' => t('Primary Key: Unique challenge ID.'),
'csid' => array(
'description' => t('CAPTCHA session ID.'),
'type' => 'serial',
'not null' => TRUE,
),
'solution' => array(
'description' => t('Solution of the challenge.'),
'type' => 'varchar',
'length' => 128,
'uid' => array(
'description' => t("User's {users}.uid."),
'type' => 'int',
'not null' => TRUE,
'default' => '',
'default' => 0,
),
'sid' => array(
'description' => t("Session ID of the user."),
......@@ -176,20 +208,51 @@ function captcha_update_6200() {
'not null' => TRUE,
'default' => '',
),
'ip_address' => array(
'description' => t('IP addres of visitor.'),
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
'timestamp' => array(
'description' => t('A Unix timestamp indicating when the challenge was generated.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'form_id' => array(
'description' => t('The form_id of the form where the CAPTCHA is added to.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'solution' => array(
'description' => t('Solution of the challenge.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'status' => array(
'description' => t('Status of the CAPTCHA session (unsolved, solved, ...)'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'attempts' => array(
'description' => t('The number of attempts.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
)
),
'primary key' => array('challenge_id'),
'primary key' => array('csid'),
'indexes' => array(
'sid' => array('sid')
'csid_ip' => array('csid', 'ip_address'),
),
);
db_create_table($items, 'captcha_challenges', $schema['captcha_challenges']);
db_create_table($items, 'captcha_sessions', $schema['captcha_sessions']);
return $items;
}
......@@ -10,11 +10,15 @@
*
*/
define('CAPTCHA_UNSOLVED_CHALLENGES_MAX', 20);
define('CAPTCHA_UNSOLVED_CHALLENGES_MAX', 20); //TODO: remove this?
define('CAPTCHA_PERSISTENCE_SHOW_ALWAYS', 1);
define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM', 2);
define('CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL', 3);
define('CAPTCHA_STATUS_UNSOLVED', 0);
define('CAPTCHA_STATUS_SOLVED', 1);
define('CAPTCHA_STATUS_EXAMPLE', 2);
/**
* Implementation of hook_help().
*/
......@@ -131,11 +135,11 @@ function captcha_theme() {
/**
* Implementation of hook_cron().
*
* Remove old entries from captcha_challenges table.
* Remove old entries from captcha_sessions table.
*/
function captcha_cron() {
// remove challenges older than 1 hour
db_query('DELETE FROM {captcha_challenges} WHERE timestamp < %d', time() - 3600);
// remove challenges older than 1 day
db_query('DELETE FROM {captcha_sessions} WHERE timestamp < %d', time() - 60*60*24);
}
/**
......@@ -168,27 +172,7 @@ function captcha_form_alter(&$form, $form_state, $form_id) {
if (!user_access('skip CAPTCHA')) {
// Visitor does not have permission to skip the CAPTCHA
require_once('captcha.pages.inc');
// Generate challenge for the form (if any) and add it to the form
$captcha_elements = _captcha_generate_challenge($form_id);
if (!$captcha_elements) {
// no CAPTCHA needed for this form_id
return;
}
$form['captcha'] = $captcha_elements['captcha'];
// Add pre_render function for additional CAPTCHA processing.
$form['#pre_render'][] = 'captcha_pre_render';
// Add pre_render function for placement of CAPTCHA formt element (above submit buttons).
$form['#pre_render'][] = 'captcha_pre_render_place_captcha';
// Add a validation function for the CAPTCHA form element.
// It is put in front of the list of the validation callbacks (if any).
// This is needed for user login protection, where the login is done in
// a validation callback (user_login_authenticate_validate), so
// captcha_validate() needs to run before that.
$form['#validate'] = array_merge(array('captcha_validate'), (array)($form['#validate']));
_captcha_form_alter_untrusted_user($form, $form_state, $form_id);
}
elseif (user_access('administer CAPTCHA settings') && variable_get('captcha_administration_mode', FALSE) && arg(0) != 'admin') {
// For administrators: show CAPTCHA info and offer link to configure it
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment