Skip to content

Commit

Permalink
Merge pull request #8 from vaxul/develop
Browse files Browse the repository at this point in the history
Additional options for no-CAPTCHA variant
  • Loading branch information
mblaschke authored Jun 20, 2016
2 parents d23ec59 + e6224d9 commit d6fdc82
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 15 deletions.
83 changes: 71 additions & 12 deletions class.tx_jmrecaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -79,11 +99,36 @@ protected function initConfiguration() {
* @return string
*/
protected function renderNoCaptcha() {
$content = '<script type="text/javascript" src="' . htmlspecialchars($this->conf['server']) . '.js?hl=' . htmlspecialchars($this->conf['lang']) . '"></script>';
$content .= '<div class="g-recaptcha" data-sitekey="' . htmlspecialchars($this->conf['public_key']) . '" data-theme="' . htmlspecialchars($this->conf['theme']) . '"></div>';
$language = $this->getLanguageCode(null);
$languageParameter = '';
if (!empty($language)) {
$languageParameter = 'hl=' . $language;
}

$content = '<script type="text/javascript" src="' . htmlspecialchars($this->conf['server'] . '.js?' . $languageParameter) . '"></script>';
$content .= '<div class="g-recaptcha"' . $this->getNoCaptchaParameters() . '></div>';
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
Expand All @@ -101,7 +146,7 @@ protected function renderReCaptcha($error) {

// Default settings
$recaptchaOptions = array(
'lang' => self::jsQuote('en'),
'lang' => self::jsQuote($this->getLanguageCode()),
);

// Theme
Expand All @@ -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();

Expand Down Expand Up @@ -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
*
Expand Down
18 changes: 15 additions & 3 deletions ext_typoscript_constants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
}
4 changes: 4 additions & 0 deletions ext_typoscript_setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}

}

0 comments on commit d6fdc82

Please sign in to comment.