Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Webklex/php-imap
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 6.1.0
Choose a base ref
...
head repository: Webklex/php-imap
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 2 commits
  • 4 files changed
  • 3 contributors

Commits on Jan 28, 2025

  1. SSL stream context options added #238 #546

    Co-authored-by: LE MOINE Laurent <[email protected]>
    llemoine and LE MOINE Laurent authored Jan 28, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    b9d8b1b View commit details
  2. Changelog updated

    Webklex committed Jan 28, 2025
    Copy the full SHA
    35bad85 View commit details
Showing with 62 additions and 2 deletions.
  1. +1 −1 CHANGELOG.md
  2. +13 −1 src/Client.php
  3. +36 −0 src/Connection/Protocols/Protocol.php
  4. +12 −0 tests/ImapProtocolTest.php
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- NaN

### Added
- NaN
- SSL stream context options added #238 #546 (thanks @llemoine)

### Breaking changes
- NaN
14 changes: 13 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
@@ -100,6 +100,16 @@ class Client {
'password' => null,
];


/**
* SSL stream context options
*
* @see https://www.php.net/manual/en/context.ssl.php for possible options
*
* @var array
*/
protected array $ssl_options = [];

/**
* Connection timeout
* @var int $timeout
@@ -184,7 +194,8 @@ class Client {
'username' => null,
'password' => null,
],
"timeout" => 30
'ssl_options' => [],
"timeout" => 30,
];

/**
@@ -436,6 +447,7 @@ public function connect(): Client {
$this->connection = new ImapProtocol($this->config, $this->validate_cert, $this->encryption);
$this->connection->setConnectionTimeout($this->timeout);
$this->connection->setProxy($this->proxy);
$this->connection->setSslOptions($this->ssl_options);
}else{
if (extension_loaded('imap') === false) {
throw new ConnectionFailedException("connection setup failed", 0, new ProtocolNotSupportedException($protocol." is an unsupported protocol"));
36 changes: 36 additions & 0 deletions src/Connection/Protocols/Protocol.php
Original file line number Diff line number Diff line change
@@ -71,6 +71,15 @@ abstract class Protocol implements ProtocolInterface {
'password' => null,
];

/**
* SSL stream context options
*
* @see https://www.php.net/manual/en/context.ssl.php for possible options
*
* @var array
*/
protected array $ssl_options = [];

/**
* Cache for uid of active folder.
*
@@ -162,6 +171,28 @@ public function getProxy(): array {
return $this->proxy;
}

/**
* Set SSL context options settings
* @var array $options
*
* @return Protocol
*/
public function setSslOptions(array $options): Protocol
{
$this->ssl_options = $options;

return $this;
}

/**
* Get the current SSL context options settings
*
* @return array
*/
public function getSslOptions(): array {
return $this->ssl_options;
}

/**
* Prepare socket options
* @return array
@@ -175,6 +206,11 @@ private function defaultSocketOptions(string $transport): array {
'verify_peer_name' => $this->getCertValidation(),
'verify_peer' => $this->getCertValidation(),
];

if (count($this->ssl_options)) {
/* Get the ssl context options from the config, but prioritize the 'validate_cert' config over the ssl context options */
$options["ssl"] = array_replace($this->ssl_options, $options["ssl"]);
}
}

if ($this->proxy["socket"] != null) {
12 changes: 12 additions & 0 deletions tests/ImapProtocolTest.php
Original file line number Diff line number Diff line change
@@ -48,5 +48,17 @@ public function testImapProtocol(): void {

self::assertSame(true, $protocol->getCertValidation());
self::assertSame("ssl", $protocol->getEncryption());

$protocol->setSslOptions([
'verify_peer' => true,
'cafile' => '/dummy/path/for/testing',
'peer_fingerprint' => ['md5' => 40],
]);

self::assertSame([
'verify_peer' => true,
'cafile' => '/dummy/path/for/testing',
'peer_fingerprint' => ['md5' => 40],
], $protocol->getSslOptions());
}
}