Skip to content

Commit

Permalink
Merge pull request #202 from D3strukt0r/master
Browse files Browse the repository at this point in the history
"google/recaptcha" library | PhpUnit | Symfony recipes | Travis-CI | Bugfix
  • Loading branch information
excelwebzone committed May 22, 2018
2 parents 6166ea6 + 7a2caf1 commit be0488f
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 108 deletions.
47 changes: 34 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
language: php

php:
- 5.6
- 7.0
- 7.1
sudo: false
dist: trusty

cache:
directories:
- $HOME/.composer/cache/files

matrix:
fast_finish: true
include:
# test 2.8 LTS
- php: 5.6
env: SYMFONY_VERSION=2.8.*
# test the latest stable 3.x release
env: SYMFONY_VERSION=^2.8
- php: 5.6
env: SYMFONY_VERSION=^3.0
# test the latest release (including beta releases)
env: SYMFONY_VERSION=^3

- php: 7.0
env: SYMFONY_VERSION=^2.8
- php: 7.0
env: SYMFONY_VERSION=^3

- php: 7.1
env: DEPENDENCIES=beta
env: SYMFONY_VERSION=^2.8
- php: 7.1
env: SYMFONY_VERSION=^3
- php: 7.1
env: SYMFONY_VERSION=^4

- php: 7.2
env: SYMFONY_VERSION=^2.8
- php: 7.2
env: SYMFONY_VERSION=^3
- php: 7.2
env: SYMFONY_VERSION=^4

before_script:
- if [ "$DEPENDENCIES" = "beta" ]; then composer config minimum-stability beta; fi;
- composer self-update
- |
if [ "$SYMFONY_VERSION" != "" ]; then
composer require "symfony/form:${SYMFONY_VERSION}" --no-update
composer require "symfony/framework-bundle:${SYMFONY_VERSION}" --no-update
composer require "symfony/security-bundle:${SYMFONY_VERSION}" --no-update
composer require "symfony/validator:${SYMFONY_VERSION}" --no-update
fi
- composer install

script:
- vendor/bin/phpunit
- phpunit
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "excelwebzone/recaptcha-bundle",
"description": "This bundle provides easy reCAPTCHA form field integration",
"keywords": ["recaptcha"],
"keywords": [
"recaptcha"
],
"homepage": "https://github.com/excelwebzone/EWZRecaptchaBundle",
"type": "symfony-bundle",
"license": "MIT",
Expand All @@ -14,13 +16,14 @@
],
"require": {
"php": ">=5.6 || ^7.0",
"google/recaptcha": "^1.1",
"symfony/form": "^2.8 || ^3.0 || ^4.0",
"symfony/security": "^2.8 || ^3.0 || ^4.0",
"symfony/validator": "^2.8 || ^3.0 || ^4.0",
"symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0"
"symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0",
"symfony/security-bundle": "^2.8 || ^3.0 || ^4.0",
"symfony/validator": "^2.8 || ^3.0 || ^4.0"
},
"require-dev": {
"phpunit/phpunit": "~5.3"
"phpunit/phpunit": "^5 || ^6 || ^7"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 13 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<?xml version="1.0" encoding="UTF-8" ?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd"
bootstrap="./tests/bootstrap.php"
colors="true">

<testsuites>
<testsuite name="EWZRecaptchaBundle">
<directory>tests/</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>

</phpunit>
14 changes: 9 additions & 5 deletions src/DependencyInjection/EWZRecaptchaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,30 @@ public function load(array $configs, ContainerBuilder $container)

/**
* Registers the form widget.
*
* @param ContainerBuilder $container
*/
protected function registerWidget(ContainerBuilder $container)
{
$templatingEngines = $container->getParameter('templating.engines');
$templatingEngines = $container->hasParameter('templating.engines')
? $container->getParameter('templating.engines')
: array('twig');

if (in_array('php', $templatingEngines)) {
$formRessource = 'EWZRecaptchaBundle:Form';
$formResource = 'EWZRecaptchaBundle:Form';

$container->setParameter('templating.helper.form.resources', array_merge(
$container->getParameter('templating.helper.form.resources'),
array($formRessource)
array($formResource)
));
}

if (in_array('twig', $templatingEngines)) {
$formRessource = 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig';
$formResource = 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig';

$container->setParameter('twig.form.resources', array_merge(
$this->getTwigFormResources($container),
array($formRessource)
array($formResource)
));
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/Extension/ReCaptcha/RequestMethod/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod;

use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;

/**
* Sends POST requests to the reCAPTCHA service.
*/
class Post implements RequestMethod
{
/**
* The reCAPTCHA verify server URL.
*
* @var string
*/
private $recaptchaVerifyUrl;

/**
* Constructor
*
* @param string $recaptchaVerifyServer
*/
public function __construct($recaptchaVerifyServer)
{
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
}

/**
* Submit the POST request with the specified parameters.
*
* @param RequestParameters $params Request parameters
* @return string Body of the reCAPTCHA response
*/
public function submit(RequestParameters $params)
{
/**
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $params->toQueryString(),
// Force the peer to validate (not needed in 5.6.0+, but still works)
'verify_peer' => true,
// Force the peer validation to use www.google.com
$peer_key => 'www.google.com',
),
);
$context = stream_context_create($options);
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
}
}
70 changes: 70 additions & 0 deletions src/Extension/ReCaptcha/RequestMethod/ProxyPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod;

use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;

/**
* Sends POST requests to the reCAPTCHA service though a proxy.
*/
class ProxyPost implements RequestMethod
{
/**
* HTTP Proxy informations.
*
* @var array
*/
private $httpProxy;

/**
* The reCAPTCHA verify server URL.
*
* @var string
*/
private $recaptchaVerifyUrl;

/**
* Constructor
*
* @param array $httpProxy proxy data to connect to
* @param string $recaptchaVerifyServer
*/
public function __construct(array $httpProxy, $recaptchaVerifyServer)
{
$this->httpProxy = $httpProxy;
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
}

/**
* Submit the POST request with the specified parameters.
*
* @param RequestParameters $params Request parameters
* @return string Body of the reCAPTCHA response
*/
public function submit(RequestParameters $params)
{
/**
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n".sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth'])),
'method' => 'POST',
'content' => $params->toQueryString(),
// Force the peer to validate (not needed in 5.6.0+, but still works)
'verify_peer' => true,
// Force the peer validation to use www.google.com
$peer_key => 'www.google.com',

'proxy' => sprintf('tcp://%s:%s', $this->httpProxy['host'], $this->httpProxy['port']),
// While this is a non-standard request format, some proxy servers require it.
'request_fulluri' => true,
),
);
$context = stream_context_create($options);
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
}
}

0 comments on commit be0488f

Please sign in to comment.