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