includes/captcha/plugins/phpbb_recaptcha_plugin.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/captcha/plugins/phpbb_recaptcha_plugin.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,345 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package VC
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2006, 2008 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +if (!class_exists('phpbb_default_captcha'))
    1.23 +{
    1.24 +	// we need the classic captcha code for tracking solutions and attempts
    1.25 +	include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx);
    1.26 +}
    1.27 +
    1.28 +/**
    1.29 +* @package VC
    1.30 +*/
    1.31 +class phpbb_recaptcha extends phpbb_default_captcha
    1.32 +{
    1.33 +	var $recaptcha_server = 'http://www.google.com/recaptcha/api';
    1.34 +	var $recaptcha_server_secure = 'https://www.google.com/recaptcha/api'; // class constants :(
    1.35 +
    1.36 +	// We are opening a socket to port 80 of this host and send
    1.37 +	// the POST request asking for verification to the path specified here.
    1.38 +	var $recaptcha_verify_server = 'www.google.com';
    1.39 +	var $recaptcha_verify_path = '/recaptcha/api/verify';
    1.40 +
    1.41 +	var $challenge;
    1.42 +	var $response;
    1.43 +
    1.44 +	// PHP4 Constructor
    1.45 +	function phpbb_recaptcha()
    1.46 +	{
    1.47 +		$this->recaptcha_server = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? $this->recaptcha_server_secure : $this->recaptcha_server;
    1.48 +	}
    1.49 +
    1.50 +	function init($type)
    1.51 +	{
    1.52 +		global $config, $db, $user;
    1.53 +
    1.54 +		$user->add_lang('captcha_recaptcha');
    1.55 +		parent::init($type);
    1.56 +		$this->challenge = request_var('recaptcha_challenge_field', '');
    1.57 +		$this->response = request_var('recaptcha_response_field', '');
    1.58 +	}
    1.59 +
    1.60 +	function &get_instance()
    1.61 +	{
    1.62 +		$instance =& new phpbb_recaptcha();
    1.63 +		return $instance;
    1.64 +	}
    1.65 +
    1.66 +	function is_available()
    1.67 +	{
    1.68 +		global $config, $user;
    1.69 +		$user->add_lang('captcha_recaptcha');
    1.70 +		return (isset($config['recaptcha_pubkey']) && !empty($config['recaptcha_pubkey']));
    1.71 +	}
    1.72 +
    1.73 +	/**
    1.74 +	*  API function
    1.75 +	*/
    1.76 +	function has_config()
    1.77 +	{
    1.78 +		return true;
    1.79 +	}
    1.80 +
    1.81 +	function get_name()
    1.82 +	{
    1.83 +		return 'CAPTCHA_RECAPTCHA';
    1.84 +	}
    1.85 +
    1.86 +	function get_class_name()
    1.87 +	{
    1.88 +		return 'phpbb_recaptcha';
    1.89 +	}
    1.90 +
    1.91 +	function acp_page($id, &$module)
    1.92 +	{
    1.93 +		global $config, $db, $template, $user;
    1.94 +
    1.95 +		$captcha_vars = array(
    1.96 +			'recaptcha_pubkey'				=> 'RECAPTCHA_PUBKEY',
    1.97 +			'recaptcha_privkey'				=> 'RECAPTCHA_PRIVKEY',
    1.98 +		);
    1.99 +
   1.100 +		$module->tpl_name = 'captcha_recaptcha_acp';
   1.101 +		$module->page_title = 'ACP_VC_SETTINGS';
   1.102 +		$form_key = 'acp_captcha';
   1.103 +		add_form_key($form_key);
   1.104 +
   1.105 +		$submit = request_var('submit', '');
   1.106 +
   1.107 +		if ($submit && check_form_key($form_key))
   1.108 +		{
   1.109 +			$captcha_vars = array_keys($captcha_vars);
   1.110 +			foreach ($captcha_vars as $captcha_var)
   1.111 +			{
   1.112 +				$value = request_var($captcha_var, '');
   1.113 +				if ($value)
   1.114 +				{
   1.115 +					set_config($captcha_var, $value);
   1.116 +				}
   1.117 +			}
   1.118 +
   1.119 +			add_log('admin', 'LOG_CONFIG_VISUAL');
   1.120 +			trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($module->u_action));
   1.121 +		}
   1.122 +		else if ($submit)
   1.123 +		{
   1.124 +			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($module->u_action));
   1.125 +		}
   1.126 +		else
   1.127 +		{
   1.128 +			foreach ($captcha_vars as $captcha_var => $template_var)
   1.129 +			{
   1.130 +				$var = (isset($_REQUEST[$captcha_var])) ? request_var($captcha_var, '') : ((isset($config[$captcha_var])) ? $config[$captcha_var] : '');
   1.131 +				$template->assign_var($template_var, $var);
   1.132 +			}
   1.133 +
   1.134 +			$template->assign_vars(array(
   1.135 +				'CAPTCHA_PREVIEW'	=> $this->get_demo_template($id),
   1.136 +				'CAPTCHA_NAME'		=> $this->get_class_name(),
   1.137 +				'U_ACTION'			=> $module->u_action,
   1.138 +			));
   1.139 +
   1.140 +		}
   1.141 +	}
   1.142 +
   1.143 +	// not needed
   1.144 +	function execute_demo()
   1.145 +	{
   1.146 +	}
   1.147 +
   1.148 +	// not needed
   1.149 +	function execute()
   1.150 +	{
   1.151 +	}
   1.152 +
   1.153 +	function get_template()
   1.154 +	{
   1.155 +		global $config, $user, $template;
   1.156 +
   1.157 +		if ($this->is_solved())
   1.158 +		{
   1.159 +			return false;
   1.160 +		}
   1.161 +		else
   1.162 +		{
   1.163 +			$explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');
   1.164 +
   1.165 +			$template->assign_vars(array(
   1.166 +				'RECAPTCHA_SERVER'			=> $this->recaptcha_server,
   1.167 +				'RECAPTCHA_PUBKEY'			=> isset($config['recaptcha_pubkey']) ? $config['recaptcha_pubkey'] : '',
   1.168 +				'RECAPTCHA_ERRORGET'		=> '',
   1.169 +				'S_RECAPTCHA_AVAILABLE'		=> $this->is_available(),
   1.170 +				'S_CONFIRM_CODE'			=> true,
   1.171 +				'S_TYPE'					=> $this->type,
   1.172 +				'L_CONFIRM_EXPLAIN'			=> $explain,
   1.173 +			));
   1.174 +
   1.175 +			return 'captcha_recaptcha.html';
   1.176 +		}
   1.177 +	}
   1.178 +
   1.179 +	function get_demo_template($id)
   1.180 +	{
   1.181 +		return $this->get_template();
   1.182 +	}
   1.183 +
   1.184 +	function get_hidden_fields()
   1.185 +	{
   1.186 +		$hidden_fields = array();
   1.187 +
   1.188 +		// this is required for posting.php - otherwise we would forget about the captcha being already solved
   1.189 +		if ($this->solved)
   1.190 +		{
   1.191 +			$hidden_fields['confirm_code'] = $this->code;
   1.192 +		}
   1.193 +		$hidden_fields['confirm_id'] = $this->confirm_id;
   1.194 +		return $hidden_fields;
   1.195 +	}
   1.196 +
   1.197 +	function uninstall()
   1.198 +	{
   1.199 +		$this->garbage_collect(0);
   1.200 +	}
   1.201 +
   1.202 +	function install()
   1.203 +	{
   1.204 +		return;
   1.205 +	}
   1.206 +
   1.207 +	function validate()
   1.208 +	{
   1.209 +		if (!parent::validate())
   1.210 +		{
   1.211 +			return false;
   1.212 +		}
   1.213 +		else
   1.214 +		{
   1.215 +			return $this->recaptcha_check_answer();
   1.216 +		}
   1.217 +	}
   1.218 +
   1.219 +// Code from here on is based on recaptchalib.php
   1.220 +/*
   1.221 + * This is a PHP library that handles calling reCAPTCHA.
   1.222 + *	- Documentation and latest version
   1.223 + *		  http://recaptcha.net/plugins/php/
   1.224 + *	- Get a reCAPTCHA API Key
   1.225 + *		  http://recaptcha.net/api/getkey
   1.226 + *	- Discussion group
   1.227 + *		  http://groups.google.com/group/recaptcha
   1.228 + *
   1.229 + * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
   1.230 + * AUTHORS:
   1.231 + *   Mike Crawford
   1.232 + *   Ben Maurer
   1.233 + *
   1.234 + * Permission is hereby granted, free of charge, to any person obtaining a copy
   1.235 + * of this software and associated documentation files (the "Software"), to deal
   1.236 + * in the Software without restriction, including without limitation the rights
   1.237 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   1.238 + * copies of the Software, and to permit persons to whom the Software is
   1.239 + * furnished to do so, subject to the following conditions:
   1.240 + *
   1.241 + * The above copyright notice and this permission notice shall be included in
   1.242 + * all copies or substantial portions of the Software.
   1.243 + *
   1.244 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   1.245 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   1.246 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   1.247 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   1.248 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   1.249 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   1.250 + * THE SOFTWARE.
   1.251 + */
   1.252 +
   1.253 +	/**
   1.254 +	* Submits an HTTP POST to a reCAPTCHA server
   1.255 +	* @param string $host
   1.256 +	* @param string $path
   1.257 +	* @param array $data
   1.258 +	* @param int port
   1.259 +	* @return array response
   1.260 +	*/
   1.261 +	function _recaptcha_http_post($host, $path, $data, $port = 80)
   1.262 +	{
   1.263 +		$req = $this->_recaptcha_qsencode ($data);
   1.264 +
   1.265 +		$http_request  = "POST $path HTTP/1.0\r\n";
   1.266 +		$http_request .= "Host: $host\r\n";
   1.267 +		$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
   1.268 +		$http_request .= "Content-Length: " . strlen($req) . "\r\n";
   1.269 +		$http_request .= "User-Agent: reCAPTCHA/PHP/phpBB\r\n";
   1.270 +		$http_request .= "\r\n";
   1.271 +		$http_request .= $req;
   1.272 +
   1.273 +		$response = '';
   1.274 +		if (false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10)))
   1.275 +		{
   1.276 +			trigger_error('Could not open socket', E_USER_ERROR);
   1.277 +		}
   1.278 +
   1.279 +		fwrite($fs, $http_request);
   1.280 +
   1.281 +		while (!feof($fs))
   1.282 +		{
   1.283 +			// One TCP-IP packet
   1.284 +			$response .= fgets($fs, 1160);
   1.285 +		}
   1.286 +		fclose($fs);
   1.287 +		$response = explode("\r\n\r\n", $response, 2);
   1.288 +
   1.289 +		return $response;
   1.290 +	}
   1.291 +
   1.292 +	/**
   1.293 +	* Calls an HTTP POST function to verify if the user's guess was correct
   1.294 +	* @param array $extra_params an array of extra variables to post to the server
   1.295 +	* @return ReCaptchaResponse
   1.296 +	*/
   1.297 +	function recaptcha_check_answer($extra_params = array())
   1.298 +	{
   1.299 +		global $config, $user;
   1.300 +
   1.301 +		//discard spam submissions
   1.302 +		if ($this->challenge == null || strlen($this->challenge) == 0 || $this->response == null || strlen($this->response) == 0)
   1.303 +		{
   1.304 +			return $user->lang['RECAPTCHA_INCORRECT'];
   1.305 +		}
   1.306 +
   1.307 +		$response = $this->_recaptcha_http_post($this->recaptcha_verify_server, $this->recaptcha_verify_path,
   1.308 +			array(
   1.309 +				'privatekey'	=> $config['recaptcha_privkey'],
   1.310 +				'remoteip'		=> $user->ip,
   1.311 +				'challenge'		=> $this->challenge,
   1.312 +				'response'		=> $this->response
   1.313 +			) + $extra_params
   1.314 +		);
   1.315 +
   1.316 +		$answers = explode("\n", $response[1]);
   1.317 +
   1.318 +		if (trim($answers[0]) === 'true')
   1.319 +		{
   1.320 +			$this->solved = true;
   1.321 +			return false;
   1.322 +		}
   1.323 +		else
   1.324 +		{
   1.325 +			return $user->lang['RECAPTCHA_INCORRECT'];
   1.326 +		}
   1.327 +	}
   1.328 +
   1.329 +	/**
   1.330 +	* Encodes the given data into a query string format
   1.331 +	* @param $data - array of string elements to be encoded
   1.332 +	* @return string - encoded request
   1.333 +	*/
   1.334 +	function _recaptcha_qsencode($data)
   1.335 +	{
   1.336 +		$req = '';
   1.337 +		foreach ($data as $key => $value)
   1.338 +		{
   1.339 +			$req .= $key . '=' . urlencode(stripslashes($value)) . '&';
   1.340 +		}
   1.341 +
   1.342 +		// Cut the last '&'
   1.343 +		$req = substr($req, 0, strlen($req) - 1);
   1.344 +		return $req;
   1.345 +	}
   1.346 +}
   1.347 +
   1.348 +?>
   1.349 \ No newline at end of file