forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protocol.php
381 lines (333 loc) · 8.83 KB
/
Protocol.php
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/*
* File: ImapProtocol.php
* Category: Protocol
* Author: M.Goldenbaum
* Created: 16.09.20 18:27
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP\Connection\Protocols;
use Webklex\PHPIMAP\Config;
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
use Webklex\PHPIMAP\IMAP;
/**
* Class Protocol
*
* @package Webklex\PHPIMAP\Connection\Protocols
*/
abstract class Protocol implements ProtocolInterface {
/**
* Default connection timeout in seconds
*/
protected int $connection_timeout = 30;
/**
* @var boolean
*/
protected bool $debug = false;
/**
* @var boolean
*/
protected bool $enable_uid_cache = true;
/**
* @var resource|mixed|boolean|null $stream
*/
public $stream = false;
/**
* @var Config $config
*/
protected Config $config;
/**
* Connection encryption method
* @var string $encryption
*/
protected string $encryption = "";
/**
* Set to false to ignore SSL certificate validation
* @var bool
*/
protected bool $cert_validation = true;
/**
* Proxy settings
* @var array
*/
protected array $proxy = [
'socket' => null,
'request_fulluri' => false,
'username' => null,
'password' => null,
];
/**
* Cache for uid of active folder.
*
* @var array
*/
protected array $uid_cache = [];
/**
* Get an available cryptographic method
*
* @return int
*/
public function getCryptoMethod(): int {
// Allow the best TLS version(s) we can
$cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT;
// PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT
// so add them back in manually if we can
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
$cryptoMethod = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
}elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) {
$cryptoMethod = STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
}
return $cryptoMethod;
}
/**
* Enable SSL certificate validation
*
* @return Protocol
*/
public function enableCertValidation(): Protocol {
$this->cert_validation = true;
return $this;
}
/**
* Disable SSL certificate validation
* @return Protocol
*/
public function disableCertValidation(): Protocol {
$this->cert_validation = false;
return $this;
}
/**
* Set SSL certificate validation
* @var int $cert_validation
*
* @return Protocol
*/
public function setCertValidation(int $cert_validation): Protocol {
$this->cert_validation = $cert_validation;
return $this;
}
/**
* Should we validate SSL certificate?
*
* @return bool
*/
public function getCertValidation(): bool {
return $this->cert_validation;
}
/**
* Set connection proxy settings
* @var array $options
*
* @return Protocol
*/
public function setProxy(array $options): Protocol {
foreach ($this->proxy as $key => $val) {
if (isset($options[$key])) {
$this->proxy[$key] = $options[$key];
}
}
return $this;
}
/**
* Get the current proxy settings
*
* @return array
*/
public function getProxy(): array {
return $this->proxy;
}
/**
* Prepare socket options
* @return array
*@var string $transport
*
*/
private function defaultSocketOptions(string $transport): array {
$options = [];
if ($this->encryption) {
$options["ssl"] = [
'verify_peer_name' => $this->getCertValidation(),
'verify_peer' => $this->getCertValidation(),
];
}
if ($this->proxy["socket"] != null) {
$options[$transport]["proxy"] = $this->proxy["socket"];
$options[$transport]["request_fulluri"] = $this->proxy["request_fulluri"];
if ($this->proxy["username"] != null) {
$auth = base64_encode($this->proxy["username"].':'.$this->proxy["password"]);
$options[$transport]["header"] = [
"Proxy-Authorization: Basic $auth"
];
}
}
return $options;
}
/**
* Create a new resource stream
* @param $transport
* @param string $host hostname or IP address of IMAP server
* @param int $port of IMAP server, default is 143 (993 for ssl)
* @param int $timeout timeout in seconds for initiating session
*
* @return resource The socket created.
* @throws ConnectionFailedException
*/
public function createStream($transport, string $host, int $port, int $timeout) {
$socket = "$transport://$host:$port";
$stream = stream_socket_client($socket, $errno, $errstr, $timeout,
STREAM_CLIENT_CONNECT,
stream_context_create($this->defaultSocketOptions($transport))
);
if (!$stream) {
throw new ConnectionFailedException($errstr, $errno);
}
if (false === stream_set_timeout($stream, $timeout)) {
throw new ConnectionFailedException('Failed to set stream timeout');
}
return $stream;
}
/**
* Get the current connection timeout
*
* @return int
*/
public function getConnectionTimeout(): int {
return $this->connection_timeout;
}
/**
* Set the connection timeout
* @param int $connection_timeout
*
* @return Protocol
*/
public function setConnectionTimeout(int $connection_timeout): Protocol {
$this->connection_timeout = $connection_timeout;
return $this;
}
/**
* Get the UID key string
* @param int|string $uid
*
* @return string
*/
public function getUIDKey(int|string $uid): string {
if ($uid == IMAP::ST_UID || $uid == IMAP::FT_UID) {
return "UID";
}
if (strlen($uid) > 0 && !is_numeric($uid)) {
return (string)$uid;
}
return "";
}
/**
* Build a UID / MSGN command
* @param string $command
* @param int|string $uid
*
* @return string
*/
public function buildUIDCommand(string $command, int|string $uid): string {
return trim($this->getUIDKey($uid)." ".$command);
}
/**
* Set the uid cache of current active folder
*
* @param array|null $uids
*/
public function setUidCache(?array $uids): void {
if (is_null($uids)) {
$this->uid_cache = [];
return;
}
$messageNumber = 1;
$uid_cache = [];
foreach ($uids as $uid) {
$uid_cache[$messageNumber++] = (int)$uid;
}
$this->uid_cache = $uid_cache;
}
/**
* Enable the uid cache
*
* @return void
*/
public function enableUidCache(): void {
$this->enable_uid_cache = true;
}
/**
* Disable the uid cache
*
* @return void
*/
public function disableUidCache(): void {
$this->enable_uid_cache = false;
}
/**
* Set the encryption method
* @param string $encryption
*
* @return void
*/
public function setEncryption(string $encryption): void {
$this->encryption = $encryption;
}
/**
* Get the encryption method
* @return string
*/
public function getEncryption(): string {
return $this->encryption;
}
/**
* Check if the current session is connected
*
* @return bool
*/
public function connected(): bool {
return (bool)$this->stream;
}
/**
* Retrieves header/metadata from the resource stream
*
* @return array
*/
public function meta(): array {
if (!$this->stream) {
return [
"crypto" => [
"protocol" => "",
"cipher_name" => "",
"cipher_bits" => 0,
"cipher_version" => "",
],
"timed_out" => true,
"blocked" => true,
"eof" => true,
"stream_type" => "tcp_socket/unknown",
"mode" => "c",
"unread_bytes" => 0,
"seekable" => false,
];
}
return stream_get_meta_data($this->stream);
}
/**
* Get the resource stream
*
* @return mixed
*/
public function getStream(): mixed {
return $this->stream;
}
/**
* Set the Config instance
*
* @return Config
*/
public function getConfig(): Config {
return $this->config;
}
}