Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
manifest
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb;
15
16use phpbb\config\config;
17use phpbb\event\dispatcher_interface;
18use Symfony\Component\HttpFoundation\JsonResponse;
19
20class manifest
21{
22    /** @var config */
23    protected $config;
24
25    /** @var dispatcher_interface */
26    protected $phpbb_dispatcher;
27
28    /** @var user */
29    protected $user;
30
31    /**
32     * Constructor for manifest controller
33     *
34     * @param config $config
35     * @param dispatcher_interface $phpbb_dispatcher
36     * @param user $user
37     */
38    public function __construct(config $config, dispatcher_interface $phpbb_dispatcher, user $user)
39    {
40        $this->config = $config;
41        $this->phpbb_dispatcher = $phpbb_dispatcher;
42        $this->user = $user;
43    }
44
45    /**
46     * Handle creation of a manifest json file for progressive web-app support
47     *
48     * @return JsonResponse
49     */
50    public function handle(): JsonResponse
51    {
52        // Get the board URL and extract the path component
53        $board_path = $this->config['force_server_vars'] ? $this->config['script_path'] : (parse_url(generate_board_url(), PHP_URL_PATH) ?? '');
54
55        // Ensure path ends with '/' for PWA scope
56        $scope = rtrim($board_path, '/\\') . '/';
57        $start_url = $scope;
58
59        $sitename = html_entity_decode($this->config['sitename'], ENT_QUOTES, 'UTF-8');
60        $sitename_short = html_entity_decode($this->config['sitename_short'], ENT_QUOTES, 'UTF-8');
61
62        $manifest = [
63            'name'            => $sitename,
64            'short_name'    => $sitename_short ?: utf8_substr($sitename, 0, 12),
65            'display'        => 'standalone',
66            'orientation'    => 'portrait',
67            'start_url'        => $start_url,
68            'scope'            => $scope,
69        ];
70
71        /**
72         * Event to modify manifest data before it is outputted
73         *
74         * @event core.modify_manifest
75         * @var    array    manifest        Array of manifest members
76         * @var string  scope           PWA scope path
77         * @var string  start_url       PWA start URL
78         * @var    string    sitename        Full name of the board
79         * @var    string    sitename_short    Shortened name of the board
80         * @since 4.0.0-a1
81         */
82        $vars = ['manifest', 'scope', 'start_url', 'sitename', 'sitename_short'];
83        extract($this->phpbb_dispatcher->trigger_event('core.modify_manifest', compact($vars)));
84
85        $response = new JsonResponse($manifest);
86        $response->setPublic();
87        $response->setMaxAge(3600);
88        $response->headers->addCacheControlDirective('must-revalidate', true);
89
90        if (!empty($this->user->data['is_bot']))
91        {
92            // Let reverse proxies know we detected a bot.
93            $response->headers->set('X-PHPBB-IS-BOT', 'yes');
94        }
95
96        return $response;
97    }
98}