Add initial Plenario live polling app
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:
commit
eb319f8c1e
120 files changed
+15572
No files matched your search
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Captcha\CaptchaFactory;
|
||||
use App\Mailer\PollMailer;
|
||||
use App\Model\Table\PollsTable;
|
||||
use App\Service\ThrottleService;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Http\Response;
|
||||
use Cake\Log\Log;
|
||||
use Cake\Routing\Router;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Public pages: start page, poll creation, joining by code, link recovery.
|
||||
*/
|
||||
class HomeController extends AppController
|
||||
{
|
||||
protected PollsTable $Polls;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
/** @var \App\Model\Table\PollsTable $polls */
|
||||
$polls = $this->fetchTable('Polls');
|
||||
$this->Polls = $polls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start page with "create poll" and "join" forms.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function index(): void
|
||||
{
|
||||
$poll = $this->Polls->newEmptyEntity();
|
||||
$captcha = CaptchaFactory::create('create')->challenge($this->request);
|
||||
$this->set(compact('poll', 'captcha'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a poll and mails the admin link.
|
||||
*
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function create(): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$captcha = CaptchaFactory::create('create');
|
||||
|
||||
$poll = $this->Polls->newEmptyEntity();
|
||||
$poll = $this->Polls->patchEntity($poll, (array)$this->request->getData(), ['fields' => ['title', 'email']]);
|
||||
|
||||
if (!$captcha->verify($this->request)) {
|
||||
$this->Flash->error(__('Die Sicherheitsabfrage war nicht korrekt. Bitte versuche es erneut.'));
|
||||
} elseif (!$poll->hasErrors() && !$this->throttle('create', 'Plenario.throttle.createPerHour')) {
|
||||
$this->Flash->error(__('Zu viele Umfragen in kurzer Zeit. Bitte versuche es später noch einmal.'));
|
||||
} elseif (!$poll->hasErrors()) {
|
||||
[$poll, $token] = $this->Polls->createPoll((array)$this->request->getData());
|
||||
if ($token !== null) {
|
||||
$adminUrl = Router::url(['_name' => 'admin', 'token' => $token], true);
|
||||
$joinUrl = Router::url(['_name' => 'participate', 'code' => $poll->join_code], true);
|
||||
$sent = $this->sendMail(fn(PollMailer $mailer) => $mailer->send('adminLink', [$poll, $adminUrl, $joinUrl]));
|
||||
|
||||
$session = $this->request->getSession();
|
||||
$session->write('Plenario.created', [
|
||||
'email' => $poll->email,
|
||||
'title' => $poll->title,
|
||||
'mailSent' => $sent,
|
||||
'adminUrl' => Configure::read('Plenario.showAdminLinkAfterCreate') ? $adminUrl : null,
|
||||
]);
|
||||
|
||||
return $this->redirect(['_name' => 'created']);
|
||||
}
|
||||
$this->Flash->error(__('Die Umfrage konnte nicht angelegt werden.'));
|
||||
}
|
||||
|
||||
// Re-render the start page with errors.
|
||||
$this->set('poll', $poll);
|
||||
$this->set('captcha', $captcha->challenge($this->request));
|
||||
$this->render('index');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirmation page after creating a poll.
|
||||
*
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function created(): ?Response
|
||||
{
|
||||
$created = $this->request->getSession()->consume('Plenario.created');
|
||||
if (!is_array($created)) {
|
||||
return $this->redirect(['_name' => 'home']);
|
||||
}
|
||||
$this->set('created', $created);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Join by code: GET /join?code=XYZ or POST from the start page form.
|
||||
*
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function join(): ?Response
|
||||
{
|
||||
$code = (string)($this->request->getData('code') ?? $this->request->getQuery('code') ?? '');
|
||||
if ($code === '') {
|
||||
$this->set('code', '');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$poll = $this->Polls->find('byJoinCode', code: $code)->first();
|
||||
if ($poll === null) {
|
||||
$this->Flash->error(__('Zu diesem Code wurde keine Umfrage gefunden.'));
|
||||
$this->set('code', $code);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends all admin links of an e-mail address. Always answers with the same
|
||||
* message to avoid leaking which addresses exist.
|
||||
*
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function recover(): ?Response
|
||||
{
|
||||
$captcha = CaptchaFactory::create('recover');
|
||||
|
||||
if ($this->request->is('post')) {
|
||||
$email = strtolower(trim((string)$this->request->getData('email')));
|
||||
if (!$captcha->verify($this->request)) {
|
||||
$this->Flash->error(__('Die Sicherheitsabfrage war nicht korrekt. Bitte versuche es erneut.'));
|
||||
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->Flash->error(__('Bitte gib eine gültige E-Mail-Adresse ein.'));
|
||||
} elseif (!$this->throttle('recover', 'Plenario.throttle.recoverPerHour')) {
|
||||
$this->Flash->error(__('Zu viele Anfragen. Bitte versuche es später noch einmal.'));
|
||||
} else {
|
||||
$this->sendRecoveryMail($email);
|
||||
$this->Flash->success(__(
|
||||
'Falls zu {0} Umfragen existieren, haben wir dir die Admin-Links per E-Mail geschickt.',
|
||||
$email,
|
||||
));
|
||||
|
||||
return $this->redirect(['_name' => 'recover']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->set('captcha', $captcha->challenge($this->request));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email Normalised e-mail
|
||||
* @return void
|
||||
*/
|
||||
protected function sendRecoveryMail(string $email): void
|
||||
{
|
||||
/** @var list<\App\Model\Entity\Poll> $polls */
|
||||
$polls = $this->Polls->find()
|
||||
->where(['email' => $email])
|
||||
->orderBy(['created' => 'DESC'])
|
||||
->limit(50)
|
||||
->all()
|
||||
->toList();
|
||||
|
||||
$links = [];
|
||||
foreach ($polls as $poll) {
|
||||
$token = $this->Polls->decryptToken($poll);
|
||||
if ($token === null) {
|
||||
continue;
|
||||
}
|
||||
$links[] = [
|
||||
'title' => $poll->title,
|
||||
'url' => Router::url(['_name' => 'admin', 'token' => $token], true),
|
||||
'created' => $poll->created->i18nFormat('dd.MM.yyyy HH:mm'),
|
||||
];
|
||||
}
|
||||
if ($links !== []) {
|
||||
$this->sendMail(fn(PollMailer $mailer) => $mailer->send('recovery', [$email, $links]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $bucket Throttle bucket
|
||||
* @param string $configKey Configure key holding the hourly limit
|
||||
* @return bool
|
||||
*/
|
||||
protected function throttle(string $bucket, string $configKey): bool
|
||||
{
|
||||
return (new ThrottleService())->attempt(
|
||||
$bucket,
|
||||
(string)$this->request->clientIp(),
|
||||
(int)Configure::read($configKey, 0),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a mail and logs (instead of throwing) transport errors.
|
||||
*
|
||||
* @param callable(\App\Mailer\PollMailer): mixed $send Callback
|
||||
* @return bool
|
||||
*/
|
||||
protected function sendMail(callable $send): bool
|
||||
{
|
||||
try {
|
||||
$send(new PollMailer());
|
||||
|
||||
return true;
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Mail delivery failed: ' . $e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user