Skip to content

Commit

Permalink
Merge pull request #2753 from shopware5/chore/clean-up-captcha
Browse files Browse the repository at this point in the history
chore: Cleanup captcha stuff and replace used font
  • Loading branch information
mitelg authored Aug 29, 2024
2 parents 3252691 + 57e4811 commit 81879a1
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 166 deletions.
80 changes: 0 additions & 80 deletions .phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -475,36 +475,6 @@ parameters:
count: 1
path: _sql/migrations/440-fix-sorder-email-template.php

-
message: "#^Method Migrations_Migration443\\:\\:disableFirstRunWizard\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/443-add-wizard-configuration-value.php

-
message: "#^Method Migrations_Migration443\\:\\:fetchElementId\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/443-add-wizard-configuration-value.php

-
message: "#^Method Migrations_Migration443\\:\\:fetchFormId\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/443-add-wizard-configuration-value.php

-
message: "#^Method Migrations_Migration443\\:\\:fixCaptchaColor\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/443-add-wizard-configuration-value.php

-
message: "#^Method Migrations_Migration443\\:\\:insertFormElement\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/443-add-wizard-configuration-value.php

-
message: "#^Method Migrations_Migration443\\:\\:insertFormTranslation\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/443-add-wizard-configuration-value.php

-
message: "#^Method Migrations_Migration444\\:\\:addImprintToBottomGroup\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -1015,21 +985,6 @@ parameters:
count: 1
path: _sql/migrations/903-add-menu-import-export-advanced.php

-
message: "#^Method Migrations_Migration904\\:\\:createAdditionalCaptchaOptions\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/904-add-captcha-selection.php

-
message: "#^Method Migrations_Migration904\\:\\:createAdditionalCaptchaOptionsTranslations\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/904-add-captcha-selection.php

-
message: "#^Method Migrations_Migration904\\:\\:moveExistingCaptchaOptions\\(\\) has no return type specified\\.$#"
count: 1
path: _sql/migrations/904-add-captcha-selection.php

-
message: "#^Method Migrations_Migration917\\:\\:addCategorySortings\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -21315,11 +21270,6 @@ parameters:
count: 1
path: engine/Shopware/Controllers/Backend/Base.php

-
message: "#^Method Shopware_Controllers_Backend_Base\\:\\:getAvailableCaptchasAction\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Backend/Base.php

-
message: "#^Method Shopware_Controllers_Backend_Base\\:\\:getAvailableHashesAction\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -24355,21 +24305,6 @@ parameters:
count: 1
path: engine/Shopware/Controllers/Widgets/Campaign.php

-
message: "#^Method Shopware_Controllers_Widgets_Captcha\\:\\:getCaptchaByNameAction\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Widgets/Captcha.php

-
message: "#^Method Shopware_Controllers_Widgets_Captcha\\:\\:indexAction\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Widgets/Captcha.php

-
message: "#^Method Shopware_Controllers_Widgets_Captcha\\:\\:refreshCaptchaAction\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Widgets/Captcha.php

-
message: "#^Access to an undefined property Enlight_Components_Session_Namespace\\:\\:\\$sOneTimeAccount\\.$#"
count: 1
Expand Down Expand Up @@ -24640,11 +24575,6 @@ parameters:
count: 1
path: engine/Shopware/Core/sAdmin.php

-
message: "#^Method sAdmin\\:\\:sNewsletterSubscription\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: engine/Shopware/Core/sAdmin.php

-
message: "#^Method sAdmin\\:\\:sRiskARTICLESFROM\\(\\) has parameter \\$order with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -25055,11 +24985,6 @@ parameters:
count: 2
path: engine/Shopware/Core/sAdmin.php

-
message: "#^Parameter \\#2 \\$request of method Shopware\\\\Components\\\\Captcha\\\\CaptchaValidator\\:\\:validateByName\\(\\) expects Enlight_Controller_Request_Request, Enlight_Controller_Request_Request\\|null given\\.$#"
count: 1
path: engine/Shopware/Core/sAdmin.php

-
message: "#^Parameter \\#2 \\$resource of method Shopware\\\\Components\\\\DependencyInjection\\\\Container\\:\\:set\\(\\) expects object\\|null, string given\\.$#"
count: 1
Expand Down Expand Up @@ -36410,11 +36335,6 @@ parameters:
count: 1
path: tests/Functional/Components/CacheSubscriberTest.php

-
message: "#^Method Shopware\\\\Tests\\\\Functional\\\\Components\\\\Captcha\\\\CaptchaRepositoryTest\\:\\:testThrowCaptchaNotFoundException\\(\\) has no return type specified\\.$#"
count: 1
path: tests/Functional/Components/Captcha/CaptchaRepositoryTest.php

-
message: "#^Method Shopware\\\\Tests\\\\Functional\\\\Components\\\\Cart\\\\BasketQueryHelperTest\\:\\:testGetInsertDiscountAttributeQuery\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
18 changes: 11 additions & 7 deletions _sql/migrations/443-add-wizard-configuration-value.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand All @@ -21,7 +23,9 @@
* our trademarks remain entirely with the shopware AG.
*/

class Migrations_Migration443 extends Shopware\Components\Migrations\AbstractMigration
use Shopware\Components\Migrations\AbstractMigration;

class Migrations_Migration443 extends AbstractMigration
{
public function up($modus)
{
Expand All @@ -36,7 +40,7 @@ public function up($modus)
}
}

private function disableFirstRunWizard()
private function disableFirstRunWizard(): void
{
$sql = <<<'EOD'
INSERT IGNORE INTO `s_core_config_values` (`element_id`, `shop_id`, `value`) VALUES
Expand All @@ -45,23 +49,23 @@ private function disableFirstRunWizard()
$this->addSql($sql);
}

private function fetchFormId()
private function fetchFormId(): void
{
$sql = <<<'EOD'
SET @formId = (SELECT id FROM s_core_config_forms WHERE `name`='Auth');
EOD;
$this->addSql($sql);
}

private function fetchElementId()
private function fetchElementId(): void
{
$sql = <<<'EOD'
SET @elementId = (SELECT id FROM s_core_config_elements WHERE name LIKE "firstRunWizardEnabled" LIMIT 1);
EOD;
$this->addSql($sql);
}

private function insertFormElement()
private function insertFormElement(): void
{
$sql = <<<'EOD'
INSERT IGNORE INTO `s_core_config_elements` (`id`, `form_id`, `name`, `value`, `label`, `description`, `type`, `required`, `position`, `scope`) VALUES
Expand All @@ -70,7 +74,7 @@ private function insertFormElement()
$this->addSql($sql);
}

private function insertFormTranslation()
private function insertFormTranslation(): void
{
$sql = <<<'EOD'
INSERT IGNORE INTO `s_core_config_element_translations` (`element_id`, `locale_id`, `label`, `description`)
Expand All @@ -79,7 +83,7 @@ private function insertFormTranslation()
$this->addSql($sql);
}

private function fixCaptchaColor()
private function fixCaptchaColor(): void
{
$sql = <<<'EOD'
UPDATE s_core_config_element_translations
Expand Down
14 changes: 9 additions & 5 deletions _sql/migrations/904-add-captcha-selection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand All @@ -21,7 +23,9 @@
* our trademarks remain entirely with the shopware AG.
*/

class Migrations_Migration904 extends Shopware\Components\Migrations\AbstractMigration
use Shopware\Components\Migrations\AbstractMigration;

class Migrations_Migration904 extends AbstractMigration
{
public function up($modus)
{
Expand All @@ -30,7 +34,7 @@ public function up($modus)
$this->createAdditionalCaptchaOptionsTranslations();
}

private function moveExistingCaptchaOptions()
private function moveExistingCaptchaOptions(): void
{
$this->addSql("SET @help_parent = (SELECT id FROM s_core_config_forms WHERE name='Frontend' LIMIT 1)");

Expand All @@ -44,7 +48,7 @@ private function moveExistingCaptchaOptions()
$this->addSql("UPDATE s_core_config_elements SET form_id=@parent WHERE name='captchaColor'");
}

private function createAdditionalCaptchaOptionsTranslations()
private function createAdditionalCaptchaOptionsTranslations(): void
{
$this->addSql("SET @captchaMethod = (SELECT id FROM s_core_config_elements WHERE name = 'captchaMethod' LIMIT 1)");
$this->addSql("SET @noCaptchaAfterLogin = (SELECT id FROM s_core_config_elements WHERE name = 'noCaptchaAfterLogin' LIMIT 1)");
Expand All @@ -59,9 +63,9 @@ private function createAdditionalCaptchaOptionsTranslations()
}

/**
* @param string $modus
* @param self::MODUS_* $modus
*/
private function createAdditionalCaptchaOptions($modus)
private function createAdditionalCaptchaOptions(string $modus): void
{
$captchaMethod = 'default';
if ($modus === self::MODUS_UPDATE) {
Expand Down
16 changes: 2 additions & 14 deletions engine/Shopware/Components/Captcha/AbstractCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
abstract class AbstractCaptcha implements CaptchaInterface
{
private const PATH_CAPTCHA_IMAGE = 'frontend/_public/src/img/bg--captcha.jpg';
private const PATH_CAPTCHA_IMAGE_FALLBACK = 'frontend/_resources/images/captcha/background.jpg';

private const PATH_CAPTCHA_FONT = 'frontend/_public/src/fonts/captcha.ttf';
private const PATH_CAPTCHA_FONT_FALLBACK = 'frontend/_resources/images/captcha/font.ttf';

protected Shopware_Components_Config $config;

Expand All @@ -62,14 +60,6 @@ protected function getImageResource($string)
$captcha = $this->getCaptchaFile(self::PATH_CAPTCHA_IMAGE);
$font = $this->getCaptchaFile(self::PATH_CAPTCHA_FONT);

if (empty($captcha)) {
$captcha = $this->getCaptchaFile(self::PATH_CAPTCHA_IMAGE_FALLBACK);
}

if (empty($font)) {
$font = $this->getCaptchaFile(self::PATH_CAPTCHA_FONT_FALLBACK);
}

if (!empty($captcha)) {
$im = imagecreatefromjpeg($captcha);
} else {
Expand Down Expand Up @@ -107,10 +97,8 @@ protected function getImageResource($string)
imagettftext($im, $rand1, $rand2, (($i + 1) * 15) + 2, $rand3 + 2, $black, $font, $string[$i]);
}
for ($i = 0; $i < 8; ++$i) {
imageline($im, Random::getInteger(30, 70), Random::getInteger(0, 50), Random::getInteger(100, 150),
Random::getInteger(20, 100), $black);
imageline($im, Random::getInteger(30, 70), Random::getInteger(0, 50), Random::getInteger(100, 150),
Random::getInteger(20, 100), $black);
imageline($im, Random::getInteger(30, 70), Random::getInteger(0, 50), Random::getInteger(100, 150), Random::getInteger(20, 100), $black);
imageline($im, Random::getInteger(30, 70), Random::getInteger(0, 50), Random::getInteger(100, 150), Random::getInteger(20, 100), $black);
}
} else {
$white = (int) imagecolorallocate($im, 255, 255, 255);
Expand Down
2 changes: 2 additions & 0 deletions engine/Shopware/Components/Captcha/CaptchaInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down
34 changes: 12 additions & 22 deletions engine/Shopware/Components/Captcha/CaptchaRepository.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
Expand Down Expand Up @@ -31,20 +33,14 @@

class CaptchaRepository
{
/**
* @var Shopware_Components_Config
*/
private $config;
private Shopware_Components_Config $config;

/**
* @var CaptchaInterface[]
* @var list<CaptchaInterface>
*/
private $captchas;
private array $captchas;

/**
* @var ContainerInterface
*/
private $container;
private ContainerInterface $container;

public function __construct(
IteratorAggregate $captchas,
Expand Down Expand Up @@ -79,7 +75,7 @@ public function getConfiguredCaptcha()
}

/**
* @return CaptchaInterface[]
* @return list<CaptchaInterface>
*/
public function getList()
{
Expand All @@ -98,31 +94,25 @@ public function getList()
public function getCaptchaByName($captchaName)
{
foreach ($this->captchas as $captcha) {
if ($captcha->getName() == $captchaName) {
if ($captcha->getName() === $captchaName) {
return $captcha;
}
}

throw new CaptchaNotFoundException(sprintf("The captcha with id '%s' is configured, but could not be found", $captchaName));
}

/**
* @return bool
*/
private function isCaptchaDisabled()
private function isCaptchaDisabled(): bool
{
$userIsLoggedIn = !empty($this->container->get('session')->get('sUserId'));
$isCustomerLoggedIn = !empty($this->container->get('session')->get('sUserId'));

if ($this->config->get('noCaptchaAfterLogin') && $userIsLoggedIn) {
if ($this->config->get('noCaptchaAfterLogin') && $isCustomerLoggedIn) {
return true;
}

// legacy way to disable the captcha
$captchaColor = $this->config->get('CaptchaColor');
if (empty($captchaColor)) {
return true;
}

return false;
return empty($captchaColor);
}
}
Loading

0 comments on commit 81879a1

Please sign in to comment.