Skip to content

Commit 7da0f4f

Browse files
committed
Toggle variable dumping
1 parent eb87270 commit 7da0f4f

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

lib/ErrorHandler.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class ErrorHandler
1414
private $errorOutputStream;
1515
private $hasColorSupport = false;
1616
private $logErrors;
17+
private $logVariables = true;
1718
private $emailCallback;
1819

1920
private static $colors = array(
@@ -123,6 +124,16 @@ public function logErrors() : bool
123124
return $this->logErrors;
124125
}
125126

127+
public function setLogVariables(bool $logVariables)
128+
{
129+
$this->logVariables = $logVariables;
130+
}
131+
132+
public function logVariables() : bool
133+
{
134+
return $this->logVariables;
135+
}
136+
126137
public function register()
127138
{
128139
set_error_handler(array($this, 'errorHandler'), error_reporting());
@@ -301,18 +312,21 @@ public function emailException(\Throwable $exception)
301312
$bodyText .= 'Stack trace:' . "\n\n" . $this->purgeTrace($currentEx->getTraceAsString()) . "\n\n";
302313
} while ($currentEx = $currentEx->getPrevious());
303314

304-
if (isset($_POST) and ! empty($_POST)) {
305-
$bodyText .= '$_POST = ' . print_r($_POST, true) . PHP_EOL;
306-
}
307315
$username = null;
308-
if (isset($_SESSION) and ! empty($_SESSION)) {
309-
$sessionText = print_r(class_exists(DoctrineDebug::class) ? DoctrineDebug::export($_SESSION, 4) : $_SESSION, true);
310-
$bodyText .= '$_SESSION = ' . $sessionText . PHP_EOL;
311-
312-
$count = 0;
313-
$username = preg_replace('/.+\[([^\]]+)?username([^\]]+)?\] => ([\w\-\.]+).+/s', '\3', $sessionText, -1, $count);
314-
if (! isset($username[0]) or isset($username[255]) or $count !== 1) {
315-
$username = null;
316+
317+
if ($this->logVariables()) {
318+
if (isset($_POST) and ! empty($_POST)) {
319+
$bodyText .= '$_POST = ' . print_r($_POST, true) . PHP_EOL;
320+
}
321+
if (isset($_SESSION) and ! empty($_SESSION)) {
322+
$sessionText = print_r(class_exists(DoctrineDebug::class) ? DoctrineDebug::export($_SESSION, 4) : $_SESSION, true);
323+
$bodyText .= '$_SESSION = ' . $sessionText . PHP_EOL;
324+
325+
$count = 0;
326+
$username = preg_replace('/.+\[([^\]]+)?username([^\]]+)?\] => ([\w\-\.]+).+/s', '\3', $sessionText, -1, $count);
327+
if (! isset($username[0]) or isset($username[255]) or $count !== 1) {
328+
$username = null;
329+
}
316330
}
317331
}
318332

tests/ErrorHandlerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,30 @@ public function testEmailException()
170170
$this->assertContains($_POST[$key], $messageText);
171171
}
172172

173+
public function testCanHideVariablesFromEmail()
174+
{
175+
$this->assertTrue($this->errorHandler->logVariables());
176+
$this->errorHandler->setLogVariables(false);
177+
$this->assertFalse($this->errorHandler->logVariables());
178+
179+
$this->errorHandler->setLogErrors(true);
180+
181+
$key = uniqid(__FUNCTION__);
182+
$_SESSION = array($key => uniqid());
183+
$_POST = array($key => uniqid());
184+
185+
$this->errorHandler->emailException($this->exception);
186+
187+
$this->assertNotEmpty($this->emailsSent);
188+
$message = current($this->emailsSent);
189+
$this->assertNotEmpty($message);
190+
191+
$messageText = $message['body'];
192+
$this->assertContains($this->exception->getMessage(), $messageText);
193+
$this->assertNotContains($_SESSION[$key], $messageText);
194+
$this->assertNotContains($_POST[$key], $messageText);
195+
}
196+
173197
public function testErroriNellInvioDellaMailVengonoComunqueLoggati()
174198
{
175199
$mailError = uniqid('mail_not_sent_');

0 commit comments

Comments
 (0)