Commit cd75b772 by Scott

Refactor app/captcha.php

parent 634a72b0
...@@ -20,67 +20,73 @@ ...@@ -20,67 +20,73 @@
More about this license: http://www.question2answer.org/license.php More about this license: http://www.question2answer.org/license.php
*/ */
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../'); header('Location: ../');
exit; exit;
} }
function qa_captcha_available() /**
/* * Return whether a captcha module has been selected and it indicates that it is fully set up to go.
Return whether a captcha module has been selected and it indicates that it is fully set up to go */
*/ function qa_captcha_available()
{ {
$module=qa_load_module('captcha', qa_opt('captcha_module')); $module = qa_load_module('captcha', qa_opt('captcha_module'));
return isset($module) && ( (!method_exists($module, 'allow_captcha')) || $module->allow_captcha()); return isset($module) && (!method_exists($module, 'allow_captcha') || $module->allow_captcha());
} }
function qa_captcha_reason_note($captchareason) /**
/* * Return an HTML string explaining $captchareason (from qa_user_captcha_reason()) to the user about why they are seeing a captcha
Return an HTML string explaining $captchareason (from qa_user_captcha_reason()) to the user about why they are seeing a captcha */
*/ function qa_captcha_reason_note($captchareason)
{ {
$notehtml=null; $notehtml = null;
switch ($captchareason) { switch ($captchareason) {
case 'login': case 'login':
$notehtml=qa_insert_login_links(qa_lang_html('misc/captcha_login_fix')); $notehtml = qa_insert_login_links(qa_lang_html('misc/captcha_login_fix'));
break; break;
case 'confirm': case 'confirm':
$notehtml=qa_insert_login_links(qa_lang_html('misc/captcha_confirm_fix')); $notehtml = qa_insert_login_links(qa_lang_html('misc/captcha_confirm_fix'));
break; break;
case 'approve': case 'approve':
$notehtml=qa_lang_html('misc/captcha_approve_fix'); $notehtml = qa_lang_html('misc/captcha_approve_fix');
break; break;
} }
return $notehtml; return $notehtml;
} }
/** /**
* Prepare $qa_content for showing a captcha, adding the element to $fields, given previous $errors, and a $note to display. * Prepare $qa_content for showing a captcha, adding the element to $fields, given previous $errors, and a $note to display.
* Returns JavaScript required to load CAPTCHA when field is shown by user (e.g. clicking comment button). * Returns JavaScript required to load CAPTCHA when field is shown by user (e.g. clicking comment button).
*/ */
function qa_set_up_captcha_field(&$qa_content, &$fields, $errors, $note=null) function qa_set_up_captcha_field(&$qa_content, &$fields, $errors, $note=null)
{ {
if (qa_captcha_available()) { if (!qa_captcha_available())
$captcha=qa_load_module('captcha', qa_opt('captcha_module')); return '';
$captcha = qa_load_module('captcha', qa_opt('captcha_module'));
$count=@++$qa_content['qa_captcha_count']; // work around fact that reCAPTCHA can only display per page // workaround for reCAPTCHA, to load multiple instances via JS
$count = @++$qa_content['qa_captcha_count'];
if ($count>1) if ($count > 1) {
$html=''; // single captcha will be moved about the page, to replace this // use blank captcha in order to load via JS
$html = '';
}
else { else {
$qa_content['script_var']['qa_captcha_in']='qa_captcha_div_1'; // first captcha is always loaded explicitly
$html=$captcha->form_html($qa_content, @$errors['captcha']); $qa_content['script_var']['qa_captcha_in'] = 'qa_captcha_div_1';
$html = $captcha->form_html($qa_content, @$errors['captcha']);
} }
$fields['captcha']=array( $fields['captcha'] = array(
'type' => 'custom', 'type' => 'custom',
'label' => qa_lang_html('misc/captcha_label'), 'label' => qa_lang_html('misc/captcha_label'),
'html' => '<div id="qa_captcha_div_'.$count.'">'.$html.'</div>', 'html' => '<div id="qa_captcha_div_'.$count.'">'.$html.'</div>',
...@@ -89,30 +95,22 @@ ...@@ -89,30 +95,22 @@
); );
return "if (!document.getElementById('qa_captcha_div_".$count."').hasChildNodes()) { recaptcha_load('qa_captcha_div_".$count."'); }"; return "if (!document.getElementById('qa_captcha_div_".$count."').hasChildNodes()) { recaptcha_load('qa_captcha_div_".$count."'); }";
} }
return '';
}
function qa_captcha_validate_post(&$errors) /**
/* * Check if captcha is submitted correctly, and if not, set $errors['captcha'] to a descriptive string.
Check if captcha is submitted correctly, and if not, set $errors['captcha'] to a descriptive string */
*/ function qa_captcha_validate_post(&$errors)
{ {
if (qa_captcha_available()) { if (qa_captcha_available()) {
$captcha=qa_load_module('captcha', qa_opt('captcha_module')); $captcha = qa_load_module('captcha', qa_opt('captcha_module'));
if (!$captcha->validate_post($error)) { if (!$captcha->validate_post($error)) {
$errors['captcha']=$error; $errors['captcha'] = $error;
return false; return false;
} }
} }
return true; return true;
} }
/*
Omit PHP closing tag to help avoid accidental output
*/
\ No newline at end of file
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