Skip to content
Permalink
Browse files

Safe output capturing via Helpers::capture()

  • Loading branch information...
dg committed Nov 3, 2019
1 parent 16691cf commit ca52715e9771822fb5d49386cc85fda6d2b83ed0
@@ -120,13 +120,14 @@ private function renderHtml(string $type, string $suffix = ''): array
{
$panels = $this->renderPanels($suffix);
ob_start(function () {});
require __DIR__ . '/assets/bar.phtml';
$bar = Helpers::fixEncoding(ob_get_clean());
ob_start(function () {});
require __DIR__ . '/assets/panels.phtml';
return ['bar' => $bar, 'panels' => Helpers::fixEncoding(ob_get_clean())];
return [
'bar' => Helpers::fixEncoding(Helpers::capture(function () use ($type, $panels) {
require __DIR__ . '/assets/bar.phtml';
})),
'panels' => Helpers::fixEncoding(Helpers::capture(function () use ($type, $panels) {
require __DIR__ . '/assets/panels.phtml';
})),
];
}
@@ -32,10 +32,10 @@ public function __construct(string $id)
*/
public function getTab(): string
{
ob_start(function () {});
$data = $this->data;
require __DIR__ . "/panels/{$this->id}.tab.phtml";
return ob_get_clean();
return Helpers::capture(function () {
$data = $this->data;
require __DIR__ . "/panels/{$this->id}.tab.phtml";
});
}
@@ -44,11 +44,11 @@ public function getTab(): string
*/
public function getPanel(): string
{
ob_start(function () {});
if (is_file(__DIR__ . "/panels/{$this->id}.panel.phtml")) {
$data = $this->data;
require __DIR__ . "/panels/{$this->id}.panel.phtml";
}
return ob_get_clean();
return Helpers::capture(function () {
if (is_file(__DIR__ . "/panels/{$this->id}.panel.phtml")) {
$data = $this->data;
require __DIR__ . "/panels/{$this->id}.panel.phtml";
}
});
}
}
@@ -80,10 +80,12 @@ public function addAction(callable $action): self
public function render(\Throwable $exception): void
{
if (Helpers::isAjax() && session_status() === PHP_SESSION_ACTIVE) {
ob_start(function () {});
$this->renderTemplate($exception, __DIR__ . '/assets/content.phtml');
$contentId = $_SERVER['HTTP_X_TRACY_AJAX'];
$_SESSION['_tracy']['bluescreen'][$contentId] = ['content' => ob_get_clean(), 'time' => time()];
$_SESSION['_tracy']['bluescreen'][$_SERVER['HTTP_X_TRACY_AJAX']] = [
'content' => Helpers::capture(function () use ($exception) {
$this->renderTemplate($exception, __DIR__ . '/assets/content.phtml');
}),
'time' => time(),
];
} else {
$this->renderTemplate($exception, __DIR__ . '/assets/page.phtml');
@@ -507,12 +507,12 @@ public static function getFireLogger(): ILogger
public static function dump($var, bool $return = false)
{
if ($return) {
ob_start(function () {});
Dumper::dump($var, [
Dumper::DEPTH => self::$maxDepth,
Dumper::TRUNCATE => self::$maxLength,
]);
return ob_get_clean();
return Helpers::capture(function () use ($var) {
Dumper::dump($var, [
Dumper::DEPTH => self::$maxDepth,
Dumper::TRUNCATE => self::$maxLength,
]);
});
} elseif (!self::$productionMode) {
Dumper::dump($var, [
@@ -317,4 +317,20 @@ private static function escapeArg(string $s): string
? '"' . str_replace('"', '""', $s) . '"'
: escapeshellarg($s);
}
/**
* Captures PHP output into a string.
*/
public static function capture(callable $func): string
{
ob_start(function () {});
try {
$func();
return ob_get_clean();
} catch (\Throwable $e) {
ob_end_clean();
throw $e;
}
}
}

0 comments on commit ca52715

Please sign in to comment.
You can’t perform that action at this time.