8000 [Debug] reintroduce charset param to ExceptionHandler · symfony/symfony@6a82cc9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a82cc9

Browse files
[Debug] reintroduce charset param to ExceptionHandler
1 parent 5ad671a commit 6a82cc9

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,37 @@
3030
class ExceptionHandler
3131
{
3232
private $debug;
33+
private $charset;
3334
private $handler;
3435
private $caughtBuffer;
3536
private $caughtLength;
3637
private $fileLinkFormat;
3738

38-
public function __construct($debug = true, $fileLinkFormat = null)
39+
public function __construct($debug = true, $charset = null, $fileLinkFormat = null)
3940
{
41+
if (false !== strpos($charset, '%') xor false === strpos($fileLinkFormat, '%')) {
42+
// Swap $charset and $fileLinkFormat for BC reasons
43+
$pivot = $fileLinkFormat;
44+
$fileLinkFormat = $charset;
45+
$charset = $pivot;
46+
}
4047
$this->debug = $debug;
48+
$this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8';
4149
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
4250
}
4351

4452
/**
4553
* Registers the exception handler.
4654
*
47-
* @param bool $debug
55+
* @param bool $debug Enable/disable debug mode, where the stack trace is displayed
56+
* @param string|null $charset The charset used by exception messages
57+
* @param string|null $fileLinkFormat The IDE link template
4858
*
4959
* @return ExceptionHandler The registered exception handler
5060
*/
51-
public static function register($debug = true, $fileLinkFormat = null)
61+
public static function register($debug = true, $charset = null, $fileLinkFormat = null)
5262
{
53-
$handler = new static($debug, $fileLinkFormat);
63+
$handler = new static($debug, $charset, $fileLinkFormat);
5464

5565
$prev = set_exception_handler(array($handler, 'handle'));
5666
if (is_array($prev) && $prev[0] instanceof ErrorHandler) {
@@ -177,6 +187,7 @@ public function sendPhpResponse($exception)
177187
foreach ($exception->getHeaders() as $name => $value) {
178188
header($name.': '.$value, false);
179189
}
190+
header('Content-Type: text/html; charset='.$this->charset);
180191
}
181192

182193
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
@@ -195,7 +206,7 @@ public function createResponse($exception)
195206
$exception = FlattenException::create($exception);
196207
}
197208

198-
return new Response($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders());
209+
return Response::create($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset);
199210
}
200211

201212
/**
@@ -223,7 +234,7 @@ public function getContent(FlattenException $exception)
223234
foreach ($exception->toArray() as $position => $e) {
224235
$ind = $count - $position + 1;
225236
$class = $this->formatClass($e['class']);
226-
$message = nl2br(self::utf8Htmlize($e['message']));
237+
$message = nl2br($this->escapeHtml($e['message']));
227238
$content .= sprintf(<<<EOF
228239
<h2 class="block_exception clear_fix">
229240
<span class="exception_counter">%d/%d</span>
@@ -337,7 +348,7 @@ private function decorate($content, $css)
337348
<!DOCTYPE html>
338349
<html>
339350
<head>
340-
<meta charset="UTF-8" />
351+
<meta charset="{$this->charset}" />
341352
<meta name="robots" content="noindex,nofollow" />
342353
<style>
343354
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
@@ -365,7 +376,7 @@ private function formatClass($class)
365376

366377
private function formatPath($path, $line)
367378
{
368-
$path = self::utf8Htmlize($path);
379+
$path = $this->escapeHtml($path);
369380
$file = preg_match('#[^/\\\\]*$#', $path, $file) ? $file[0] : $path;
370381

371382
if ($linkFormat = $this->fileLinkFormat) {
@@ -393,15 +404,15 @@ private function formatArgs(array $args)
393404
} elseif ('array' === $item[0]) {
394405
$formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
395406
} elseif ('string' === $item[0]) {
396-
$formattedValue = sprintf("'%s'", self::utf8Htmlize($item[1]));
407+
$formattedValue = sprintf("'%s'", $this->escapeHtml($item[1]));
397408
} elseif ('null' === $item[0]) {
398409
$formattedValue = '<em>null</em>';
399410
} elseif ('boolean' === $item[0]) {
400411
$formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
401412
} elseif ('resource' === $item[0]) {
402413
$formattedValue = '<em>resource</em>';
403414
} else {
404-
$formattedValue = str_replace("\n", '', var_export(self::utf8Htmlize((string) $item[1]), true));
415+
$formattedValue = str_replace("\n", '', var_export($this->escapeHtml((string) $item[1]), true));
405416
}
406417

407418
$result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
@@ -429,6 +440,14 @@ protected static function utf8Htmlize($str)
429440
return htmlspecialchars($str, ENT_QUOTES | (PHP_VERSION_ID >= 50400 ? ENT_SUBSTITUTE : 0), 'UTF-8');
430441
}
431442

443+
/**
444+
* HTML-encodes a string
445+
*/
446+
private function escapeHtml($str)
447+
{
448+
return htmlspecialchars($str, ENT_QUOTES | (PHP_VERSION_ID >= 50400 ? ENT_SUBSTITUTE : 0), $this->charset);
449+
}
450+
432451
/**
433452
* @internal
434453
*/

0 commit comments

Comments
 (0)
0