Slides (except YouTube) can carry one image. Uploads are validated, rotated by EXIF orientation, scaled to 1600 px and stored as WebP outside webroot, which also strips camera metadata. Images have an alt text (or are marked decorative) and a layout: top, side or full screen (content slides). Files are removed after the commit when a slide is deleted or its image is replaced, with the poll, and `bin/cake images_cleanup` sweeps leftovers. Duplicated slides get their own file. Docker image gains gd with WebP. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
250 lines
8.9 KiB
PHP
250 lines
8.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Model\Entity\Poll;
|
|
use App\Model\Entity\Slide;
|
|
use App\Model\Table\PollsTable;
|
|
use App\Service\ImageService;
|
|
use App\Service\PollStateService;
|
|
use App\Service\VoteException;
|
|
use App\Service\VoteService;
|
|
use Cake\Event\EventInterface;
|
|
use Cake\Http\Cookie\Cookie;
|
|
use Cake\Http\Exception\NotFoundException;
|
|
use Cake\Http\Response;
|
|
use Cake\I18n\DateTime;
|
|
|
|
/**
|
|
* Participant pages under /p/{code}.
|
|
*
|
|
* Participants are anonymous. A random id is stored in the cookie
|
|
* `plenario_pid`; only an HMAC of it (scoped per poll) ends up in the database.
|
|
*/
|
|
class ParticipateController extends AppController
|
|
{
|
|
public const COOKIE = 'plenario_pid';
|
|
|
|
protected PollsTable $Polls;
|
|
|
|
protected string $participantId;
|
|
|
|
protected bool $newParticipant = false;
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function initialize(): void
|
|
{
|
|
parent::initialize();
|
|
/** @var \App\Model\Table\PollsTable $polls */
|
|
$polls = $this->fetchTable('Polls');
|
|
$this->Polls = $polls;
|
|
}
|
|
|
|
/**
|
|
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
|
|
* @return void
|
|
*/
|
|
public function beforeFilter(EventInterface $event): void
|
|
{
|
|
parent::beforeFilter($event);
|
|
if ($this->request->getParam('action') === 'image') {
|
|
// Cacheable static content: no participant id, no cookie.
|
|
return;
|
|
}
|
|
|
|
$pid = (string)$this->request->getCookie(self::COOKIE, '');
|
|
if (!preg_match('/^[a-f0-9]{32}$/', $pid)) {
|
|
$pid = bin2hex(random_bytes(16));
|
|
$this->newParticipant = true;
|
|
}
|
|
$this->participantId = $pid;
|
|
}
|
|
|
|
/**
|
|
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
|
|
* @return void
|
|
*/
|
|
public function afterFilter(EventInterface $event): void
|
|
{
|
|
parent::afterFilter($event);
|
|
if ($this->newParticipant) {
|
|
$this->response = $this->response->withCookie(Cookie::create(self::COOKIE, $this->participantId, [
|
|
'expires' => new DateTime('+1 year'),
|
|
'path' => '/',
|
|
'httponly' => true,
|
|
'secure' => $this->request->is('https'),
|
|
'samesite' => 'Lax',
|
|
]));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $code Join code
|
|
* @return void
|
|
*/
|
|
public function view(string $code): void
|
|
{
|
|
$poll = $this->loadPoll($code);
|
|
$stateService = new PollStateService();
|
|
$slide = $poll->is_active ? $stateService->currentSlide($poll) : null;
|
|
$participantHash = $this->participantHash($poll);
|
|
$state = $stateService->forParticipant($poll, $participantHash, $slide);
|
|
$ownText = $slide !== null && VoteService::hasEditableText($slide) && $state['hasResponded']
|
|
? (new VoteService())->ownText($slide, $participantHash)
|
|
: null;
|
|
|
|
$this->set(compact('poll', 'slide', 'state', 'participantHash', 'ownText'));
|
|
$this->setPollInterval();
|
|
}
|
|
|
|
/**
|
|
* Serves the image of a slide of this poll. The file name is random and changes with every new
|
|
* image, so the answer can be cached for good; unknown or replaced images are 404.
|
|
*
|
|
* @param string $code Join code
|
|
* @param string $file Stored file name
|
|
* @return \Cake\Http\Response
|
|
*/
|
|
public function image(string $code, string $file): Response
|
|
{
|
|
$poll = $this->loadPoll($code);
|
|
$path = $this->fetchTable('Slides')->exists(['poll_id' => $poll->id, 'image' => $file])
|
|
? (new ImageService())->path($poll->id, $file)
|
|
: null;
|
|
if ($path === null) {
|
|
throw new NotFoundException(__('Bild nicht gefunden.'));
|
|
}
|
|
|
|
return $this->response
|
|
->withFile($path)
|
|
->withHeader('Content-Type', 'image/webp')
|
|
->withHeader('Cache-Control', 'private, max-age=31536000, immutable');
|
|
}
|
|
|
|
/**
|
|
* @param string $code Join code
|
|
* @return \Cake\Http\Response
|
|
*/
|
|
public function state(string $code): Response
|
|
{
|
|
$poll = $this->loadPoll($code);
|
|
|
|
return $this->json((new PollStateService())->forParticipant($poll, $this->participantHash($poll)));
|
|
}
|
|
|
|
/**
|
|
* Stores an answer. AJAX requests (word cloud entries and upvotes) get JSON
|
|
* `{ok, message, state}` instead of a redirect with flash message.
|
|
*
|
|
* @param string $code Join code
|
|
* @return \Cake\Http\Response|null
|
|
*/
|
|
public function vote(string $code): ?Response
|
|
{
|
|
$this->request->allowMethod(['post']);
|
|
$poll = $this->loadPoll($code);
|
|
$stateService = new PollStateService();
|
|
$slide = $stateService->currentSlide($poll);
|
|
$submittedSlideId = (int)$this->request->getData('slide_id');
|
|
$ajax = $this->request->is('ajax');
|
|
$status = 200;
|
|
|
|
try {
|
|
// Generous per IP (a whole audience may share one Wi-Fi), only stops scripted floods.
|
|
if (!$this->throttle('vote', 'Plenario.throttle.votesPerMinute', 60)) {
|
|
$status = 429;
|
|
throw new VoteException(__('Zu viele Antworten in kurzer Zeit. Bitte warte einen Moment und versuche es dann noch einmal.'));
|
|
}
|
|
if ($slide === null || $slide->id !== $submittedSlideId) {
|
|
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
|
|
}
|
|
(new VoteService())->submit($poll, $slide, $this->participantHash($poll), (array)$this->request->getData());
|
|
$ok = true;
|
|
$message = match (true) {
|
|
!$slide->isQa() => __('Danke für deine Antwort!'),
|
|
$this->request->getData('vote') === 'up' => __('Deine Stimme ist angekommen.'),
|
|
$this->request->getData('vote') === 'remove' => __('Deine Stimme wurde zurückgenommen.'),
|
|
$this->request->getData('vote') === 'edit' => __('Deine Frage wurde geändert.'),
|
|
$this->request->getData('vote') === 'delete' => __('Deine Frage wurde gelöscht.'),
|
|
default => __('Danke, deine Frage ist angekommen!'),
|
|
};
|
|
} catch (VoteException $e) {
|
|
$ok = false;
|
|
$message = $e->getMessage();
|
|
}
|
|
|
|
if ($ajax) {
|
|
return $this->json([
|
|
'ok' => $ok,
|
|
'message' => $message,
|
|
'state' => $stateService->forParticipant($poll, $this->participantHash($poll), $slide),
|
|
], $status);
|
|
}
|
|
$ok ? $this->Flash->success($message) : $this->Flash->error($message);
|
|
|
|
return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]);
|
|
}
|
|
|
|
/**
|
|
* Replaces the own answer text (open question) or comment (scale) while voting is open,
|
|
* e.g. to fix a typo. See VoteService::editText().
|
|
*
|
|
* @param string $code Join code
|
|
* @return \Cake\Http\Response|null
|
|
*/
|
|
public function edit(string $code): ?Response
|
|
{
|
|
$this->request->allowMethod(['post']);
|
|
$poll = $this->loadPoll($code);
|
|
$slide = (new PollStateService())->currentSlide($poll);
|
|
|
|
try {
|
|
if (!$this->throttle('vote', 'Plenario.throttle.votesPerMinute', 60)) {
|
|
throw new VoteException(__('Zu viele Antworten in kurzer Zeit. Bitte warte einen Moment und versuche es dann noch einmal.'));
|
|
}
|
|
if ($slide === null || $slide->id !== (int)$this->request->getData('slide_id')) {
|
|
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
|
|
}
|
|
$data = (array)$this->request->getData();
|
|
(new VoteService())->editText($poll, $slide, $this->participantHash($poll), $data);
|
|
$comment = $data['comment'] ?? '';
|
|
$this->Flash->success(match (true) {
|
|
$slide->type === Slide::TYPE_OPEN => __('Deine Antwort wurde geändert.'),
|
|
!is_scalar($comment) || trim((string)$comment) === '' => __('Dein Kommentar wurde entfernt.'),
|
|
default => __('Dein Kommentar wurde gespeichert.'),
|
|
});
|
|
} catch (VoteException $e) {
|
|
$this->Flash->error($e->getMessage());
|
|
}
|
|
|
|
return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]);
|
|
}
|
|
|
|
/**
|
|
* @param string $code Join code
|
|
* @return \App\Model\Entity\Poll
|
|
*/
|
|
protected function loadPoll(string $code): Poll
|
|
{
|
|
/** @var \App\Model\Entity\Poll|null $poll */
|
|
$poll = $this->Polls->find('byJoinCode', code: $code)->first();
|
|
if ($poll === null) {
|
|
throw new NotFoundException(__('Zu diesem Code wurde keine Umfrage gefunden.'));
|
|
}
|
|
|
|
return $poll;
|
|
}
|
|
|
|
/**
|
|
* @param \App\Model\Entity\Poll $poll Poll
|
|
* @return string
|
|
*/
|
|
protected function participantHash(Poll $poll): string
|
|
{
|
|
return VoteService::participantHash($this->participantId, $poll->id);
|
|
}
|
|
}
|