Files
provider_imap/lib/Client/Protocol/ProtocolWriter.php
T
Sebastian 3dd9c2f983 feat: append command
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-06-23 15:13:42 -04:00

59 lines
1.6 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);
}
/**
* Write raw bytes to the connection without tagging or logging the payload.
*
* Used to send literal data (e.g. an APPEND message body) after the server
* has issued a command continuation request.
*/
public function writeRaw(string $payload): void
{
$this->logger?->debug('IMAP literal sent (bytes={bytes})', [
'bytes' => strlen($payload),
]);
$this->connection->write($payload);
}
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;
}
}