contao-components/altcha
Customized ALTCHA script packaged for integration with Contao Open Source CMS, providing a drop-in way to use ALTCHA within Contao installations.
Installation
composer require contao-components/altcha
Ensure your project uses Contao 4.x (LTS) with PHP 8.0+.
Enable the Bundle
Register in config/bundles.php:
return [
ContaoComponents\AltchaBundle\AltchaBundle::class => ['all' => true],
];
Clear Contao’s cache:
php bin/console contao:clear-cache
First Use Case
altcha widget to a Contao form via the Form Builder (under "CAPTCHA" widgets).AltchaWidget in a custom DCA field:
$GLOBALS['TL_DCA']['tl_form']['fields']['captcha'] = [
'inputType' => 'altcha',
'eval' => ['tl_class' => 'altcha'],
];
Form Integration
Widget system to inject ALTCHA into forms:
use ContaoComponents\AltchaBundle\Widget\AltchaWidget;
$widget = new AltchaWidget('my_form', 'captcha');
echo $widget->generate();
Form::onSubmit:
$GLOBALS['TL_HOOKS']['formOnSubmit'][] = function ($form) {
if ($form->id === 'contact_form') {
$validator = new \ContaoComponents\AltchaBundle\Validator\AltchaValidator();
if (!$validator->validate(\Input::post('ALTCHA_RESPONSE'))) {
$form->addError('CAPTCHA validation failed.');
}
}
};
Configuration Management
System::getContainer() to access ALTCHA config:
$apiKey = \System::getContainer()->getParameter('altcha.api_key');
$siteKey = \System::getContainer()->getParameter('altcha.site_key');
.env:
ALTCHA_API_KEY=your_key_here
ALTCHA_SITE_KEY=your_site_key
Theming and Assets
vendor/contao-components/altcha/src/Resources/views/ to templates/altcha/.assets directory and load via:
$this->addToTemplate('altchaJs', '<script src="%s/assets/altcha/custom.js"></script>');
Validation Logic
use ContaoComponents\AltchaBundle\Validator\AltchaValidator;
class CustomAltchaValidator extends AltchaValidator {
protected function validateResponse($response) {
// Add custom logic (e.g., rate-limiting)
return parent::validateResponse($response);
}
}
Session Conflicts
$altcha = new \ContaoComponents\Altcha\Altcha();
$altcha->setSessionNamespace('TL_SESSION');
Caching Issues
config/packages/framework.yaml:
framework:
cache:
pools:
app.cache.warmup:
paths: ['^/(?!altcha/).*$']
JavaScript Dependencies
config/packages/altcha.yaml:
altcha:
assets:
js: '%kernel.project_dir%/vendor/contao-components/altcha/src/Resources/public/altcha/altcha.js'
css: '%kernel.project_dir%/vendor/contao-components/altcha/src/Resources/public/altcha/altcha.css'
Multilingual Sites
generate() method in AltchaWidget to pass the current language:
$widget->setLanguage(\Input::get('lang') ?: \System::getContainer()->get('request_stack')->getCurrentRequest()->getLocale());
Enable Debug Mode: Add to config/packages/dev/altcha.yaml:
altcha:
debug: true
This logs token generation/validation to Contao’s log file.
Validation Errors: Check tl_error table for CAPTCHA-related errors:
SELECT * FROM tl_error WHERE message LIKE '%ALTCHA%';
Network Requests: Use browser dev tools to verify ALTCHA’s API calls to https://api.altcha.org.
Custom Validation
AltchaValidator to add rate-limiting or IP-based checks:
class RateLimitedAltchaValidator extends AltchaValidator {
public function validate($response) {
$ip = \Input::get('REMOTE_ADDR');
if ($this->isRateLimited($ip)) {
throw new \Exception('Too many requests.');
}
return parent::validate($response);
}
}
Alternative Storage
$altcha->setStorage(new \ContaoComponents\Altcha\Storage\DatabaseStorage(\Database::getInstance()));
Backend Integration
$GLOBALS['TL_DCA']['tl_member']['fields']['altcha'] = [
'inputType' => 'altcha',
'eval' => ['mandatory' => true, 'tl_class' => 'altcha'],
];
Fallback Mechanism
if (\Input::get('js_enabled') === 'false') {
$widget = new \Contao\Widget\TextCaptchaWidget();
} else {
$widget = new AltchaWidget();
}
How can I help you explore Laravel packages today?