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

/phpBB/phpbb/controller/helper.php

http://github.com/phpbb/phpbb
PHP | 328 lines | 161 code | 42 blank | 125 comment | 16 complexity | 493f40343a0fdae60debaf8739ee73d9 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  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. $this->display_footer();
  126. $headers = !empty($this->user->data['is_bot']) ? array('X-PHPBB-IS-BOT' => 'yes') : array();
  127. return new Response($this->template->assign_display('body'), $status_code, $headers);
  128. }
  129. /**
  130. * Generate a URL to a route
  131. *
  132. * @param string $route Name of the route to travel
  133. * @param array $params String or array of additional url parameters
  134. * @param bool $is_amp Is url using &amp; (true) or & (false)
  135. * @param string|bool $session_id Possibility to use a custom session id instead of the global one
  136. * @param int $reference_type The type of reference to be generated (one of the constants)
  137. * @return string The URL already passed through append_sid()
  138. */
  139. public function route($route, array $params = array(), $is_amp = true, $session_id = false, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)
  140. {
  141. return $this->routing_helper->route($route, $params, $is_amp, $session_id, $reference_type);
  142. }
  143. /**
  144. * Output an error, effectively the same thing as trigger_error
  145. *
  146. * @param string $message The error message
  147. * @param int $code The error code (e.g. 404, 500, 503, etc.)
  148. * @return Response A Response instance
  149. *
  150. * @deprecated 3.1.3 (To be removed: 4.0.0) Use exceptions instead.
  151. */
  152. public function error($message, $code = 500)
  153. {
  154. return $this->message($message, array(), 'INFORMATION', $code);
  155. }
  156. /**
  157. * Output a message
  158. *
  159. * In case of an error, please throw an exception instead
  160. *
  161. * @param string $message The message to display (must be a language variable)
  162. * @param array $parameters The parameters to use with the language var
  163. * @param string $title Title for the message (must be a language variable)
  164. * @param int $code The HTTP status code (e.g. 404, 500, 503, etc.)
  165. * @return Response A Response instance
  166. */
  167. public function message($message, array $parameters = array(), $title = 'INFORMATION', $code = 200)
  168. {
  169. array_unshift($parameters, $message);
  170. $message_text = call_user_func_array(array($this->language, 'lang'), $parameters);
  171. $message_title = $this->language->lang($title);
  172. if ($this->request->is_ajax())
  173. {
  174. global $refresh_data;
  175. return new JsonResponse(
  176. array(
  177. 'MESSAGE_TITLE' => $message_title,
  178. 'MESSAGE_TEXT' => $message_text,
  179. 'S_USER_WARNING' => false,
  180. 'S_USER_NOTICE' => false,
  181. 'REFRESH_DATA' => (!empty($refresh_data)) ? $refresh_data : null
  182. ),
  183. $code
  184. );
  185. }
  186. $this->template->assign_vars(array(
  187. 'MESSAGE_TEXT' => $message_text,
  188. 'MESSAGE_TITLE' => $message_title,
  189. ));
  190. return $this->render('message_body.html', $message_title, $code);
  191. }
  192. /**
  193. * Assigns automatic refresh time meta tag in template
  194. *
  195. * @param int $time time in seconds, when redirection should occur
  196. * @param string $url the URL where the user should be redirected
  197. * @return void
  198. */
  199. public function assign_meta_refresh_var($time, $url)
  200. {
  201. $this->template->assign_vars(array(
  202. 'META' => '<meta http-equiv="refresh" content="' . $time . '; url=' . $url . '" />',
  203. ));
  204. }
  205. /**
  206. * Return the current url
  207. *
  208. * @return string
  209. */
  210. public function get_current_url()
  211. {
  212. return generate_board_url(true) . $this->request->escape($this->symfony_request->getRequestUri(), true);
  213. }
  214. /**
  215. * Handle display actions for footer, e.g. SQL report and credit line
  216. *
  217. * @param bool $run_cron Flag whether cron should be run
  218. *
  219. * @return void
  220. */
  221. public function display_footer($run_cron = true)
  222. {
  223. $this->display_sql_report();
  224. $this->template->assign_vars([
  225. 'DEBUG_OUTPUT' => phpbb_generate_debug_output($this->db, $this->config, $this->auth, $this->user, $this->dispatcher),
  226. 'TRANSLATION_INFO' => $this->language->is_set('TRANSLATION_INFO') ? $this->language->lang('TRANSLATION_INFO') : '',
  227. 'CREDIT_LINE' => $this->language->lang('POWERED_BY', '<a href="https://www.phpbb.com/">phpBB</a>&reg; Forum Software &copy; phpBB Limited'),
  228. '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) : '',
  229. ]);
  230. if ($run_cron)
  231. {
  232. $this->set_cron_task();
  233. }
  234. }
  235. /**
  236. * Display SQL report
  237. *
  238. * @return void
  239. */
  240. public function display_sql_report()
  241. {
  242. if ($this->sql_explain && $this->request->variable('explain', false) && $this->auth->acl_get('a_'))
  243. {
  244. $this->db->sql_report('display');
  245. }
  246. }
  247. /**
  248. * Set cron task for footer
  249. *
  250. * @return void
  251. */
  252. protected function set_cron_task()
  253. {
  254. // Call cron-type script
  255. $call_cron = false;
  256. if (!defined('IN_CRON') && !$this->config['use_system_cron'] && !$this->config['board_disable'] && !$this->user->data['is_bot'] && !$this->cache->get('_cron.lock_check'))
  257. {
  258. $call_cron = true;
  259. $time_now = (!empty($this->user->time_now) && is_int($this->user->time_now)) ? $this->user->time_now : time();
  260. // Any old lock present?
  261. if (!empty($this->config['cron_lock']))
  262. {
  263. $cron_time = explode(' ', $this->config['cron_lock']);
  264. // If 1 hour lock is present we do not set a cron task
  265. if ($cron_time[0] + 3600 >= $time_now)
  266. {
  267. $call_cron = false;
  268. }
  269. }
  270. }
  271. // Call cron job?
  272. if ($call_cron)
  273. {
  274. $task = $this->cron_manager->find_one_ready_task();
  275. if ($task)
  276. {
  277. $url = $task->get_url();
  278. $this->template->assign_var('RUN_CRON_TASK', '<img src="' . $url . '" width="1" height="1" alt="cron" />');
  279. }
  280. else
  281. {
  282. $this->cache->put('_cron.lock_check', true, 60);
  283. }
  284. }
  285. }
  286. }