Source for file recaptcha.php
Documentation is available at recaptcha.php
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* Based on the official recaptcha library( https://developers.google.com/recaptcha/docs/php )
* Load the language file on instantiation.
* @param string $id The id of the field.
* @return Boolean True on success, false otherwise
$lang =
$this->_getLanguage();
$pubkey =
$this->params->get('public_key', '');
$theme =
$this->params->get('theme', 'clean');
if ($pubkey ==
null ||
$pubkey ==
'')
throw
new Exception(JText::_('PLG_RECAPTCHA_ERROR_NO_PUBLIC_KEY'));
$server =
self::RECAPTCHA_API_SERVER;
if ($app->isSSLConnection())
$server =
self::RECAPTCHA_API_SECURE_SERVER;
JHtml::_('script', $server .
'/js/recaptcha_ajax.js');
$document->addScriptDeclaration('window.addEvent(\'domready\', function()
Recaptcha.create("' .
$pubkey .
'", "dynamic_recaptcha_1", {theme: "' .
$theme .
'",' .
$lang .
'tabindex: 0});});'
* Gets the challenge HTML
* @param string $name The name of the field.
* @param string $id The id of the field.
* @param string $class The class of the field.
* @return string The HTML to be embedded in the form.
public function onDisplay($name, $id, $class)
return '<div id="dynamic_recaptcha_1"></div>';
* Calls an HTTP POST function to verify if the user's guess was correct
* @return True if the answer is correct, false otherwise
$privatekey =
$this->params->get('private_key');
$remoteip =
$input->server->get('REMOTE_ADDR', '', 'string');
$challenge =
$input->get('recaptcha_challenge_field', '', 'string');
$response =
$input->get('recaptcha_response_field', '', 'string');
$this->_subject->setError(JText::_('PLG_RECAPTCHA_ERROR_NO_PRIVATE_KEY'));
// Discard spam submissions
if ($challenge ==
null ||
strlen($challenge) ==
0 ||
$response ==
null ||
strlen($response) ==
0)
$this->_subject->setError(JText::_('PLG_RECAPTCHA_ERROR_EMPTY_SOLUTION'));
$response =
$this->_recaptcha_http_post(
self::RECAPTCHA_VERIFY_SERVER, "/verify",
'privatekey' =>
$privatekey,
'challenge' =>
$challenge,
$answers =
explode("\n", $response[1]);
if (trim($answers[0]) ==
'true')
// @todo use exceptions here
* Encodes the given data into a query string format.
* @param array $data Array of string elements to be encoded
* @return string Encoded request
private function _recaptcha_qsencode($data)
foreach ($data as $key =>
$value)
* Submits an HTTP POST to a reCAPTCHA server.
private function _recaptcha_http_post($host, $path, $data, $port =
80)
$req =
$this->_recaptcha_qsencode($data);
$http_request =
"POST $path HTTP/1.0\r\n";
$http_request .=
"Host: $host\r\n";
$http_request .=
"Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .=
"Content-Length: " .
strlen($req) .
"\r\n";
$http_request .=
"User-Agent: reCAPTCHA/PHP\r\n";
if (($fs =
@fsockopen($host, $port, $errno, $errstr, 10)) ==
false )
die('Could not open socket');
$response .=
fgets($fs, 1160);
$response =
explode("\r\n\r\n", $response, 2);
* Get the language tag or a custom translation
private function _getLanguage()
$tag =
explode('-', $language->getTag());
$available =
array('en', 'pt', 'fr', 'de', 'nl', 'ru', 'es', 'tr');
return "lang : '" .
$tag .
"',";
// If the default language is not available, let's search for a custom translation
if ($language->hasKey('PLG_RECAPTCHA_CUSTOM_LANG'))
$custom[] =
'custom_translations : {';
$custom[] =
"\t" .
'instructions_visual : "' .
JText::_('PLG_RECAPTCHA_INSTRUCTIONS_VISUAL') .
'",';
$custom[] =
"\t" .
'instructions_audio : "' .
JText::_('PLG_RECAPTCHA_INSTRUCTIONS_AUDIO') .
'",';
$custom[] =
"\t" .
'play_again : "' .
JText::_('PLG_RECAPTCHA_PLAY_AGAIN') .
'",';
$custom[] =
"\t" .
'cant_hear_this : "' .
JText::_('PLG_RECAPTCHA_CANT_HEAR_THIS') .
'",';
$custom[] =
"\t" .
'visual_challenge : "' .
JText::_('PLG_RECAPTCHA_VISUAL_CHALLENGE') .
'",';
$custom[] =
"\t" .
'audio_challenge : "' .
JText::_('PLG_RECAPTCHA_AUDIO_CHALLENGE') .
'",';
$custom[] =
"\t" .
'refresh_btn : "' .
JText::_('PLG_RECAPTCHA_REFRESH_BTN') .
'",';
$custom[] =
"\t" .
'help_btn : "' .
JText::_('PLG_RECAPTCHA_HELP_BTN') .
'",';
$custom[] =
"\t" .
'incorrect_try_again : "' .
JText::_('PLG_RECAPTCHA_INCORRECT_TRY_AGAIN') .
'",';
$custom[] =
"lang : '" .
$tag .
"',";
// If nothing helps fall back to english
Documentation generated on Tue, 19 Nov 2013 15:11:37 +0100 by phpDocumentor 1.4.3