Commit 68493a99 authored by Arnab Nandi's avatar Arnab Nandi
Browse files

Upgraded to FormAPI for 4.7

parent 0bf3a074
##Captcha Readme##
To Install:
Enable the module, and then go to admin/settings/captcha to enable captchas for various actions.
Then go to admin/access control to enable the access to the captcha (access captchas) for roles.
1. Enable the module, and then go to admin/settings/captcha to enable captchas for various actions.
2. Then go to admin/access control to enable the access to the captcha (access captchas) for roles.
Notes:
- Uses GD to draw stuff, emits in PNG
- Captcha for comments is contingent upon a patch(#14708) for now. One day, this will reach core.
- I've added function_exists checks for it to degrade gracefully across multiple versions of GD,
please file issues with the exact GD, PHP information if you have problems.
Captcha Demo:
http://arnab.drupaldevs.org
please file issues with the exact GD, PHP information if you have problems.
\ No newline at end of file
......@@ -27,21 +27,17 @@ function captcha_help($section = "admin/help#captcha") {
*/
function captcha_menu($may_cache) {
$items = array();
$suffix = '';
if (arg(2)!=null) $suffix='/'.arg(2);
$items[] = array('path' => 'captcha/image'.$suffix, 'title' => t('captcha image'),
'callback' => '_captcha_image', 'access' => user_access('access captchas'),
'type' => MENU_CALLBACK);
return $items;
}
/**
* Implementation of hook_perm, in response to issue #19254
* This adds yet another value to configure, but it's a safer option.
*/
function captcha_perm() {
return array('access captchas');
}
......@@ -69,13 +65,36 @@ function captcha_settings() {
}
$output .= form_checkbox(t('Check during user registration'), 'captcha_user_register', 'true', _captcha_istrue("captcha_user_register", "true"), 'If enabled, users will be asked to recognize an image during user registration.');
$form['captcha_user_register'] = array(
'#type' => 'checkbox',
'#title' => t('Check during user registration'),
'#default_value' => _captcha_istrue("captcha_user_register", "true"),
'#description' => 'If enabled, users will be asked to recognize an image during user registration.',
);
$form['captcha_comment_anonymous'] = array(
'#type' => 'checkbox',
'#title' => t('Check during anonymous comments.'),
'#default_value' => _captcha_istrue("captcha_comment_anonymous", "true"),
'#description' => 'If enabled, anonymous users will be asked to recognize an image while posting .',
);
$form['captcha_comment_registered'] = array(
'#type' => 'checkbox',
'#title' => t('Check during registered user comments.'),
'#default_value' => _captcha_istrue("captcha_comment_registered", "true"),
'#description' => 'If enabled, registered users will be asked to recognize an image while posting .',
);
$form['captcha_fonts_path'] = array(
'#type' => 'textfield',
'#title' => t('TrueType Fonts Path'),
'#default_value' => $fonts_path,
'#size' => 30,
'#maxlength' => 255,
'#description' => t('Location of the directory where the Truetype (.ttf) fonts are stored. If you do not provide any fonts, the module will use the default font for text.'),
);
$output .= form_checkbox(t('Check during anonymous comments'), 'captcha_comment_anonymous', 'true', _captcha_istrue("captcha_comment_anonymous"), 'If enabled, anonymous users will be asked to recognize an image while posting .');
$output .= form_checkbox(t('Check during registered user comments'), 'captcha_comment_registered', 'true', _captcha_istrue("captcha_comment_registered"), 'If enabled, registered users will be asked to recognize an image while posting .');
$output .= form_textfield(t('TrueType Fonts Path'), 'captcha_fonts_path', $fonts_path, 30, 255, 'Location of the directory where the Truetype (.ttf) fonts are stored. If you do not provide any fonts, the module will use the default font for text.');
if (isset($fonts_path)) {
$imagefontinfo .= t('Number of fonts found: ').count(_captcha_font_list());
......@@ -87,9 +106,13 @@ function captcha_settings() {
$imagefontinfo .= ($gdinfo["FreeType Support"]==true) ? 'True' : 'False';
$imagefontinfo .= '<br />';
$output .= form_item("Image and font information", '', $imagefontinfo);
$form['captcha_info'] = array (
'#type' => 'item',
'#title' => t('Image and font information'),
'#value' => $imagefontinfo,
);
return $output;
return $form;
}
/**
......@@ -101,73 +124,104 @@ function captcha_user($type, &$edit, &$newuser, $category = NULL) {
// What type of registration action are we taking?
// make sure this is a registration, and captcha is enabled for registration
if (_captcha_istrue("captcha_user_register") && !$newuser->uid && !$user->uid)
switch ($type) {
case 'register':
// Add two items to the resigtration form.
$output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
$output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, t('Please type in the letters/numbers that are shown in the image above.'), NULL, TRUE);
return array(array('title' => t('Verify Registration'), 'data'=>$output));
break;
case 'validate':
// The user has filled out the form and checked the "accept" box.
if (strtolower($edit['captchaword']) == strtolower($_SESSION['captcha'])) {
// on success return the values you want to store
return array("captcha_correct" => 1);
}
else {
// on error return an error message
form_set_error('captchaword', t("Please re-recognize the word shown in the image."));
return $edit;
if (_captcha_istrue("captcha_user_register") && !$newuser->uid && !$user->uid) {
switch ($type) {
case 'register':
// Add two items to the resigtration form.
return array( t('Verify Registration') => _captcha_form());
break;
case 'validate':
// The user has filled out the form and checked the "accept" box.
if (_captcha_validate($edit['captcha_word'])) {
$_SESSION['captcha']='';
// on success return the values you want to store
return array("captcha_correct" => 1);
}
else {
// on error return an error message
form_set_error('captcha_word', t("Please re-recognize the word shown in the image."));
return $edit;
}
break;
}
break;
}
}
/**
* Comment callback; adds captcha field to new comment form.
* NOTE: This will work ONLY if the patch at http://drupal.org/node/14708 has been applied. Let's hope the patch makes HEAD!
*/
function captcha_comment($op,$edit) {
function captcha_comment($edit, $op) {
global $user;
// check if captcha is enabled for form type
if ($user->uid == 0 && !_captcha_istrue("captcha_comment_anonymous")) return;
if ($user->uid != 0 && !_captcha_istrue("captcha_comment_registered")) return;
//if ($user->uid == 0 && !_captcha_istrue("captcha_comment_anonymous")) return;
//if ($user->uid != 0 && !_captcha_istrue("captcha_comment_registered")) return;
switch ($op) {
case 'validate':
case 'validate': //this is redundant, not followed
// only validate captcha once for a comment.
// this implementation basically sets a flag when you've successfully validated a captcha;
// any successive comment inserted uses and invalidates the set flag.
if ($_SESSION['captcha_comment_correct']!='ok') {
if (strtolower($edit['captchaword']) != '' && strtolower($edit['captchaword']) == strtolower($_SESSION['captcha'])) {
$_SESSION['captcha_comment_correct'] = 'ok';
//reset captcha variable to prevent session highjacking vulnerability #26741
if (!$_SESSION['captcha_comment_correct']) {
if (_captcha_validate($edit['captcha_word'])) {
$_SESSION['captcha_comment_correct'] = true;
//reset captcha variable to prevent session highjacking vulnerability #26741
$_SESSION['captcha']='';
}
else {
form_set_error('captcha', t('The user verification code you entered is not correct.'));
form_set_error('captcha_word', t('The captcha verification code you entered is not correct.'));
}
}
break;
case 'insert':
//invalidate captcha after one comment insert
$_SESSION['captcha_comment_correct'] = '';
$_SESSION['captcha_comment_correct'] = false;
$_SESSION['captcha']='';
break;
}
}
function _captcha_form() {
$form['captcha_image'] = array (
'#type' => 'item',
'#title' => 'captcha image',
'#value' => '<img src="' . url('captcha/image/'.time()) . '" alt="Captcha Image: you will need to recognize the text in it."/>',
);
$form['captcha_word'] = array (
'#type' => 'textfield',
'#title' => t('Word'),
'#defaultvalue' => '',
'#description' => t('Please type in the letters/numbers that are shown in the image above.'),
);
case 'form':
$form_html = "";
if ($_SESSION['captcha_comment_correct']!='ok') {
$output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
$output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, 'Please type in the letters/numbers that are shown in the image above.', NULL, TRUE);
$form_html = form_group(t('Verify comment authorship'), $output);
return $form;
}
function captcha_form_alter($formid, &$form) {
switch($formid) {
case 'comment_form':
// include logic to remove captcha if the current entry is a valid captcha
if (!$_SESSION['captcha_comment_correct'] && !($_POST['edit']['captcha_word'] != '' && _captcha_validate($_POST['edit']['captcha_word']))) {
$form['captcha'] = _captcha_form();
} else {
$_SESSION['captcha']='';
unset($form['captcha']);
}
return $form_html;
break;
}
}
function _captcha_validate($string) {
$captcha_word = drupal_strtolower($string);
if ($captcha_word != $_SESSION['captcha']) {
return false;
}
return true;
}
/**
......@@ -180,16 +234,16 @@ function _captcha_code() {
for ($x=0; $x < 6; $x++) {
mt_srand ((double) microtime() * 1000000);
$const[$x] = substr($consts,mt_rand(0,strlen($consts)-1),1);
$vow[$x] = substr($vowels,mt_rand(0,strlen($vowels)-1),1);
$const[$x] = drupal_substr($consts,mt_rand(0,drupal_strlen($consts)-1),1);
$vow[$x] = drupal_substr($vowels,mt_rand(0,drupal_strlen($vowels)-1),1);
}
$string = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$string = substr($string,0,rand(5,8));
$string = drupal_substr($string,0,rand(5,8));
//everytime we create a new code, we write it to session
$_SESSION['captcha'] = $string;
$_SESSION['captcha'] = drupal_strtolower($string);
return $string;
}
......@@ -198,7 +252,7 @@ function _captcha_code() {
* Returns a random string for use in a captcha
*/
function _captcha_istrue($variable, $default="true") {
return (variable_get($variable, "")=="true");
return (variable_get($variable, "")== true);
}
/**
......@@ -235,7 +289,7 @@ function _captcha_image() {
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Content-type: image/png');
$string = _captcha_code();
......@@ -263,13 +317,13 @@ function _captcha_image() {
$y = 30;
// iterate over characters
for ($i=0;$i<strlen($string);$i++) {
for ($i=0;$i<drupal_strlen($string);$i++) {
// define angle and position of character based on previous character dimension
$x += ($charWidth * rand(1.0, 1.6));
$y += rand(-5,5);
$charAngle = rand(-5,5);
$charSize += rand(-2,2);
$char = substr($string,$i,1);
$char = drupal_substr($string,$i,1);
// select random font
$font = $fonts[rand(0,count($fonts)-1)];
......@@ -288,8 +342,8 @@ function _captcha_image() {
$x = 10;
$y = 0;
for ($i=0;$i<strlen($string);$i++) {
imagestring($im,5,$x,$y,substr($string,$i,1),$foreground);
for ($i=0;$i<drupal_strlen($string);$i++) {
imagestring($im,5,$x,$y,drupal_substr($string,$i,1),$foreground);
$x += rand(10,15);
$y += rand(-4,4);
}
......@@ -305,7 +359,7 @@ function _captcha_image() {
imagedestroy($im);
$im = $im2;
}
// add cloud only if function is defined (many PHP installations have this function missing)
if (function_exists('imagecolorallocatealpha')) {
$middleground = imagecolorallocatealpha($im, rand(160, 200), rand(160, 200), rand(160, 200), 80);
......@@ -316,7 +370,7 @@ function _captcha_image() {
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
imagepng($im);
imagedestroy($im);
......
Supports Markdown
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