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 $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)); } }