diff --git a/lib/Client/Protocol/Command/AppendCommand.php b/lib/Client/Protocol/Command/AppendCommand.php index e50d1c4..e296472 100644 --- a/lib/Client/Protocol/Command/AppendCommand.php +++ b/lib/Client/Protocol/Command/AppendCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\ImapException; use KTXM\ProviderImap\Client\Protocol\RequestFrame; use KTXM\ProviderImap\Client\Protocol\Response\ContinuationResponse; @@ -57,7 +58,7 @@ final class AppendCommand implements CommandInterface return new RequestFrame(sprintf( 'APPEND %s %s{%d}', - $this->quote($this->mailbox), + StringEncoder::quote($this->mailbox), $flagSegment, strlen($this->literal), )); @@ -86,10 +87,6 @@ final class AppendCommand implements CommandInterface throw new ImapException('APPEND did not receive a tagged completion response.'); } - private function quote(string $mailbox): string - { - return '"' . addcslashes($mailbox, "\\\"") . '"'; - } private function parseAppendUid(string $text): ?int { diff --git a/lib/Client/Protocol/Command/Argument/SearchCriteriaBuilder.php b/lib/Client/Protocol/Command/Argument/SearchCriteriaBuilder.php index 705e204..02fd558 100644 --- a/lib/Client/Protocol/Command/Argument/SearchCriteriaBuilder.php +++ b/lib/Client/Protocol/Command/Argument/SearchCriteriaBuilder.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command\Argument; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Protocol\SequenceSet; use DateTimeInterface; @@ -190,8 +191,8 @@ final class SearchCriteriaBuilder { return $this->pushExpression(sprintf( 'HEADER %s %s', - $this->formatString($name), - $this->formatString($value), + StringEncoder::quote($name), + StringEncoder::quote($value), )); } @@ -287,7 +288,7 @@ final class SearchCriteriaBuilder return $this->pushExpression(sprintf( '%s %s', $key, - $quote ? $this->formatString($value) : $value, + $quote ? StringEncoder::quote($value) : $value, )); } @@ -371,9 +372,4 @@ final class SearchCriteriaBuilder default => SequenceSet::parse($value)->toCommand(), }; } - - private function formatString(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/CreateCommand.php b/lib/Client/Protocol/Command/CreateCommand.php index c64c2ca..b95928a 100644 --- a/lib/Client/Protocol/Command/CreateCommand.php +++ b/lib/Client/Protocol/Command/CreateCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Result\CommandStatusResult; use KTXM\ProviderImap\Client\ImapException; use KTXM\ProviderImap\Client\Protocol\RequestFrame; @@ -38,7 +39,7 @@ final class CreateCommand implements CommandInterface { unset($tag, $context); - return new RequestFrame(sprintf('CREATE %s', $this->quote($this->mailbox))); + return new RequestFrame(sprintf('CREATE %s', StringEncoder::quote($this->mailbox))); } public function handle(ResponseStream $responses, SessionContext $context): CommandStatusResult @@ -57,9 +58,4 @@ final class CreateCommand implements CommandInterface throw new ImapException('CREATE did not receive a tagged completion response.'); } - - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/DeleteCommand.php b/lib/Client/Protocol/Command/DeleteCommand.php index 6f104df..f2c22d8 100644 --- a/lib/Client/Protocol/Command/DeleteCommand.php +++ b/lib/Client/Protocol/Command/DeleteCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Result\CommandStatusResult; use KTXM\ProviderImap\Client\ImapException; use KTXM\ProviderImap\Client\Protocol\RequestFrame; @@ -38,7 +39,7 @@ final class DeleteCommand implements CommandInterface { unset($tag, $context); - return new RequestFrame(sprintf('DELETE %s', $this->quote($this->mailbox))); + return new RequestFrame(sprintf('DELETE %s', StringEncoder::quote($this->mailbox))); } public function handle(ResponseStream $responses, SessionContext $context): CommandStatusResult @@ -60,9 +61,4 @@ final class DeleteCommand implements CommandInterface throw new ImapException('DELETE did not receive a tagged completion response.'); } - - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/ListCommand.php b/lib/Client/Protocol/Command/ListCommand.php index 1be8801..e223af5 100644 --- a/lib/Client/Protocol/Command/ListCommand.php +++ b/lib/Client/Protocol/Command/ListCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Protocol\Parser\ListResponseParser; use KTXM\ProviderImap\Client\Protocol\Parser\StatusResponseParser; use Generator; @@ -66,8 +67,8 @@ final class ListCommand implements CommandInterface $command .= sprintf( ' %s %s', - $this->quote($this->reference), - $this->quote($this->pattern), + StringEncoder::quote($this->reference), + StringEncoder::quote($this->pattern), ); $returnOptions = $this->returnOptions->toCommand(); @@ -158,9 +159,4 @@ final class ListCommand implements CommandInterface $mailbox->readOnly(), ); } - - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/LoginCommand.php b/lib/Client/Protocol/Command/LoginCommand.php index 14aa885..c932a0e 100644 --- a/lib/Client/Protocol/Command/LoginCommand.php +++ b/lib/Client/Protocol/Command/LoginCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Result\CommandStatusResult; use KTXM\ProviderImap\Client\ImapException; use KTXM\ProviderImap\Client\Protocol\RequestFrame; @@ -38,8 +39,8 @@ final class LoginCommand implements CommandInterface return new RequestFrame(sprintf( 'LOGIN %s %s', - $this->quote($this->username), - $this->quote($this->password), + StringEncoder::quote($this->username), + StringEncoder::quote($this->password), )); } @@ -59,9 +60,4 @@ final class LoginCommand implements CommandInterface throw new ImapException('LOGIN did not receive a tagged completion response.'); } - - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/MessageTransferCommand.php b/lib/Client/Protocol/Command/MessageTransferCommand.php index 9e9e658..ae4f550 100644 --- a/lib/Client/Protocol/Command/MessageTransferCommand.php +++ b/lib/Client/Protocol/Command/MessageTransferCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Result\MessageTransferResult; use KTXM\ProviderImap\Client\Protocol\Command\Argument\MessageTarget; use KTXM\ProviderImap\Client\Protocol\IdentifierMode; @@ -66,7 +67,7 @@ final class MessageTransferCommand implements CommandInterface $this->identifierMode === IdentifierMode::Uid ? 'UID ' : '', $this->operation, $this->sequenceSet->toCommand(), - $this->quote($this->destinationMailbox), + StringEncoder::quote($this->destinationMailbox), )); } @@ -235,9 +236,4 @@ final class MessageTransferCommand implements CommandInterface 'text' => trim($matches[3] ?? ''), ]; } - - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/RenameCommand.php b/lib/Client/Protocol/Command/RenameCommand.php index e04b1a4..5a2118c 100644 --- a/lib/Client/Protocol/Command/RenameCommand.php +++ b/lib/Client/Protocol/Command/RenameCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Result\CommandStatusResult; use KTXM\ProviderImap\Client\ImapException; use KTXM\ProviderImap\Client\Protocol\RequestFrame; @@ -41,8 +42,8 @@ final class RenameCommand implements CommandInterface return new RequestFrame(sprintf( 'RENAME %s %s', - $this->quote($this->fromMailbox), - $this->quote($this->toMailbox), + StringEncoder::quote($this->fromMailbox), + StringEncoder::quote($this->toMailbox), )); } @@ -64,9 +65,4 @@ final class RenameCommand implements CommandInterface throw new ImapException('RENAME did not receive a tagged completion response.'); } - - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/SelectCommand.php b/lib/Client/Protocol/Command/SelectCommand.php index a3dc846..13f014d 100644 --- a/lib/Client/Protocol/Command/SelectCommand.php +++ b/lib/Client/Protocol/Command/SelectCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\ImapException; use KTXM\ProviderImap\Client\Mailbox; use KTXM\ProviderImap\Client\Protocol\RequestFrame; @@ -43,7 +44,7 @@ final class SelectCommand implements CommandInterface return new RequestFrame(sprintf( '%s %s', $this->name(), - $this->quote($this->mailbox), + StringEncoder::quote($this->mailbox), )); } @@ -116,9 +117,4 @@ final class SelectCommand implements CommandInterface return preg_split('/\s+/', $flags) ?: []; } - - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/Command/StatusCommand.php b/lib/Client/Protocol/Command/StatusCommand.php index 68470f8..89bb0ef 100644 --- a/lib/Client/Protocol/Command/StatusCommand.php +++ b/lib/Client/Protocol/Command/StatusCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace KTXM\ProviderImap\Client\Protocol\Command; +use KTXM\ProviderImap\Client\Protocol\StringEncoder; use KTXM\ProviderImap\Client\Protocol\Parser\StatusResponseParser; use KTXM\ProviderImap\Client\Result\StatusResult; use KTXM\ProviderImap\Client\ImapException; @@ -51,7 +52,7 @@ final class StatusCommand implements CommandInterface return new RequestFrame(sprintf( 'STATUS %s (%s)', - $this->quote($this->mailbox), + StringEncoder::quote($this->mailbox), implode(' ', $this->normalizeItems($this->items)), )); } @@ -112,8 +113,4 @@ final class StatusCommand implements CommandInterface return $normalized; } - private function quote(string $value): string - { - return '"' . addcslashes($value, "\\\"") . '"'; - } } \ No newline at end of file diff --git a/lib/Client/Protocol/StringEncoder.php b/lib/Client/Protocol/StringEncoder.php new file mode 100644 index 0000000..1e30003 --- /dev/null +++ b/lib/Client/Protocol/StringEncoder.php @@ -0,0 +1,13 @@ +