PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/install/controller/helper.php

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