8000 [Debug] reintroduce charset param to ExceptionHandler by nicolas-grekas · Pull Request #13902 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Debug] reintroduce charset param to ExceptionHandler #13902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Debug] reintroduce charset param to ExceptionHandler
  • Loading branch information
nicolas-grekas committed Mar 12, 2015
commit c8bd8674148a11baccd48dc216d40e50e0423e55
41 changes: 30 additions & 11 deletions src/Symfony/Component/Debug/ExceptionHandler.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,37 @@
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) {
Expand Down Expand Up @@ -177,6 +187,7 @@ public function sendPhpResponse($exception)
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));
Expand All @@ -195,7 +206,7 @@ public function createResponse($exception)
$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);
}

/**
Expand Down Expand Up @@ -223,7 +234,7 @@ public function getContent(FlattenException $exception)
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(<<<EOF
<h2 class="block_exception clear_fix">
<span class="exception_counter">%d/%d</span>
Expand Down Expand Up @@ -251,7 +262,7 @@ public function getContent(FlattenException $exception)
} 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.';
}
Expand Down Expand Up @@ -337,7 +348,7 @@ private function decorate($content, $css)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta charset="{$this->charset}" />
<meta name="robots" content="noindex,nofollow" />
<style>
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
Expand Down Expand Up @@ -365,7 +376,7 @@ private function formatClass($class)

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

if ($linkFormat = $this->fileLinkFormat) {
Expand Down Expand Up @@ -393,15 +404,15 @@ private function formatArgs(array $args)
} elseif ('array' === $item[0]) {
$formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
} elseif ('string' === $item[0]) {
$formattedValue = sprintf("'%s'", self::utf8Htmlize($item[1]));
$formattedValue = sprintf("'%s'", $this->escapeHtml($item[1]));
} elseif ('null' === $item[0]) {
$formattedValue = '<em>null</em>';
} elseif ('boolean' === $item[0]) {
$formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
} elseif ('resource' === $item[0]) {
$formattedValue = '<em>resource</em>';
} else {
$formattedValue = str_replace("\n", '', var_export(self::utf8Htmlize((string) $item[1]), true));
$formattedValue = str_replace("\n", '', var_export($this->escapeHtml((string) $item[1]), true));
}

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

/**
* HTML-encodes a string
*/
private function escapeHtml($str)
{
return htmlspecialchars($str, ENT_QUOTES | (PHP_VERSION_ID >= 50400 ? ENT_SUBSTITUTE : 0), $this->charset);
}

/**
* @internal
*/
Expand Down
0