<?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 KeyCAPTCHA Verification (https://www.keycaptcha.com/)
 *
 * @package 	vBulletin
 * @version		$Revision: 29078 $
 * @date 		$Date: 2009-01-12 06:11:24 -0600 (Mon, 12 Jan 2009) $
 *
 */
class vB_HumanVerify_KeyCAPTCHA extends vB_HumanVerify_Abstract
{
	/**
	 * KeyCAPTCHA user ID
	 *
	 * @var integer
	 */
	var $userid = '';
	
	/**
	 * KeyCAPTCHA private key
	 *
	 * @var string
	 */
	var $privatekey = '';
	
	/**
	 * Constructor
	 *
	 * @return	void
	 */
	function __construct(&$registry)
	{
		parent::__construct($registry);
		
		$options = explode('0', trim($this->registry->options['hv_keycaptcha_privatekey']), 2);
		
		$this->privatekey = $options[0];
		$this->userid = intval($options[1]);
	}

	/**
	 * Verify is supplied token/reponse is valid
	 *
	 * @param	array	Values given by user 'input' and 'hash'
	 *
	 * @return	bool
	 */
	function verify_token($input)
	{
		if ($input['hash'])
		{
			$parts = explode('|', $input['hash']);
			if (count($parts) < 4)
			{
				$this->error = 'humanverify_keycaptcha_wrong_solution';
				return false;
			}
			else if ($parts[0] !== md5('accept' . $parts[1] . $this->privatekey . $parts[2]))
			{
				$this->error = 'humanverify_keycaptcha_wrong_solution';
				return false;
			}
			else if (substr($parts[2], 0, 7) !== 'http://')
			{
				$this->error = 'humanverify_keycaptcha_wrong_solution';
				return false;
			}
			else
			{
				$vurl = new vB_vURL($this->registry);
				$vurl->set_option(VURL_URL, $parts[2]);
				$vurl->set_option(VURL_USERAGENT, 'vBulletin ' . FILE_VERSION);
				$vurl->set_option(VURL_RETURNTRANSFER, 1);
				$vurl->set_option(VURL_CLOSECONNECTION, 1);
				
				if (($result = $vurl->exec()) === false)
				{
					$this->error = 'humanverify_unreachable';
					return false;
				}
				else
				{
					return (trim($result) == '1');
				}
			}
		}
		else
		{
			$this->error = 'humanverify_keycaptcha_wrong_solution';
			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();
		$sessionid = uniqid('vbkeycaptcha');
		
		$humanverify['userid'] = $this->userid;
		$humanverify['sessionid'] = $sessionid;
		$humanverify['sign'] = md5($sessionid . IPADDRESS . $this->privatekey);
		$humanverify['sign2'] = md5($sessionid  . $this->privatekey);

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

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

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