PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/phpbb/controller/helper.php

https://github.com/AJenbo/ubuntudanmark.dk
PHP | 267 lines | 125 code | 37 blank | 105 comment | 12 complexity | 1e013d503ef4771680ae5e9550369e1b 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\controller;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Generator\UrlGenerator;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Routing\RequestContext;
  19. /**
  20. * Controller helper class, contains methods that do things for controllers
  21. */
  22. class helper
  23. {
  24. /**
  25. * Template object
  26. * @var \phpbb\template\template
  27. */
  28. protected $template;
  29. /**
  30. * User object
  31. * @var \phpbb\user
  32. */
  33. protected $user;
  34. /**
  35. * config object
  36. * @var \phpbb\config\config
  37. */
  38. protected $config;
  39. /* @var \phpbb\symfony_request */
  40. protected $symfony_request;
  41. /* @var \phpbb\request\request_interface */
  42. protected $request;
  43. /**
  44. * @var \phpbb\filesystem The filesystem object
  45. */
  46. protected $filesystem;
  47. /**
  48. * phpBB root path
  49. * @var string
  50. */
  51. protected $phpbb_root_path;
  52. /**
  53. * PHP file extension
  54. * @var string
  55. */
  56. protected $php_ext;
  57. /**
  58. * Constructor
  59. *
  60. * @param \phpbb\template\template $template Template object
  61. * @param \phpbb\user $user User object
  62. * @param \phpbb\config\config $config Config object
  63. *
  64. * @param \phpbb\controller\provider $provider Path provider
  65. * @param \phpbb\extension\manager $manager Extension manager object
  66. * @param \phpbb\symfony_request $symfony_request Symfony Request object
  67. * @param \phpbb\request\request_interface $request phpBB request object
  68. * @param \phpbb\filesystem $filesystem The filesystem object
  69. * @param string $phpbb_root_path phpBB root path
  70. * @param string $php_ext PHP file extension
  71. */
  72. public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, \phpbb\controller\provider $provider, \phpbb\extension\manager $manager, \phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, \phpbb\filesystem $filesystem, $phpbb_root_path, $php_ext)
  73. {
  74. $this->template = $template;
  75. $this->user = $user;
  76. $this->config = $config;
  77. $this->symfony_request = $symfony_request;
  78. $this->request = $request;
  79. $this->filesystem = $filesystem;
  80. $this->phpbb_root_path = $phpbb_root_path;
  81. $this->php_ext = $php_ext;
  82. $provider->find_routing_files($manager->get_finder());
  83. $this->route_collection = $provider->find($phpbb_root_path)->get_routes();
  84. }
  85. /**
  86. * Automate setting up the page and creating the response object.
  87. *
  88. * @param string $template_file The template handle to render
  89. * @param string $page_title The title of the page to output
  90. * @param int $status_code The status code to be sent to the page header
  91. * @param bool $display_online_list Do we display online users list
  92. * @param int $item_id Restrict online users to item id
  93. * @param string $item Restrict online users to a certain session item, e.g. forum for session_forum_id
  94. * @param bool $send_headers Whether headers should be sent by page_header(). Defaults to false for controllers.
  95. *
  96. * @return Response object containing rendered page
  97. */
  98. public function render($template_file, $page_title = '', $status_code = 200, $display_online_list = false, $item_id = 0, $item = 'forum', $send_headers = false)
  99. {
  100. page_header($page_title, $display_online_list, $item_id, $item, $send_headers);
  101. $this->template->set_filenames(array(
  102. 'body' => $template_file,
  103. ));
  104. page_footer(true, false, false);
  105. $headers = !empty($this->user->data['is_bot']) ? array('X-PHPBB-IS-BOT' => 'yes') : array();
  106. return new Response($this->template->assign_display('body'), $status_code, $headers);
  107. }
  108. /**
  109. * Generate a URL to a route
  110. *
  111. * @param string $route Name of the route to travel
  112. * @param array $params String or array of additional url parameters
  113. * @param bool $is_amp Is url using &amp; (true) or & (false)
  114. * @param string|bool $session_id Possibility to use a custom session id instead of the global one
  115. * @param bool|string $reference_type The type of reference to be generated (one of the constants)
  116. * @return string The URL already passed through append_sid()
  117. */
  118. public function route($route, array $params = array(), $is_amp = true, $session_id = false, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
  119. {
  120. $anchor = '';
  121. if (isset($params['#']))
  122. {
  123. $anchor = '#' . $params['#'];
  124. unset($params['#']);
  125. }
  126. $context = new RequestContext();
  127. $context->fromRequest($this->symfony_request);
  128. if ($this->config['force_server_vars'])
  129. {
  130. $context->setHost($this->config['server_name']);
  131. $context->setScheme(substr($this->config['server_protocol'], 0, -3));
  132. $context->setHttpPort($this->config['server_port']);
  133. $context->setHttpsPort($this->config['server_port']);
  134. $context->setBaseUrl(rtrim($this->config['script_path'], '/'));
  135. }
  136. $script_name = $this->symfony_request->getScriptName();
  137. $page_name = substr($script_name, -1, 1) == '/' ? '' : utf8_basename($script_name);
  138. $base_url = $context->getBaseUrl();
  139. // Append page name if base URL does not contain it
  140. if (!empty($page_name) && strpos($base_url, '/' . $page_name) === false)
  141. {
  142. $base_url .= '/' . $page_name;
  143. }
  144. // If enable_mod_rewrite is false we need to replace the current front-end by app.php, otherwise we need to remove it.
  145. $base_url = str_replace('/' . $page_name, empty($this->config['enable_mod_rewrite']) ? '/app.' . $this->php_ext : '', $base_url);
  146. // We need to update the base url to move to the directory of the app.php file if the current script is not app.php
  147. if ($page_name !== 'app.php' && !$this->config['force_server_vars'])
  148. {
  149. if (empty($this->config['enable_mod_rewrite']))
  150. {
  151. $base_url = str_replace('/app.' . $this->php_ext, '/' . $this->phpbb_root_path . 'app.' . $this->php_ext, $base_url);
  152. }
  153. else
  154. {
  155. $base_url .= preg_replace(get_preg_expression('path_remove_dot_trailing_slash'), '$2', $this->phpbb_root_path);
  156. }
  157. }
  158. $base_url = $this->request->escape($this->filesystem->clean_path($base_url), true);
  159. $context->setBaseUrl($base_url);
  160. $url_generator = new UrlGenerator($this->route_collection, $context);
  161. $route_url = $url_generator->generate($route, $params, $reference_type);
  162. if ($is_amp)
  163. {
  164. $route_url = str_replace(array('&amp;', '&'), array('&', '&amp;'), $route_url);
  165. }
  166. if ($reference_type === UrlGeneratorInterface::RELATIVE_PATH && empty($this->config['enable_mod_rewrite']))
  167. {
  168. $route_url = 'app.' . $this->php_ext . '/' . $route_url;
  169. }
  170. return append_sid($route_url . $anchor, false, $is_amp, $session_id, true);
  171. }
  172. /**
  173. * Output an error, effectively the same thing as trigger_error
  174. *
  175. * @param string $message The error message
  176. * @param int $code The error code (e.g. 404, 500, 503, etc.)
  177. * @return Response A Response instance
  178. *
  179. * @deprecated 3.1.3 (To be removed: 3.3.0) Use exceptions instead.
  180. */
  181. public function error($message, $code = 500)
  182. {
  183. return $this->message($message, array(), 'INFORMATION', $code);
  184. }
  185. /**
  186. * Output a message
  187. *
  188. * In case of an error, please throw an exception instead
  189. *
  190. * @param string $message The message to display (must be a language variable)
  191. * @param array $parameters The parameters to use with the language var
  192. * @param string $title Title for the message (must be a language variable)
  193. * @param int $code The HTTP status code (e.g. 404, 500, 503, etc.)
  194. * @return Response A Response instance
  195. */
  196. public function message($message, array $parameters = array(), $title = 'INFORMATION', $code = 200)
  197. {
  198. array_unshift($parameters, $message);
  199. $message_text = call_user_func_array(array($this->user, 'lang'), $parameters);
  200. $message_title = $this->user->lang($title);
  201. if ($this->request->is_ajax())
  202. {
  203. global $refresh_data;
  204. return new JsonResponse(
  205. array(
  206. 'MESSAGE_TITLE' => $message_title,
  207. 'MESSAGE_TEXT' => $message_text,
  208. 'S_USER_WARNING' => false,
  209. 'S_USER_NOTICE' => false,
  210. 'REFRESH_DATA' => (!empty($refresh_data)) ? $refresh_data : null
  211. ),
  212. $code
  213. );
  214. }
  215. $this->template->assign_vars(array(
  216. 'MESSAGE_TEXT' => $message_text,
  217. 'MESSAGE_TITLE' => $message_title,
  218. ));
  219. return $this->render('message_body.html', $message_title, $code);
  220. }
  221. /**
  222. * Return the current url
  223. *
  224. * @return string
  225. */
  226. public function get_current_url()
  227. {
  228. return generate_board_url(true) . $this->request->escape($this->symfony_request->getRequestUri(), true);
  229. }
  230. }