Skip to content

πŸš€βœ¨ Add Full PHP 8.4 Support πŸ˜πŸ”§πŸ’₯ #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 22 additions & 37 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,34 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [v0.1.11](https://github.com/javanile/php-imap2/compare/0.1.10...0.1.11)

## [v0.1.10](https://github.com/javanile/php-imap2/compare/0.1.9...0.1.10) - 2022-10-25
- **INFO** - Release date 2022-11-05
- **CHANGED** - Added support for search on `imap2_sort` ([#25](https://github.com/javanile/php-imap2/issues/25))
- **FIXED** - Undefined variables on `imap2_bodystruct` ([#5](https://github.com/javanile/php-imap2/issues/5))
- **FIXED** - Missing body peek on `imap2_fetch_overview` ([#24](https://github.com/javanile/php-imap2/issues/24))
- **FIXED** - Missing body peek on `imap2_fetchheader` ([#24](https://github.com/javanile/php-imap2/issues/24))

### Changed
## [v0.1.10](https://github.com/javanile/php-imap2/compare/0.1.9...0.1.10)

- Updated imap2_fetchstructure message with support over 2000 emails ([#10](https://github.com/javanile/php-imap2/issues/10))
- **INFO** - Release date 2022-10-25
- **CHANGED** - Updated `imap2_fetchstructure` message with support over 2000 emails ([#10](https://github.com/javanile/php-imap2/issues/10))
- **FIXED** - Fixed `imap2_headers` missing un-flagged emails ([#17](https://github.com/javanile/php-imap2/issues/17))

### Fixed
## [v0.1.9](https://github.com/javanile/php-imap2/compare/0.1.8...0.1.9)

- Fixed imap2_headers missing un-flagged emails ([#17](https://github.com/javanile/php-imap2/issues/17))
- **INFO** - Release date 2022-09-15
- **CHANGED** - Updated `imap2_last_error` message ([#3](https://github.com/javanile/php-imap2/issues/3))
- **FIXED** - Fixed `imap2_fetchmime` unexpected behaviour ([#10](https://github.com/javanile/php-imap2/issues/10))

## [v0.1.8](https://github.com/javanile/php-imap2/compare/0.1.7...0.1.8)

- **INFO** - Release date 2022-09-05
- **CHANGED** - New reopen error message ([#3](https://github.com/javanile/php-imap2/issues/3))
- **FIXED** - Fixed reopen functions ([#3](https://github.com/javanile/php-imap2/issues/3))

## [v0.1.9](https://github.com/javanile/php-imap2/compare/0.1.8...0.1.9) - 2022-09-15
## [v0.1.7](https://github.com/javanile/php-imap2/compare/0.1.0...0.1.7)

### Changed

- Updated imap2_last_error message ([#3](https://github.com/javanile/php-imap2/issues/3))

### Fixed

- Fixed imap2_fetchmime unexpected behaviour ([#10](https://github.com/javanile/php-imap2/issues/10))



## [v0.1.8](https://github.com/javanile/php-imap2/compare/0.1.7...0.1.8) - 2022-09-05

### Changed

- New reopen error message ([#3](https://github.com/javanile/php-imap2/issues/3))

### Fixed

- Fixed reopen functions ([#3](https://github.com/javanile/php-imap2/issues/3))



## [v0.1.7](https://github.com/javanile/php-imap2/compare/0.1.0...0.1.7) - 2022-01-01

### Changed

- Documentation amends ([#3](https://github.com/javanile/php-imap2/issues/3))

### Fixed

- Generic imap2_open bugs ([#3](https://github.com/javanile/php-imap2/issues/3))
- **INFO** - Release date 2022-01-01
- **CHANGED** - Documentation amends ([#3](https://github.com/javanile/php-imap2/issues/3))
- **FIXED** - Generic `imap2_open` bugs ([#3](https://github.com/javanile/php-imap2/issues/3))
695 changes: 674 additions & 21 deletions LICENSE

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ install:
update:
@docker-compose run --rm composer update

require:
@docker-compose run --rm composer require webklex/php-imap

dump-autoload:
@docker-compose run --rm composer dump-autoload

Expand Down Expand Up @@ -122,12 +125,18 @@ test-get-mailboxes:
test-delete-mailbox:
@docker-compose run --rm phpunit tests --filter CompatibilityTest::testDeleteMailbox

test-body-structure:
test-fetch-structure-2:
@docker-compose run --rm phpunit tests --filter BodyStructureTest::testFetchStructure

test-body-struct:
@docker-compose run --rm phpunit tests --filter BodyStructureTest::testBodyStruct

test-search:
@docker-compose run --rm phpunit tests --filter SearchTest

test-sort-search:
@docker-compose run --rm phpunit tests --filter SearchTest::testSortSearch

test-timeout:
@docker-compose run --rm phpunit tests --filter XoauthTest::testTimeout

Expand Down Expand Up @@ -158,6 +167,9 @@ test-cleaning:
test-retrofit:
@docker-compose run --rm phpunit tests --filter RetrofitTest

test-imap-client:
@docker-compose run --rm phpunit tests --filter ImapClientTest

test-errors:
@docker-compose run --rm phpunit tests --filter ErrorsTest

Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
],
"require": {
"php": ">=7.0",
"zbateson/mail-mime-parser": "^2.2"
"zbateson/mail-mime-parser": "^2.2",
"webklex/php-imap": "v5.x-dev"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
Expand All @@ -47,8 +48,13 @@
"minimum-stability": "dev",
"extra": {
"thanks": {
"name": "Roundcube",
"url": "https://github.com/roundcube"
"name": "PHP",
"url": "https://github.com/php/php-src"
}
},
"config": {
"allow-plugins": {
"kylekatarnls/update-helper": false
}
}
}
}
33 changes: 29 additions & 4 deletions src/BodyStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,33 @@ public static function fromMessage($message)
return self::extractBodyStructure($message->bodystructure);
}

protected static function extractBodyStructure($structure)
public static function searchSection($structure, $section, $currentSection = '')
{
if ($section == $currentSection) {
unset($structure->parts);

return $structure;
}

if (empty($structure->parts)) {
return false;
}

$index = 1;
foreach ($structure->parts as $part) {
$nextSection = strval($currentSection ? $currentSection.'.'.$index : $index);
$sectionStructure = self::searchSection($part, $section, $nextSection);
if ($sectionStructure) {
return $sectionStructure;
}
$index++;
}

return false;
}

protected static function extractBodyStructure($structure)
{
/* if NIL */
if ( is_null($structure) )
return null;
Expand Down Expand Up @@ -239,11 +263,12 @@ protected static function extractBodyStructure($structure)
++$index;
}

if ( count( $body->parameters = self::extractParameters($structure[2], []) ) )
if (isset($structure[2]) && is_array($structure[2]) &&
count( $body->parameters = self::extractParameters($structure[2], []))) {
$body->ifparameters = 1;
else
} else {
$body->parameters = (object)[];

}
}

if ( is_null($body->description) ) unset($body->description);
Expand Down
53 changes: 35 additions & 18 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Javanile\Imap2;

use Javanile\Imap2\Roundcube\ImapClient;
use Webklex\PHPIMAP\ClientManager;

class Connection
{
Expand Down Expand Up @@ -42,9 +42,35 @@ public function __construct($mailbox, $user, $password, $flags = 0, $retries = 0

$this->openMailbox($mailbox);

$this->client = new ImapClient();
$clientManager = new ClientManager();

if (defined('IMAP2_DEBUG') && IMAP2_DEBUG) {
echo "\n\n";
$clientManager->setConfig([
'options' => [
'debug' => true
]
]);
}

$this->client = $clientManager->make([
'host' => $this->host,
'port' => $this->port,
'protocol' => 'imap',
'encryption' => 'ssl',
'validate_cert' => true,
'username' => $user,
'password' => $password,
'authentication' => $this->flags & OP_XOAUTH2 ? 'oauth' : null,
]);
}

/**
*
* @param $mailbox
*
* @return void
*/
public function openMailbox($mailbox)
{
$this->mailbox = $mailbox;
Expand Down Expand Up @@ -129,21 +155,15 @@ protected function connect()
$client = $this->getClient();
#$client->setDebug(true);

$success = $client->connect($this->host, $this->user, $this->password, [
'port' => $this->port,
'ssl_mode' => $this->sslMode,
'auth_type' => $this->flags & OP_XOAUTH2 ? 'XOAUTH2' : 'CHECK',
'timeout' => -1,
'force_caps' => false,
]);

if (empty($success)) {
try {
$client->connect();
} catch (\Throwable $error) {
return false;
}

if (empty($this->currentMailbox)) {
$mailboxes = $this->client->listMailboxes('', '*');
if (in_array('INBOX', $mailboxes)) {
$mailboxes = $this->client->connection->folders('', '*');
if (isset($mailboxes['INBOX']) && $mailboxes['INBOX']) {
$this->currentMailbox = 'INBOX';
$this->mailbox .= 'INBOX';
}
Expand Down Expand Up @@ -217,7 +237,7 @@ public function getHost()
*/
public function selectMailbox()
{
$success = $this->client->select($this->currentMailbox);
$success = $this->client->connection->selectFolder($this->currentMailbox);

if (empty($success)) {
$this->rewriteMailbox('<no_mailbox>');
Expand All @@ -234,11 +254,8 @@ public static function close($imap, $flags = 0)
}

$client = $imap->getClient();
if ($client->close()) {
return true;
}

$client->closeConnection();
$client->disconnect();

return true;
}
Expand Down
28 changes: 15 additions & 13 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public static function check($imap)
$imap->selectMailbox();

$client = $imap->getClient();
$status = $client->status($imap->getMailboxName(), ['MESSAGES', 'RECENT']);
$status = $client->connection->examineFolder($imap->getMailboxName());

return (object) [
'Date' => date('D, j M Y G:i:s').' +0000 (UTC)',
'Driver' => 'imap',
'Mailbox' => $imap->getMailbox(),
'Nmsgs' => intval($status['MESSAGES']),
'Recent' => intval($status['RECENT']),
'Nmsgs' => intval($status['exists']),
'Recent' => intval($status['recent']),
];

} elseif (IMAP2_RETROFIT_MODE && is_resource($imap) && get_resource_type($imap) == 'imap') {
Expand Down Expand Up @@ -205,17 +205,20 @@ public static function getMailboxes($imap, $reference, $pattern)
$client = $imap->getClient();
#$client->setDebug(true);
$return = [];
$delimiter = $client->getHierarchyDelimiter();
$mailboxes = $client->listMailboxes($referenceParts[1], $pattern);
foreach ($mailboxes as $mailbox) {
//$delimiter = $client->getHierarchyDelimiter();
$mailboxes = $client->connection->folders($referenceParts[1], $pattern);
foreach ($mailboxes as $mailboxName => $mailbox) {
/*
// TODO: Restore check of google folder
$attributesValue = Functions::getListAttributesValue($client->data['LIST'][$mailbox]);
if ($mailbox == '[Gmail]' && $imap->getHost() == 'imap.gmail.com') {
$attributesValue = 34;
}
*/
$return[] = (object) [
'name' => $referenceParts[0].'}'.$mailbox,
'attributes' => $attributesValue,
'delimiter' => $delimiter,
'name' => $referenceParts[0].'}'.$mailboxName,
'attributes' => $mailbox['flags'],
'delimiter' => $mailbox['delimiter'],
];
}

Expand Down Expand Up @@ -266,12 +269,11 @@ public static function deleteMailbox($imap, $mailbox)
$mailbox = (string) \preg_replace('/^{.+}/', '', $mailbox);
}

$result = $client->execute('DELETE', array($client->escape($mailbox)), ImapClient::COMMAND_RAW_LASTLINE);

$success = $result[0] == ImapClient::ERROR_OK;
$success = $client->connection->deleteFolder($mailbox);
//$result = $client->execute('DELETE', array($client->escape($mailbox)), ImapClient::COMMAND_RAW_LASTLINE);

if (!$success && $imap->getRegistryValue('mailbox', $mailbox, 'deleted')) {
Errors::appendError($result[1]);
Errors::appendError("Server error: debug IMAP2 for info.");
} elseif (!$success) {
Errors::appendError("Can't delete mailbox {$mailbox}: no such mailbox");
} else {
Expand Down
Loading