diff --git a/class.tx_jmrecaptcha.php b/class.tx_jmrecaptcha.php index 3f14470..df70edc 100644 --- a/class.tx_jmrecaptcha.php +++ b/class.tx_jmrecaptcha.php @@ -38,6 +38,26 @@ class tx_jmrecaptcha extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin { public $conf; + /** + * List of valid parameters of the no-CAPTCHA variant + * @see https://developers.google.com/recaptcha/docs/display#render_param + * + * @var array + */ + private $validNoCaptchaParameters = array( + 'sitekey', 'theme', 'type', 'size', 'tabindex', 'callback', 'expired-callback' + ); + + /** + * Mapping of conf to no-CAPTCHA parameters + * + * @var array + */ + private $conf2NoCaptchaParametersMapping = array( + 'public_key' => 'sitekey', + 'expired_callback' => 'expired-callback' + ); + /** * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */ @@ -79,11 +99,36 @@ protected function initConfiguration() { * @return string */ protected function renderNoCaptcha() { - $content = ''; - $content .= '
'; + $language = $this->getLanguageCode(null); + $languageParameter = ''; + if (!empty($language)) { + $languageParameter = 'hl=' . $language; + } + + $content = ''; + $content .= '
getNoCaptchaParameters() . '>
'; return $content; } + /** + * Get all available parameters as collected data-attributes for no-CAPTCHA variant + * @return string $noCaptchaParameters + */ + protected function getNoCaptchaParameters() { + $noCaptchaParameters = ' '; + + foreach ($this->conf as $parameter => $value) { + if (!empty($value)) { + $parameter = isset($this->conf2NoCaptchaParametersMapping[$parameter]) ? $this->conf2NoCaptchaParametersMapping[$parameter] : $parameter; + if (in_array($parameter, $this->validNoCaptchaParameters)) { + $noCaptchaParameters .= 'data-' . $parameter . '="' . htmlspecialchars($value) . '" '; + } + } + } + + return $noCaptchaParameters; + } + /** * @param $error * @return string @@ -101,7 +146,7 @@ protected function renderReCaptcha($error) { // Default settings $recaptchaOptions = array( - 'lang' => self::jsQuote('en'), + 'lang' => self::jsQuote($this->getLanguageCode()), ); // Theme @@ -119,15 +164,6 @@ protected function renderReCaptcha($error) { $recaptchaOptions['custom_theme_widget'] = self::jsQuote($this->conf['custom_theme_widget']); } - // Language detection - if (!empty($this->conf['lang'])) { - // language from plugin configuration - $recaptchaOptions['lang'] = self::jsQuote($this->conf['lang']); - } elseif (!empty($this->typoscriptFrontendController->tmpl->setup['config.']['language'])) { - // automatic language detection (TYPO3 settings) - $recaptchaOptions['lang'] = self::jsQuote($this->typoscriptFrontendController->tmpl->setup['config.']['language']); - } - // Custom translations $customTranslations = array(); @@ -168,6 +204,29 @@ protected static function jsQuote($value) { return '\'' . addslashes((string)$value) . '\''; } + /** + * Get the language code + * + * Get from extension configuration first, fallback to TYPO3 setting config.language. + * + * @return string + */ + protected function getLanguageCode($fallback = 'en') { + $languageCode = ''; + + if (!empty($fallback)) { + $languageCode = $fallback; + } + if (!empty($this->conf['lang'])) { + // language from plugin configuration + $languageCode = $this->conf['lang']; + } elseif (!empty($this->typoscriptFrontendController->tmpl->setup['config.']['language'])) { + // automatic language detection (TYPO3 settings) + $languageCode = $this->typoscriptFrontendController->tmpl->setup['config.']['language']; + } + return $languageCode; + } + /** * Validate reCAPTCHA challenge/response * diff --git a/ext_typoscript_constants.txt b/ext_typoscript_constants.txt index fa1f6f8..535c5d5 100644 --- a/ext_typoscript_constants.txt +++ b/ext_typoscript_constants.txt @@ -21,12 +21,24 @@ plugin.tx_jmrecaptcha { # cat=plugin.recaptcha/enable/07; type=boolean; label= Force SSL: Force use of SSL (api_server_secure) use_ssl = 0 - # cat=plugin.recaptcha//08; type=options[light,dark,red,white,blackglass,clean,custom]; label= reCAPTCHA Theme: Predefined reCAPTCHA themes (dark, light only for NoCaptcha) + # cat=plugin.recaptcha//08; type=options[light,dark,red,white,blackglass,clean,custom]; label= reCAPTCHA Theme: Predefined reCAPTCHA themes (light, dark only for NoCaptcha) theme = - # cat=plugin.recaptcha//09; type=int+; label= reCAPTCHA TabIndex: TabIndex of reCAPTCHA field + # cat=plugin.recaptcha//09; type=options[image,audio]; label= reCAPTCHA Fallback type: Optional. The type of CAPTCHA to serve. + type = image + + # cat=plugin.recaptcha//10; type=options[normal,compact]; label= reCAPTCHA Size: Optional. The size of the widget. + size = normal + + # cat=plugin.recaptcha//11; type=int+; label= reCAPTCHA TabIndex: Optional. The tabindex of the widget and challenge. If other elements in your page use tabindex, it should be set to make user navigation easier. tabindex = 0 - # cat=plugin.recaptcha//10; type=string; label= reCAPTCHA language: Language of reCAPTCHA frontend (autodetected if empty) + # cat=plugin.recaptcha//12; type=string; label= reCAPTCHA Callback function: Optional. The name of your callback function to be executed when the user submits a successful CAPTCHA response. The user's response, g-recaptcha-response, will be the input for your callback function. + callback = + + # cat=plugin.recaptcha//13; type=string; label= reCAPTCHA Expire callback function: Optional. The name of your callback function to be executed when the recaptcha response expires and the user needs to solve a new CAPTCHA. + expired_callback = + + # cat=plugin.recaptcha//14; type=string; label= reCAPTCHA language: Optional. Forces the widget to render in a specific language. Auto-detects the user's language if unspecified. lang = } diff --git a/ext_typoscript_setup.txt b/ext_typoscript_setup.txt index d1ddf68..1ec3f0f 100644 --- a/ext_typoscript_setup.txt +++ b/ext_typoscript_setup.txt @@ -12,7 +12,11 @@ plugin.tx_jmrecaptcha { use_ssl = {$plugin.tx_jmrecaptcha.use_ssl} theme = {$plugin.tx_jmrecaptcha.theme} + type = {$plugin.tx_jmrecaptcha.type} + size = {$plugin.tx_jmrecaptcha.size} tabindex = {$plugin.tx_jmrecaptcha.tabindex} + callback = {$plugin.tx_jmrecaptcha.callback} + expired_callback = {$plugin.tx_jmrecaptcha.expired_callback} lang = {$plugin.tx_jmrecaptcha.lang} }