PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/controller/helper.php

https://github.com/phpbb/area51-phpbb3
PHP | 375 lines | 175 code | 48 blank | 152 comment | 17 complexity | 718d928bee7d2fc307b2580f8aacfefe 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 phpbb\auth\auth;
  15. use phpbb\cache\driver\driver_interface as cache_interface;
  16. use phpbb\config\config;
  17. use phpbb\cron\manager;
  18. use phpbb\db\driver\driver_interface;
  19. use phpbb\event\dispatcher;
  20. use phpbb\language\language;
  21. use phpbb\request\request_interface;
  22. use phpbb\routing\helper as routing_helper;
  23. use phpbb\symfony_request;
  24. use phpbb\template\template;
  25. use phpbb\user;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. /**
  30. * Controller helper class, contains methods that do things for controllers
  31. */
  32. class helper
  33. {
  34. /** @var auth */
  35. protected $auth;
  36. /** @var cache_interface */
  37. protected $cache;
  38. /** @var config */
  39. protected $config;
  40. /** @var manager */
  41. protected $cron_manager;
  42. /** @var driver_interface */
  43. protected $db;
  44. /** @var dispatcher */
  45. protected $dispatcher;
  46. /** @var language */
  47. protected $language;
  48. /* @var request_interface */
  49. protected $request;
  50. /** @var routing_helper */
  51. protected $routing_helper;
  52. /* @var symfony_request */
  53. protected $symfony_request;
  54. /** @var template */
  55. protected $template;
  56. /** @var user */
  57. protected $user;
  58. /** @var string */
  59. protected $admin_path;
  60. /** @var string */
  61. protected $php_ext;
  62. /** @var bool $sql_explain */
  63. protected $sql_explain;
  64. /**
  65. * Constructor
  66. *
  67. * @param auth $auth Auth object
  68. * @param cache_interface $cache
  69. * @param config $config Config object
  70. * @param manager $cron_manager
  71. * @param driver_interface $db DBAL object
  72. * @param dispatcher $dispatcher
  73. * @param language $language
  74. * @param request_interface $request phpBB request object
  75. * @param routing_helper $routing_helper Helper to generate the routes
  76. * @param symfony_request $symfony_request Symfony Request object
  77. * @param template $template Template object
  78. * @param user $user User object
  79. * @param string $root_path phpBB root path
  80. * @param string $admin_path Admin path
  81. * @param string $php_ext PHP extension
  82. * @param bool $sql_explain Flag whether to display sql explain
  83. */
  84. public function __construct(auth $auth, cache_interface $cache, config $config, manager $cron_manager,
  85. driver_interface $db, dispatcher $dispatcher, language $language,
  86. request_interface $request, routing_helper $routing_helper,
  87. symfony_request $symfony_request, template $template, user $user, $root_path,
  88. $admin_path, $php_ext, $sql_explain = false)
  89. {
  90. $this->auth = $auth;
  91. $this->cache = $cache;
  92. $this->cron_manager = $cron_manager;
  93. $this->db = $db;
  94. $this->dispatcher = $dispatcher;
  95. $this->language = $language;
  96. $this->template = $template;
  97. $this->user = $user;
  98. $this->config = $config;
  99. $this->symfony_request = $symfony_request;
  100. $this->request = $request;
  101. $this->routing_helper = $routing_helper;
  102. $this->admin_path = $root_path . $admin_path;
  103. $this->php_ext = $php_ext;
  104. $this->sql_explain = $sql_explain;
  105. }
  106. /**
  107. * Automate setting up the page and creating the response object.
  108. *
  109. * @param string $template_file The template handle to render
  110. * @param string $page_title The title of the page to output
  111. * @param int $status_code The status code to be sent to the page header
  112. * @param bool $display_online_list Do we display online users list
  113. * @param int $item_id Restrict online users to item id
  114. * @param string $item Restrict online users to a certain session item, e.g. forum for session_forum_id
  115. * @param bool $send_headers Whether headers should be sent by page_header(). Defaults to false for controllers.
  116. *
  117. * @return Response object containing rendered page
  118. */
  119. public function render($template_file, $page_title = '', $status_code = 200, $display_online_list = false, $item_id = 0, $item = 'forum', $send_headers = false)
  120. {
  121. page_header($page_title, $display_online_list, $item_id, $item, $send_headers);
  122. $this->template->set_filenames(array(
  123. 'body' => $template_file,
  124. ));
  125. $run_cron = true;
  126. $page_footer_override = false;
  127. /**
  128. * Execute code and/or overwrite page_footer()
  129. *
  130. * @event core.page_footer
  131. * @var bool run_cron Shall we run cron tasks
  132. * @var bool page_footer_override Shall we skip displaying the page footer
  133. * @since 3.1.0-a1
  134. * @changed 3.3.1-RC1 Added to controller helper render() method for backwards compatibility
  135. */
  136. $vars = ['run_cron', 'page_footer_override'];
  137. extract($this->dispatcher->trigger_event('core.page_footer', compact($vars)));
  138. if (!$page_footer_override)
  139. {
  140. $this->display_footer($run_cron);
  141. }
  142. $headers = !empty($this->user->data['is_bot']) ? ['X-PHPBB-IS-BOT' => 'yes'] : [];
  143. $display_template = true;
  144. $exit_handler = true; // not used
  145. /**
  146. * Execute code and/or modify output before displaying the template.
  147. *
  148. * @event core.page_footer_after
  149. * @var bool display_template Whether or not to display the template
  150. * @var bool exit_handler Whether or not to run the exit_handler() (no effect on controller pages)
  151. *
  152. * @since 3.1.0-RC5
  153. * @changed 3.3.1-RC1 Added to controller helper render() method for backwards compatibility
  154. */
  155. $vars = ['display_template', 'exit_handler'];
  156. extract($this->dispatcher->trigger_event('core.page_footer_after', compact($vars)));
  157. $response = new Response($display_template ? $this->template->assign_display('body') : '', $status_code, $headers);
  158. /**
  159. * Modify response before output
  160. *
  161. * @event core.controller_helper_render_response
  162. * @var Response response Symfony response object
  163. *
  164. * @since 3.3.1-RC1
  165. */
  166. $vars = ['response'];
  167. extract($this->dispatcher->trigger_event('core.controller_helper_render_response', compact($vars)));
  168. return $response;
  169. }
  170. /**
  171. * Generate a URL to a route
  172. *
  173. * @param string $route Name of the route to travel
  174. * @param array $params String or array of additional url parameters
  175. * @param bool $is_amp Is url using &amp; (true) or & (false)
  176. * @param string|bool $session_id Possibility to use a custom session id instead of the global one
  177. * @param int $reference_type The type of reference to be generated (one of the constants)
  178. * @return string The URL already passed through append_sid()
  179. */
  180. public function route($route, array $params = array(), $is_amp = true, $session_id = false, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
  181. {
  182. return $this->routing_helper->route($route, $params, $is_amp, $session_id, $reference_type);
  183. }
  184. /**
  185. * Output an error, effectively the same thing as trigger_error
  186. *
  187. * @param string $message The error message
  188. * @param int $code The error code (e.g. 404, 500, 503, etc.)
  189. * @return Response A Response instance
  190. *
  191. * @deprecated 3.1.3 (To be removed: 4.0.0) Use exceptions instead.
  192. */
  193. public function error($message, $code = 500)
  194. {
  195. return $this->message($message, array(), 'INFORMATION', $code);
  196. }
  197. /**
  198. * Output a message
  199. *
  200. * In case of an error, please throw an exception instead
  201. *
  202. * @param string $message The message to display (must be a language variable)
  203. * @param array $parameters The parameters to use with the language var
  204. * @param string $title Title for the message (must be a language variable)
  205. * @param int $code The HTTP status code (e.g. 404, 500, 503, etc.)
  206. * @return Response A Response instance
  207. */
  208. public function message($message, array $parameters = array(), $title = 'INFORMATION', $code = 200)
  209. {
  210. array_unshift($parameters, $message);
  211. $message_text = call_user_func_array(array($this->language, 'lang'), $parameters);
  212. $message_title = $this->language->lang($title);
  213. if ($this->request->is_ajax())
  214. {
  215. global $refresh_data;
  216. return new JsonResponse(
  217. array(
  218. 'MESSAGE_TITLE' => $message_title,
  219. 'MESSAGE_TEXT' => $message_text,
  220. 'S_USER_WARNING' => false,
  221. 'S_USER_NOTICE' => false,
  222. 'REFRESH_DATA' => (!empty($refresh_data)) ? $refresh_data : null
  223. ),
  224. $code
  225. );
  226. }
  227. $this->template->assign_vars(array(
  228. 'MESSAGE_TEXT' => $message_text,
  229. 'MESSAGE_TITLE' => $message_title,
  230. ));
  231. return $this->render('message_body.html', $message_title, $code);
  232. }
  233. /**
  234. * Assigns automatic refresh time meta tag in template
  235. *
  236. * @param int $time time in seconds, when redirection should occur
  237. * @param string $url the URL where the user should be redirected
  238. * @return void
  239. */
  240. public function assign_meta_refresh_var($time, $url)
  241. {
  242. $this->template->assign_vars(array(
  243. 'META' => '<meta http-equiv="refresh" content="' . $time . '; url=' . $url . '" />',
  244. ));
  245. }
  246. /**
  247. * Return the current url
  248. *
  249. * @return string
  250. */
  251. public function get_current_url()
  252. {
  253. return generate_board_url(true) . $this->request->escape($this->symfony_request->getRequestUri(), true);
  254. }
  255. /**
  256. * Handle display actions for footer, e.g. SQL report and credit line
  257. *
  258. * @param bool $run_cron Flag whether cron should be run
  259. *
  260. * @return void
  261. */
  262. public function display_footer($run_cron = true)
  263. {
  264. $this->display_sql_report();
  265. $this->template->assign_vars([
  266. 'DEBUG_OUTPUT' => phpbb_generate_debug_output($this->db, $this->config, $this->auth, $this->user, $this->dispatcher),
  267. 'TRANSLATION_INFO' => $this->language->is_set('TRANSLATION_INFO') ? $this->language->lang('TRANSLATION_INFO') : '',
  268. 'CREDIT_LINE' => $this->language->lang('POWERED_BY', '<a href="https://www.phpbb.com/">phpBB</a>&reg; Forum Software &copy; phpBB Limited'),
  269. 'U_ACP' => ($this->auth->acl_get('a_') && !empty($this->user->data['is_registered'])) ? append_sid("{$this->admin_path}index.{$this->php_ext}", false, true, $this->user->session_id) : '',
  270. ]);
  271. if ($run_cron)
  272. {
  273. $this->set_cron_task();
  274. }
  275. }
  276. /**
  277. * Display SQL report
  278. *
  279. * @return void
  280. */
  281. public function display_sql_report()
  282. {
  283. if ($this->sql_explain && $this->request->variable('explain', false) && $this->auth->acl_get('a_'))
  284. {
  285. $this->db->sql_report('display');
  286. }
  287. }
  288. /**
  289. * Set cron task for footer
  290. *
  291. * @return void
  292. */
  293. protected function set_cron_task()
  294. {
  295. // Call cron-type script
  296. $call_cron = false;
  297. if (!defined('IN_CRON') && !$this->config['use_system_cron'] && !$this->config['board_disable'] && !$this->user->data['is_bot'] && !$this->cache->get('_cron.lock_check'))
  298. {
  299. $call_cron = true;
  300. $time_now = (!empty($this->user->time_now) && is_int($this->user->time_now)) ? $this->user->time_now : time();
  301. // Any old lock present?
  302. if (!empty($this->config['cron_lock']))
  303. {
  304. $cron_time = explode(' ', $this->config['cron_lock']);
  305. // If 1 hour lock is present we do not set a cron task
  306. if ($cron_time[0] + 3600 >= $time_now)
  307. {
  308. $call_cron = false;
  309. }
  310. }
  311. }
  312. // Call cron job?
  313. if ($call_cron)
  314. {
  315. $task = $this->cron_manager->find_one_ready_task();
  316. if ($task)
  317. {
  318. $url = $task->get_url();
  319. $this->template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');
  320. }
  321. else
  322. {
  323. $this->cache->put('_cron.lock_check', true, 60);
  324. }
  325. }
  326. }
  327. }