qa-app-captcha.php 3.56 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-app-captcha.php
	Version: See define()s at top of qa-include/qa-base.php
	Description: Wrapper functions and utilities for captcha modules


	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	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.

	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
		header('Location: ../');
		exit;
	}


	function qa_captcha_available()
/*
	Return whether a captcha module has been selected and it indicates that it is fully set up to go
*/
	{
		$module=qa_load_module('captcha', qa_opt('captcha_module'));
Scott Vivian committed
39

Gideon Greenspan committed
40 41
		return isset($module) && ( (!method_exists($module, 'allow_captcha')) || $module->allow_captcha());
	}
Scott Vivian committed
42 43


Gideon Greenspan committed
44
	function qa_captcha_reason_note($captchareason)
Gideon Greenspan committed
45 46 47
/*
	Return an HTML string explaining $captchareason (from qa_user_captcha_reason()) to the user about why they are seeing a captcha
*/
Gideon Greenspan committed
48 49
	{
		$notehtml=null;
Scott Vivian committed
50

Gideon Greenspan committed
51 52 53 54
		switch ($captchareason) {
			case 'login':
				$notehtml=qa_insert_login_links(qa_lang_html('misc/captcha_login_fix'));
				break;
Scott Vivian committed
55

Gideon Greenspan committed
56 57 58
			case 'confirm':
				$notehtml=qa_insert_login_links(qa_lang_html('misc/captcha_confirm_fix'));
				break;
Scott Vivian committed
59

Gideon Greenspan committed
60 61
			case 'approve':
				$notehtml=qa_lang_html('misc/captcha_approve_fix');
Scott Vivian committed
62
				break;
Gideon Greenspan committed
63
		}
Scott Vivian committed
64

Gideon Greenspan committed
65 66 67 68
		return $notehtml;
	}


Gideon Greenspan committed
69 70 71 72 73 74 75
	function qa_set_up_captcha_field(&$qa_content, &$fields, $errors, $note=null)
/*
	Prepare $qa_content for showing a captcha, adding the element to $fields, given previous $errors, and a $note to display
*/
	{
		if (qa_captcha_available()) {
			$captcha=qa_load_module('captcha', qa_opt('captcha_module'));
Scott Vivian committed
76

Gideon Greenspan committed
77
			$count=@++$qa_content['qa_captcha_count']; // work around fact that reCAPTCHA can only display per page
Scott Vivian committed
78

Gideon Greenspan committed
79 80 81 82 83 84
			if ($count>1)
				$html='[captcha placeholder]'; // single captcha will be moved about the page, to replace this
			else {
				$qa_content['script_var']['qa_captcha_in']='qa_captcha_div_1';
				$html=$captcha->form_html($qa_content, @$errors['captcha']);
			}
Scott Vivian committed
85

Gideon Greenspan committed
86 87 88
			$fields['captcha']=array(
				'type' => 'custom',
				'label' => qa_lang_html('misc/captcha_label'),
Gideon Greenspan committed
89
				'html' => '<div id="qa_captcha_div_'.$count.'">'.$html.'</div>',
Gideon Greenspan committed
90 91 92
				'error' => @array_key_exists('captcha', $errors) ? qa_lang_html('misc/captcha_error') : null,
				'note' => $note,
			);
Scott Vivian committed
93

Gideon Greenspan committed
94 95
			return "if (qa_captcha_in!='qa_captcha_div_".$count."') { document.getElementById('qa_captcha_div_".$count."').innerHTML=document.getElementById(qa_captcha_in).innerHTML; document.getElementById(qa_captcha_in).innerHTML=''; qa_captcha_in='qa_captcha_div_".$count."'; }";
		}
Scott Vivian committed
96

Gideon Greenspan committed
97 98 99 100 101 102 103 104 105 106 107
		return '';
	}


	function qa_captcha_validate_post(&$errors)
/*
	Check if captcha is submitted correctly, and if not, set $errors['captcha'] to a descriptive string
*/
	{
		if (qa_captcha_available()) {
			$captcha=qa_load_module('captcha', qa_opt('captcha_module'));
Scott Vivian committed
108

Gideon Greenspan committed
109 110 111 112 113
			if (!$captcha->validate_post($error)) {
				$errors['captcha']=$error;
				return false;
			}
		}
Scott Vivian committed
114

Gideon Greenspan committed
115 116 117 118 119 120 121
		return true;
	}


/*
	Omit PHP closing tag to help avoid accidental output
*/