forked from DirectoryTree/ImapEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailbox.php
More file actions
222 lines (192 loc) · 5.12 KB
/
Mailbox.php
File metadata and controls
222 lines (192 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace DirectoryTree\ImapEngine;
use DirectoryTree\ImapEngine\Connection\ConnectionInterface;
use DirectoryTree\ImapEngine\Connection\ImapConnection;
use DirectoryTree\ImapEngine\Connection\Loggers\EchoLogger;
use DirectoryTree\ImapEngine\Connection\Loggers\FileLogger;
use DirectoryTree\ImapEngine\Connection\Streams\ImapStream;
use DirectoryTree\ImapEngine\Connection\Tokens\Atom;
class Mailbox implements MailboxInterface
{
/**
* The mailbox configuration.
*/
protected array $config = [
'port' => 993,
'host' => '',
'timeout' => 30,
'debug' => false,
'username' => '',
'password' => '',
'encryption' => 'ssl',
'validate_cert' => true,
'authentication' => 'plain',
'proxy' => [
'socket' => null,
'username' => null,
'password' => null,
'request_fulluri' => false,
],
];
/**
* The currently selected folder.
*/
protected ?FolderInterface $selected = null;
/**
* The mailbox connection.
*/
protected ?ConnectionInterface $connection = null;
/**
* Constructor.
*/
public function __construct(array $config = [])
{
$this->config = array_merge($this->config, $config);
}
/**
* Prepare the cloned instance.
*/
public function __clone(): void
{
$this->connection = null;
}
/**
* Make a new mailbox instance.
*/
public static function make(array $config = []): static
{
return new static($config);
}
/**
* {@inheritDoc}
*/
public function config(?string $key = null, mixed $default = null): mixed
{
if (is_null($key)) {
return $this->config;
}
return data_get($this->config, $key, $default);
}
/**
* {@inheritDoc}
*/
public function connection(): ConnectionInterface
{
if (! $this->connection) {
$this->connect();
}
return $this->connection;
}
/**
* {@inheritDoc}
*/
public function connected(): bool
{
return (bool) $this->connection?->connected();
}
/**
* {@inheritDoc}
*/
public function reconnect(): void
{
$this->disconnect();
$this->connect();
}
/**
* {@inheritDoc}
*/
public function connect(?ConnectionInterface $connection = null): void
{
if ($this->connected()) {
return;
}
$debug = $this->config('debug');
$this->connection = $connection ?? new ImapConnection(new ImapStream, match (true) {
class_exists($debug) => new $debug,
is_string($debug) => new FileLogger($debug),
is_bool($debug) && $debug => new EchoLogger,
default => null,
});
$this->connection->connect($this->config('host'), $this->config('port'), [
'proxy' => $this->config('proxy'),
'debug' => $this->config('debug'),
'timeout' => $this->config('timeout'),
'encryption' => $this->config('encryption'),
'validate_cert' => $this->config('validate_cert'),
]);
$this->authenticate();
}
/**
* Authenticate the current session.
*/
protected function authenticate(): void
{
if ($this->config('authentication') === 'oauth') {
$this->connection->authenticate(
$this->config('username'),
$this->config('password')
);
} else {
$this->connection->login(
$this->config('username'),
$this->config('password'),
);
}
}
/**
* {@inheritDoc}
*/
public function disconnect(): void
{
if ($this->connected()) {
$this->connection->logout();
$this->connection->disconnect();
}
$this->connection = null;
}
/**
* {@inheritDoc}
*/
public function inbox(): FolderInterface
{
// "INBOX" is a special name reserved for the user's primary mailbox.
// See: https://datatracker.ietf.org/doc/html/rfc9051#section-5.1
return $this->folders()->find('INBOX');
}
/**
* {@inheritDoc}
*/
public function folders(): FolderRepositoryInterface
{
// Ensure the connection is established.
$this->connection();
return new FolderRepository($this);
}
/**
* {@inheritDoc}
*/
public function capabilities(): array
{
return array_map(
fn (Atom $token) => $token->value,
$this->connection()->capability()->tokensAfter(1)
);
}
/**
* {@inheritDoc}
*/
public function select(FolderInterface $folder, bool $force = false): void
{
if (! $this->selected($folder) || $force) {
$this->connection()->select($folder->path());
}
$this->selected = $folder;
}
/**
* {@inheritDoc}
*/
public function selected(FolderInterface $folder): bool
{
return $this->selected?->is($folder) ?? false;
}
}