From c8bd8674148a11baccd48dc216d40e50e0423e55 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 11 Mar 2015 14:18:58 +0100 Subject: [PATCH] [Debug] reintroduce charset param to ExceptionHandler --- .../Component/Debug/ExceptionHandler.php | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Debug/ExceptionHandler.php b/src/Symfony/Component/Debug/ExceptionHandler.php index 42e274b2cc..fa89d23a43 100644 --- a/src/Symfony/Component/Debug/ExceptionHandler.php +++ b/src/Symfony/Component/Debug/ExceptionHandler.php @@ -30,27 +30,37 @@ use Symfony\Component\Debug\Exception\OutOfMemoryException; class ExceptionHandler { private $debug; + private $charset; private $handler; private $caughtBuffer; private $caughtLength; private $fileLinkFormat; - public function __construct($debug = true, $fileLinkFormat = null) + public function __construct($debug = true, $charset = null, $fileLinkFormat = null) { + if (false !== strpos($charset, '%') xor false === strpos($fileLinkFormat, '%')) { + // Swap $charset and $fileLinkFormat for BC reasons + $pivot = $fileLinkFormat; + $fileLinkFormat = $charset; + $charset = $pivot; + } $this->debug = $debug; + $this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8'; $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format'); } /** * Registers the exception handler. * - * @param bool $debug + * @param bool $debug Enable/disable debug mode, where the stack trace is displayed + * @param string|null $charset The charset used by exception messages + * @param string|null $fileLinkFormat The IDE link template * * @return ExceptionHandler The registered exception handler */ - public static function register($debug = true, $fileLinkFormat = null) + public static function register($debug = true, $charset = null, $fileLinkFormat = null) { - $handler = new static($debug, $fileLinkFormat); + $handler = new static($debug, $charset, $fileLinkFormat); $prev = set_exception_handler(array($handler, 'handle')); if (is_array($prev) && $prev[0] instanceof ErrorHandler) { @@ -177,6 +187,7 @@ class ExceptionHandler foreach ($exception->getHeaders() as $name => $value) { header($name.': '.$value, false); } + header('Content-Type: text/html; charset='.$this->charset); } echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception)); @@ -195,7 +206,7 @@ class ExceptionHandler $exception = FlattenException::create($exception); } - return new Response($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders()); + return Response::create($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset); } /** @@ -223,7 +234,7 @@ class ExceptionHandler foreach ($exception->toArray() as $position => $e) { $ind = $count - $position + 1; $class = $this->formatClass($e['class']); - $message = nl2br(self::utf8Htmlize($e['message'])); + $message = nl2br($this->escapeHtml($e['message'])); $content .= sprintf(<< %d/%d @@ -251,7 +262,7 @@ EOF } catch (\Exception $e) { // something nasty happened and we cannot throw an exception anymore if ($this->debug) { - $title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), self::utf8Htmlize($e->getMessage())); + $title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $this->escapeHtml($e->getMessage())); } else { $title = 'Whoops, looks like something went wrong.'; } @@ -337,7 +348,7 @@ EOF; - +