includes/captcha/plugins/phpbb_recaptcha_plugin.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Thu, 08 Mar 2012 01:28:49 +0400
changeset 10 2829504a06be
parent 0 ceef6d64c21d
permissions -rw-r--r--
cubometry-style based on prosilver
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package VC
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2006, 2008 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
if (!class_exists('phpbb_default_captcha'))
indvdum@0
    20
{
indvdum@0
    21
	// we need the classic captcha code for tracking solutions and attempts
indvdum@0
    22
	include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx);
indvdum@0
    23
}
indvdum@0
    24
indvdum@0
    25
/**
indvdum@0
    26
* @package VC
indvdum@0
    27
*/
indvdum@0
    28
class phpbb_recaptcha extends phpbb_default_captcha
indvdum@0
    29
{
indvdum@0
    30
	var $recaptcha_server = 'http://www.google.com/recaptcha/api';
indvdum@0
    31
	var $recaptcha_server_secure = 'https://www.google.com/recaptcha/api'; // class constants :(
indvdum@0
    32
indvdum@0
    33
	// We are opening a socket to port 80 of this host and send
indvdum@0
    34
	// the POST request asking for verification to the path specified here.
indvdum@0
    35
	var $recaptcha_verify_server = 'www.google.com';
indvdum@0
    36
	var $recaptcha_verify_path = '/recaptcha/api/verify';
indvdum@0
    37
indvdum@0
    38
	var $challenge;
indvdum@0
    39
	var $response;
indvdum@0
    40
indvdum@0
    41
	// PHP4 Constructor
indvdum@0
    42
	function phpbb_recaptcha()
indvdum@0
    43
	{
indvdum@0
    44
		$this->recaptcha_server = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? $this->recaptcha_server_secure : $this->recaptcha_server;
indvdum@0
    45
	}
indvdum@0
    46
indvdum@0
    47
	function init($type)
indvdum@0
    48
	{
indvdum@0
    49
		global $config, $db, $user;
indvdum@0
    50
indvdum@0
    51
		$user->add_lang('captcha_recaptcha');
indvdum@0
    52
		parent::init($type);
indvdum@0
    53
		$this->challenge = request_var('recaptcha_challenge_field', '');
indvdum@0
    54
		$this->response = request_var('recaptcha_response_field', '');
indvdum@0
    55
	}
indvdum@0
    56
indvdum@0
    57
	function &get_instance()
indvdum@0
    58
	{
indvdum@0
    59
		$instance =& new phpbb_recaptcha();
indvdum@0
    60
		return $instance;
indvdum@0
    61
	}
indvdum@0
    62
indvdum@0
    63
	function is_available()
indvdum@0
    64
	{
indvdum@0
    65
		global $config, $user;
indvdum@0
    66
		$user->add_lang('captcha_recaptcha');
indvdum@0
    67
		return (isset($config['recaptcha_pubkey']) && !empty($config['recaptcha_pubkey']));
indvdum@0
    68
	}
indvdum@0
    69
indvdum@0
    70
	/**
indvdum@0
    71
	*  API function
indvdum@0
    72
	*/
indvdum@0
    73
	function has_config()
indvdum@0
    74
	{
indvdum@0
    75
		return true;
indvdum@0
    76
	}
indvdum@0
    77
indvdum@0
    78
	function get_name()
indvdum@0
    79
	{
indvdum@0
    80
		return 'CAPTCHA_RECAPTCHA';
indvdum@0
    81
	}
indvdum@0
    82
indvdum@0
    83
	function get_class_name()
indvdum@0
    84
	{
indvdum@0
    85
		return 'phpbb_recaptcha';
indvdum@0
    86
	}
indvdum@0
    87
indvdum@0
    88
	function acp_page($id, &$module)
indvdum@0
    89
	{
indvdum@0
    90
		global $config, $db, $template, $user;
indvdum@0
    91
indvdum@0
    92
		$captcha_vars = array(
indvdum@0
    93
			'recaptcha_pubkey'				=> 'RECAPTCHA_PUBKEY',
indvdum@0
    94
			'recaptcha_privkey'				=> 'RECAPTCHA_PRIVKEY',
indvdum@0
    95
		);
indvdum@0
    96
indvdum@0
    97
		$module->tpl_name = 'captcha_recaptcha_acp';
indvdum@0
    98
		$module->page_title = 'ACP_VC_SETTINGS';
indvdum@0
    99
		$form_key = 'acp_captcha';
indvdum@0
   100
		add_form_key($form_key);
indvdum@0
   101
indvdum@0
   102
		$submit = request_var('submit', '');
indvdum@0
   103
indvdum@0
   104
		if ($submit && check_form_key($form_key))
indvdum@0
   105
		{
indvdum@0
   106
			$captcha_vars = array_keys($captcha_vars);
indvdum@0
   107
			foreach ($captcha_vars as $captcha_var)
indvdum@0
   108
			{
indvdum@0
   109
				$value = request_var($captcha_var, '');
indvdum@0
   110
				if ($value)
indvdum@0
   111
				{
indvdum@0
   112
					set_config($captcha_var, $value);
indvdum@0
   113
				}
indvdum@0
   114
			}
indvdum@0
   115
indvdum@0
   116
			add_log('admin', 'LOG_CONFIG_VISUAL');
indvdum@0
   117
			trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($module->u_action));
indvdum@0
   118
		}
indvdum@0
   119
		else if ($submit)
indvdum@0
   120
		{
indvdum@0
   121
			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($module->u_action));
indvdum@0
   122
		}
indvdum@0
   123
		else
indvdum@0
   124
		{
indvdum@0
   125
			foreach ($captcha_vars as $captcha_var => $template_var)
indvdum@0
   126
			{
indvdum@0
   127
				$var = (isset($_REQUEST[$captcha_var])) ? request_var($captcha_var, '') : ((isset($config[$captcha_var])) ? $config[$captcha_var] : '');
indvdum@0
   128
				$template->assign_var($template_var, $var);
indvdum@0
   129
			}
indvdum@0
   130
indvdum@0
   131
			$template->assign_vars(array(
indvdum@0
   132
				'CAPTCHA_PREVIEW'	=> $this->get_demo_template($id),
indvdum@0
   133
				'CAPTCHA_NAME'		=> $this->get_class_name(),
indvdum@0
   134
				'U_ACTION'			=> $module->u_action,
indvdum@0
   135
			));
indvdum@0
   136
indvdum@0
   137
		}
indvdum@0
   138
	}
indvdum@0
   139
indvdum@0
   140
	// not needed
indvdum@0
   141
	function execute_demo()
indvdum@0
   142
	{
indvdum@0
   143
	}
indvdum@0
   144
indvdum@0
   145
	// not needed
indvdum@0
   146
	function execute()
indvdum@0
   147
	{
indvdum@0
   148
	}
indvdum@0
   149
indvdum@0
   150
	function get_template()
indvdum@0
   151
	{
indvdum@0
   152
		global $config, $user, $template;
indvdum@0
   153
indvdum@0
   154
		if ($this->is_solved())
indvdum@0
   155
		{
indvdum@0
   156
			return false;
indvdum@0
   157
		}
indvdum@0
   158
		else
indvdum@0
   159
		{
indvdum@0
   160
			$explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');
indvdum@0
   161
indvdum@0
   162
			$template->assign_vars(array(
indvdum@0
   163
				'RECAPTCHA_SERVER'			=> $this->recaptcha_server,
indvdum@0
   164
				'RECAPTCHA_PUBKEY'			=> isset($config['recaptcha_pubkey']) ? $config['recaptcha_pubkey'] : '',
indvdum@0
   165
				'RECAPTCHA_ERRORGET'		=> '',
indvdum@0
   166
				'S_RECAPTCHA_AVAILABLE'		=> $this->is_available(),
indvdum@0
   167
				'S_CONFIRM_CODE'			=> true,
indvdum@0
   168
				'S_TYPE'					=> $this->type,
indvdum@0
   169
				'L_CONFIRM_EXPLAIN'			=> $explain,
indvdum@0
   170
			));
indvdum@0
   171
indvdum@0
   172
			return 'captcha_recaptcha.html';
indvdum@0
   173
		}
indvdum@0
   174
	}
indvdum@0
   175
indvdum@0
   176
	function get_demo_template($id)
indvdum@0
   177
	{
indvdum@0
   178
		return $this->get_template();
indvdum@0
   179
	}
indvdum@0
   180
indvdum@0
   181
	function get_hidden_fields()
indvdum@0
   182
	{
indvdum@0
   183
		$hidden_fields = array();
indvdum@0
   184
indvdum@0
   185
		// this is required for posting.php - otherwise we would forget about the captcha being already solved
indvdum@0
   186
		if ($this->solved)
indvdum@0
   187
		{
indvdum@0
   188
			$hidden_fields['confirm_code'] = $this->code;
indvdum@0
   189
		}
indvdum@0
   190
		$hidden_fields['confirm_id'] = $this->confirm_id;
indvdum@0
   191
		return $hidden_fields;
indvdum@0
   192
	}
indvdum@0
   193
indvdum@0
   194
	function uninstall()
indvdum@0
   195
	{
indvdum@0
   196
		$this->garbage_collect(0);
indvdum@0
   197
	}
indvdum@0
   198
indvdum@0
   199
	function install()
indvdum@0
   200
	{
indvdum@0
   201
		return;
indvdum@0
   202
	}
indvdum@0
   203
indvdum@0
   204
	function validate()
indvdum@0
   205
	{
indvdum@0
   206
		if (!parent::validate())
indvdum@0
   207
		{
indvdum@0
   208
			return false;
indvdum@0
   209
		}
indvdum@0
   210
		else
indvdum@0
   211
		{
indvdum@0
   212
			return $this->recaptcha_check_answer();
indvdum@0
   213
		}
indvdum@0
   214
	}
indvdum@0
   215
indvdum@0
   216
// Code from here on is based on recaptchalib.php
indvdum@0
   217
/*
indvdum@0
   218
 * This is a PHP library that handles calling reCAPTCHA.
indvdum@0
   219
 *	- Documentation and latest version
indvdum@0
   220
 *		  http://recaptcha.net/plugins/php/
indvdum@0
   221
 *	- Get a reCAPTCHA API Key
indvdum@0
   222
 *		  http://recaptcha.net/api/getkey
indvdum@0
   223
 *	- Discussion group
indvdum@0
   224
 *		  http://groups.google.com/group/recaptcha
indvdum@0
   225
 *
indvdum@0
   226
 * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
indvdum@0
   227
 * AUTHORS:
indvdum@0
   228
 *   Mike Crawford
indvdum@0
   229
 *   Ben Maurer
indvdum@0
   230
 *
indvdum@0
   231
 * Permission is hereby granted, free of charge, to any person obtaining a copy
indvdum@0
   232
 * of this software and associated documentation files (the "Software"), to deal
indvdum@0
   233
 * in the Software without restriction, including without limitation the rights
indvdum@0
   234
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
indvdum@0
   235
 * copies of the Software, and to permit persons to whom the Software is
indvdum@0
   236
 * furnished to do so, subject to the following conditions:
indvdum@0
   237
 *
indvdum@0
   238
 * The above copyright notice and this permission notice shall be included in
indvdum@0
   239
 * all copies or substantial portions of the Software.
indvdum@0
   240
 *
indvdum@0
   241
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
indvdum@0
   242
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
indvdum@0
   243
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
indvdum@0
   244
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
indvdum@0
   245
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
indvdum@0
   246
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
indvdum@0
   247
 * THE SOFTWARE.
indvdum@0
   248
 */
indvdum@0
   249
indvdum@0
   250
	/**
indvdum@0
   251
	* Submits an HTTP POST to a reCAPTCHA server
indvdum@0
   252
	* @param string $host
indvdum@0
   253
	* @param string $path
indvdum@0
   254
	* @param array $data
indvdum@0
   255
	* @param int port
indvdum@0
   256
	* @return array response
indvdum@0
   257
	*/
indvdum@0
   258
	function _recaptcha_http_post($host, $path, $data, $port = 80)
indvdum@0
   259
	{
indvdum@0
   260
		$req = $this->_recaptcha_qsencode ($data);
indvdum@0
   261
indvdum@0
   262
		$http_request  = "POST $path HTTP/1.0\r\n";
indvdum@0
   263
		$http_request .= "Host: $host\r\n";
indvdum@0
   264
		$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
indvdum@0
   265
		$http_request .= "Content-Length: " . strlen($req) . "\r\n";
indvdum@0
   266
		$http_request .= "User-Agent: reCAPTCHA/PHP/phpBB\r\n";
indvdum@0
   267
		$http_request .= "\r\n";
indvdum@0
   268
		$http_request .= $req;
indvdum@0
   269
indvdum@0
   270
		$response = '';
indvdum@0
   271
		if (false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10)))
indvdum@0
   272
		{
indvdum@0
   273
			trigger_error('Could not open socket', E_USER_ERROR);
indvdum@0
   274
		}
indvdum@0
   275
indvdum@0
   276
		fwrite($fs, $http_request);
indvdum@0
   277
indvdum@0
   278
		while (!feof($fs))
indvdum@0
   279
		{
indvdum@0
   280
			// One TCP-IP packet
indvdum@0
   281
			$response .= fgets($fs, 1160);
indvdum@0
   282
		}
indvdum@0
   283
		fclose($fs);
indvdum@0
   284
		$response = explode("\r\n\r\n", $response, 2);
indvdum@0
   285
indvdum@0
   286
		return $response;
indvdum@0
   287
	}
indvdum@0
   288
indvdum@0
   289
	/**
indvdum@0
   290
	* Calls an HTTP POST function to verify if the user's guess was correct
indvdum@0
   291
	* @param array $extra_params an array of extra variables to post to the server
indvdum@0
   292
	* @return ReCaptchaResponse
indvdum@0
   293
	*/
indvdum@0
   294
	function recaptcha_check_answer($extra_params = array())
indvdum@0
   295
	{
indvdum@0
   296
		global $config, $user;
indvdum@0
   297
indvdum@0
   298
		//discard spam submissions
indvdum@0
   299
		if ($this->challenge == null || strlen($this->challenge) == 0 || $this->response == null || strlen($this->response) == 0)
indvdum@0
   300
		{
indvdum@0
   301
			return $user->lang['RECAPTCHA_INCORRECT'];
indvdum@0
   302
		}
indvdum@0
   303
indvdum@0
   304
		$response = $this->_recaptcha_http_post($this->recaptcha_verify_server, $this->recaptcha_verify_path,
indvdum@0
   305
			array(
indvdum@0
   306
				'privatekey'	=> $config['recaptcha_privkey'],
indvdum@0
   307
				'remoteip'		=> $user->ip,
indvdum@0
   308
				'challenge'		=> $this->challenge,
indvdum@0
   309
				'response'		=> $this->response
indvdum@0
   310
			) + $extra_params
indvdum@0
   311
		);
indvdum@0
   312
indvdum@0
   313
		$answers = explode("\n", $response[1]);
indvdum@0
   314
indvdum@0
   315
		if (trim($answers[0]) === 'true')
indvdum@0
   316
		{
indvdum@0
   317
			$this->solved = true;
indvdum@0
   318
			return false;
indvdum@0
   319
		}
indvdum@0
   320
		else
indvdum@0
   321
		{
indvdum@0
   322
			return $user->lang['RECAPTCHA_INCORRECT'];
indvdum@0
   323
		}
indvdum@0
   324
	}
indvdum@0
   325
indvdum@0
   326
	/**
indvdum@0
   327
	* Encodes the given data into a query string format
indvdum@0
   328
	* @param $data - array of string elements to be encoded
indvdum@0
   329
	* @return string - encoded request
indvdum@0
   330
	*/
indvdum@0
   331
	function _recaptcha_qsencode($data)
indvdum@0
   332
	{
indvdum@0
   333
		$req = '';
indvdum@0
   334
		foreach ($data as $key => $value)
indvdum@0
   335
		{
indvdum@0
   336
			$req .= $key . '=' . urlencode(stripslashes($value)) . '&';
indvdum@0
   337
		}
indvdum@0
   338
indvdum@0
   339
		// Cut the last '&'
indvdum@0
   340
		$req = substr($req, 0, strlen($req) - 1);
indvdum@0
   341
		return $req;
indvdum@0
   342
	}
indvdum@0
   343
}
indvdum@0
   344
indvdum@0
   345
?>