<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 3.8.4 Patch Level 5
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2009 Jelsoft Enterprises Ltd. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

if (!isset($GLOBALS['vbulletin']->db))
{
	exit;
}

require_once(DIR . '/includes/class_vurl.php');

/**
* Human Verification class for reCAPTCHA v2 Verification (https://www.google.com/recaptcha)
*
* @package 		vBulletin
* @version		$Revision: 29078 $
* @date 		$Date: 2009-01-12 06:11:24 -0600 (Mon, 12 Jan 2009) $
*
*/
class vB_HumanVerify_Recaptcha_v2 extends vB_HumanVerify_Abstract
{
	/**
	 * Constructor
	 *
	 * @return	void
	 */
	function __construct(&$registry)
	{
		parent::__construct($registry);
	}

	/**
	 * Verify is supplied token/reponse is valid
	 *
	 * @param	array	Values given by user 'input' and 'hash'
	 *
	 * @return	bool
	 */
	function verify_token($input)
	{
		$this->registry->input->clean_array_gpc('p', array(
			'g-recaptcha-response'  => TYPE_STR,
		));

		if ($this->delete_token($input['hash']) AND $this->registry->GPC['g-recaptcha-response'])
		{	// Contact https://www.google.com/recaptcha/admin
			$secret_key = ($this->registry->options['hv_recaptcha_v2_secretkey'] ? $this->registry->options['hv_recaptcha_v2_secretkey'] : '6LfHsgMAAAAAACYsFwZz6cqcG-WWnfay7NIrciyU');
			$query = array(
				'secret=' . urlencode($secret_key),
				'response=' . urlencode($this->registry->GPC['g-recaptcha-response']),
				'remoteip=' . urlencode(IPADDRESS),			
			);

			$vurl = new vB_vURL($this->registry);
			$vurl->set_option(VURL_URL, 'https://www.google.com/recaptcha/api/siteverify');
			$vurl->set_option(VURL_USERAGENT, 'vBulletin ' . FILE_VERSION);
			$vurl->set_option(VURL_POST, 1);
			$vurl->set_option(VURL_POSTFIELDS, implode('&', $query));
			$vurl->set_option(VURL_RETURNTRANSFER, 1);
			$vurl->set_option(VURL_CLOSECONNECTION, 1);

			if (($result = $vurl->exec()) === false)
			{
				$this->error = 'humanverify_unreachable';
				return false;
			}
			else
			{
				$result = json_decode($result, true);
				if ($result['success'] === true)
				{
					return true;
				}

				switch ($result['error-codes'][0])
				{
					case 'missing-input-secret':
					case 'invalid-input-secret':
						$this->error = 'humanverify_recaptcha_v2_invalid_secret';
						break;
					case 'missing-input-response':
					case 'invalid-input-response':
						$this->error = 'humanverify_recaptcha_v2_invalid_response';
						break;
					case 'bad-request':
						$this->error = 'humanverify_recaptcha_v2_bad_request';
						break;
					default:
						$this->error = 'humanverify_captcha_wrong';
				}

				return false;
			}
		}
		else
		{
			$this->error = 'humanverify_captcha_wrong';
			return false;
		}
	}

	/**
	 * Returns the HTML to be displayed to the user for Human Verification
	 *
	 * @param	string	Passed to template
	 *
	 * @return 	string	HTML to output
	 *
	 */
	function output_token($var_prefix = 'humanverify')
	{
		global $vbphrase, $stylevar, $show;
		
		$vbulletin =& $this->registry;

		$humanverify = $this->generate_token();

		$humanverify['sitekey'] = ($this->registry->options['hv_recaptcha_v2_sitekey'] ? $this->registry->options['hv_recaptcha_v2_sitekey'] : '6LfHsgMAAAAAAMVjkB1nC_nI5qfAjVk0qxz4VtPV');
		$humanverify['theme'] = $this->registry->options['hv_recaptcha_v2_theme'];

		if (preg_match('#^([a-z]{2})-?#i', $stylevar['languagecode'], $matches))
		{
			$humanverify['langcode'] = strtolower($matches[1]);
		}

		eval('$output = "' . fetch_template('humanverify_recaptcha_v2') . '";');

		return $output;
	}

	/**
	 * expected answer - with this class, we don't know the answer
	 *
	 * @return	string
	 */
	function fetch_answer()
	{
		return '';
	}
}

/*======================================================================*\
|| ####################################################################
|| # CVS: $RCSfile$ - $Revision: 29078 $
|| ####################################################################
\*======================================================================*/
?>