Skip to content

Commit

Permalink
Allow override of reCAPTCHA request method (cf. issue ltb-project#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
plewin committed Aug 9, 2017
1 parent 6ddbc1b commit e15491c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 7 deletions.
3 changes: 3 additions & 0 deletions conf/config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@
$recaptcha_theme = "light";
$recaptcha_type = "image";
$recaptcha_size = "normal";
# reCAPTCHA request method, null for default, Fully Qualified Class Name to override
# Useful when allow_url_fopen=0 ex. $recaptcha_request_method = '\ReCaptcha\RequestMethod\Socket';
$recaptcha_request_method = null;

## Default action
# change
Expand Down
2 changes: 1 addition & 1 deletion pages/change.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
#==============================================================================
if ( $result === "" ) {
if ( $use_recaptcha) {
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey);
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey, is_null($recaptcha_request_method) ? null : new $recaptcha_request_method());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$result = "badcaptcha";
Expand Down
2 changes: 1 addition & 1 deletion pages/changesshkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#==============================================================================
if ( $result === "" ) {
if ( $use_recaptcha) {
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey);
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey, is_null($recaptcha_request_method) ? null : new $recaptcha_request_method());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$result = "badcaptcha";
Expand Down
2 changes: 1 addition & 1 deletion pages/resetbyquestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#==============================================================================
if ( $result === "" ) {
if ( $use_recaptcha ) {
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey);
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey, is_null($recaptcha_request_method) ? null : new $recaptcha_request_method());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$result = "badcaptcha";
Expand Down
2 changes: 1 addition & 1 deletion pages/resetbytoken.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
#==============================================================================
if ( $result === "" ) {
if ( $use_recaptcha ) {
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey);
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey, is_null($recaptcha_request_method) ? null : new $recaptcha_request_method());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$result = "badcaptcha";
Expand Down
2 changes: 1 addition & 1 deletion pages/sendsms.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
#==============================================================================
if ( $result === "" ) {
if ( $use_recaptcha ) {
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey);
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey, is_null($recaptcha_request_method) ? null : new $recaptcha_request_method());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$result = "badcaptcha";
Expand Down
2 changes: 1 addition & 1 deletion pages/sendtoken.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#==============================================================================
if ( $result === "" ) {
if ( $use_recaptcha ) {
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey);
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey, is_null($recaptcha_request_method) ? null : new $recaptcha_request_method());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$result = "badcaptcha";
Expand Down
2 changes: 1 addition & 1 deletion pages/setquestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#==============================================================================
if ( $result === "" ) {
if ( $use_recaptcha) {
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey);
$recaptcha = new \ReCaptcha\ReCaptcha($recaptcha_privatekey, is_null($recaptcha_request_method) ? null : new $recaptcha_request_method());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$result = "badcaptcha";
Expand Down
26 changes: 26 additions & 0 deletions tests/ReCaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require_once __DIR__ . '/../lib/vendor/autoload.php';

class ReCaptchaTest extends \PHPUnit_Framework_TestCase
{
/**
* Verifies that alternative request method is available (for $recaptcha_request_method in config)
* @dataProvider requestMethodsProvider
* @param $requestMethod string Request method FQCN
*/
public function testRequestMethodAvailable($requestMethod)
{
$this->assertInstanceOf('\ReCaptcha\RequestMethod', new $requestMethod);
}

public function requestMethodsProvider()
{
return [
['\ReCaptcha\RequestMethod\Post'], // default reCAPTCHA request method
['\ReCaptcha\RequestMethod\SocketPost'],
['\ReCaptcha\RequestMethod\CurlPost'],
];
}
}

0 comments on commit e15491c

Please sign in to comment.