Skip to content

Commit be77b96

Browse files
[Debug] add charset param to ExceptionHandler
1 parent 6963887 commit be77b96

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,27 @@ class ExceptionHandler
3434
private $caughtBuffer;
3535
private $caughtLength;
3636
private $fileLinkFormat;
37+
private $charset;
3738

38-
public function __construct($debug = true, $fileLinkFormat = null)
39+
public function __construct($debug = true, $fileLinkFormat = null, $charset = null)
3940
{
4041
$this->debug = $debug;
4142
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
43+
$this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8';
4244
}
4345

4446
/**
4547
* Registers the exception handler.
4648
*
47-
* @param bool $debug
49+
* @param bool $debug Enable/disable debug mode, where the stack trace is displayed
50+
* @param string|null $fileLinkFormat The IDE link template
51+
* @param string|null $charset The charset used by exception messages
4852
*
4953
* @return ExceptionHandler The registered exception handler
5054
*/
51-
public static function register($debug = true, $fileLinkFormat = null)
55+
public static function register($debug = true, $fileLinkFormat = null, $charset = null)
5256
{
53-
$handler = new static($debug, $fileLinkFormat);
57+
$handler = new static($debug, $fileLinkFormat, $charset);
5458

5559
$prev = set_exception_handler(array($handler, 'handle'));
5660
if (is_array($prev) && $prev[0] instanceof ErrorHandler) {
@@ -177,6 +181,7 @@ public function sendPhpResponse($exception)
177181
foreach ($exception->getHeaders() as $name => $value) {
178182
header($name.': '.$value, false);
179183
}
184+
header('Content-Type: text/html; charset='.$this->charset);
180185
}
181186

182187
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
@@ -195,7 +200,7 @@ public function createResponse($exception)
195200
$exception = FlattenException::create($exception);
196201
}
197202

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

201206
/**
@@ -223,7 +228,7 @@ public function getContent(FlattenException $exception)
223228
foreach ($exception->toArray() as $position => $e) {
224229
$ind = $count - $position + 1;
225230
$class = $this->formatClass($e['class']);
226-
$message = nl2br(self::utf8Htmlize($e['message']));
231+
$message = nl2br($this->escapeHtml($e['message']));
227232
$content .= sprintf(<<<EOF
228233
<h2 class="block_exception clear_fix">
229234
<span class="exception_counter">%d/%d</span>
@@ -337,7 +342,7 @@ private function decorate($content, $css)
337342
<!DOCTYPE html>
338343
<html>
339344
<head>
340-
<meta charset="UTF-8" />
345+
<meta charset="{$this->charset}" />
341346
<meta name="robots" content="noindex,nofollow" />
342347
<style>
343348
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
@@ -365,7 +370,7 @@ private function formatClass($class)
365370

366371
private function formatPath($path, $line)
367372
{
368-
$path = self::utf8Htmlize($path);
373+
$path = $this->escapeHtml($path);
369374
$file = preg_match('#[^/\\\\]*$#', $path, $file) ? $file[0] : $path;
370375

371376
if ($linkFormat = $this->fileLinkFormat) {
@@ -393,15 +398,15 @@ private function formatArgs(array $args)
393398
} elseif ('array' === $item[0]) {
394399
$formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
395400
} elseif ('string' === $item[0]) {
396-
$formattedValue = sprintf("'%s'", self::utf8Htmlize($item[1]));
401+
$formattedValue = sprintf("'%s'", $this->escapeHtml($item[1]));
397402
} elseif ('null' === $item[0]) {
398403
$formattedValue = '<em>null</em>';
399404
} elseif ('boolean' === $item[0]) {
400405
$formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
401406
} elseif ('resource' === $item[0]) {
402407
$formattedValue = '<em>resource</em>';
403408
} else {
404-
$formattedValue = str_replace("\n", '', var_export(self::utf8Htmlize((string) $item[1]), true));
409+
$formattedValue = str_replace("\n", '', var_export($this->escapeHtml((string) $item[1]), true));
405410
}
406411

407412
$result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
@@ -412,23 +417,24 @@ private function formatArgs(array $args)
412417

413418
/**
414419
* Returns an UTF-8 and HTML encoded string
420+
*
421+
* @deprecated since version 2.7, to be removed in 3.0.
415422
*/
416423
protected static function utf8Htmlize($str)
417424
{
418-
if (!preg_match('//u', $str) && function_exists('iconv')) {
419-
set_error_handler('var_dump', 0);
420-
$charset = ini_get('default_charset');
421-
if ('UTF-8' === $charset || $str !== @iconv($charset, $charset, $str)) {
422-
$charset = 'CP1252';
423-
}
424-
restore_error_handler();
425-
426-
$str = iconv($charset, 'UTF-8', $str);
427-
}
425+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.7 and will be removed in 3.0.', E_USER_DEPRECATED);
428426

429427
return htmlspecialchars($str, ENT_QUOTES | (PHP_VERSION_ID >= 50400 ? ENT_SUBSTITUTE : 0), 'UTF-8');
430428
}
431429

430+
/**
431+
* HTML-encodes a string
432+
*/
433+
private function escapeHtml($str)
434+
{
435+
return htmlspecialchars($str, ENT_QUOTES | (PHP_VERSION_ID >= 50400 ? ENT_SUBSTITUTE : 0), $this->charset);
436+
}
437+
432438
/**
433439
* @internal
434440
*/

0 commit comments

Comments
 (0)