Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
manifest
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
20
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 path_helper */
26    protected $path_helper;
27
28    /** @var dispatcher_interface */
29    protected $phpbb_dispatcher;
30
31    /** @var user */
32    protected $user;
33
34    /**
35     * Constructor for manifest controller
36     *
37     * @param config $config
38     * @param path_helper $path_helper
39     * @param dispatcher_interface $phpbb_dispatcher
40     * @param user $user
41     */
42    public function __construct(config $config, path_helper $path_helper, dispatcher_interface $phpbb_dispatcher, user $user)
43    {
44        $this->config = $config;
45        $this->path_helper = $path_helper;
46        $this->phpbb_dispatcher = $phpbb_dispatcher;
47        $this->user = $user;
48    }
49
50    /**
51     * Handle creation of a manifest json file for progressive web-app support
52     *
53     * @return JsonResponse
54     */
55    public function handle(): JsonResponse
56    {
57        $board_path = $this->config['force_server_vars'] ? $this->config['script_path'] : $this->path_helper->get_web_root_path();
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'        => $board_path,
68            'scope'            => $board_path,
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    board_path        Path to the board root
77         * @var    string    sitename        Full name of the board
78         * @var    string    sitename_short    Shortened name of the board
79         * @since 4.0.0-a1
80         */
81        $vars = ['manifest', 'board_path', 'sitename', 'sitename_short'];
82        extract($this->phpbb_dispatcher->trigger_event('core.modify_manifest', compact($vars)));
83
84        $response = new JsonResponse($manifest);
85        $response->setPublic();
86        $response->setMaxAge(3600);
87        $response->headers->addCacheControlDirective('must-revalidate', true);
88
89        if (!empty($this->user->data['is_bot']))
90        {
91            // Let reverse proxies know we detected a bot.
92            $response->headers->set('X-PHPBB-IS-BOT', 'yes');
93        }
94
95        return $response;
96    }
97}