qa-recaptcha-captcha.php 3.49 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-plugin/recaptcha-captcha/qa-recaptcha-captcha.php
	Description: Captcha module for reCAPTCHA


	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.

	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
*/

Scott committed
23 24 25 26 27 28 29
class qa_recaptcha_captcha
{
	private $directory;

	public function load_module($directory, $urltoroot)
	{
		$this->directory=$directory;
Scott committed
30 31 32
	}


Scott committed
33 34 35
	public function admin_form()
	{
		$saved=false;
Scott committed
36

Scott committed
37 38 39
		if (qa_clicked('recaptcha_save_button')) {
			qa_opt('recaptcha_public_key', qa_post_text('recaptcha_public_key_field'));
			qa_opt('recaptcha_private_key', qa_post_text('recaptcha_private_key_field'));
Scott committed
40

Scott committed
41
			$saved=true;
Scott committed
42 43
		}

Scott committed
44 45
		$form=array(
			'ok' => $saved ? 'reCAPTCHA settings saved' : null,
Scott committed
46

Scott committed
47 48 49 50 51
			'fields' => array(
				'public' => array(
					'label' => 'reCAPTCHA public key:',
					'value' => qa_opt('recaptcha_public_key'),
					'tags' => 'name="recaptcha_public_key_field"',
Scott committed
52 53
				),

Scott committed
54 55 56 57 58
				'private' => array(
					'label' => 'reCAPTCHA private key:',
					'value' => qa_opt('recaptcha_private_key'),
					'tags' => 'name="recaptcha_private_key_field"',
					'error' => $this->recaptcha_error_html(),
Scott committed
59
				),
Scott committed
60
			),
Scott committed
61

Scott committed
62 63 64 65 66 67 68
			'buttons' => array(
				array(
					'label' => 'Save Changes',
					'tags' => 'name="recaptcha_save_button"',
				),
			),
		);
Scott committed
69

Scott committed
70 71
		return $form;
	}
Scott committed
72 73


Scott committed
74 75 76 77
	public function recaptcha_error_html()
	{
		if (!function_exists('fsockopen'))
			return 'To use reCAPTCHA, the fsockopen() PHP function must be enabled on your server. Please check with your system administrator.';
Scott committed
78

Scott committed
79 80
		elseif ( (!strlen(trim(qa_opt('recaptcha_public_key')))) || (!strlen(trim(qa_opt('recaptcha_private_key')))) ) {
			require_once $this->directory.'recaptchalib.php';
Scott committed
81

Scott committed
82
			$url=recaptcha_get_signup_url(@$_SERVER['HTTP_HOST'], qa_opt('site_title'));
Scott committed
83

Scott committed
84
			return 'To use reCAPTCHA, you must <a href="'.qa_html($url).'">sign up</a> to get these keys.';
Scott committed
85 86
		}

Scott committed
87 88
		return null;
	}
Scott committed
89 90


Scott committed
91 92 93 94
	public function allow_captcha()
	{
		return function_exists('fsockopen') && strlen(trim(qa_opt('recaptcha_public_key'))) && strlen(trim(qa_opt('recaptcha_private_key')));
	}
Scott committed
95 96


Scott committed
97 98 99
	public function form_html(&$qa_content, $error)
	{
		require_once $this->directory.'recaptchalib.php';
Scott committed
100

Scott committed
101 102 103
		$language=qa_opt('site_language');
		if (strpos('|en|nl|fr|de|pt|ru|es|tr|', '|'.$language.'|')===false) // supported as of 3/2010
			$language='en';
Scott committed
104

Scott committed
105 106 107 108 109 110
		$qa_content['script_lines'][]=array(
			"var RecaptchaOptions={",
			"\ttheme:'white',",
			"\tlang:".qa_js($language),
			"};",
		);
Scott committed
111

Scott committed
112 113
		return recaptcha_get_html(qa_opt('recaptcha_public_key'), $error, qa_is_https_probably());
	}
Scott committed
114 115


Scott committed
116 117 118 119
	public function validate_post(&$error)
	{
		if ( (!empty($_POST['recaptcha_challenge_field'])) && (!empty($_POST['recaptcha_response_field'])) ) {
			require_once $this->directory.'recaptchalib.php';
Scott committed
120

Scott committed
121 122 123 124 125 126
			$answer=recaptcha_check_answer(
				qa_opt('recaptcha_private_key'),
				qa_remote_ip_address(),
				$_POST['recaptcha_challenge_field'],
				$_POST['recaptcha_response_field']
			);
Scott committed
127

Scott committed
128 129
			if ($answer->is_valid)
				return true;
Scott committed
130

Scott committed
131
			$error=@$answer->error;
Scott committed
132 133
		}

Scott committed
134
		return false;
Scott committed
135
	}
Scott committed
136
}