Skip to content

Commit c34864a

Browse files
committed
Replace recaptcha by own captcha system
1 parent 9325112 commit c34864a

20 files changed

+282
-267
lines changed

.env

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.
3232
MAILER_DSN=smtp://localhost
3333
###< symfony/mailer ###
3434

35-
GOOGLE_RECAPTCHA_SITE_KEY=
36-
GOOGLE_RECAPTCHA_SECRET_KEY=
37-
3835
# Set this setting to false to disable forum functionality
3936
UW_FORUM_ENABLED=true
4037
# Development: Override construction time to 1 second (set 0 to disable)

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"doctrine/common": "^3.5",
1212
"doctrine/doctrine-bundle": "^3.2",
1313
"doctrine/orm": "^3.6",
14-
"google/recaptcha": "^1.3",
1514
"symfony/asset": "^8.0",
1615
"symfony/console": "^8.0",
1716
"symfony/dotenv": "^8.0",

composer.lock

Lines changed: 5 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/routes.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,11 @@ Site/ResetPassword:
774774
controller: FrankProjects\UltimateWarfare\Controller\Site\ResetPasswordController::requestPasswordReset
775775
methods: [GET, POST]
776776

777+
Site/Captcha:
778+
path: /captcha
779+
controller: FrankProjects\UltimateWarfare\Controller\Site\CaptchaController::captcha
780+
methods: [GET]
781+
777782
Site/Register:
778783
path: /register
779784
controller: FrankProjects\UltimateWarfare\Controller\Site\RegisterController::register

config/services.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Put parameters here that don't need to change on each machine where the app is deployed
55
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
66
parameters:
7-
app.gg_recaptcha_site_key: '%env(GOOGLE_RECAPTCHA_SITE_KEY)%'
87
app.uw_forum_enabled: '%env(bool:UW_FORUM_ENABLED)%'
98
app.uw_construction_time_override: '%env(default::int:UW_CONSTRUCTION_TIME_OVERRIDE)%'
109
app.uw_research_time_override: '%env(default::int:UW_RESEARCH_TIME_OVERRIDE)%'
@@ -43,10 +42,6 @@ services:
4342
$researchRegistry: '@FrankProjects\UltimateWarfare\Repository\ResearchRegistry'
4443
$researchTimeOverride: '%app.uw_research_time_override%'
4544

46-
ReCaptcha\ReCaptcha:
47-
arguments:
48-
- '%env(GOOGLE_RECAPTCHA_SECRET_KEY)%'
49-
5045
Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
5146
arguments:
5247
- '%env(DATABASE_URL)%'

public/js/recaptcha.js

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FrankProjects\UltimateWarfare\Controller\Site;
6+
7+
use FrankProjects\UltimateWarfare\Service\CaptchaGenerator;
8+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9+
use Symfony\Component\HttpFoundation\Response;
10+
11+
final class CaptchaController extends AbstractController
12+
{
13+
public function __construct(
14+
private readonly CaptchaGenerator $captchaGenerator
15+
) {
16+
}
17+
18+
public function captcha(): Response
19+
{
20+
$imageData = $this->captchaGenerator->generateImage();
21+
22+
return new Response($imageData, Response::HTTP_OK, [
23+
'Content-Type' => 'image/png',
24+
'Cache-Control' => 'no-store, no-cache, must-revalidate',
25+
]);
26+
}
27+
}

src/Controller/Site/ContactController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function contact(Request $request): Response
3737
'site/contact.html.twig',
3838
[
3939
'form' => $form->createView(),
40-
'gg_recaptcha_site_key' => $this->getParameter('app.gg_recaptcha_site_key')
4140
]
4241
);
4342
}

src/Controller/Site/RegisterController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function register(Request $request): Response
4545
'site/register.html.twig',
4646
[
4747
'form' => $form->createView(),
48-
'gg_recaptcha_site_key' => $this->getParameter('app.gg_recaptcha_site_key')
4948
]
5049
);
5150
}

src/Form/CaptchaType.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FrankProjects\UltimateWarfare\Form;
6+
7+
use FrankProjects\UltimateWarfare\Form\EventListener\CaptchaValidationListener;
8+
use FrankProjects\UltimateWarfare\Service\CaptchaGenerator;
9+
use Symfony\Component\Form\AbstractType;
10+
use Symfony\Component\Form\Extension\Core\Type\TextType;
11+
use Symfony\Component\Form\FormBuilderInterface;
12+
use Symfony\Component\OptionsResolver\OptionsResolver;
13+
14+
/** @extends AbstractType<null> */
15+
class CaptchaType extends AbstractType
16+
{
17+
public function __construct(
18+
private readonly CaptchaGenerator $captchaGenerator
19+
) {
20+
}
21+
22+
public function buildForm(FormBuilderInterface $builder, array $options): void
23+
{
24+
/** @var string $invalidMessage */
25+
$invalidMessage = $options['invalid_message'];
26+
27+
$subscriber = new CaptchaValidationListener($this->captchaGenerator);
28+
$subscriber->setInvalidMessage($invalidMessage);
29+
$builder->addEventSubscriber($subscriber);
30+
}
31+
32+
public function configureOptions(OptionsResolver $resolver): void
33+
{
34+
$resolver
35+
->setDefault('invalid_message', 'The captcha answer is incorrect. Please try again.');
36+
}
37+
38+
public function getParent(): string
39+
{
40+
return TextType::class;
41+
}
42+
43+
public function getBlockPrefix(): string
44+
{
45+
return 'captcha';
46+
}
47+
}

0 commit comments

Comments
 (0)