- AppController: joinUrl(), adminUrlFor(), setPollInterval() and protect() instead of copies in Admin-, Home-, Participate- and EmbedController. - PollMailer::sendSafely() replaces two try/catch blocks around mail sending. - Slide::optionIds() replaces five identical array_map() calls. - PollStateService: one pollPayload() for presenter and embed; the vote action passes the already loaded slide instead of loading it again. - ResultsService::forSlide() takes the answer/comment limit and the rating flag, so ReportService no longer queries answers, comments and the scale distribution twice and the export loads the slides only once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
226 lines
7.1 KiB
PHP
226 lines
7.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Captcha\CaptchaFactory;
|
|
use App\Mailer\PollMailer;
|
|
use App\Model\Entity\Slide;
|
|
use App\Model\Table\PollsTable;
|
|
use Cake\Core\Configure;
|
|
use Cake\Http\Response;
|
|
|
|
/**
|
|
* Public pages: start page, poll creation, joining by code, link recovery.
|
|
*/
|
|
class HomeController extends AppController
|
|
{
|
|
/**
|
|
* Name of the invisible honeypot field in the "create poll" form
|
|
* (see templates/element/honeypot.php). Must stay empty.
|
|
*/
|
|
public const HONEYPOT_FIELD = 'website';
|
|
|
|
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'));
|
|
$this->setSlideTypes();
|
|
}
|
|
|
|
/**
|
|
* Slide types for the start page, listed from the model so new types show up automatically.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function setSlideTypes(): void
|
|
{
|
|
$this->set('typeLabels', Slide::typeLabels());
|
|
$this->set('typeDescriptions', Slide::typeDescriptions());
|
|
}
|
|
|
|
/**
|
|
* 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->newPoll((array)$this->request->getData());
|
|
|
|
if ($this->isHoneypotFilled()) {
|
|
$this->Flash->error(__('Die Umfrage konnte nicht angelegt werden.'));
|
|
} elseif (!$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->insertPoll($poll);
|
|
if ($token !== null) {
|
|
$adminUrl = $this->adminUrlFor($token);
|
|
$sent = (new PollMailer())->sendSafely('adminLink', [$poll, $adminUrl, $this->joinUrl($poll)]);
|
|
|
|
$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->setSlideTypes();
|
|
$this->render('index');
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* True if the invisible honeypot field was filled in, i.e. the request
|
|
* most likely comes from a bot.
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function isHoneypotFilled(): bool
|
|
{
|
|
$value = $this->request->getData(self::HONEYPOT_FIELD);
|
|
|
|
return $value !== null && $value !== '';
|
|
}
|
|
|
|
/**
|
|
* 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' => $this->adminUrlFor($token),
|
|
'created' => $poll->created->i18nFormat('dd.MM.yyyy HH:mm'),
|
|
];
|
|
}
|
|
if ($links !== []) {
|
|
(new PollMailer())->sendSafely('recovery', [$email, $links]);
|
|
}
|
|
}
|
|
}
|