- md5
- 8433fa72418ad963b2a5400a5b44a930
- sha1
- f91ab266eb4b6b4ac0d745915c6fa18f41d38906
- sha256
- 12c02d9d8a815ebd6d691c9a9dc899c4438a8c9743b9a81141371bf281b66980
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
<?php
/**
* @package Joomla.Plugin
* @subpackage Captcha
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\CMS\Captcha\Google\HttpBridgePostRequestMethod;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use ReCaptcha\ReCaptcha;
/**
* Recaptcha Plugin.
* Based on the official recaptcha library( https://developers.google.com/recaptcha/docs/php )
*
* @since 2.5
*/
class PlgCaptchaRecaptcha extends CMSPlugin
{
/**
* Load the language file on instantiation.
*
* @var boolean
* @since 3.1
*/
protected $autoloadLanguage = true;
/**
* Initialise the captcha
*
* @param string $id The id of the field.
*
* @return Boolean True on success, false otherwise
*
* @throws Exception
*
* @since 2.5
*/
public function onInit($id = 'dynamic_recaptcha_1')
{
$pubkey = $this->params->get('public_key', '');
if ($pubkey === '')
{
throw new Exception(Text::_('PLG_RECAPTCHA_ERROR_NO_PUBLIC_KEY'));
}
// Load callback first for browser compatibility
HTMLHelper::_('script', 'plg_captcha_recaptcha/recaptcha.min.js', array('version' => 'auto', 'relative' => true));
HTMLHelper::_(
'script',
'https://www.google.com/recaptcha/api.js?onload=JoomlaInitReCaptcha2&render=explicit&hl=' . Factory::getLanguage()->getTag()
);
return true;
}
/**
* Gets the challenge HTML
*
* @param string $name The name of the field. Not Used.
* @param string $id The id of the field.
* @param string $class The class of the field. This should be passed as
* e.g. 'class="required"'.
*
* @return string The HTML to be embedded in the form.
*
* @since 2.5
*/
public function onDisplay($name = null, $id = 'dynamic_recaptcha_1', $class = '')
{
return '<div id="' . $id . '" ' . str_replace('class="', 'class="g-recaptcha ', $class)
. ' data-sitekey="' . $this->params->get('public_key', '')
. '" data-theme="' . $this->params->get('theme2', 'light')
. '" data-size="' . $this->params->get('size', 'normal')
. '"></div>';
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
*
* @param string $code Answer provided by user. Not needed for the Recaptcha implementation
*
* @return True if the answer is correct, false otherwise
*
* @since 2.5
*/
public function onCheckAnswer($code = null)
{
$input = Factory::getApplication()->input;
$privatekey = $this->params->get('private_key');
$version = $this->params->get('version', '2.0');
$remoteip = $input->server->get('REMOTE_ADDR', '', 'string');
$challenge = null;
$response = null;
$spam = false;
switch ($version)
{
case '2.0':
// Challenge Not needed in 2.0 but needed for getResponse call
$challenge = null;
$response = $input->get('g-recaptcha-response', '', 'string');
$spam = ($response === '');
break;
}
// Check for Private Key
if (empty($privatekey))
{
throw new RuntimeException(Text::_('PLG_RECAPTCHA_ERROR_NO_PRIVATE_KEY'), 500);
}
// Check for IP
if (empty($remoteip))
{
throw new RuntimeException(Text::_('PLG_RECAPTCHA_ERROR_NO_IP'), 500);
}
// Discard spam submissions
if ($spam)
{
throw new RuntimeException(Text::_('PLG_RECAPTCHA_ERROR_EMPTY_SOLUTION'), 500);
}
return $this->getResponse($privatekey, $remoteip, $response, $challenge);
}
/**
* Get the reCaptcha response.
*
* @param string $privatekey The private key for authentication.
* @param string $remoteip The remote IP of the visitor.
* @param string $response The response received from Google.
*
* @return bool True if response is good | False if response is bad.
*
* @since 3.4
*/
private function getResponse(string $privatekey, string $remoteip, string $response)
{
$version = $this->params->get('version', '2.0');
switch ($version)
{
case '2.0':
$apiResponse = (new ReCaptcha($privatekey, new HttpBridgePostRequestMethod))->verify($response, $remoteip);
if (!$apiResponse->isSuccess())
{
foreach ($apiResponse->getErrorCodes() as $error)
{
throw new RuntimeException($error, 403);
}
return false;
}
break;
}
return true;
}
}
The same content is in 2 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha6. See them all, or every version of this file.