[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/phpbb/event/ -> kernel_exception_subscriber.php (source)

   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\event;
  15  
  16  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17  use Symfony\Component\HttpFoundation\JsonResponse;
  18  use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  19  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  20  use Symfony\Component\HttpKernel\KernelEvents;
  21  use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  22  use Symfony\Component\HttpFoundation\Response;
  23  
  24  class 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      /** @var \phpbb\request\type_cast_helper */
  48      protected $type_caster;
  49  
  50      /**
  51      * Construct method
  52      *
  53      * @param \phpbb\template\template    $template    Template object
  54      * @param \phpbb\language\language    $language    Language object
  55      * @param bool                        $debug        Set to true to show full exception messages
  56      */
  57  	public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, $debug = false)
  58      {
  59          $this->debug = $debug || defined('DEBUG');
  60          $this->template = $template;
  61          $this->language = $language;
  62          $this->type_caster = new \phpbb\request\type_cast_helper();
  63      }
  64  
  65      /**
  66      * This listener is run when the KernelEvents::EXCEPTION event is triggered
  67      *
  68      * @param GetResponseForExceptionEvent $event
  69      * @return null
  70      */
  71  	public function on_kernel_exception(GetResponseForExceptionEvent $event)
  72      {
  73          $exception = $event->getException();
  74  
  75          $message = $exception->getMessage();
  76          $this->type_caster->set_var($message, $message, 'string', true, false);
  77  
  78          if ($exception instanceof \phpbb\exception\exception_interface)
  79          {
  80              $message = $this->language->lang_array($message, $exception->get_parameters());
  81          }
  82          else if (!$this->debug && $exception instanceof NotFoundHttpException)
  83          {
  84              $message = $this->language->lang('PAGE_NOT_FOUND');
  85          }
  86  
  87          // Show <strong> text in bold
  88          $message = preg_replace('#&lt;(/?strong)&gt;#i', '<$1>', $message);
  89  
  90          if (!$event->getRequest()->isXmlHttpRequest())
  91          {
  92              page_header($this->language->lang('INFORMATION'));
  93  
  94              $this->template->assign_vars(array(
  95                  'MESSAGE_TITLE' => $this->language->lang('INFORMATION'),
  96                  'MESSAGE_TEXT'  => $message,
  97              ));
  98  
  99              $this->template->set_filenames(array(
 100                  'body' => 'message_body.html',
 101              ));
 102  
 103              page_footer(true, false, false);
 104  
 105              $response = new Response($this->template->assign_display('body'), 500);
 106          }
 107          else
 108          {
 109              $data = array();
 110  
 111              if (!empty($message))
 112              {
 113                  $data['message'] = $message;
 114              }
 115  
 116              if ($this->debug)
 117              {
 118                  $data['trace'] = $exception->getTrace();
 119              }
 120  
 121              $response = new JsonResponse($data, 500);
 122          }
 123  
 124          if ($exception instanceof HttpExceptionInterface)
 125          {
 126              $response->setStatusCode($exception->getStatusCode());
 127              $response->headers->add($exception->getHeaders());
 128          }
 129  
 130          $event->setResponse($response);
 131      }
 132  
 133  	static public function getSubscribedEvents()
 134      {
 135          return array(
 136              KernelEvents::EXCEPTION        => 'on_kernel_exception',
 137          );
 138      }
 139  }


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1