captcha.module 7.91 KB
Newer Older
Arnab Nandi's avatar
 
Arnab Nandi committed
1 2 3
<?php
// $Id$

wundo's avatar
wundo committed
4
/**
5
 * Implementation of hook_help(). 
wundo's avatar
wundo committed
6 7 8
 */
function captcha_help($section = 'admin/help#captcha') {
  $output = '';
Arnab Nandi's avatar
 
Arnab Nandi committed
9 10

  switch ($section) {
Arnab Nandi's avatar
 
Arnab Nandi committed
11
  case 'admin/help#captcha':
wundo's avatar
wundo committed
12 13
    $output .= '<p>'. t('Adds a Captcha to various forms to help prevent spam submissions.') .'</p>';
    // TODO: More help needed here.
Arnab Nandi's avatar
 
Arnab Nandi committed
14
    break;
Arnab Nandi's avatar
 
Arnab Nandi committed
15 16 17 18
  }
  return $output;
}

wundo's avatar
wundo committed
19 20 21 22 23 24 25 26 27 28 29 30 31
/**
 * Implementation of hook_menu().
 */
function captcha_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/captcha',
      'title' => t('Captcha'),
      'description' => t('Administer how and where Captchas are used.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('captcha_admin_settings'),
      'access' => user_access('administer site configuration'),
wundo's avatar
wundo committed
32 33 34 35 36 37 38
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/settings/captcha/captcha',
      'title' => t('Captcha'),
      'description' => t('Administer how and where Captchas are used.'),
      'access' => user_access('administer site configuration'),
wundo's avatar
wundo committed
39
      'type' => MENU_DEFAULT_LOCAL_TASK,
wundo's avatar
wundo committed
40
    );
wundo's avatar
wundo committed
41
    $items[] = array(
wundo's avatar
wundo committed
42
      'path' => 'admin/settings/captcha/points',
wundo's avatar
wundo committed
43
      'title' => t('Captcha Points'),
wundo's avatar
wundo committed
44 45 46 47 48
      'description' => t('Administer in what forms Captchas are applied.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('captcha_admin_points'),
      'access' => user_access('administer site configuration'),
      'type' => MENU_LOCAL_TASK,
wundo's avatar
wundo committed
49
    );
wundo's avatar
wundo committed
50 51 52
  }
  return $items;
}
53

Arnab Nandi's avatar
 
Arnab Nandi committed
54

wundo's avatar
wundo committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
function _captcha_supported_challenges_types(){
  static $captcha_challenges = NULL;
  
  if($captcha_challenges != NULL) {
  	return $captcha_challenges; 
  }	
  
  $captcha_challenges['none'] = 'none';
  
  foreach(module_implements('captcha') as $module) {
    $result = call_user_func_array($module .'_captcha', 'list');
    if (isset($result)) { //&& is_array($result)) {
      foreach($result as $challenge) {
      	$captcha_challenges[$module.'/'.$challenge] = $module.'/'.$challenge;
      }
    }
  }
  
  
  return $captcha_challenges;
}

wundo's avatar
wundo committed
77 78 79 80 81 82 83 84
function _captcha_points() {
  $captcha_points = variable_get('captcha_points',NULL);
  if($captcha_points == NULL) {
    $captcha_points = array(
      'comment_form' => t('Comment form'),
      'contact_mail_user' => t('User contact form'),
      'contact_mail_page' => t('Sitewide contact form'),
    );
wundo's avatar
wundo committed
85 86 87 88
    variable_set('captcha_points',serialize($captcha_points));
  }
  else {
    $captcha_points = unserialize($captcha_points);
wundo's avatar
wundo committed
89
  }
wundo's avatar
wundo committed
90
  return $captcha_points;
wundo's avatar
wundo committed
91
}
92

wundo's avatar
wundo committed
93 94
function captcha_admin_points() {
  $captcha_points = _captcha_points();
wundo's avatar
wundo committed
95
  $output = '';
wundo's avatar
wundo committed
96
  
wundo's avatar
wundo committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  if(function_exists('form_store_get_all')) {
    $form = array('#description' => t('Enable/Disable captcha points.'));
    $known_forms = form_store_get_all();
    $form['captcha_points']['#tree'] = true;
    foreach($known_forms as $item) {
      $form['captcha_points'][$item->form_id] = array(
        '#type' => 'checkbox',
        '#title' => $item->form_id.' - '.$item->description,
        '#default_value' => isset($captcha_points[$item->form_id]),
      );
    }
    $output = system_settings_form($form);
  }

  return $output;
}

function captcha_admin_points_submit($form_id, $form_values) {
  if($form_id == 'captcha_admin_points') {
    $known_forms = form_store_get_all();
    $captcha_points = array();
    foreach($form_values['captcha_points'] as $key => $selected) {
      if($selected)
        $captcha_points[$key] = $known_forms[$key]->description;
    }
    variable_set('captcha_points',serialize($captcha_points));
    drupal_set_message(t('Your captcha points were sucessful updated, please configure the challenge options in the other tab.'),'status');
  }
wundo's avatar
wundo committed
125 126
}

wundo's avatar
wundo committed
127 128 129
/**
 * Helper function generates admin settings page.
 */
wundo's avatar
wundo committed
130
function captcha_admin_settings() {
wundo's avatar
wundo committed
131
  $captcha_points = _captcha_points();
wundo's avatar
wundo committed
132 133 134
  $roles = variable_get('captcha_roles',user_roles());
  $captcha_challenges = _captcha_supported_challenges_types();
  
135
  foreach($roles as $role) {
wundo's avatar
wundo committed
136 137 138 139
    $varsuffix = strtr($role, ' ', '_') .'_captcha';
    $form[$varsuffix] = array(
      '#type' => 'fieldset',
      '#title' => t('Captcha points for the role @role', array('@role' => $role)),
wundo's avatar
wundo committed
140
      '#description' => t('Select what kind of challenge you want to pose to the user in each captcha point.'),
wundo's avatar
wundo committed
141 142 143 144
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    foreach($captcha_points as $captcha_point => $captcha_point_description) {
145 146
      $varname = $captcha_point .'_'. $varsuffix;
      $form[$varsuffix][$varname] = array(
wundo's avatar
wundo committed
147 148 149 150
        '#type' => 'select',
        '#title' => t('@point', array('@point' => $captcha_point_description)),
        '#default_value' => variable_get($varname, 'captcha/Math'),
        '#options' => $captcha_challenges,
wundo's avatar
wundo committed
151
      );
152 153
    }
  }
wundo's avatar
wundo committed
154
  
wundo's avatar
wundo committed
155
  return system_settings_form($form);
Arnab Nandi's avatar
 
Arnab Nandi committed
156 157
}

wundo's avatar
wundo committed
158 159 160 161
/**
 * Implementation of hook_form_alter().
 */
function captcha_form_alter($form_id, &$form) {
wundo's avatar
wundo committed
162
  global $captcha;
Arnab Nandi's avatar
Arnab Nandi committed
163
  global $user;
wundo's avatar
wundo committed
164

wundo's avatar
wundo committed
165 166
  $seed = $form['form_token']['#default_value'];
  if($_SESSION['captcha'][$seed]['success'] === TRUE)
wundo's avatar
wundo committed
167
    return;
Arnab Nandi's avatar
Arnab Nandi committed
168

wundo's avatar
wundo committed
169 170 171
  foreach($user->roles as $role) {	
  	$candidate_trigger = $form_id .'_'. strtr($role, ' ', '_') .'_captcha';
  	$captcha_type = variable_get($candidate_trigger,NULL);
Arnab Nandi's avatar
Arnab Nandi committed
172

wundo's avatar
wundo committed
173 174 175 176
  	if($captcha_type != NULL) {
  	  //Found one valid captcha challenge for this point.
  	  break;
  	}
Arnab Nandi's avatar
Arnab Nandi committed
177
  }
178

wundo's avatar
wundo committed
179 180 181
  if($captcha_type == 'none') {
    //One of the user roles doesn't have captcha challenge for this point, aborting.
    return;
182
  }
wundo's avatar
wundo committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  elseif($captcha_type == NULL) {
  	//captcha_type null;
  	return;
  }
     
  $challenge = explode('/',$captcha_type);
   
  $result = module_invoke($challenge[0], 'captcha', 'generate', $challenge[1]);
  
  $form = array_merge($form,$result['form']);
  
  $form['#validate'] += array('captcha_validate' => array());
  $form['#pre_render'] = array('captcha_prerender');

  $_SESSION['captcha'][$seed]['success'] = FALSE;
  $_SESSION['captcha'][$seed]['form_id'] = $form_id;
  $_SESSION['captcha'][$seed]['form'] = $result['form'];
  $_SESSION['captcha'][$seed]['new_value'] = $result['value'];
  $_SESSION['captcha'][$seed]['preprocess'] = isset($result['preprocess'])? $result['preprocess'] : FALSE;
  $_SESSION['captcha'][$seed]['module'] = $challenge[0];
  $_SESSION['captcha'][$seed]['type'] = $challenge[1];
204
}
Arnab Nandi's avatar
Arnab Nandi committed
205

wundo's avatar
wundo committed
206 207
function captcha_validate($form_id, $form_item) {
  $seed = $form_item['form_token'];
Arnab Nandi's avatar
Arnab Nandi committed
208

wundo's avatar
wundo committed
209 210
  if($_SESSION['captcha'][$seed]['preprocess']) {
    $value = module_invoke($_SESSION['captcha'][$seed]['module'],'captcha','process',$_SESSION['captcha'][$seed]['type'],$form_item['captcha_challenge']);
wundo's avatar
wundo committed
211
  }
wundo's avatar
wundo committed
212 213 214
  else {
  	$value = $form_item['captcha_challenge'];
  } 
wundo's avatar
wundo committed
215

wundo's avatar
wundo committed
216 217 218
  if($value == $_SESSION['captcha'][$seed]['value']) {
  	$_SESSION['captcha'][$seed]['success'] = TRUE;
    return;
219
  }
wundo's avatar
wundo committed
220
  form_set_error('captcha_response', t('The answer you entered to the captcha challenge is incorrect.'));  
221
}
Arnab Nandi's avatar
 
Arnab Nandi committed
222

wundo's avatar
wundo committed
223
/**
wundo's avatar
wundo committed
224 225
 * Implementation of form #pre_render.
 *
226
 */
wundo's avatar
wundo committed
227 228 229 230 231 232 233
function captcha_prerender($form_id, $form) {
  $seed = $form['#post']['form_token'];
  $_SESSION['captcha'][$seed]['value'] = $_SESSION['captcha'][$seed]['new_value'];
  if($_SESSION['captcha'][$seed]['success']) {
  	unset($form['captcha_challenge']);
  	unset($_SESSION['captcha'][$seed]);
  }
234
}
235

wundo's avatar
wundo committed
236 237


wundo's avatar
wundo committed
238
/**
wundo's avatar
wundo committed
239
 * Default implementation of hook_captcha
wundo's avatar
wundo committed
240
 */
wundo's avatar
wundo committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
function captcha_captcha() {
  $args = func_get_args();
  $op = array_shift($args); 
  switch($op) {
  	case 'list':
  	  return array("Math");
  	case 'generate':
  	  $captcha_type = array_shift($args);
  	  $result = array();
  	  if($captcha_type == "Math") {
        $x = rand(1,10);
        $y = rand(1,10);
	
        $result['value'] = (string)($x + $y);
        $result['form']['captcha_challenge'] = array (
          '#type' => 'textfield',
          '#title' => t('Math Question: What is %problem?', array('%problem' => $x .' + '. $y)),
          '#description' => t('Please solve the math problem above and type in the result. e.g. for 1+1, type 2.'),
          '#weight' => 0,
          '#required' => TRUE,
        );
  	  }
	  return $result;
264
  }
Arnab Nandi's avatar
Arnab Nandi committed
265
}