Lines 81.81% 18 / 22
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
StatusGet 81.81% 18 / 22 50.00% 1 / 2 6.22 0.00% 0 / 1
 readResponse 75.00% 12 / 16 0.00% 0 / 1 2.06
 assertStatusAccess 100.00% 6 / 6 100.00% 1 / 1 4
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsbackend\Status\Api;
9
10use BO\Slim\Render;
11use BO\Zmsbackend\Status\Exception\StatusAuthenticationFailed;
12use BO\Zmsbackend\Status\Service\Status;
13use BO\Zmsentities\Exception\UserAccountMissingLogin;
14use BO\Zmsentities\Exception\UserAccountMissingRights;
15
16class StatusGet extends \BO\Zmsbackend\Api\BaseController
17{
18    /**
19     * @SuppressWarnings(Param)
20     * @return \Psr\Http\Message\ResponseInterface
21     */
22    #[\Override]
23    public function readResponse(
24        \Psr\Http\Message\RequestInterface $request,
25        \Psr\Http\Message\ResponseInterface $response,
26        array $args
27    ) {
28        $this->assertStatusAccess($request);
29
30        $validator = $request->getAttribute('validator');
31        $includeProcessStats = $validator->getParameter('includeProcessStats')->isNumber()->setDefault(1)->getValue();
32        $status = (new \BO\Zmsbackend\Status\Service\Status())->readEntity(\App::$now, $includeProcessStats);
33        $status['version'] = \BO\Zmsbackend\Helper\Version::getArray();
34        $status['phpVersion'] = PHP_VERSION;
35        if (\App::DEBUG) {
36            $status['opcache'] = [
37                'config' => opcache_get_configuration(),
38                'status' => opcache_get_status(false)
39            ];
40        }
41
42        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
43        $message->data = $status;
44
45        $response = Render::withLastModified($response, time(), '0');
46        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
47        return $response;
48    }
49
50    /**
51     * Allow access with a logged-in workstation (admin UI) or X-Token matching
52     * ZMS_CONFIG_SECURE_TOKEN (Grafana / status-logger). Same idea as ConfigGet,
53     * with strict token comparison and a narrow auth-exception catch.
54     */
55    private function assertStatusAccess(\Psr\Http\Message\RequestInterface $request): void
56    {
57        try {
58            (new \BO\Zmsbackend\Helper\User($request))->checkPermissions();
59            return;
60        } catch (UserAccountMissingLogin | UserAccountMissingRights $exception) {
61            // Fall through to X-Token for scrapers / unauthenticated callers.
62        }
63
64        $token = $request->getHeaderLine('X-Token');
65        if ($token === '' || !hash_equals((string) \App::SECURE_TOKEN, $token)) {
66            throw new StatusAuthenticationFailed();
67        }
68    }
69}