Skip to content

Commit

Permalink
Merge pull request #51 from maikschneider/release-4.0.2
Browse files Browse the repository at this point in the history
release 4.0.2
  • Loading branch information
github-actions[bot] authored Sep 29, 2023
2 parents f4f537a + 1589192 commit f31099d
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 120 deletions.
4 changes: 4 additions & 0 deletions Classes/Domain/Model/FormElements/CaptchaElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;
use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement;

class CaptchaElement extends AbstractFormElement
{
/**
* @throws InvalidConfigurationTypeException
*/
public function initializeFormElement(): void
{
parent::initializeFormElement();
Expand Down
2 changes: 1 addition & 1 deletion Classes/Middleware/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function process(
return $handler->handle($request);
}

$languageCode = $request->getAttribute('language')->getTwoLetterIsoCode();
$languageCode = $request->getAttribute('language')?->getTwoLetterIsoCode() ?? '';
$body = $request->getParsedBody();

$ts = $this->configurationManager->getConfiguration(
Expand Down
2 changes: 1 addition & 1 deletion Classes/Utility/AudioBuilderUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function joinwavs(array $wavs): string
$info = unpack($fields, $header);
// read optional extra stuff
if (isset($info['Subchunk1Size']) && $info['Subchunk1Size'] > 16) {
$header .= fread($fp, ($info['Subchunk1Size'] - 16));
$header .= fread($fp, max(0, (int)$info['Subchunk1Size'] - 16));
}
// read SubChunk2ID
$header .= fread($fp, 4);
Expand Down
2 changes: 1 addition & 1 deletion Classes/Utility/CaptchaBuilderUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function getRandomFontFileFromSettings(array $settings): ?string
// check 1: file storage path
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
try {
$randomFontFile = $resourceFactory->retrieveFileOrFolderObject($fontFiles[0])->getPublicUrl();
$randomFontFile = $resourceFactory->retrieveFileOrFolderObject($fontFiles[0])?->getPublicUrl() ?? '';
$randomFontFile = Environment::getPublicPath() . $randomFontFile;
} catch (\Exception $e) {
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Captcha/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gregwar/captcha",
"type": "captcha",
"type": "library",
"description": "Captcha generator",
"keywords": ["captcha", "spam", "bot"],
"homepage": "https://github.com/Gregwar/Captcha",
Expand Down
14 changes: 6 additions & 8 deletions Libraries/Captcha/src/Gregwar/Captcha/CaptchaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function __construct($phrase = null, PhraseBuilderInterface $builder = nu
} else {
$this->builder = $builder;
}

$this->phrase = is_string($phrase) ? $phrase : $this->builder->build($phrase);
}

Expand Down Expand Up @@ -341,12 +341,12 @@ protected function writePhrase($image, $phrase, $font, $width, $height)
}

// Gets the text size and start position
$size = $width / $length - $this->rand(0, 3) - 1;
$size = (int) round($width / $length) - $this->rand(0, 3) - 1;
$box = \imagettfbbox($size, 0, $font, $phrase);
$textWidth = $box[2] - $box[0];
$textHeight = $box[1] - $box[7];
$x = ($width - $textWidth) / 2;
$y = ($height - $textHeight) / 2 + $size;
$x = (int) round(($width - $textWidth) / 2);
$y = (int) round(($height - $textHeight) / 2) + $size;

if (!$this->textColor) {
$textColor = array($this->rand(0, 150), $this->rand(0, 150), $this->rand(0, 150));
Expand All @@ -362,7 +362,7 @@ protected function writePhrase($image, $phrase, $font, $width, $height)
$w = $box[2] - $box[0];
$angle = $this->rand(-$this->maxAngle, $this->maxAngle);
$offset = $this->rand(-$this->maxOffset, $this->maxOffset);
\imagettftext($image, $size, $angle, (int)$x, (int)($y + $offset), $col, $font, $symbol);
\imagettftext($image, $size, $angle, $x, $y + $offset, $col, $font, $symbol);
$x += $w;
}

Expand Down Expand Up @@ -427,7 +427,6 @@ public function build($width = 150, $height = 40, $font = null, $fingerprint = n
$color = $this->backgroundColor;
$bg = imagecolorallocate($image, $color[0], $color[1], $color[2]);
}
$this->background = $bg;
imagefill($image, 0, 0, $bg);
} else {
// use a random background image
Expand Down Expand Up @@ -605,7 +604,7 @@ protected function rand($min, $max)
$value = current($this->fingerprint);
next($this->fingerprint);
} else {
$value = mt_rand($min, $max);
$value = mt_rand((int)$min, (int)$max);
$this->fingerprint[] = $value;
}

Expand Down Expand Up @@ -731,7 +730,6 @@ protected function createBackgroundImageFromType($backgroundImage, $imageType)

default:
throw new Exception('Not supported file type for background image!');
break;
}

return $image;
Expand Down
73 changes: 40 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Captcha extension for TYPO3 form

This extension adds a captcha element for the TYPO3 form component. The captcha generation uses [Gregwar/Captcha](https://github.com/Gregwar/Captcha), **no Google or 3rd party** includes.
This extension adds a captcha element for the TYPO3 form component. The captcha generation
uses [Gregwar/Captcha](https://github.com/Gregwar/Captcha), **no Google or 3rd party** includes.

![Frontend Captcha example](Documentation/Images/Example.jpg)

Expand All @@ -11,7 +12,7 @@ This extension adds a captcha element for the TYPO3 form component. The captcha

## Usage

Add the captcha element via Form Editor to your form or directly to your yaml form.
Add the captcha element via Form Editor to your form or directly to your yaml form.

### Via Form Editor

Expand All @@ -21,14 +22,13 @@ Add the captcha element via Form Editor to your form or directly to your yaml fo

```yaml
renderables:
-
type: Captcha
identifier: captcha
label: Captcha
properties:
fluidAdditionalAttributes:
required: required
autocomplete: 'off'
- type: Captcha
identifier: captcha
label: Captcha
properties:
fluidAdditionalAttributes:
required: required
autocomplete: 'off'
```
### Configuration
Expand All @@ -43,49 +43,49 @@ plugin.tx_bwcaptcha {

# Show audio button for speech output
audioButton =

# The length of the captcha
length =

# The charset of the captcha
charset =

# The width of the image
width =

# The height of the image
height =

# Custom font file(s) to use (comma-separated)
fontFiles =

# Text color (e.g. 255,0,0)
textColor =

# Line color (e.g. 0,0,0)
lineColor =

# Background color (e.g. 255,255,255)
backgroundColor =

# Distortion
distortion =

# The maximum number of lines to draw in front of
maxFrontLines =

# The maximum number of lines to draw behind
maxBehindLines =

# The maximum angle of char
maxAngle =

# The maximum offset of char
maxOffset =

# Is the interpolation enabled?
interpolation =

# Ignore all effects
ignoreAllEffects =
}
Expand All @@ -94,7 +94,8 @@ plugin.tx_bwcaptcha {

### Overriding the captcha element

To override the captcha partial, copy it to your extension and add the partial path to your [form setup](https://docs.typo3.org/c/typo3/cms-form/main/en-us/I/Concepts/Configuration/Index.html#yaml-registration-for-the-frontend):
To override the captcha partial, copy it to your extension and add the partial path to
your [form setup](https://docs.typo3.org/c/typo3/cms-form/main/en-us/I/Concepts/Configuration/Index.html#yaml-registration-for-the-frontend):

```yaml
TYPO3:
Expand All @@ -111,26 +112,32 @@ TYPO3:
## Migration from version 3.x to 4.x
This version aims to make solving the captcha more accessible. It introduces a new audio feature that reads out the current captcha code. Missing `ARIA` properties have been added.
This version aims to make solving the captcha more accessible. It introduces a new audio feature that reads out the
current captcha code. Missing `ARIA` properties have been added.

* Check out the [new captcha partial](https://github.com/maikschneider/bw_captcha/blob/master/Resources/Private/Frontend/Partials/Captcha.html)
* Check out
the [new captcha partial](https://github.com/maikschneider/bw_captcha/blob/master/Resources/Private/Frontend/Partials/Captcha.html)
* Audio button is enabled by default (can be disabled via `plugin.tx_bwcaptcha.settings.audioButton`)

## Migration from version 2.x to 3.x
## Migration from version 2.x to 3.x

The generation of the captcha moved to a middleware, which solves a lot of caching issues. Therefore, adjustments to the form element partial have been made. If you've modified the partial, you need to update the image tag and refresh button link.
The generation of the captcha moved to a middleware, which solves a lot of caching issues. Therefore, adjustments to the
form element partial have been made. If you've modified the partial, you need to update the image tag and refresh button
link.

**tl;dr**:

* Check out the [new captcha partial](https://github.com/maikschneider/bw_captcha/blob/master/Resources/Private/Frontend/Partials/Captcha.html)
* Check out
the [new captcha partial](https://github.com/maikschneider/bw_captcha/blob/master/Resources/Private/Frontend/Partials/Captcha.html)
* Reload button is enabled by default (can be disabled via `plugin.tx_bwcaptcha.settings.refreshButton`)
* You can re-enable the page cache, if disabled it because of this element
* You can re-enable the page cache, if disabled it because of this element

## Troubleshooting

### Refresh button not working

If your site is configured to use trailing slashes, the refresh url cannot be resolved. A simple fix is to add a setting for the pageType 3413, e.g.:
If your site is configured to use trailing slashes, the refresh url cannot be resolved. A simple fix is to add a setting
for the pageType 3413, e.g.:

```yaml
routeEnhancers:
Expand Down
66 changes: 33 additions & 33 deletions Resources/Private/Language/de.locallang.xlf
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="EXT:bw_captcha/Resources/Private/Language/locallang.xlf"
date="2012-10-26T12:00:32Z" product-name="bw_captcha">
<header/>
<body>
<trans-unit id="validator.captcha.notvalid" resname="validator.captcha.notvalid">
<source>The entered captcha is not correct.</source>
<target>Das eingegebene Captcha ist nicht korrekt.</target>
</trans-unit>
<trans-unit id="formEditor.elements.Captcha" resname="formEditor.elements.Captcha">
<source>Captcha</source>
<target>Captcha</target>
</trans-unit>
<trans-unit id="partial.refresh.title">
<source>Load new captcha</source>
<target>Neues Captcha laden</target>
</trans-unit>
<trans-unit id="partial.refresh.label">
<source>Refresh</source>
<target>Erneuern</target>
</trans-unit>
<trans-unit id="partial.audio.title">
<source>Read captcha aloud</source>
<target>Captcha vorlesen</target>
</trans-unit>
<trans-unit id="partial.audio.label">
<source>Read</source>
<target>Vorlesen</target>
</trans-unit>
</body>
</file>
</xliff>
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="EXT:bw_captcha/Resources/Private/Language/locallang.xlf"
date="2012-10-26T12:00:32Z" product-name="bw_captcha">
<header/>
<body>
<trans-unit id="validator.captcha.notvalid" resname="validator.captcha.notvalid">
<source>The entered captcha is not correct.</source>
<target>Das eingegebene Captcha ist nicht korrekt.</target>
</trans-unit>
<trans-unit id="formEditor.elements.Captcha" resname="formEditor.elements.Captcha">
<source>Captcha</source>
<target>Captcha</target>
</trans-unit>
<trans-unit id="partial.refresh.title">
<source>Load new captcha</source>
<target>Neues Captcha laden</target>
</trans-unit>
<trans-unit id="partial.refresh.label">
<source>Refresh</source>
<target>Erneuern</target>
</trans-unit>
<trans-unit id="partial.audio.title">
<source>Read captcha aloud</source>
<target>Captcha vorlesen</target>
</trans-unit>
<trans-unit id="partial.audio.label">
<source>Read</source>
<target>Vorlesen</target>
</trans-unit>
</body>
</file>
</xliff>
34 changes: 17 additions & 17 deletions Resources/Private/Language/fr.locallang.xlf
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="fr" datatype="plaintext" original="EXT:bw_captcha/Resources/Private/Language/locallang.xlf"
date="2012-10-26T12:00:32Z" product-name="bw_captcha">
<header/>
<body>
<trans-unit id="validator.captcha.notvalid" resname="validator.captcha.notvalid">
<source>The entered captcha is not correct.</source>
<target>Le captcha saisi n'est pas correct.</target>
</trans-unit>
<trans-unit id="formEditor.elements.Captcha" resname="formEditor.elements.Captcha">
<source>Captcha</source>
<target>Captcha</target>
</trans-unit>
</body>
</file>
</xliff>
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="fr" datatype="plaintext" original="EXT:bw_captcha/Resources/Private/Language/locallang.xlf"
date="2012-10-26T12:00:32Z" product-name="bw_captcha">
<header/>
<body>
<trans-unit id="validator.captcha.notvalid" resname="validator.captcha.notvalid">
<source>The entered captcha is not correct.</source>
<target>Le captcha saisi n'est pas correct.</target>
</trans-unit>
<trans-unit id="formEditor.elements.Captcha" resname="formEditor.elements.Captcha">
<source>Captcha</source>
<target>Captcha</target>
</trans-unit>
</body>
</file>
</xliff>
Loading

0 comments on commit f31099d

Please sign in to comment.