summaryrefslogtreecommitdiff
path: root/apps/um/plugins/ReCaptcha.class.php
blob: 165493bc717ef0726972950a6cba8a2d49cdc1bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// We only include the recaptchalib.php file when we use it because we don't
// want it polluting the global namespace thing.

class ReCaptcha extends Plugin {
	protected $config = array('public_key'=>'',
	                          'private_key'=>'');
	public static function description() {
		return 'Add a reCaptcha to keep out spam users.';
	}
	public static function configList() {
		return array('public_key'=>'text',
		             'private_key'=>'text');
	}
	public function init() {}

	private $resp = null;
	private function getResp() {
		if ($this->resp===null) {
			require_once('recaptchalib.php');
			@$response = $_POST['recaptcha_response_field'];
			@$challenge = $_POST['recaptcha_challenge_field'];
			$this->resp = recaptcha_check_answer($this->config['private_key'],
			                                     $_SERVER['REMOTE_ADDR'],
			                                     $challenge,
			                                     $response);
		}
		return $this->resp;
	}

	private function getError() {
		if ($_POST["recaptcha_response_field"] && !$this->antispam_verify()) {
			return $this->getResp()->error;
		} else {
			return false;
		}
	}

	public function antispam_verify() {
		return $this->getResp()->is_valid;
	}
	
	public function antispam_html() {
		require_once('recaptchalib.php');
		return recaptcha_get_html($this->config['public_key'], $this->getError());
	}
}