From e802847f06ece037f3f2626afd8a392d9cc97483 Mon Sep 17 00:00:00 2001 From: Gabriel Peleskei Date: Wed, 19 Jul 2023 09:05:40 +0000 Subject: [PATCH] twig support tested manually --- .../Controller/ImageGeneratorController.php | 10 ++--- Application/Core/Captcha.php | 6 +-- Application/Shared/Captcha.php | 8 +--- Application/Shared/Options.php | 7 ++- .../translations/de/oecaptcha_de_lang.php | 1 + .../translations/en/oecaptcha_en_lang.php | 2 + .../de/oe_captcha_admin_de_lang.php | 0 .../en/oe_captcha_admin_en_lang.php | 0 .../de/oe_captcha_admin_de_lang.php | 0 .../en/oe_captcha_admin_en_lang.php | 0 README.md | 44 ++++++++++++++----- metadata.php | 19 +------- .../smarty/block/oe_captcha_form.tpl | 3 -- .../tpl => smarty/tpl/include}/oe_captcha.tpl | 2 +- views/smarty/tpl/oe_captcha.tpl | 20 --------- .../themes/default/form/contact.html.twig | 6 +++ .../themes/default/form/pricealarm.html.twig | 6 +++ .../form/privatesales/invite.html.twig | 6 +++ views/twig/oe_captcha.html.twig | 26 +++++++++++ 19 files changed, 97 insertions(+), 69 deletions(-) rename views/admin_smarty/de/oe_catpcha_admin_de_lang.php => Application/views/admin_smarty/de/oe_captcha_admin_de_lang.php (100%) rename {views => Application/views}/admin_smarty/en/oe_captcha_admin_en_lang.php (100%) rename views/admin_twig/de/oe_catpcha_admin_de_lang.php => Application/views/admin_twig/de/oe_captcha_admin_de_lang.php (100%) rename {views => Application/views}/admin_twig/en/oe_captcha_admin_en_lang.php (100%) delete mode 100644 views/admin_twig/smarty/block/oe_captcha_form.tpl rename views/{admin_twig/smarty/tpl => smarty/tpl/include}/oe_captcha.tpl (92%) delete mode 100644 views/smarty/tpl/oe_captcha.tpl create mode 100644 views/twig/extensions/themes/default/form/contact.html.twig create mode 100644 views/twig/extensions/themes/default/form/pricealarm.html.twig create mode 100644 views/twig/extensions/themes/default/form/privatesales/invite.html.twig create mode 100644 views/twig/oe_captcha.html.twig diff --git a/Application/Controller/ImageGeneratorController.php b/Application/Controller/ImageGeneratorController.php index 15edbdb..e53422a 100644 --- a/Application/Controller/ImageGeneratorController.php +++ b/Application/Controller/ImageGeneratorController.php @@ -15,8 +15,8 @@ class ImageGeneratorController extends FrontendController use Options; protected $emac; - protected $imageHeight = 18; - protected $imageWidth = 80; + protected int $imageHeight = 18; + protected int $imageWidth = 80; protected $fontSize = 14; public function init() @@ -80,8 +80,8 @@ class ImageGeneratorController extends FrontendController default: return null; } - $textX = ($this->imageWidth - strlen($this->emac) * imagefontwidth($this->fontSize)) / 2; - $textY = ($this->imageHeight - imagefontheight($this->fontSize)) / 2; + $textX = (int)ceil(($this->imageWidth - strlen($this->emac) * imagefontwidth($this->fontSize)) / 2); + $textY = (int)ceil(($this->imageHeight - imagefontheight($this->fontSize)) / 2) - 1; $colors = [ 'text' => imagecolorallocate($image, 0, 0, 0), @@ -92,7 +92,7 @@ class ImageGeneratorController extends FrontendController ]; imagefill($image, 0, 0, $colors['background']); - imagerectangle($image, 0, 0, $this->imageWidth - 1, $this->imageHeight - 1, $colors['border']); + imagerectangle($image, 0, 0, $this->imageWidth - 2, $this->imageHeight - 2, $colors['border']); imagestring($image, $this->fontSize, $textX + 1, $textY + 0, $this->emac, $colors['shadow2']); imagestring($image, $this->fontSize, $textX + 0, $textY + 1, $this->emac, $colors['shadow1']); imagestring($image, $this->fontSize, $textX, $textY, $this->emac, $colors['text']); diff --git a/Application/Core/Captcha.php b/Application/Core/Captcha.php index e7b2f27..cba5d81 100644 --- a/Application/Core/Captcha.php +++ b/Application/Core/Captcha.php @@ -95,8 +95,8 @@ class Captcha $return = true; // spam spider prevention - $mac = Registry::getConfig()->getRequestParameter('c_mac'); - $macHash = Registry::getConfig()->getRequestParameter('c_mach'); + $mac = Registry::getRequest()->getRequestParameter('c_mac'); + $macHash = Registry::getRequest()->getRequestParameter('c_mach'); if (!$this->pass($mac, $macHash)) { $return = false; @@ -131,7 +131,7 @@ class Captcha $key = $this->getOeCaptchaKey(); $encryptor = new \OxidEsales\Eshop\Core\Encryptor(); - return $config->getCurrentShopUrl() . sprintf('?cl=ith_basic_captcha_generator&e_mac=%s&shp=%d', $encryptor->encrypt($this->getText(), $key), $config->getShopId()); + return $config->getCurrentShopUrl() . sprintf('?cl=oe_captcha_generator&e_mac=%s&shp=%d', $encryptor->encrypt($this->getText(), $key), $config->getShopId()); } /** diff --git a/Application/Shared/Captcha.php b/Application/Shared/Captcha.php index a41ebe2..0ba062e 100644 --- a/Application/Shared/Captcha.php +++ b/Application/Shared/Captcha.php @@ -8,14 +8,10 @@ use OxidProfessionalServices\Captcha\Application\Core\Captcha as CaptchaCore; trait Captcha { - protected ?CaptchaCore $captcha; + protected ?CaptchaCore $oeCaptcha; public function getCaptcha(): CaptchaCore { - if (!$this->captcha) { - $this->captcha = CaptchaCore::getInstance(); - } - - return $this->captcha; + return $this->oeCaptcha ??= CaptchaCore::getInstance(); } } diff --git a/Application/Shared/Options.php b/Application/Shared/Options.php index 9359bab..1391e1e 100644 --- a/Application/Shared/Options.php +++ b/Application/Shared/Options.php @@ -6,17 +6,16 @@ namespace OxidProfessionalServices\Captcha\Application\Shared; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface; +use OxidProfessionalServices\Captcha\Application\Core\Captcha; trait Options { - public const ENCRYPT_KEY = 'fq45QS09_fqyx09239QQ'; - public function getOeCaptchaKey(): string { $bridge = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class); - $key = $bridge->getString('oeCaptchaKey', 'oecaptcha')->toString(); + $key = $bridge->getString('oecaptchakey', 'oecaptcha')->toString(); if (!trim($key)) { - return static::ENCRYPT_KEY; + return Captcha::ENCRYPT_KEY; } return $key; diff --git a/Application/translations/de/oecaptcha_de_lang.php b/Application/translations/de/oecaptcha_de_lang.php index cce27b9..02977fe 100755 --- a/Application/translations/de/oecaptcha_de_lang.php +++ b/Application/translations/de/oecaptcha_de_lang.php @@ -13,4 +13,5 @@ $sLangName = 'Deutsch'; $aLang = [ 'charset' => 'UTF-8', 'MESSAGE_WRONG_VERIFICATION_CODE' => 'Der Prüfcode, den Sie eingegeben haben, ist nicht korrekt. Bitte versuchen Sie es erneut!', + 'OECAPTCHA_PLACEHOLDER' => 'Enter verification code here', ]; diff --git a/Application/translations/en/oecaptcha_en_lang.php b/Application/translations/en/oecaptcha_en_lang.php index 0af4aec..0c84406 100755 --- a/Application/translations/en/oecaptcha_en_lang.php +++ b/Application/translations/en/oecaptcha_en_lang.php @@ -13,4 +13,6 @@ $sLangName = 'English'; $aLang = [ 'charset' => 'UTF-8', 'MESSAGE_WRONG_VERIFICATION_CODE' => 'The verification code you entered is not correct. Please try again.', + 'OECAPTCHA_PLACEHOLDER' => 'Prüfcode hier eingeben', + ]; diff --git a/views/admin_smarty/de/oe_catpcha_admin_de_lang.php b/Application/views/admin_smarty/de/oe_captcha_admin_de_lang.php similarity index 100% rename from views/admin_smarty/de/oe_catpcha_admin_de_lang.php rename to Application/views/admin_smarty/de/oe_captcha_admin_de_lang.php diff --git a/views/admin_smarty/en/oe_captcha_admin_en_lang.php b/Application/views/admin_smarty/en/oe_captcha_admin_en_lang.php similarity index 100% rename from views/admin_smarty/en/oe_captcha_admin_en_lang.php rename to Application/views/admin_smarty/en/oe_captcha_admin_en_lang.php diff --git a/views/admin_twig/de/oe_catpcha_admin_de_lang.php b/Application/views/admin_twig/de/oe_captcha_admin_de_lang.php similarity index 100% rename from views/admin_twig/de/oe_catpcha_admin_de_lang.php rename to Application/views/admin_twig/de/oe_captcha_admin_de_lang.php diff --git a/views/admin_twig/en/oe_captcha_admin_en_lang.php b/Application/views/admin_twig/en/oe_captcha_admin_en_lang.php similarity index 100% rename from views/admin_twig/en/oe_captcha_admin_en_lang.php rename to Application/views/admin_twig/en/oe_captcha_admin_en_lang.php diff --git a/README.md b/README.md index dd83b41..98bfc8c 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,7 @@ It is used to ensure that only a user who can read the distorted characters and can submit the following forms: - contact - invite - - pricealarm - - suggest + - pricealarm (not bound in twig) The captcha module then validates the submitted value against the expected one and then decides whether to process the request (e.g. send contact mail to shop administrator) or refuse and show an error message instead. @@ -24,29 +23,54 @@ Please proceed with one of the following ways to install the module: In order to install the module via composer, run the following commands in commandline of your shop base directory (where the shop's composer.json file resides). -``` +```bash composer require oxid-projects/captcha-module ``` ### Module installation via repository cloning Clone the module to your OXID eShop **modules/oe/** directory: -``` +```bash git clone https://github.com/OXIDprojects/captcha-module.git captcha ``` - -### Module installation from zip package - -* Make a new folder "captcha" in the **modules/oe/ directory** of your shop installation. -* Download the https://github.com/OXIDprojects/captcha-module/archive/master.zip file and unpack it into the created folder. +And add repository to root composer: +```bash +composer config repositories.oxid-projects/captcha-module path "source/modules/oe/captcha" +``` +And install module: +```bash +composer require oxid-projects/captcha-module +vendor/bin/oe-console oe:module:install source/modules/oe/captcha +# And activate +vendor/bin/oe-console oe:module:activate oecaptcha +``` ## Activate Module - Activate the module in the administration panel. +- Or use console +```bash +vendor/bin/oe-console oe:module:activate oecaptcha +``` ## Uninstall -Disable the module in administration area and delete the module folder. +Disable the module in administration area or by executing following shell command. +```bash +vendor/bin/oe-console oe:module:deactivate oecaptcha +``` +If installed over composer (packagist): +```bash +composer remove oxid-projects/captcha-module +``` +else if cloned: +```bash +vendor/bin/oe-console oe:module:uninstall oecaptcha +composer remove oxid-projects/captcha-module +composer config --unset repositories.oxid-projects/captcha-module +# and remove the source itself +rm -rf source/modules/oe/captcha +``` ## License diff --git a/metadata.php b/metadata.php index 6b21e8c..1146a10 100755 --- a/metadata.php +++ b/metadata.php @@ -43,7 +43,7 @@ $aModule = [ 'url' => 'https://www.oxid-esales.com/', 'email' => '', 'controllers' => [ - 'ith_basic_captcha_generator' => OxidProfessionalServices\Captcha\Application\Controller\ImageGeneratorController::class, + 'oe_captcha_generator' => OxidProfessionalServices\Captcha\Application\Controller\ImageGeneratorController::class, ], 'extend' => [ OxidEsales\Eshop\Application\Controller\ArticleDetailsController::class => OxidProfessionalServices\Captcha\Application\Controller\DetailsController::class, @@ -55,7 +55,7 @@ $aModule = [ OxidEsales\Eshop\Application\Component\Widget\ArticleDetails::class => OxidProfessionalServices\Captcha\Application\Component\Widget\ArticleDetails::class, ], 'templates' => [ - 'oe_captcha.tpl' => 'views/smarty/tpl/oe_captcha.tpl', + 'oe_captcha.tpl' => 'views/smarty/tpl/include/oe_captcha.tpl', ], 'blocks' => [ [ @@ -63,11 +63,6 @@ $aModule = [ 'block' => 'captcha_form', 'file' => 'views/smarty/blocks/oe_captcha_form.tpl', ], - [ - 'template' => 'form/newsletter.tpl', - 'block' => 'captcha_form', - 'file' => 'views/smarty/blocks/oe_captcha_form.tpl', - ], [ 'template' => 'form/privatesales/invite.tpl', 'block' => 'captcha_form', @@ -78,16 +73,6 @@ $aModule = [ 'block' => 'captcha_form', 'file' => 'views/smarty/blocks/oe_captcha_form.tpl', ], - [ - 'template' => 'form/suggest.tpl', - 'block' => 'captcha_form', - 'file' => 'views/smarty/blocks/oe_captcha_form.tpl', - ], - [ - 'template' => 'form/forgotpwd_email.tpl', - 'block' => 'captcha_form', - 'file' => 'views/smarty/blocks/oe_captcha_form.tpl', - ], ], 'settings' => [ [ diff --git a/views/admin_twig/smarty/block/oe_captcha_form.tpl b/views/admin_twig/smarty/block/oe_captcha_form.tpl deleted file mode 100644 index 4c9afc7..0000000 --- a/views/admin_twig/smarty/block/oe_captcha_form.tpl +++ /dev/null @@ -1,3 +0,0 @@ -[{$smarty.block.parent}] - -[{include file="oe_captcha.tpl"}] \ No newline at end of file diff --git a/views/admin_twig/smarty/tpl/oe_captcha.tpl b/views/smarty/tpl/include/oe_captcha.tpl similarity index 92% rename from views/admin_twig/smarty/tpl/oe_captcha.tpl rename to views/smarty/tpl/include/oe_captcha.tpl index 4c8b439..e7c5c06 100644 --- a/views/admin_twig/smarty/tpl/oe_captcha.tpl +++ b/views/smarty/tpl/include/oe_captcha.tpl @@ -1,4 +1,4 @@ -[{assign var="oCaptcha" value=$oView->getCaptcha()}] +[{assign var="oCaptcha" value=$oView->getOeCaptcha()}]
diff --git a/views/smarty/tpl/oe_captcha.tpl b/views/smarty/tpl/oe_captcha.tpl deleted file mode 100644 index 4c8b439..0000000 --- a/views/smarty/tpl/oe_captcha.tpl +++ /dev/null @@ -1,20 +0,0 @@ -[{assign var="oCaptcha" value=$oView->getCaptcha()}] - - -
- - -
-
- - [{if $oCaptcha->isImageVisible()}] - - [{else}] - [{$oCaptcha->getText()}] - [{/if}] - - -
-
-
\ No newline at end of file diff --git a/views/twig/extensions/themes/default/form/contact.html.twig b/views/twig/extensions/themes/default/form/contact.html.twig new file mode 100644 index 0000000..cab35c5 --- /dev/null +++ b/views/twig/extensions/themes/default/form/contact.html.twig @@ -0,0 +1,6 @@ +{% extends "form/contact.html.twig" %} + +{% block captcha_form %} + {{ parent() }} + {% include "@oecaptcha/oe_captcha.html.twig" %} +{% endblock %} \ No newline at end of file diff --git a/views/twig/extensions/themes/default/form/pricealarm.html.twig b/views/twig/extensions/themes/default/form/pricealarm.html.twig new file mode 100644 index 0000000..6ea0119 --- /dev/null +++ b/views/twig/extensions/themes/default/form/pricealarm.html.twig @@ -0,0 +1,6 @@ +{% extends "form/pricealarm.html.twig" %} + +{% block captcha_form %} + {{ parent() }} + {% include "@oecaptcha/oe_captcha.html.twig" %} +{% endblock %} \ No newline at end of file diff --git a/views/twig/extensions/themes/default/form/privatesales/invite.html.twig b/views/twig/extensions/themes/default/form/privatesales/invite.html.twig new file mode 100644 index 0000000..9822eb1 --- /dev/null +++ b/views/twig/extensions/themes/default/form/privatesales/invite.html.twig @@ -0,0 +1,6 @@ +{% extends "form/privatesales/invite.html.twig" %} + +{% block captcha_form %} + {{ parent() }} + {% include "@oecaptcha/oe_captcha.html.twig" %} +{% endblock %} \ No newline at end of file diff --git a/views/twig/oe_captcha.html.twig b/views/twig/oe_captcha.html.twig new file mode 100644 index 0000000..0bccbc6 --- /dev/null +++ b/views/twig/oe_captcha.html.twig @@ -0,0 +1,26 @@ +{% set oCaptcha = oView.getCaptcha() %} + +{% block style %} + +{% endblock %} +
+ + +
+
+ + {% if oCaptcha.isImageVisible() %} + + {% else %} + {{ oCaptcha.getText() }} + {% endif %} + + +
+
+
\ No newline at end of file