| 1 |
<?php |
<?php |
| 2 |
// $Id: captcha.module,v 1.21 2006/02/12 08:54:56 arnabdotorg Exp $ |
// $Id: captcha.module,v 1.22 2006/03/03 22:42:41 arnabdotorg Exp $ |
| 3 |
|
|
| 4 |
function captcha_help($section = "admin/help#captcha") { |
function captcha_help($section = "admin/help#captcha") { |
| 5 |
$output = ""; |
$output = ""; |
| 10 |
$output .= "<p>More help needed here.</p>"; |
$output .= "<p>More help needed here.</p>"; |
| 11 |
break; |
break; |
| 12 |
case 'admin/modules#description': |
case 'admin/modules#description': |
|
$output = t("Adds a Captcha to the registration form."); |
|
|
break; |
|
| 13 |
case 'admin/modules/captcha': |
case 'admin/modules/captcha': |
|
$output = t("Adds a Captcha to the registration form."); |
|
|
break; |
|
| 14 |
case 'admin/captcha': |
case 'admin/captcha': |
| 15 |
$output = t("Adds a Captcha to the registration form."); |
$output = t("Adds a Captcha to the registration form."); |
| 16 |
break; |
break; |
| 18 |
return $output; |
return $output; |
| 19 |
} |
} |
| 20 |
|
|
|
/** |
|
|
* Implementation of hook_menu(). |
|
|
*/ |
|
|
function captcha_menu($may_cache) { |
|
|
$items = array(); |
|
|
|
|
|
$suffix = ''; |
|
|
|
|
|
if ($may_cache) { |
|
|
if (arg(2)!=null) $suffix='/'.arg(2); |
|
|
$items[] = array( |
|
|
'path' => 'captcha/image'.$suffix, 'title' => t('captcha image'), |
|
|
'callback' => '_captcha_call', |
|
|
'callback arguments' => array('_captcha_image'), |
|
|
'access' => user_access('access captchas'), |
|
|
'type' => MENU_CALLBACK |
|
|
); |
|
|
} |
|
|
|
|
|
return $items; |
|
|
} |
|
|
|
|
|
function captcha_perm() { |
|
|
return array('access captchas'); |
|
|
} |
|
| 21 |
|
|
| 22 |
function captcha_settings() { |
function captcha_settings() { |
| 23 |
|
|
|
//check for GD |
|
|
if (!function_exists(imagecreate)) |
|
|
form_set_error('No GD', t('Image library not available. Captcha needs the GD library extension to be installed. Please install GD.')); |
|
|
|
|
|
else { |
|
|
|
|
|
$fonts_path = variable_get("captcha_fonts_path", ""); |
|
|
|
|
|
//check for TTF support |
|
|
if (!function_exists(imagettftext)) |
|
|
|
|
|
drupal_set_message(t('Your image library does not seem to have TrueType font support. Captcha will work, but will use the default inbuilt font.'),'status'); |
|
|
|
|
|
else { |
|
|
|
|
|
//check for valid font path |
|
|
if ($fonts_path!="" && !is_dir($fonts_path)) |
|
|
form_set_error('Invalid font path', t('The current font path is invalid. The default font will be used.')); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
| 24 |
//this is where you can add more captcha points |
//this is where you can add more captcha points |
| 25 |
$captcha_points = array( |
$captcha_points = array( |
| 26 |
'comment_form' => t('Comment Form'), |
'comment_form' => t('Comment Form'), |
| 49 |
} |
} |
| 50 |
} |
} |
| 51 |
|
|
| 52 |
|
// preprocess array into a map |
| 53 |
|
foreach(module_implements('captchachallenge') as $module) { |
| 54 |
|
$captchamodules[$module] = $module; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
$form['captcha_type'] = array( |
$form['captcha_type'] = array( |
| 58 |
'#type' => 'select', |
'#type' => 'select', |
| 59 |
'#title' => t('Type of captcha to use'), |
'#title' => t('Type of captcha to use'), |
| 60 |
'#default_value' => variable_get('captcha_type','math'), |
'#default_value' => variable_get('captcha_type','captcha'), |
| 61 |
'#options' => _captcha_types(), |
'#options' => $captchamodules, |
| 62 |
'#description' => t('Select what kind of challenge you want to pose to the user') |
'#description' => t('Select what kind of challenge you want to pose to the user') |
| 63 |
); |
); |
| 64 |
|
|
|
$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.'), |
|
|
); |
|
|
|
|
|
if (isset($fonts_path)) { |
|
|
$imagefontinfo .= t('Number of fonts found: ').count(_captcha_font_list()); |
|
|
} |
|
|
|
|
|
$gdinfo = gd_info(); |
|
|
$imagefontinfo .= '<br />'.t('GD Version: ').$gdinfo["GD Version"]; |
|
|
$imagefontinfo .= '<br />'.t(' FreeType Support: '); |
|
|
$imagefontinfo .= ($gdinfo["FreeType Support"]==true) ? 'True' : 'False'; |
|
|
$imagefontinfo .= '<br />'; |
|
|
|
|
|
$form['captcha_info'] = array ( |
|
|
'#type' => 'item', |
|
|
'#title' => t('Image and font information'), |
|
|
'#value' => $imagefontinfo, |
|
|
); |
|
|
|
|
| 65 |
return $form; |
return $form; |
| 66 |
} |
} |
| 67 |
|
|
| 71 |
|
|
| 72 |
global $user; |
global $user; |
| 73 |
$captcha_type = variable_get("captcha_type", NULL); |
$captcha_type = variable_get("captcha_type", NULL); |
| 74 |
|
|
| 75 |
if (!$captcha_type) return; |
if (!$captcha_type) return; |
| 76 |
|
|
| 77 |
$flag = true; |
$flag = true; |
| 87 |
break; |
break; |
| 88 |
} |
} |
| 89 |
} |
} |
|
|
|
| 90 |
if ($flag && isset($trigger)) { |
if ($flag && isset($trigger)) { |
| 91 |
if ($captcha_type = _captcha_load()) { |
$form['#submit'] = array('captcha_submit' => array()) + $form['#submit']; |
| 92 |
$form['#submit'] = array('captcha_submit' => array()) + $form['#submit']; |
if (!_captcha_validate($_POST['edit']['captcha_response'])) { |
| 93 |
if (!_captcha_validate($_POST['edit']['captcha_response'])) { |
//use call_func because module_invoke does not allow call by reference. |
| 94 |
call_user_func_array('_captcha_'. $captcha_type .'_challenge', array(&$form, &$_SESSION['captcha'])); |
if (module_hook($captcha_type, 'captchachallenge')) { |
| 95 |
|
call_user_func_array($captcha_type.'_captchachallenge', array(&$form, &$_SESSION['captcha'])); |
| 96 |
} |
} |
| 97 |
} |
} |
| 98 |
} |
} |
| 105 |
function captcha_submit() { |
function captcha_submit() { |
| 106 |
if($_SESSION['captcha_correct']) { |
if($_SESSION['captcha_correct']) { |
| 107 |
unset($_SESSION['captcha_correct']); |
unset($_SESSION['captcha_correct']); |
| 108 |
unset($_SESSION['captcha']); |
unset($_SESSION['captcha']); |
| 109 |
} |
} |
| 110 |
} |
} |
| 111 |
|
|
| 117 |
if (trim($captcha_response) == '') return FALSE; |
if (trim($captcha_response) == '') return FALSE; |
| 118 |
|
|
| 119 |
global $user; |
global $user; |
| 120 |
|
$captcha_type = variable_get("captcha_type", NULL); |
| 121 |
$trigger = NULL; |
$trigger = NULL; |
| 122 |
if ($captcha_type = _captcha_load()) { |
|
| 123 |
call_user_func_array('_captcha_'. $captcha_type .'_validate', array(&$captcha_response, &$_SESSION['captcha_correct'])); |
if (module_hook($captcha_type, 'captchavalidate')) { |
| 124 |
|
call_user_func_array($captcha_type.'_captchavalidate', array(&$captcha_response, &$_SESSION['captcha_correct'])); |
| 125 |
} |
} |
| 126 |
|
|
| 127 |
return $_SESSION['captcha_correct']; |
return $_SESSION['captcha_correct']; |
| 128 |
} |
} |
| 129 |
|
|
| 130 |
|
/* |
| 131 |
/** |
* Default implementation of the captcha challenge & validation |
| 132 |
* Returns an array of files with TTF extensions in the specified directory. |
*/ |
| 133 |
*/ |
function captcha_captchachallenge(&$form, &$captcha) { |
| 134 |
function _captcha_font_list() { |
|
| 135 |
$fontdir = variable_get("captcha_fonts_path", ""); |
$x = rand(1,10); |
| 136 |
|
$y = rand(1,10); |
| 137 |
$filelist = array(); |
|
| 138 |
if ($handle = opendir($fontdir)) { |
$captcha = ($x + $y) . ''; |
| 139 |
while ($file = readdir($handle)) { |
$form['captcha_response'] = array ( |
| 140 |
if (preg_match("/\.ttf$/i",$file) == 1) |
'#type' => 'textfield', |
| 141 |
$filelist[] = $fontdir.'/'.$file; |
'#title' => t('Math Question: What is '. $x .' + '. $y .'?'), |
| 142 |
} |
'#defaultvalue' => '', |
| 143 |
closedir($handle); |
'#description' => t('Please solve the math problem above and type in the result. e.g. for 1+1, type 2'), |
| 144 |
} |
'#weight' => 0, |
| 145 |
|
'#required' => TRUE, |
| 146 |
return $filelist; |
'#validate' => array('_captcha_validate' => array()) |
| 147 |
} |
); |
|
|
|
|
function _captcha_call($func) { |
|
|
_captcha_load(); |
|
|
if (function_exists($func)) call_user_func_array($func, array()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Loads the current captcha system into memory |
|
|
*/ |
|
|
function _captcha_load() { |
|
|
|
|
|
$captcha_type = variable_get("captcha_type", 'math'); |
|
|
$path = drupal_get_path('module', 'captcha'); |
|
|
include_once($path.'/captcha_'.$captcha_type.'.inc'); |
|
|
|
|
|
$function_challenge = '_captcha_'. $captcha_type .'_challenge'; |
|
|
$function_validate = '_captcha_'. $captcha_type .'_validate'; |
|
|
|
|
|
if(function_exists($function_challenge) && function_exists($function_validate)) { |
|
|
return $captcha_type; |
|
|
} |
|
|
else return false; |
|
| 148 |
|
|
| 149 |
} |
} |
| 150 |
|
|
| 151 |
/** |
function captcha_captchavalidate(&$captcha_word, &$correct) { |
| 152 |
* Generates list of available captcha methods |
$captcha_word = drupal_strtolower($captcha_word); |
| 153 |
*/ |
if ($captcha_word == $_SESSION['captcha']) { |
| 154 |
function _captcha_types() { |
$correct = TRUE; |
|
static $types; |
|
|
|
|
|
if (!isset($types)) { |
|
|
$types = array(); |
|
|
$path = drupal_get_path('module', 'captcha'); |
|
|
$files = file_scan_directory($path, '^captcha_.*\.inc$'); |
|
|
foreach ($files as $filename => $file) { |
|
|
include_once($filename); |
|
|
$function_challenge = '_'. $file->name .'_challenge'; |
|
|
$function_validate = '_'. $file->name .'_validate'; |
|
|
if(function_exists($function_challenge) && function_exists($function_validate)) { |
|
|
$types[] = substr($file->name, 8); |
|
|
} |
|
|
} |
|
| 155 |
} |
} |
| 156 |
//post process types |
else { |
| 157 |
foreach($types as $type) { |
$correct = FALSE; |
| 158 |
$r_types[$type] = $type; |
form_set_error('captcha_response', t('The answer you entered to the math problem is incorrect.')); |
| 159 |
} |
} |
|
return $r_types; |
|
| 160 |
} |
} |
| 161 |
|
|
| 162 |
?> |
?> |