Add initial Plenario live polling app
CI / Deploy / test (push) Successful in 49s
CI / Deploy / deploy (push) Failing after 5s

CakePHP 5.4 app for live polls: create via email without login, manage
via secret admin link, join via code. Includes slide types (choice,
wordcloud, open, scale, content), presenter mode, CSV export, captcha,
throttling, tests and Gitea CI/deploy workflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 13:24:29 +01:00
commit eb319f8c1e
120 files changed
+15572

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Captcha;
use Cake\Core\Configure;
use InvalidArgumentException;
/**
* Builds the captcha configured in `Plenario.captcha.type`.
*/
class CaptchaFactory
{
/**
* @param string $scope Form scope ("create", "recover", ...)
* @param string|null $type Override the configured type
* @return \App\Captcha\CaptchaInterface
*/
public static function create(string $scope, ?string $type = null): CaptchaInterface
{
$type ??= (string)Configure::read('Plenario.captcha.type', 'math');
return match (strtolower($type)) {
'none', '', 'off', 'false' => new NoCaptcha(),
'math' => new MathCaptcha($scope),
'recaptcha' => new RecaptchaCaptcha(
(string)Configure::read('Plenario.captcha.recaptcha.siteKey', ''),
(string)Configure::read('Plenario.captcha.recaptcha.secretKey', ''),
),
default => throw new InvalidArgumentException(sprintf(
'Unknown captcha type "%s". Use none, math or recaptcha.',
$type,
)),
};
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Captcha;
use Cake\Http\ServerRequest;
/**
* A captcha protects the public forms (create poll, recover links).
*
* Lifecycle: the controller calls challenge() when rendering the form and passes
* the returned array to the `captcha` element; on submit it calls verify().
*/
interface CaptchaInterface
{
/**
* @return string 'none' | 'math' | 'recaptcha'
*/
public function getType(): string;
/**
* Prepares a challenge and returns the data the view element needs.
*
* @param \Cake\Http\ServerRequest $request Current request (session access)
* @return array<string, mixed> Always contains 'type'
*/
public function challenge(ServerRequest $request): array;
/**
* @param \Cake\Http\ServerRequest $request Submitted request
* @return bool
*/
public function verify(ServerRequest $request): bool;
}
+76
View File
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Captcha;
use Cake\Http\ServerRequest;
/**
* Simple arithmetic captcha ("Was ist 4 + 7?"). The expected answer is kept in
* the session per form scope and is valid for a single verification only.
*
* Request field: `captcha_answer`.
*/
class MathCaptcha implements CaptchaInterface
{
public const FIELD = 'captcha_answer';
/**
* @param string $scope Form scope, e.g. "create" or "recover"
*/
public function __construct(protected string $scope)
{
}
/**
* @inheritDoc
*/
public function getType(): string
{
return 'math';
}
/**
* @return string
*/
protected function sessionKey(): string
{
return 'Captcha.math.' . $this->scope;
}
/**
* @inheritDoc
*/
public function challenge(ServerRequest $request): array
{
$a = random_int(1, 12);
$b = random_int(1, 9);
if (random_int(0, 1) === 1 && $a > $b) {
$question = sprintf('%d − %d', $a, $b);
$answer = $a - $b;
} else {
$question = sprintf('%d + %d', $a, $b);
$answer = $a + $b;
}
$request->getSession()->write($this->sessionKey(), $answer);
return ['type' => 'math', 'question' => $question, 'field' => self::FIELD];
}
/**
* @inheritDoc
*/
public function verify(ServerRequest $request): bool
{
$session = $request->getSession();
$expected = $session->read($this->sessionKey());
$session->delete($this->sessionKey());
$given = trim((string)$request->getData(self::FIELD, ''));
if ($expected === null || $given === '' || !preg_match('/^-?\d+$/', $given)) {
return false;
}
return (int)$given === (int)$expected;
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Captcha;
use Cake\Http\ServerRequest;
/**
* Captcha disabled (CAPTCHA_TYPE=none).
*/
class NoCaptcha implements CaptchaInterface
{
/**
* @inheritDoc
*/
public function getType(): string
{
return 'none';
}
/**
* @inheritDoc
*/
public function challenge(ServerRequest $request): array
{
return ['type' => 'none'];
}
/**
* @inheritDoc
*/
public function verify(ServerRequest $request): bool
{
return true;
}
}
+83
View File
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace App\Captcha;
use Cake\Http\Client;
use Cake\Http\ServerRequest;
use Cake\Log\Log;
use Throwable;
/**
* Google reCAPTCHA v2 checkbox. Requires RECAPTCHA_SITE_KEY and RECAPTCHA_SECRET_KEY.
* Fails closed: missing keys or network errors reject the submission.
*
* Request field: `g-recaptcha-response` (set by Google's widget).
*/
class RecaptchaCaptcha implements CaptchaInterface
{
public const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
public const FIELD = 'g-recaptcha-response';
protected Client $client;
/**
* @param string $siteKey Public site key
* @param string $secretKey Secret key
* @param \Cake\Http\Client|null $client HTTP client (injectable for tests)
*/
public function __construct(
protected string $siteKey,
protected string $secretKey,
?Client $client = null,
) {
$this->client = $client ?? new Client(['timeout' => 5]);
}
/**
* @inheritDoc
*/
public function getType(): string
{
return 'recaptcha';
}
/**
* @inheritDoc
*/
public function challenge(ServerRequest $request): array
{
return ['type' => 'recaptcha', 'siteKey' => $this->siteKey];
}
/**
* @inheritDoc
*/
public function verify(ServerRequest $request): bool
{
$token = (string)$request->getData(self::FIELD, '');
if ($token === '') {
return false;
}
if ($this->siteKey === '' || $this->secretKey === '') {
Log::error('reCAPTCHA is enabled but RECAPTCHA_SITE_KEY / RECAPTCHA_SECRET_KEY are not configured.');
return false;
}
try {
$response = $this->client->post(self::VERIFY_URL, [
'secret' => $this->secretKey,
'response' => $token,
'remoteip' => $request->clientIp(),
]);
$json = $response->getJson();
} catch (Throwable $e) {
Log::error('reCAPTCHA verification failed: ' . $e->getMessage());
return false;
}
return is_array($json) && ($json['success'] ?? false) === true;
}
}