Files
provider_imap/lib/Client/Protocol/ProtocolWriter.php
2026-05-08 00:16:43 -04:00

44 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ProviderImap\Client\Protocol;
use KTXM\ProviderImap\Client\Transport\ConnectionInterface;
use Psr\Log\LoggerInterface;
final class ProtocolWriter
{
public function __construct(
private readonly ConnectionInterface $connection,
private readonly ?LoggerInterface $logger = null,
) {}
public function write(string $tag, RequestFrame $frame): void
{
$wire = $frame->toWire($tag);
$this->logger?->debug('IMAP command sent: {raw}', [
'tag' => $tag,
'command' => strtok($frame->commandLine(), ' ') ?: $frame->commandLine(),
'raw' => $this->sanitizeWire($wire),
]);
$this->connection->write($wire);
}
private function sanitizeWire(string $wire): string
{
$trimmed = rtrim($wire, "\r\n");
if (preg_match('/^(\S+\s+LOGIN\s+".*?"\s+)".*"$/i', $trimmed, $matches)) {
return $matches[1] . '"[REDACTED]"';
}
if (preg_match('/^(\S+\s+AUTHENTICATE\s+\S+)(?:\s+.+)?$/i', $trimmed, $matches)) {
return $matches[1] . ' [REDACTED]';
}
return $trimmed;
}
}