<?php
// $Id: antispamquestion.module $
//
// Anti-Spam Question Module
// for Drupal 4.7.x
// by Mike Boone (contact me at: http://boonedocks.net/mailmike.php)
// Version 0.1, 2007-05-30
//
// Version History
// 0.1, 2007-05-30, first working version
//
// To use, rename this file to antispamquestion.module and place it in your modules
// directory in Drupal. Go to the Admin/Modules page on your site and activate it.
// Then go into Admin/Settings/Antispamquestion and set the question and answer you
// want to use. That's it! Note that the answer is not case-sensitive.

/*
  Provided under the GNU General Public License
  Contact me for use outside the bounds of that license
  
  ---------------------------------------------------------------    
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  The GNU General Public License can be found at:
  http://www.gnu.org/copyleft/gpl.html
  ---------------------------------------------------------------
*/

define('ANTISPAM_DEFAULT_QUESTION','2 + 2 = ?');
define('ANTISPAM_DEFAULT_ANSWER','4');

/**
 * Implementation of hook_help().
 */
function antispamquestion_help($section) {
  switch (
$section) {
    case 
'admin/modules#description':
      
// appears on the admin module selection page
      
return t('Adds an anti-spam question to the user registration form.');
  }
}

/**
 * Implementation of hook_user().
 */
function antispamquestion_user($type$edit, &$user$category NULL) {
  switch (
$type) {
    case 
'register':
      
$form['antispamquestion'] = array(
        
'#type'        => 'fieldset',
        
'#title'       => t('Anti-spam Question'),
        
'#weight'      => 1,
        
'#collapsible' => FALSE
      
);
      
$form['antispamquestion']['antispamquestion_answer'] = array(
        
'#type'          => 'textfield',
        
'#title'         => t(variable_get('antispamquestion_question'ANTISPAM_DEFAULT_QUESTION)),
        
'#default_value' => $edit['antispamquestion_answer'],
        
'#description'   => t('This question is here to help deter spam bots from creating user accounts.'),
        
'#maxlength' => 64,
        
'#required' => TRUE
      
);
      return 
$form;
    break;
    case 
'validate':
      
// only do this check for new accounts
      
if ($category == 'account' && !$user->uid ) {
        if (
strtolower($edit['antispamquestion_answer']) != strtolower(variable_get('antispamquestion_answer'ANTISPAM_DEFAULT_ANSWER))) {
          
watchdog('antispamquestion't('Someone attempted to register as %name from %mail and incorrectly answered the anti-spam question with \'%answer\'.', array('%name' => $edit['name'], '%mail' => $edit['mail'], '%answer' => $edit['antispamquestion_answer'])));
          
form_set_error('antispamquestion_answer't('You did not answer the question correctly. Please try again.'));
        }
      }
    break;
  }
}
/**
 * Implementation of hook_settings().
 */
function antispamquestion_settings() {
  
$form['antispamquestion_question'] = array(
    
'#type'          => 'textfield',
    
'#title'         => t('Anti-spam Question'),
    
'#size'          => 30,
    
'#maxlength'     => 255,
    
'#default_value' => variable_get('antispamquestion_question'ANTISPAM_DEFAULT_QUESTION),
    
'#description'   => t('This is the question that users must answer to register.'),
  );
  
$form['antispamquestion_answer'] = array(
    
'#type'          => 'textfield',
    
'#title'         => t('Anti-spam Answer'),
    
'#size'          => 30,
    
'#maxlength'     => 64,
    
'#default_value' => variable_get('antispamquestion_answer'ANTISPAM_DEFAULT_ANSWER),
    
'#description'   => t('This is the answer to the Anti-spam question.'),
  );
  return 
$form;
}