Lettersail: online English school for kids on MODX 3 (templates, chunks, snippets, plugins, scripts)
This commit is contained in:
commit
3799cf8de5
96 files changed
+4423
No files matched your search
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
require_once '/var/www/html/elements/lib/altcha/autoload.php';
|
||||
|
||||
use AltchaOrg\Altcha\Algorithm\Pbkdf2;
|
||||
use AltchaOrg\Altcha\Altcha;
|
||||
use AltchaOrg\Altcha\Payload;
|
||||
use AltchaOrg\Altcha\VerifySolutionOptions;
|
||||
|
||||
$fail = static function (string $field, string $message) use ($hook): bool {
|
||||
$hook->addError($field, $message);
|
||||
return false;
|
||||
};
|
||||
|
||||
$value = static fn(string $key): string => trim((string) $hook->getValue($key));
|
||||
|
||||
$host = strtolower((string) parse_url('//' . ($_SERVER['HTTP_HOST'] ?? ''), PHP_URL_HOST));
|
||||
$origin = (string) ($_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '');
|
||||
$originHost = strtolower((string) parse_url($origin, PHP_URL_HOST));
|
||||
if ($origin === '' || $host === '' || $originHost !== $host) {
|
||||
return $fail('guard', 'origin');
|
||||
}
|
||||
|
||||
$secret = hash_hmac('sha256', 'lettersail-captcha', (string) $modx->getOption('site_id', null, 'lettersail'));
|
||||
$cookie = explode('.', (string) ($_COOKIE['ls_t'] ?? ''), 2);
|
||||
$opened = (int) ($cookie[0] ?? 0);
|
||||
if ($opened <= 0 || !hash_equals(hash_hmac('sha256', (string) $opened, $secret), (string) ($cookie[1] ?? ''))) {
|
||||
return $fail('guard', 'time');
|
||||
}
|
||||
$elapsed = (int) (microtime(true) * 1000) - $opened;
|
||||
if ($elapsed < 2500 || $elapsed > 86400000) {
|
||||
return $fail('guard', 'time');
|
||||
}
|
||||
|
||||
$digits = preg_replace('/\D/', '', $value('phone'));
|
||||
if (strlen($digits) < 10 || strlen($digits) > 15) {
|
||||
return $fail('phone', $modx->lexicon('ls_err_phone'));
|
||||
}
|
||||
|
||||
$raw = $value('altcha');
|
||||
if ($raw === '' || strlen($raw) > 4000) {
|
||||
return $fail('guard', 'captcha');
|
||||
}
|
||||
try {
|
||||
$payload = Payload::fromBase64($raw);
|
||||
$result = (new Altcha(hmacSignatureSecret: $secret))->verifySolution(
|
||||
new VerifySolutionOptions(algorithm: new Pbkdf2(), payload: $payload)
|
||||
);
|
||||
} catch (\Throwable $error) {
|
||||
return $fail('guard', 'captcha');
|
||||
}
|
||||
if (!$result->verified || $payload->challenge->signature === null) {
|
||||
return $fail('guard', 'captcha');
|
||||
}
|
||||
$used = 'altcha_' . hash('sha256', (string) $payload->challenge->signature);
|
||||
$cacheOptions = [xPDO::OPT_CACHE_KEY => 'lead'];
|
||||
if ($modx->cacheManager->get($used, $cacheOptions)) {
|
||||
return $fail('guard', 'captcha');
|
||||
}
|
||||
$one = 1;
|
||||
$modx->cacheManager->set($used, $one, 3600, $cacheOptions);
|
||||
|
||||
$hook->setValue('phone', '+' . $digits);
|
||||
$hook->setValue('sent_at', date('d.m.Y H:i:s'));
|
||||
$hook->setValue('page', mb_substr((string) ($_SERVER['HTTP_REFERER'] ?? ''), 0, 200));
|
||||
$hook->setValue('consent_version', (string) $modx->getOption('ls_consent_version', null, '22.10.2026'));
|
||||
|
||||
return true;
|
||||
Reference in new issue
Block a user