Files
Plenario/src/Controller/AppController.php
T
gnilebeinandClaude Opus 5.5 3e44ded107
CI / Deploy / test (push) Successful in 53s
CI / Deploy / deploy (push) Skipped
Share repeated controller and service code
- 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>
2026-11-08 22:50:56 +01:00

110 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller;
use App\Model\Entity\Poll;
use App\Service\ThrottleService;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Http\Response;
use Cake\Routing\Router;
/**
* Base controller of PlenarIO. Security headers (CSP, framing, nosniff) are set for every
* response by \App\Middleware\SecurityHeadersMiddleware.
*/
class AppController extends Controller
{
/**
* @return void
*/
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Flash');
}
/**
* Pages whose URL contains a secret (admin token, embed key): never send it as referrer,
* never index them, never cache them.
*
* @param \Cake\Http\Response $response Response
* @return \Cake\Http\Response
*/
protected function protect(Response $response): Response
{
return $response
->withHeader('Referrer-Policy', 'no-referrer')
->withHeader('X-Robots-Tag', 'noindex, nofollow')
->withHeader('Cache-Control', 'no-store, private');
}
/**
* Absolute participant URL of a poll (QR code, mails, presenter).
*
* @param \App\Model\Entity\Poll $poll Poll
* @return string
*/
protected function joinUrl(Poll $poll): string
{
return Router::url(['_name' => 'participate', 'code' => $poll->join_code], true);
}
/**
* Absolute admin URL for a plain token (only for mails and the confirmation page).
*
* @param string $token Plain admin token
* @return string
*/
protected function adminUrlFor(string $token): string
{
return Router::url(['_name' => 'admin', 'token' => $token], true);
}
/**
* Passes the polling interval of live pages to the view (`pollIntervalMs`).
*
* @return void
*/
protected function setPollInterval(): void
{
$this->set('pollIntervalMs', (int)Configure::read('Plenario.pollIntervalMs', 2000));
}
/**
* Registers a hit of the client IP in a throttle bucket (see ThrottleService).
*
* @param string $bucket Throttle bucket
* @param string $configKey Configure key holding the limit (0 = unlimited)
* @param int $windowSeconds Length of the window
* @return bool Whether the request is still allowed
*/
protected function throttle(string $bucket, string $configKey, int $windowSeconds = ThrottleService::WINDOW_SECONDS): bool
{
return (new ThrottleService())->attempt(
$bucket,
(string)$this->request->clientIp(),
(int)Configure::read($configKey, 0),
$windowSeconds,
);
}
/**
* Returns a JSON response without going through the view layer.
*
* @param array<string, mixed> $data Payload
* @param int $status HTTP status
* @return \Cake\Http\Response
*/
protected function json(array $data, int $status = 200): Response
{
return $this->response
->withStatus($status)
->withType('application/json')
->withHeader('Cache-Control', 'no-store')
->withStringBody((string)json_encode($data, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
}
}