PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/controller/helper.php

https://github.com/bantu/phpbb
PHP | 195 lines | 76 code | 23 blank | 96 comment | 1 complexity | 0dffd33a2b52e88f90e4634c15b540ea 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\UrlGeneratorInterface;
  17. /**
  18. * Controller helper class, contains methods that do things for controllers
  19. */
  20. class helper
  21. {
  22. /**
  23. * Template object
  24. * @var \phpbb\template\template
  25. */
  26. protected $template;
  27. /**
  28. * User object
  29. * @var \phpbb\user
  30. */
  31. protected $user;
  32. /**
  33. * config object
  34. * @var \phpbb\config\config
  35. */
  36. protected $config;
  37. /* @var \phpbb\symfony_request */
  38. protected $symfony_request;
  39. /* @var \phpbb\request\request_interface */
  40. protected $request;
  41. /**
  42. * @var \phpbb\routing\helper
  43. */
  44. protected $routing_helper;
  45. /**
  46. * Constructor
  47. *
  48. * @param \phpbb\template\template $template Template object
  49. * @param \phpbb\user $user User object
  50. * @param \phpbb\config\config $config Config object
  51. * @param \phpbb\symfony_request $symfony_request Symfony Request object
  52. * @param \phpbb\request\request_interface $request phpBB request object
  53. * @param \phpbb\routing\helper $routing_helper Helper to generate the routes
  54. */
  55. public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\config\config $config, \phpbb\symfony_request $symfony_request, \phpbb\request\request_interface $request, \phpbb\routing\helper $routing_helper)
  56. {
  57. $this->template = $template;
  58. $this->user = $user;
  59. $this->config = $config;
  60. $this->symfony_request = $symfony_request;
  61. $this->request = $request;
  62. $this->routing_helper = $routing_helper;
  63. }
  64. /**
  65. * Automate setting up the page and creating the response object.
  66. *
  67. * @param string $template_file The template handle to render
  68. * @param string $page_title The title of the page to output
  69. * @param int $status_code The status code to be sent to the page header
  70. * @param bool $display_online_list Do we display online users list
  71. * @param int $item_id Restrict online users to item id
  72. * @param string $item Restrict online users to a certain session item, e.g. forum for session_forum_id
  73. * @param bool $send_headers Whether headers should be sent by page_header(). Defaults to false for controllers.
  74. *
  75. * @return Response object containing rendered page
  76. */
  77. public function render($template_file, $page_title = '', $status_code = 200, $display_online_list = false, $item_id = 0, $item = 'forum', $send_headers = false)
  78. {
  79. page_header($page_title, $display_online_list, $item_id, $item, $send_headers);
  80. $this->template->set_filenames(array(
  81. 'body' => $template_file,
  82. ));
  83. page_footer(true, false, false);
  84. $headers = !empty($this->user->data['is_bot']) ? array('X-PHPBB-IS-BOT' => 'yes') : array();
  85. return new Response($this->template->assign_display('body'), $status_code, $headers);
  86. }
  87. /**
  88. * Generate a URL to a route
  89. *
  90. * @param string $route Name of the route to travel
  91. * @param array $params String or array of additional url parameters
  92. * @param bool $is_amp Is url using &amp; (true) or & (false)
  93. * @param string|bool $session_id Possibility to use a custom session id instead of the global one
  94. * @param bool|string $reference_type The type of reference to be generated (one of the constants)
  95. * @return string The URL already passed through append_sid()
  96. */
  97. public function route($route, array $params = array(), $is_amp = true, $session_id = false, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
  98. {
  99. return $this->routing_helper->route($route, $params, $is_amp, $session_id, $reference_type);
  100. }
  101. /**
  102. * Output an error, effectively the same thing as trigger_error
  103. *
  104. * @param string $message The error message
  105. * @param int $code The error code (e.g. 404, 500, 503, etc.)
  106. * @return Response A Response instance
  107. *
  108. * @deprecated 3.1.3 (To be removed: 4.0.0) Use exceptions instead.
  109. */
  110. public function error($message, $code = 500)
  111. {
  112. return $this->message($message, array(), 'INFORMATION', $code);
  113. }
  114. /**
  115. * Output a message
  116. *
  117. * In case of an error, please throw an exception instead
  118. *
  119. * @param string $message The message to display (must be a language variable)
  120. * @param array $parameters The parameters to use with the language var
  121. * @param string $title Title for the message (must be a language variable)
  122. * @param int $code The HTTP status code (e.g. 404, 500, 503, etc.)
  123. * @return Response A Response instance
  124. */
  125. public function message($message, array $parameters = array(), $title = 'INFORMATION', $code = 200)
  126. {
  127. array_unshift($parameters, $message);
  128. $message_text = call_user_func_array(array($this->user, 'lang'), $parameters);
  129. $message_title = $this->user->lang($title);
  130. if ($this->request->is_ajax())
  131. {
  132. global $refresh_data;
  133. return new JsonResponse(
  134. array(
  135. 'MESSAGE_TITLE' => $message_title,
  136. 'MESSAGE_TEXT' => $message_text,
  137. 'S_USER_WARNING' => false,
  138. 'S_USER_NOTICE' => false,
  139. 'REFRESH_DATA' => (!empty($refresh_data)) ? $refresh_data : null
  140. ),
  141. $code
  142. );
  143. }
  144. $this->template->assign_vars(array(
  145. 'MESSAGE_TEXT' => $message_text,
  146. 'MESSAGE_TITLE' => $message_title,
  147. ));
  148. return $this->render('message_body.html', $message_title, $code);
  149. }
  150. /**
  151. * Assigns automatic refresh time meta tag in template
  152. *
  153. * @param int $time time in seconds, when redirection should occur
  154. * @param string $url the URL where the user should be redirected
  155. * @return null
  156. */
  157. public function assign_meta_refresh_var($time, $url)
  158. {
  159. $this->template->assign_vars(array(
  160. 'META' => '<meta http-equiv="refresh" content="' . $time . '; url=' . $url . '" />',
  161. ));
  162. }
  163. /**
  164. * Return the current url
  165. *
  166. * @return string
  167. */
  168. public function get_current_url()
  169. {
  170. return generate_board_url(true) . $this->request->escape($this->symfony_request->getRequestUri(), true);
  171. }
  172. }