Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,39 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0, 8.1]
symfony: [2.8, 3.4, 4.4, 5.2, 6.0]
php: [7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2]
symfony: [2.8, 3.4, 4.4, 5.2, 6.0, 7.0]
exclude:
- php: 7.1
symfony: 5.2
- php: 8.0
symfony: 3.4
- php: 7.1
symfony: 6.0
- php: 7.1
symfony: 7.0
- php: 7.2
symfony: 6.0
- php: 7.2
symfony: 7.0
- php: 7.3
symfony: 6.0
- php: 7.3
symfony: 7.0
- php: 7.4
symfony: 6.0
- php: 7.4
symfony: 7.0
- php: 8.0
symfony: 3.4
- php: 8.0
symfony: 7.0
- php: 8.1
symfony: 3.4
- php: 8.1
symfony: 7.0
- php: 8.2
symfony: 3.4
- php: 8.3
symfony: 3.4
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
phpunit.xml
vendor/
composer.lock
/nbproject/
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ EWZRecaptchaBundle
[![Actions Status](https://github.com/excelwebzone/EWZRecaptchaBundle/workflows/CI/badge.svg)](https://github.com/excelwebzone/EWZRecaptchaBundle/actions)

This bundle provides easy reCAPTCHA form field for Symfony.
This is a fork of excelwebzone/EWZRecaptchaBundle compliant with Symfony 7.0 requirements.

## Changelog
29-12-2023: updated compatibilty with symfony 7.0
29-12-2023: fix some minor bugs.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
"allow-contrib": "true"
}
}
}
}
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getConfigTreeBuilder(): TreeBuilder
return $treeBuilder;
}

private function addHttpClientConfiguration(ArrayNodeDefinition $node)
private function addHttpClientConfiguration(ArrayNodeDefinition $node): void
{
$node
->children()
Expand All @@ -67,7 +67,7 @@ private function addHttpClientConfiguration(ArrayNodeDefinition $node)
;
}

private function addServiceDefinitionConfiguration(ArrayNodeDefinition $node)
private function addServiceDefinitionConfiguration(ArrayNodeDefinition $node): void
{
$node
->children()
Expand Down
9 changes: 7 additions & 2 deletions src/DependencyInjection/EWZRecaptchaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function registerWidget(ContainerBuilder $container, int $version = 2)
? $container->getParameter('templating.engines')
: array('twig');

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

$container->setParameter('templating.helper.form.resources', array_merge(
Expand All @@ -89,7 +89,12 @@ protected function registerWidget(ContainerBuilder $container, int $version = 2)
}
}

private function getTwigFormResources(ContainerBuilder $container)
/**
*
* @param ContainerBuilder $container
* @return array|string|false
*/
private function getTwigFormResources(ContainerBuilder $container): array|string|false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Union types to remove

{
if (!$container->hasParameter('twig.form.resources')) {
return [];
Expand Down
8 changes: 4 additions & 4 deletions src/Extension/ReCaptcha/RequestMethod/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ public function submit(RequestParameters $params): string
* 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(
$options = [
'http' => [
'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',
),
);
],
];
if (null !== $this->timeout) {
$options['http']['timeout'] = $this->timeout;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Extension/ReCaptcha/RequestMethod/ProxyPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function submit(RequestParameters $params): string
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peerKey = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$options = array(
'http' => array(
$options = [
'http' => [
'header' => sprintf("Content-type: application/x-www-form-urlencoded\r\n%s", $proxyAuth),
'method' => 'POST',
'content' => $params->toQueryString(),
Expand All @@ -85,8 +85,8 @@ public function submit(RequestParameters $params): string
'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,
),
);
],
];
if (null !== $this->timeout) {
$options['http']['timeout'] = $this->timeout;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Factory/EWZRecaptchaV2FormBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

class EWZRecaptchaV2FormBuilderFactory
{
private $builder;
private FormFactoryInterface $builder;

public function __construct(FormFactoryInterface $builder)
{
$this->builder = $builder;
}

public function get(array $options = array())
public function get(array $options = array()) :FormBuilderInterface
{
$constraint = array(
'constraints' => array(
$constraint = [
'constraints' => [
new IsTrue(),
),
);
],
];

return $this->builder->createBuilder(EWZRecaptchaType::class, null, array_merge($options, $constraint));
}
Expand Down
13 changes: 7 additions & 6 deletions src/Factory/EWZRecaptchaV3FormBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueV3;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormBuilderInterface;

class EWZRecaptchaV3FormBuilderFactory
{
private $builder;
private FormFactoryInterface $builder;

public function __construct(FormFactoryInterface $builder)
{
$this->builder = $builder;
}

public function get(array $options = array())
public function get(array $options = []) :FormBuilderInterface
{
$constraint = array(
'constraints' => array(
$constraint = [
'constraints' => [
new IsTrueV3(),
),
);
],
];

return $this->builder->createBuilder(EWZRecaptchaV3Type::class, null, array_merge($options, $constraint));
}
Expand Down
8 changes: 4 additions & 4 deletions src/Form/Type/AbstractEWZRecaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ abstract class AbstractEWZRecaptchaType extends AbstractType
*
* @var string
*/
protected $publicKey;
protected string $publicKey;

/**
* Enable recaptcha?
*
* @var bool
*/
protected $enabled;
protected bool $enabled;

/**
* The API server host name.
*
* @var string
*/
protected $apiHost;
protected string $apiHost;

/**
* The reCAPTCHA server URL.
*
* @var string
*/
protected $recaptchaApiServer;
protected string $recaptchaApiServer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please removes all property types from that class


/**
* @param string $publicKey Recaptcha public key
Expand Down
22 changes: 12 additions & 10 deletions src/Form/Type/EWZRecaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function __construct(string $publicKey, bool $enabled, bool $ajax, Locale
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
$resolver->setDefaults(
[
'compound' => false,
'language' => $this->localeResolver->resolve(),
'public_key' => null,
Expand All @@ -61,11 +62,12 @@ public function configureOptions(OptionsResolver $resolver): void
'badge' => null,
),
),
));
]);
}

/**
* {@inheritdoc}
*
* @return string
*/
public function getParent(): string
{
Expand All @@ -79,7 +81,7 @@ public function getParent(): string
*
* @return string The javascript source URL
*/
public function getScriptURL($key)
public function getScriptURL(string $key): ?string
{
return isset($this->scripts[$key]) ? $this->scripts[$key] : null;
}
Expand All @@ -89,22 +91,22 @@ public function getScriptURL($key)
*/
protected function addCustomVars(FormView $view, FormInterface $form, array $options): void
{
$view->vars = array_replace($view->vars, array(
$view->vars = array_replace($view->vars, [
'ewz_recaptcha_ajax' => $this->ajax,
));
]);

if (!isset($options['language'])) {
$options['language'] = $this->localeResolver->resolve();
}

if (!$this->ajax) {
$view->vars = array_replace($view->vars, array(
$view->vars = array_replace($view->vars, [
'url_challenge' => sprintf('%s?hl=%s', $this->recaptchaApiServer, $options['language']),
));
]);
} else {
$view->vars = array_replace($view->vars, array(
$view->vars = array_replace($view->vars, [
'url_api' => sprintf('//%s/recaptcha/api/js/recaptcha_ajax.js', $this->apiHost),
));
]);
}
}
}
3 changes: 2 additions & 1 deletion src/Form/Type/EWZRecaptchaV3Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function configureOptions(OptionsResolver $resolver): void
}

/**
* {@inheritdoc}
*
* @return string
*/
public function getParent(): string
{
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/views/Form/ewz_recaptcha_widget.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

<script>
var onReCaptchaSuccess = function() {
var errorDivs = document.getElementsByClassName('recaptcha-error');
const errorDivs = document.getElementsByClassName('recaptcha-error');
if (errorDivs.length) {
errorDivs[0].className = '';
}

var errorMsgs = document.getElementsByClassName('recaptcha-error-message');
const errorMsgs = document.getElementsByClassName('recaptcha-error-message');
if (errorMsgs.length) {
errorMsgs[0].parentNode.removeChild(errorMsgs[0]);
}

var forms = document.getElementsByClassName('recaptcha-form');
const forms = document.getElementsByClassName('recaptcha-form');
if (forms.length) {
var recaptchaSubmitEvent = document.createEvent('Event');
let recaptchaSubmitEvent = document.createEvent('Event');
recaptchaSubmitEvent.initEvent('submit', true, true);
forms[0].addEventListener('submit', function (e) {
e.target.submit();
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/views/Form/ewz_recaptcha_widget.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

<script>
var onReCaptchaSuccess = function() {
var errorDivs = document.getElementsByClassName('recaptcha-error');
const errorDivs = document.getElementsByClassName('recaptcha-error');
if (errorDivs.length) {
errorDivs[0].className = '';
}
var errorMsgs = document.getElementsByClassName('recaptcha-error-message');
const errorMsgs = document.getElementsByClassName('recaptcha-error-message');
if (errorMsgs.length) {
errorMsgs[0].parentNode.removeChild(errorMsgs[0]);
}
var forms = document.getElementsByClassName('recaptcha-form');
const forms = document.getElementsByClassName('recaptcha-form');
if (forms.length) {
var recaptchaSubmitEvent = document.createEvent('Event');
let recaptchaSubmitEvent = document.createEvent('Event');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to Symfony 7

recaptchaSubmitEvent.initEvent('submit', true, true);
forms[0].addEventListener('submit', function (e) {
e.target.submit();
Expand Down
5 changes: 3 additions & 2 deletions src/Validator/Constraints/IsTrue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace EWZ\Bundle\RecaptchaBundle\Validator\Constraints;

use Attribute;
use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target("PROPERTY")
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
class IsTrue extends Constraint
{
public $message = 'This value is not a valid captcha.';
Expand All @@ -26,7 +27,7 @@ public function __construct(array $options = null, string $message = null, strin
/**
* @return string|string[]
*/
public function getTargets()
public function getTargets(): array|string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Union Type is not supported by PHP 7.x, please remove it

{
return Constraint::PROPERTY_CONSTRAINT;
}
Expand Down
Loading