Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
report
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 4
1640
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 1
702
 assign_template_data
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
90
 check_captcha
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
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
14namespace phpbb\report\controller;
15
16use phpbb\exception\http_exception;
17use phpbb\report\report_handler_interface;
18use Symfony\Component\HttpFoundation\RedirectResponse;
19
20class report
21{
22    /**
23     * @var \phpbb\config\config
24     */
25    protected $config;
26
27    /**
28     * @var \phpbb\user
29     */
30    protected $user;
31
32    /**
33     * @var \phpbb\template\template
34     */
35    protected $template;
36
37    /**
38     * @var \phpbb\controller\helper
39     */
40    protected $helper;
41
42    /**
43     * @var \phpbb\request\request_interface
44     */
45    protected $request;
46
47    /**
48     * @var \phpbb\captcha\factory
49     */
50    protected $captcha_factory;
51
52    /**
53     * @var string
54     */
55    protected $phpbb_root_path;
56
57    /**
58     * @var string
59     */
60    protected $php_ext;
61
62    /**
63     * @var \phpbb\report\handler_factory
64     */
65    protected $report_factory;
66
67    /** @var report_handler_interface */
68    protected $report_handler;
69
70    /**
71     * @var \phpbb\report\report_reason_list_provider
72     */
73    protected $report_reason_provider;
74
75    public function __construct(\phpbb\config\config $config, \phpbb\user $user, \phpbb\template\template $template, \phpbb\controller\helper $helper, \phpbb\request\request_interface $request, \phpbb\captcha\factory $captcha_factory, \phpbb\report\handler_factory $report_factory, \phpbb\report\report_reason_list_provider $ui_provider, $phpbb_root_path, $php_ext)
76    {
77        $this->config            = $config;
78        $this->user                = $user;
79        $this->template            = $template;
80        $this->helper            = $helper;
81        $this->request            = $request;
82        $this->phpbb_root_path    = $phpbb_root_path;
83        $this->php_ext            = $php_ext;
84        $this->captcha_factory    = $captcha_factory;
85        $this->report_factory    = $report_factory;
86
87        // User interface factory
88        $this->report_reason_provider = $ui_provider;
89    }
90
91    /**
92     * Controller for /path_to_entities/{id}/report routes
93     *
94     * Because of how phpBB organizes routes $mode must be set in the route config.
95     *
96     * @param int        $id        ID of the entity to report
97     * @param string    $mode
98     * @return \Symfony\Component\HttpFoundation\Response a Symfony response object
99     * @throws http_exception when $mode or $id is invalid for some reason
100     */
101    public function handle($id, $mode)
102    {
103        // Get report handler
104        $this->report_handler = $this->report_factory->get_instance($mode);
105
106        $this->user->add_lang('mcp');
107
108        $user_notify    = ($this->user->data['is_registered']) ? $this->request->variable('notify', 0) : false;
109        $reason_id        = $this->request->variable('reason_id', 0);
110        $report_text    = $this->request->variable('report_text', '', true);
111
112        /**
113         * Replace Emojis and other 4bit UTF-8 chars not allowed by MySQL to UCR/NCR.
114         * Using their Numeric Character Reference's Hexadecimal notation.
115         */
116        $report_text    = utf8_encode_ucr($report_text);
117
118        $submit = $this->request->variable('submit', '');
119        $cancel = $this->request->variable('cancel', '');
120
121        $error = array();
122        $s_hidden_fields = '';
123
124        $redirect_url = append_sid(
125            $this->phpbb_root_path . ( ($mode === 'pm') ? 'ucp' : 'viewtopic' ) . ".{$this->php_ext}",
126            ($mode == 'pm') ? "i=pm&mode=view&p=$id" : "p=$id"
127        );
128        $redirect_url .= ($mode === 'post') ? "#p$id" : '';
129
130        // Set up CAPTCHA if necessary
131        if ($this->config['enable_post_confirm'] && !$this->user->data['is_registered'])
132        {
133            $captcha = $this->captcha_factory->get_instance($this->config['captcha_plugin']);
134            $captcha->init(CONFIRM_REPORT);
135        }
136
137        //Has the report been cancelled?
138        if (!empty($cancel))
139        {
140            return new RedirectResponse($redirect_url, 302);
141        }
142
143        // Check CAPTCHA, if the form was submited
144        if (!empty($submit) && isset($captcha))
145        {
146            $captcha_template_array = $this->check_captcha($captcha);
147            $error = $captcha_template_array['error'];
148            $s_hidden_fields = $captcha_template_array['hidden_fields'];
149        }
150
151        // Handle request
152        try
153        {
154            if (!empty($submit) && count($error) === 0)
155            {
156                $this->report_handler->add_report(
157                    (int) $id,
158                    (int) $reason_id,
159                    (string) $report_text,
160                    (int) $user_notify
161                );
162
163                // Send success message
164                switch ($mode)
165                {
166                    case 'pm':
167                        $lang_return = $this->user->lang['RETURN_PM'];
168                        $lang_success = $this->user->lang['PM_REPORTED_SUCCESS'];
169                    break;
170                    case 'post':
171                        $lang_return = $this->user->lang['RETURN_TOPIC'];
172                        $lang_success = $this->user->lang['POST_REPORTED_SUCCESS'];
173                    break;
174                }
175
176                $this->helper->assign_meta_refresh_var(3, $redirect_url);
177                $message = $lang_success . '<br /><br />' . sprintf($lang_return, '<a href="' . $redirect_url . '">', '</a>');
178                return $this->helper->message($message);
179            }
180            else
181            {
182                $this->report_handler->validate_report_request($id);
183            }
184        }
185        catch (\phpbb\report\exception\pm_reporting_disabled_exception $exception)
186        {
187            throw new http_exception(404, 'PAGE_NOT_FOUND');
188        }
189        catch (\phpbb\report\exception\already_reported_exception $exception)
190        {
191            switch ($mode)
192            {
193                case 'pm':
194                    $message = $this->user->lang['ALREADY_REPORTED_PM'];
195                    $message .= '<br /><br />' . sprintf($this->user->lang['RETURN_PM'], '<a href="' . $redirect_url . '">', '</a>');
196                break;
197                case 'post':
198                    $message = $this->user->lang['ALREADY_REPORTED'];
199                    $message .= '<br /><br />' . sprintf($this->user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
200                break;
201            }
202
203            return $this->helper->message($message);
204        }
205        catch (\phpbb\report\exception\report_permission_denied_exception $exception)
206        {
207            $message = $exception->getMessage();
208            if (isset($this->user->lang[$message]))
209            {
210                $message = $this->user->lang[$message];
211            }
212
213            throw new http_exception(403, $message);
214        }
215        catch (\phpbb\report\exception\entity_not_found_exception $exception)
216        {
217            $message = $exception->getMessage();
218            if (isset($this->user->lang[$message]))
219            {
220                $message = $this->user->lang[$message];
221            }
222
223            throw new http_exception(404, $message);
224        }
225        catch (\phpbb\report\exception\empty_report_exception $exception)
226        {
227            $error[] = $this->user->lang['EMPTY_REPORT'];
228        }
229        catch (\phpbb\report\exception\invalid_report_exception $exception)
230        {
231            return $this->helper->message($exception->getMessage());
232        }
233
234        // Setting up an rendering template
235        $page_title = ($mode === 'pm') ? $this->user->lang['REPORT_MESSAGE'] : $this->user->lang['REPORT_POST'];
236        $this->assign_template_data(
237            $mode,
238            $id,
239            $reason_id,
240            $report_text,
241            $user_notify,
242            $error,
243            $s_hidden_fields,
244            ( isset($captcha) ? $captcha : false )
245        );
246
247        return $this->helper->render('report_body.html', $page_title);
248    }
249
250    /**
251     * Assigns template variables
252     *
253     * @param    string    $mode
254     * @param    int        $id
255     * @param    int        $reason_id
256     * @param    string    $report_text
257     * @param    mixed    $user_notify
258     * @param     array    $error
259     * @param    string    $s_hidden_fields
260     * @param    mixed    $captcha
261     * @return    void
262     */
263    protected function assign_template_data($mode, $id, $reason_id, $report_text, $user_notify, $error = array(), $s_hidden_fields = '', $captcha = false)
264    {
265        if ($captcha !== false && $captcha->is_solved() === false)
266        {
267            $this->template->assign_vars(array(
268                'S_CONFIRM_CODE'    => true,
269                'CAPTCHA_TEMPLATE'    => $captcha->get_template(),
270            ));
271        }
272
273        $this->report_reason_provider->display_reasons($reason_id);
274
275        switch ($mode)
276        {
277            case 'pm':
278                $report_route = $this->helper->route('phpbb_report_pm_controller', array('id' => $id));
279            break;
280            case 'post':
281                $report_route = $this->helper->route('phpbb_report_post_controller', array('id' => $id));
282            break;
283        }
284
285        $this->template->assign_vars(array(
286            'ERROR'                => (count($error) > 0) ? implode('<br />', $error) : '',
287            'S_REPORT_POST'        => ($mode === 'pm') ? false : true,
288            'REPORT_TEXT'        => $report_text,
289            'S_HIDDEN_FIELDS'    => (!empty($s_hidden_fields)) ? $s_hidden_fields : null,
290            'S_REPORT_ACTION'    => $report_route,
291
292            'S_NOTIFY'            => $user_notify,
293            'S_CAN_NOTIFY'        => ($this->user->data['is_registered']) ? true : false,
294            'S_IN_REPORT'        => true,
295        ));
296    }
297
298    /**
299     * Check CAPTCHA
300     *
301     * @param    object    $captcha    A phpBB CAPTCHA object
302     * @return    array    template variables which ensures that CAPTCHA's work correctly
303     */
304    protected function check_captcha($captcha)
305    {
306        $error = array();
307        $captcha_hidden_fields = '';
308
309        $visual_confirmation_response = $captcha->validate();
310        if ($visual_confirmation_response)
311        {
312            $error[] = $visual_confirmation_response;
313        }
314
315        if (count($error) === 0)
316        {
317            $captcha->reset();
318        }
319        else if ($captcha->is_solved() !== false)
320        {
321            $captcha_hidden_fields = build_hidden_fields($captcha->get_hidden_fields());
322        }
323
324        return array(
325            'error' => $error,
326            'hidden_fields' => $captcha_hidden_fields,
327        );
328    }
329}