Skip to content

Commit

Permalink
feat: Support tls >= 1.2 by using horde_imap_client
Browse files Browse the repository at this point in the history
  • Loading branch information
jeboehm committed Dec 18, 2022
1 parent 14b7ea0 commit 02707f4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 49 deletions.
20 changes: 3 additions & 17 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,14 @@ jobs:
tools: phive
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: |
composer install --no-progress --prefer-dist --optimize-autoloader
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Install Phive dependencies
run: |
phive install --force-accept-unsigned --trust-gpg-keys 0xE82B2FB314E9906E
run: phive install

- name: Compile phar
run: |
composer run build
run: composer run build

- name: Run phar
run: ./imap-tester.phar
Expand Down
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="humbug/box" version="^3.16.0" installed="3.16.0" location="./tools/box" copy="false"/>
<phar name="php-cs-fixer" version="^3.8.0" installed="3.8.0" location="./tools/php-cs-fixer" copy="false"/>
<phar name="php-cs-fixer" version="^3.8.0" installed="3.13.1" location="./tools/php-cs-fixer" copy="false"/>
</phive>
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
FROM composer:2.3 AS composer
FROM php:8.0-alpine

RUN apk --no-cache add openssl-dev imap-dev gnupg icu-dev && \
docker-php-ext-configure imap --with-imap --with-imap-ssl && \
docker-php-ext-install imap intl && \
RUN apk --no-cache add gnupg icu-dev && \
docker-php-ext-install intl && \
wget -O phive.phar "https://phar.io/releases/phive.phar" && \
wget -O phive.phar.asc "https://phar.io/releases/phive.phar.asc" && \
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x6AF725270AB81E04D79442549D8A98B29B2D5D79 && \
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
"description": "Tests imap and pop3 mailboxes.",
"require": {
"php": "^8.0",
"ext-imap": "*",
"symfony/console": "~5.4",
"php-imap/php-imap": "~5.0"
"bytestream/horde-imap-client": "^2.31"
},
"license": "MIT",
"autoload": {
Expand Down
52 changes: 26 additions & 26 deletions src/Service/ImapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

namespace Service;

use InvalidArgumentException;
use PhpImap\Mailbox;
use RuntimeException;

class ImapService
{
private Mailbox $mailbox;
/** @var \Horde_Imap_Client_Socket_Pop3|\Horde_Imap_Client_Socket */
private \Horde_Imap_Client_Base $client;

public function __construct($host, $port, $username, $password)
{
Expand All @@ -41,42 +38,45 @@ public function __construct($host, $port, $username, $password)
break;

default:
throw new InvalidArgumentException('Port not known');
throw new \InvalidArgumentException('Port not known');
}

$this->mailbox = new Mailbox(
sprintf(
'{%s:%s/%s/%s/novalidate-cert}',
$host,
$port,
$serverType,
$sslType,
),
$username,
$password
);

$this->mailbox->setConnectionArgs(CL_EXPUNGE);
$options = [
'username' => $username,
'password' => $password,
'hostspec' => $host,
'port' => $port,
'secure' => $sslType,
];

if ('pop3' === $serverType) {
$this->client = new \Horde_Imap_Client_Socket_Pop3($options);
} else {
$this->client = new \Horde_Imap_Client_Socket($options);
}
}

public function doCount(string $folder): int
{
$this->mailbox->switchMailbox($folder);
$results = $this->client->search($folder)['match'];

return $this->mailbox->countMails();
return is_countable($results) ? count($results) : 0;
}

public function doMove(string $folder, int $messageIndex, string $targetFolder): bool
{
$this->mailbox->switchMailbox($folder);
$mailsIds = $this->mailbox->searchMailbox();
$mails = $this->client->search($folder)['match'];

if (!array_key_exists($messageIndex, $mailsIds)) {
throw new RuntimeException('Mail was not found.');
if (!array_key_exists($messageIndex, $mails->ids)) {
throw new \RuntimeException('Mail was not found.');
}

$countBefore = $this->doCount($folder);
$this->mailbox->moveMail($mailsIds[0], $targetFolder);
$this->client->copy($folder, $targetFolder, [
'ids' => new \Horde_Imap_Client_Ids($mails->ids[$messageIndex]),
'move' => true,
]);
$this->client->close();

return $this->doCount($folder) !== $countBefore;
}
Expand Down

0 comments on commit 02707f4

Please sign in to comment.