Skip to content

Commit 7f93507

Browse files
committed
Session username in email subject
1 parent 5fe45a3 commit 7f93507

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/ErrorHandler.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,24 @@ public function emailException(\Throwable $exception)
302302
if (isset($_POST) and ! empty($_POST)) {
303303
$bodyText .= '$_POST = ' . print_r($_POST, true) . PHP_EOL;
304304
}
305+
$username = null;
305306
if (isset($_SESSION) and ! empty($_SESSION)) {
306-
$bodyText .= '$_SESSION = ' . print_r(class_exists(DoctrineDebug::class) ? DoctrineDebug::export($_SESSION, 4) : $_SESSION, true) . PHP_EOL;
307+
$sessionText = print_r(class_exists(DoctrineDebug::class) ? DoctrineDebug::export($_SESSION, 4) : $_SESSION, true);
308+
$bodyText .= '$_SESSION = ' . $sessionText . PHP_EOL;
309+
310+
$count = 0;
311+
$username = preg_replace('/.+\[([^\]]+)?username([^\]]+)?\] => ([\w\-\.]+).+/s', '\3', $sessionText, -1, $count);
312+
if (! isset($username[0]) or isset($username[255]) or $count !== 1) {
313+
$username = null;
314+
}
307315
}
308316

309317
$subject = sprintf('Error: %s', $exception->getMessage());
318+
$subject = sprintf('%s%s: %s',
319+
'Error',
320+
$username ? sprintf(' [%s]', $username) : '',
321+
$exception->getMessage()
322+
);
310323

311324
$callback = $this->emailCallback;
312325
try {

tests/ErrorHandlerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,14 @@ public function testErroriNellInvioDellaMailVengonoComunqueLoggati()
188188

189189
public function testUsernameInEmailSubject()
190190
{
191-
$_SESSION = array('custom_username_key' => uniqid('bob_'));
191+
$username = uniqid('bob_');
192+
$_SESSION = array('custom_username_key' => $username);
192193

193194
$this->errorHandler->setLogErrors(true);
195+
$this->errorHandler->emailException($this->exception);
196+
197+
$message = current($this->emailsSent);
198+
199+
$this->assertContains($username, $message['subject']);
194200
}
195201
}

0 commit comments

Comments
 (0)