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