Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit be0488f

Browse files
authored
Merge pull request #202 from D3strukt0r/master
"google/recaptcha" library | PhpUnit | Symfony recipes | Travis-CI | Bugfix
2 parents 6166ea6 + 7a2caf1 commit be0488f

9 files changed

Lines changed: 211 additions & 108 deletions

File tree

.travis.yml

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
11
language: php
22

3-
php:
4-
- 5.6
5-
- 7.0
6-
- 7.1
3+
sudo: false
4+
dist: trusty
5+
6+
cache:
7+
directories:
8+
- $HOME/.composer/cache/files
79

810
matrix:
911
fast_finish: true
1012
include:
11-
# test 2.8 LTS
1213
- php: 5.6
13-
env: SYMFONY_VERSION=2.8.*
14-
# test the latest stable 3.x release
14+
env: SYMFONY_VERSION=^2.8
1515
- php: 5.6
16-
env: SYMFONY_VERSION=^3.0
17-
# test the latest release (including beta releases)
16+
env: SYMFONY_VERSION=^3
17+
18+
- php: 7.0
19+
env: SYMFONY_VERSION=^2.8
20+
- php: 7.0
21+
env: SYMFONY_VERSION=^3
22+
1823
- php: 7.1
19-
env: DEPENDENCIES=beta
24+
env: SYMFONY_VERSION=^2.8
25+
- php: 7.1
26+
env: SYMFONY_VERSION=^3
27+
- php: 7.1
28+
env: SYMFONY_VERSION=^4
29+
30+
- php: 7.2
31+
env: SYMFONY_VERSION=^2.8
32+
- php: 7.2
33+
env: SYMFONY_VERSION=^3
34+
- php: 7.2
35+
env: SYMFONY_VERSION=^4
2036

2137
before_script:
22-
- if [ "$DEPENDENCIES" = "beta" ]; then composer config minimum-stability beta; fi;
23-
- composer self-update
38+
- |
39+
if [ "$SYMFONY_VERSION" != "" ]; then
40+
composer require "symfony/form:${SYMFONY_VERSION}" --no-update
41+
composer require "symfony/framework-bundle:${SYMFONY_VERSION}" --no-update
42+
composer require "symfony/security-bundle:${SYMFONY_VERSION}" --no-update
43+
composer require "symfony/validator:${SYMFONY_VERSION}" --no-update
44+
fi
2445
- composer install
2546

2647
script:
27-
- vendor/bin/phpunit
48+
- phpunit

composer.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "excelwebzone/recaptcha-bundle",
33
"description": "This bundle provides easy reCAPTCHA form field integration",
4-
"keywords": ["recaptcha"],
4+
"keywords": [
5+
"recaptcha"
6+
],
57
"homepage": "https://github.com/excelwebzone/EWZRecaptchaBundle",
68
"type": "symfony-bundle",
79
"license": "MIT",
@@ -14,13 +16,14 @@
1416
],
1517
"require": {
1618
"php": ">=5.6 || ^7.0",
19+
"google/recaptcha": "^1.1",
1720
"symfony/form": "^2.8 || ^3.0 || ^4.0",
18-
"symfony/security": "^2.8 || ^3.0 || ^4.0",
19-
"symfony/validator": "^2.8 || ^3.0 || ^4.0",
20-
"symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0"
21+
"symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0",
22+
"symfony/security-bundle": "^2.8 || ^3.0 || ^4.0",
23+
"symfony/validator": "^2.8 || ^3.0 || ^4.0"
2124
},
2225
"require-dev": {
23-
"phpunit/phpunit": "~5.3"
26+
"phpunit/phpunit": "^5 || ^6 || ^7"
2427
},
2528
"autoload": {
2629
"psr-4": {

phpunit.xml.dist

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd"
5+
bootstrap="./tests/bootstrap.php"
6+
colors="true">
37

48
<testsuites>
59
<testsuite name="EWZRecaptchaBundle">
6-
<directory>tests/</directory>
10+
<directory>tests</directory>
711
</testsuite>
812
</testsuites>
913

14+
<filter>
15+
<whitelist>
16+
<directory suffix=".php">src</directory>
17+
</whitelist>
18+
</filter>
19+
1020
</phpunit>

src/DependencyInjection/EWZRecaptchaExtension.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,30 @@ public function load(array $configs, ContainerBuilder $container)
3232

3333
/**
3434
* Registers the form widget.
35+
*
36+
* @param ContainerBuilder $container
3537
*/
3638
protected function registerWidget(ContainerBuilder $container)
3739
{
38-
$templatingEngines = $container->getParameter('templating.engines');
40+
$templatingEngines = $container->hasParameter('templating.engines')
41+
? $container->getParameter('templating.engines')
42+
: array('twig');
3943

4044
if (in_array('php', $templatingEngines)) {
41-
$formRessource = 'EWZRecaptchaBundle:Form';
45+
$formResource = 'EWZRecaptchaBundle:Form';
4246

4347
$container->setParameter('templating.helper.form.resources', array_merge(
4448
$container->getParameter('templating.helper.form.resources'),
45-
array($formRessource)
49+
array($formResource)
4650
));
4751
}
4852

4953
if (in_array('twig', $templatingEngines)) {
50-
$formRessource = 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig';
54+
$formResource = 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig';
5155

5256
$container->setParameter('twig.form.resources', array_merge(
5357
$this->getTwigFormResources($container),
54-
array($formRessource)
58+
array($formResource)
5559
));
5660
}
5761
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod;
4+
5+
use ReCaptcha\RequestMethod;
6+
use ReCaptcha\RequestParameters;
7+
8+
/**
9+
* Sends POST requests to the reCAPTCHA service.
10+
*/
11+
class Post implements RequestMethod
12+
{
13+
/**
14+
* The reCAPTCHA verify server URL.
15+
*
16+
* @var string
17+
*/
18+
private $recaptchaVerifyUrl;
19+
20+
/**
21+
* Constructor
22+
*
23+
* @param string $recaptchaVerifyServer
24+
*/
25+
public function __construct($recaptchaVerifyServer)
26+
{
27+
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
28+
}
29+
30+
/**
31+
* Submit the POST request with the specified parameters.
32+
*
33+
* @param RequestParameters $params Request parameters
34+
* @return string Body of the reCAPTCHA response
35+
*/
36+
public function submit(RequestParameters $params)
37+
{
38+
/**
39+
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
40+
* Using "CN_name" will still work, but it will raise deprecated errors.
41+
*/
42+
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
43+
$options = array(
44+
'http' => array(
45+
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
46+
'method' => 'POST',
47+
'content' => $params->toQueryString(),
48+
// Force the peer to validate (not needed in 5.6.0+, but still works)
49+
'verify_peer' => true,
50+
// Force the peer validation to use www.google.com
51+
$peer_key => 'www.google.com',
52+
),
53+
);
54+
$context = stream_context_create($options);
55+
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
56+
}
57+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod;
4+
5+
use ReCaptcha\RequestMethod;
6+
use ReCaptcha\RequestParameters;
7+
8+
/**
9+
* Sends POST requests to the reCAPTCHA service though a proxy.
10+
*/
11+
class ProxyPost implements RequestMethod
12+
{
13+
/**
14+
* HTTP Proxy informations.
15+
*
16+
* @var array
17+
*/
18+
private $httpProxy;
19+
20+
/**
21+
* The reCAPTCHA verify server URL.
22+
*
23+
* @var string
24+
*/
25+
private $recaptchaVerifyUrl;
26+
27+
/**
28+
* Constructor
29+
*
30+
* @param array $httpProxy proxy data to connect to
31+
* @param string $recaptchaVerifyServer
32+
*/
33+
public function __construct(array $httpProxy, $recaptchaVerifyServer)
34+
{
35+
$this->httpProxy = $httpProxy;
36+
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
37+
}
38+
39+
/**
40+
* Submit the POST request with the specified parameters.
41+
*
42+
* @param RequestParameters $params Request parameters
43+
* @return string Body of the reCAPTCHA response
44+
*/
45+
public function submit(RequestParameters $params)
46+
{
47+
/**
48+
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
49+
* Using "CN_name" will still work, but it will raise deprecated errors.
50+
*/
51+
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
52+
$options = array(
53+
'http' => array(
54+
'header' => "Content-type: application/x-www-form-urlencoded\r\n".sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth'])),
55+
'method' => 'POST',
56+
'content' => $params->toQueryString(),
57+
// Force the peer to validate (not needed in 5.6.0+, but still works)
58+
'verify_peer' => true,
59+
// Force the peer validation to use www.google.com
60+
$peer_key => 'www.google.com',
61+
62+
'proxy' => sprintf('tcp://%s:%s', $this->httpProxy['host'], $this->httpProxy['port']),
63+
// While this is a non-standard request format, some proxy servers require it.
64+
'request_fulluri' => true,
65+
),
66+
);
67+
$context = stream_context_create($options);
68+
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
69+
}
70+
}

0 commit comments

Comments
 (0)