Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.97% covered (warning)
58.97%
23 / 39
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
kernel_exception_subscriber
58.97% covered (warning)
58.97%
23 / 39
33.33% covered (danger)
33.33%
1 / 3
21.94
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 on_kernel_exception
58.06% covered (warning)
58.06%
18 / 31
0.00% covered (danger)
0.00%
0 / 1
14.97
 getSubscribedEvents
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
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\event;
15
16use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17use Symfony\Component\HttpFoundation\JsonResponse;
18use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
19use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20use Symfony\Component\HttpKernel\KernelEvents;
21use Symfony\Component\HttpKernel\Event\ExceptionEvent;
22use Symfony\Component\HttpFoundation\Response;
23
24class kernel_exception_subscriber implements EventSubscriberInterface
25{
26    /**
27     * Set to true to show full exception messages
28     *
29     * @var bool
30     */
31    protected $debug;
32
33    /**
34    * Template object
35    *
36    * @var \phpbb\template\template
37    */
38    protected $template;
39
40    /**
41    * Language object
42    *
43    * @var \phpbb\language\language
44    */
45    protected $language;
46
47    /**
48    * User object
49    *
50    * @var \phpbb\user
51    */
52    protected $user;
53
54    /** @var \phpbb\request\type_cast_helper */
55    protected $type_caster;
56
57    /**
58    * Construct method
59    *
60    * @param \phpbb\template\template    $template    Template object
61    * @param \phpbb\language\language    $language    Language object
62    * @param \phpbb\user                $user        User object
63    * @param bool                        $debug        Set to true to show full exception messages
64    */
65    public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\user $user, $debug = false)
66    {
67        $this->debug = $debug || defined('DEBUG');
68        $this->template = $template;
69        $this->language = $language;
70        $this->user = $user;
71        $this->type_caster = new \phpbb\request\type_cast_helper();
72    }
73
74    /**
75    * This listener is run when the KernelEvents::EXCEPTION event is triggered
76    *
77    * @param ExceptionEvent $event
78    * @return void
79    */
80    public function on_kernel_exception(ExceptionEvent $event)
81    {
82        $exception = $event->getThrowable();
83
84        $message = $exception->getMessage();
85        $this->type_caster->set_var($message, $message, 'string', true, false);
86
87        if ($exception instanceof \phpbb\exception\exception_interface)
88        {
89            $message = $this->language->lang_array($message, $exception->get_parameters());
90        }
91        else if (!$this->debug && $exception instanceof NotFoundHttpException)
92        {
93            $message = $this->language->lang('PAGE_NOT_FOUND');
94        }
95
96        // Do not update user session page if it does not exist
97        if ($exception instanceof NotFoundHttpException)
98        {
99            $this->user->update_session_page = false;
100        }
101
102        // Show <strong> text in bold
103        $message = preg_replace('#&lt;(/?strong)&gt;#i', '<$1>', $message);
104
105        if (!$event->getRequest()->isXmlHttpRequest())
106        {
107            page_header($this->language->lang('INFORMATION'));
108
109            $this->template->assign_vars(array(
110                'MESSAGE_TITLE' => $this->language->lang('INFORMATION'),
111                'MESSAGE_TEXT'  => $message,
112            ));
113
114            $this->template->set_filenames(array(
115                'body' => 'message_body.html',
116            ));
117
118            page_footer(true, false, false);
119
120            $response = new Response($this->template->assign_display('body'), 500);
121        }
122        else
123        {
124            $data = array();
125
126            if (!empty($message))
127            {
128                $data['message'] = $message;
129            }
130
131            if ($this->debug)
132            {
133                $data['trace'] = $exception->getTrace();
134            }
135
136            $response = new JsonResponse($data, 500);
137        }
138
139        if ($exception instanceof HttpExceptionInterface)
140        {
141            $response->setStatusCode($exception->getStatusCode());
142            $response->headers->add($exception->getHeaders());
143        }
144
145        $event->setResponse($response);
146    }
147
148    public static function getSubscribedEvents()
149    {
150        return array(
151            KernelEvents::EXCEPTION        => 'on_kernel_exception',
152        );
153    }
154}