Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
helper
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 10
2162
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 render
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 route
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 handle_language_select
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
 handle_navigation
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
72
 page_header
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 render_navigation
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
110
 render_language_select
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 get_active_main_menu
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
72
 sort_navigation_level
0.00% covered (danger)
0.00%
0 / 9
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\install\controller;
15
16use phpbb\install\helper\config;
17use phpbb\install\helper\navigation\navigation_provider;
18use phpbb\language\language;
19use phpbb\language\language_file_helper;
20use phpbb\path_helper;
21use phpbb\request\request;
22use phpbb\request\request_interface;
23use phpbb\routing\router;
24use phpbb\symfony_request;
25use phpbb\template\template;
26use Symfony\Component\HttpFoundation\Response;
27use Symfony\Component\HttpFoundation\Cookie;
28
29/**
30 * A duplicate of \phpbb\controller\helper
31 *
32 * This class is necessary because of controller\helper's legacy function calls
33 * to page_header() page_footer() functions which has unavailable dependencies.
34 */
35class helper
36{
37    /**
38     * @var config
39     */
40    protected $installer_config;
41
42    /**
43     * @var language
44     */
45    protected $language;
46
47    /**
48     * @var bool|string
49     */
50    protected $language_cookie;
51
52    /**
53     * @var language_file_helper
54     */
55    protected $lang_helper;
56
57    /**
58     * @var navigation_provider
59     */
60    protected $navigation_provider;
61
62    /**
63     * @var template
64     */
65    protected $template;
66
67    /**
68     * @var path_helper
69     */
70    protected $path_helper;
71
72    /**
73     * @var request
74     */
75    protected $phpbb_request;
76
77    /**
78     * @var symfony_request
79     */
80    protected $request;
81
82    /**
83     * @var router
84     */
85    protected $router;
86
87    /**
88     * @var string
89     */
90    protected $phpbb_admin_path;
91
92    /**
93     * @var string
94     */
95    protected $phpbb_root_path;
96
97    /**
98     * Constructor
99     *
100     * @param config                $config
101     * @param language                $language
102     * @param language_file_helper    $lang_helper
103     * @param navigation_provider    $nav
104     * @param template                $template
105     * @param path_helper            $path_helper
106     * @param request                $phpbb_request
107     * @param symfony_request        $request
108     * @param router                $router
109     * @param string                $phpbb_root_path
110     */
111    public function __construct(config $config, language $language, language_file_helper $lang_helper, navigation_provider $nav, template $template, path_helper $path_helper, request $phpbb_request, symfony_request $request, router $router, $phpbb_root_path)
112    {
113        $this->installer_config = $config;
114        $this->language = $language;
115        $this->language_cookie = false;
116        $this->lang_helper = $lang_helper;
117        $this->navigation_provider = $nav;
118        $this->template = $template;
119        $this->path_helper = $path_helper;
120        $this->phpbb_request = $phpbb_request;
121        $this->request = $request;
122        $this->router = $router;
123        $this->phpbb_root_path = $phpbb_root_path;
124        $this->phpbb_admin_path = $phpbb_root_path . 'adm/';
125    }
126
127    /**
128     * Automate setting up the page and creating the response object.
129     *
130     * @param string    $template_file        The template handle to render
131     * @param string    $page_title            The title of the page to output
132     * @param bool        $selected_language    True to enable language selector it, false otherwise
133     * @param int        $status_code        The status code to be sent to the page header
134     *
135     * @return Response object containing rendered page
136     */
137    public function render($template_file, $page_title = '', $selected_language = false, $status_code = 200)
138    {
139        $this->page_header($page_title, $selected_language);
140
141        $this->template->set_filenames(array(
142            'body'    => $template_file,
143        ));
144
145        $response = new Response($this->template->assign_display('body'), $status_code);
146
147        // Set language cookie
148        if ($this->language_cookie !== false)
149        {
150            $cookie = new Cookie('lang', $this->language_cookie, time() + 3600);
151            $response->headers->setCookie($cookie);
152
153            $this->language_cookie = false;
154        }
155
156        return $response;
157    }
158
159    /**
160     * Returns path from route name
161     *
162     * @param string    $route_name
163     * @param array        $parameters
164     *
165     * @return string
166     */
167    public function route($route_name, $parameters = array())
168    {
169        $url = $this->router->generate($route_name, $parameters);
170
171        return $url;
172    }
173
174    /**
175     * Handles language selector form
176     */
177    public function handle_language_select()
178    {
179        $lang = null;
180
181        // Check if language form has been submited
182        $submit = $this->phpbb_request->variable('change_lang', '');
183        if (!empty($submit))
184        {
185            $lang = $this->phpbb_request->variable('language', '');
186        }
187
188        // Retrieve language from cookie
189        $lang_cookie = $this->phpbb_request->variable('lang', '', false, request_interface::COOKIE);
190        if (empty($lang) && !empty($lang_cookie))
191        {
192            $lang = $lang_cookie;
193        }
194
195        $lang = (!empty($lang) && strpos($lang, '/') === false) ? $lang : null;
196        $this->language_cookie = $lang;
197
198        $this->render_language_select($lang);
199
200        if ($lang !== null)
201        {
202            $this->language->set_user_language($lang, true);
203            $this->installer_config->set('user_language', $lang);
204        }
205    }
206
207    /**
208     * Process navigation data to reflect active/completed stages
209     *
210     * @param \phpbb\install\helper\iohandler\iohandler_interface|null    $iohandler
211     */
212    public function handle_navigation($iohandler = null)
213    {
214        $nav_data = $this->installer_config->get_navigation_data();
215
216        // Set active navigation stage
217        if (isset($nav_data['active']) && is_array($nav_data['active']))
218        {
219            if ($iohandler !== null)
220            {
221                $iohandler->set_active_stage_menu($nav_data['active']);
222            }
223
224            $this->navigation_provider->set_nav_property($nav_data['active'], array(
225                'selected'    => true,
226                'completed'    => false,
227            ));
228        }
229
230        // Set finished navigation stages
231        if (isset($nav_data['finished']) && is_array($nav_data['finished']))
232        {
233            foreach ($nav_data['finished'] as $finished_stage)
234            {
235                if ($iohandler !== null)
236                {
237                    $iohandler->set_finished_stage_menu($finished_stage);
238                }
239
240                $this->navigation_provider->set_nav_property($finished_stage, array(
241                    'selected'    => false,
242                    'completed'    => true,
243                ));
244            }
245        }
246    }
247
248    /**
249     * Set default template variables
250     *
251     * @param string    $page_title            Title of the page
252     * @param bool        $selected_language    True to enable language selector it, false otherwise
253     */
254    protected function page_header($page_title, $selected_language = false)
255    {
256        // Path to templates
257        $paths = array($this->phpbb_root_path . 'install/update/new/adm/', $this->phpbb_admin_path);
258        $paths = array_filter($paths, 'is_dir');
259        $path = array_shift($paths);
260        $path = substr($path, strlen($this->phpbb_root_path));
261
262        $this->template->assign_vars(array(
263            'L_CHANGE'                => $this->language->lang('CHANGE'),
264            'L_COLON'                => $this->language->lang('COLON'),
265            'L_INSTALL_PANEL'        => $this->language->lang('INSTALL_PANEL'),
266            'L_SELECT_LANG'            => $this->language->lang('SELECT_LANG'),
267            'L_SKIP'                => $this->language->lang('SKIP'),
268            'PAGE_TITLE'            => $this->language->lang($page_title),
269            'T_JQUERY_LINK'            => $this->path_helper->get_web_root_path() . $path . '../assets/javascript/jquery-3.7.1.min.js',
270            'T_FONT_AWESOME_LINK'    => $this->path_helper->get_web_root_path() . $path . '../assets/css/font-awesome.min.css',
271            'T_TEMPLATE_PATH'        => $this->path_helper->get_web_root_path() . $path . 'style',
272            'T_ASSETS_PATH'            => $this->path_helper->get_web_root_path() . $path . '../assets',
273
274            'S_CONTENT_DIRECTION'     => $this->language->lang('DIRECTION'),
275            'S_CONTENT_FLOW_BEGIN'    => ($this->language->lang('DIRECTION') === 'ltr') ? 'left' : 'right',
276            'S_CONTENT_FLOW_END'    => ($this->language->lang('DIRECTION') === 'ltr') ? 'right' : 'left',
277            'S_CONTENT_ENCODING'     => 'UTF-8',
278            'S_LANG_SELECT'            => $selected_language,
279
280            'S_USER_LANG'            => $this->language->lang('USER_LANG'),
281        ));
282
283        $this->render_navigation();
284    }
285
286    /**
287     * Render navigation
288     */
289    protected function render_navigation()
290    {
291        // Get navigation items
292        $nav_array = $this->navigation_provider->get();
293        $nav_array = $this->sort_navigation_level($nav_array);
294
295        $active_main_menu = $this->get_active_main_menu($nav_array);
296
297        // Pass navigation to template
298        foreach ($nav_array as $key => $entry)
299        {
300            $this->template->assign_block_vars('t_block1', array(
301                'L_TITLE' => $this->language->lang($entry['label']),
302                'S_SELECTED' => ($active_main_menu === $key),
303                'U_TITLE' => $this->route($entry['route']),
304            ));
305
306            if (is_array($entry[0]) && $active_main_menu === $key)
307            {
308                $entry[0] = $this->sort_navigation_level($entry[0]);
309
310                foreach ($entry[0] as $name => $sub_entry)
311                {
312                    if (isset($sub_entry['stage']) && $sub_entry['stage'] === true)
313                    {
314                        $this->template->assign_block_vars('l_block2', array(
315                            'L_TITLE' => $this->language->lang($sub_entry['label']),
316                            'S_SELECTED' => (isset($sub_entry['selected']) && $sub_entry['selected'] === true),
317                            'S_COMPLETE' => (isset($sub_entry['completed']) && $sub_entry['completed'] === true),
318                            'STAGE_NAME' => $name,
319                        ));
320                    }
321                    else
322                    {
323                        $this->template->assign_block_vars('l_block1', array(
324                            'L_TITLE' => $this->language->lang($sub_entry['label']),
325                            'S_SELECTED' => (isset($sub_entry['route']) && $sub_entry['route'] === $this->request->get('_route')),
326                            'U_TITLE' => $this->route($sub_entry['route']),
327                        ));
328                    }
329                }
330            }
331        }
332    }
333
334    /**
335     * Render language select form
336     *
337     * @param string    $selected_language
338     */
339    protected function render_language_select($selected_language = null)
340    {
341        $langs = $this->lang_helper->get_available_languages();
342        foreach ($langs as $lang)
343        {
344            $this->template->assign_block_vars('language_select_item', array(
345                'VALUE' => $lang['iso'],
346                'NAME' => $lang['local_name'],
347                'SELECTED' => ($lang['iso'] === $selected_language),
348            ));
349        }
350    }
351
352    /**
353     * Returns the name of the active main menu item
354     *
355     * @param array    $nav_array
356     *
357     * @return string|bool    Returns the name of the active main menu element, if the element not found, returns false
358     */
359    protected function get_active_main_menu($nav_array)
360    {
361        $active_route = $this->request->get('_route');
362
363        foreach ($nav_array as $nav_name => $nav_options)
364        {
365            $current_menu = $nav_name;
366
367            if (isset($nav_options['route']) && $nav_options['route'] === $active_route)
368            {
369                return $nav_name;
370            }
371
372            if (is_array($nav_options[0]))
373            {
374                foreach ($nav_options[0] as $sub_menus)
375                {
376                    if (isset($sub_menus['route']) && $sub_menus['route'] === $active_route)
377                    {
378                        return $current_menu;
379                    }
380                }
381            }
382        }
383
384        return false;
385    }
386
387    /**
388     * Sorts the top level of navigation array
389     *
390     * @param array    $nav_array    Navigation array
391     *
392     * @return array
393     */
394    protected function sort_navigation_level($nav_array)
395    {
396        $sorted = array();
397        foreach ($nav_array as $key => $nav)
398        {
399            $order = (isset($nav['order'])) ? $nav['order'] : 0;
400            $sorted[$order][$key] = $nav;
401        }
402
403        // Linearization of navigation array
404        $nav_array = array();
405        ksort($sorted);
406        foreach ($sorted as $nav)
407        {
408            $nav_array = array_merge($nav_array, $nav);
409        }
410
411        return $nav_array;
412    }
413}