Because of the server environment and other immutable factors, we can only modify the program source code to support the new reCAPTCHA v2.
Download the
check_recaptcha
andload_recaptcha
components from theadministrator/components
directoryadministrator └──components ├── check_recaptcha └── load_recaptcha
Open the
check_recaptcha/check_recaptcha.php
2.1. Download the ReCaptcha = 1.1.2
library and unzip it into check_recaptcha/vendor
# Download link
https://github.com/google/recaptcha/archive/1.1.2.tar.gz
# Tree
.
├── check_recaptcha.ctp
├── check_recaptcha.php
├── index.html
└── vendor
└── ReCaptcha
├── ReCaptcha
│ ├── ReCaptcha.php
│ ├── RequestMethod
│ │ ├── Curl.php
│ │ ├── CurlPost.php
│ │ ├── Post.php
│ │ ├── Socket.php
│ │ └── SocketPost.php
│ ├── RequestMethod.php
│ ├── RequestParameters.php
│ └── Response.php
└── autoload.php
2.2. Add the following line
require_once dirname(__FILE__).'/vendor/ReCaptcha/autoload.php';
use ReCaptcha\ReCaptcha;
2.3. change run
, recaptcha_check_answer
and load
methods, and then remove the _recaptcha_http_post
methods
function run($form, $actiondata)
{
$params = new JParameter($actiondata->params);
if(!defined('RECAPTCHA_VERIFY_SERVER')){
define('RECAPTCHA_VERIFY_SERVER', $params->get('verify_server'));
}
$resp = $this->recaptcha_check_answer(
$params->get('private_key'),
JRequest::getVar("g-recaptcha-response"),
$_SERVER["REMOTE_ADDR"]
);
if ( !$resp->is_valid ) {
$form->validation_errors['recaptcha'] = $params->get('error', "The reCAPTCHA wasn't entered correctly. Please try it again.");
$form->debug[] = "( reCAPTCHA said: ".$resp->error." )";
$this->events['fail'] = 1;
}else{
$this->events['success'] = 1;
}
}
function recaptcha_check_answer ($privkey, $recaptcha_response_param, $remoteip, $extra_params = array())
{
if ( $privkey == null || $privkey == '' ) {
die ("To use reCAPTCHA you must get an API key from
<a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
}
if ( $remoteip == null || $remoteip == '' ) {
die ("For security reasons, you must pass the remote ip to reCAPTCHA");
}
//discard spam submissions
if ($recaptcha_response_param == null || strlen($recaptcha_response_param) == 0) {
$recaptcha_response = new CFReCaptchaResponse();
$recaptcha_response->is_valid = false;
$recaptcha_response->error = 'incorrect-captcha-sol';
return $recaptcha_response;
}
$recaptcha = new ReCaptcha($privkey);
$response = $recaptcha->verify($recaptcha_response_param, $remoteip);
$recaptcha_response = new CFReCaptchaResponse();
if ($response->isSuccess()) {
$recaptcha_response->is_valid = true;
} else {
$recaptcha_response->is_valid = false;
$recaptcha_response->error = array_shift($response->getErrorCodes());
}
return $recaptcha_response;
}
function load($clear){
if($clear){
$action_params = array(
'private_key' => 'DEMO-PUBLIC-KEY',
'verify_server' => 'www.google.com',
'error' => "The reCAPTCHA wasn't entered correctly. Please try it again."
);
}
return array('action_params' => $action_params);
}
- Open the
load_recaptcha/load_recaptcha.php
3.1. Change run
, recaptcha_get_html
and load
methods
function run($form, $actiondata)
{
$mainframe = JFactory::getApplication();
$params = new JParameter($actiondata->params);
if(!defined('RECAPTCHA_API_SERVER')){
define('RECAPTCHA_API_SERVER', $params->get('api_server'));
}
if(!defined('RECAPTCHA_API_SECURE_SERVER')){
define('RECAPTCHA_API_SECURE_SERVER', $params->get('api_secure_server'));
}
if (!defined('RECAPTCHA_API_LANG')) {
define('RECAPTCHA_API_LANG', $params->get('lang', 'en'));
}
if (!defined('RECAPTCHA_API_THEME')) {
define('RECAPTCHA_API_THEME', $params->get('theme', 'light'));
}
$recaptcha_load = "<div id='recaptcha'>".$this->recaptcha_get_html($params->get('public_key'))."</div>";
//replace the string
$form->form_details->content = str_replace('{ReCaptcha}', $recaptcha_load, $form->form_details->content);
}
function recaptcha_get_html($pubkey, $error = null, $use_ssl = false)
{
if ( $pubkey == null || $pubkey == '' ) {
die ("To use reCAPTCHA you must get an API key from
<a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
}
if ( $use_ssl ) {
$server = RECAPTCHA_API_SECURE_SERVER;
} else {
$server = RECAPTCHA_API_SERVER;
}
$html = '<div class="g-recaptcha" data-sitekey="'.$pubkey.'" data-theme="'.RECAPTCHA_API_THEME.'"></div>';
$html .= '<script type="text/javascript" src="'.$server.'/api.js?hl='.RECAPTCHA_API_LANG.'"></script>';
return $html;
}
function load($clear)
{
if ( $clear ) {
$action_params = array(
'public_key' => 'DEMO-PUBLIC-KEY',
'ssl_server' => '0',
'theme' => 'white',
'lang' => 'en',
'api_server' => 'http://www.google.com/recaptcha',
'api_secure_server' => 'https://www.google.com/recaptcha'
);
}
return array('action_params' => $action_params);
}
3.2. Open the load_recaptcha/load_recaptcha.ctp
, and change the recaptcha theme options
<?php echo $HtmlHelper->input('action_load_recaptcha_{n}_theme_config', array(
'type' => 'select',
'label' => 'Theme',
'options' => array(
'light' => 'Light',
'dark' => 'Dark'
)
)
);
?>
- Save and upload the changed file to server, and login to the control panel update the reCAPTCHA public and private key
- Finished