maikschneider / bw_captcha

TYPO3 extension that adds a captcha element with audio support for the TYPO3 form component. The captcha generation uses Gregwar/Captcha, no Google or 3rd party includes.
6 stars 6 forks source link

Rendering form in BE in preview results in Exception due to not inialized TSFE #25

Closed sypets closed 1 year ago

sypets commented 1 year ago

Reproduce

  1. Create new form
  2. Add captcha field
  3. In Backend: Click on "preview mode" image

Exception is displayed

Exception

#1476107295 TYPO3\CMS\Core\Error\Exception
PHP Warning: Undefined global variable $TSFE in /var/www/site-uol11/htdocs/typo3conf/ext/bw_captcha/Classes/Hooks/FormElementCaptchaHook.php line 62

Code

62 $captchaIds = $GLOBALS['TSFE']->fe_user->getKey('ses', 'captchaIds') ?? [];

Additional information

It is not good practice to use $GLOBALS['TSFE'] anymore. Please see also new documentation: https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/TSFE/Index.html (however currently I am not sure how to handle this differently in the hook though, because we do not have the $request object.

Also, TSFE is not inialized in BE (AFAIK) and maybe not in this case (preview mode). Probably, that part should not be executed in the BE anyway, can check for context, see e.g. https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Context/Index.html#preview-aspect or you can just check if $GLOBALS['TSFE'] is set and if not do not execute that part.

maikschneider commented 1 year ago

Hey @sypets, thank you very much!

In an upcoming version ( #21 ) the whole captcha validation is done via middleware, where the request is available: There I'll make sure that the new method is used.

Best!