Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Connection/Protocols/Protocol.php

raw symbols SCIP
1 <?php
2 /*
3 * File: ImapProtocol.php
4 * Category: Protocol
5 * Author: M.Goldenbaum
6 * Created: 16.09.20 18:27
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP\Connection\Protocols;
14
15 use Webklex\PHPIMAP\Config;
16 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
17 use Webklex\PHPIMAP\IMAP;
18
19 /**
20 * Class Protocol
21 *
22 * @package Webklex\PHPIMAP\Connection\Protocols
23 */
24 abstract class Protocol implements ProtocolInterface {
25
26 /**
27 * Default connection timeout in seconds
28 */
29 protected int $connection_timeout = 30;
30
31 /**
32 * @var boolean
33 */
34 protected bool $debug = false;
35
36 /**
37 * @var boolean
38 */
39 protected bool $enable_uid_cache = true;
40
41 /**
42 * @var resource|mixed|boolean|null $stream
43 */
44 public $stream = false;
45
46 /**
47 * @var Config $config
48 */
49 protected Config $config;
50
51 /**
52 * Connection encryption method
53 * @var string $encryption
54 */
55 protected string $encryption = "";
56
57 /**
58 * Set to false to ignore SSL certificate validation
59 * @var bool
60 */
61 protected bool $cert_validation = true;
62
63 /**
64 * Proxy settings
65 * @var array
66 */
67 protected array $proxy = [
68 'socket' => null,
69 'request_fulluri' => false,
70 'username' => null,
71 'password' => null,
72 ];
73
74 /**
75 * SSL stream context options
76 *
77 * @see https://www.php.net/manual/en/context.ssl.php for possible options
78 *
79 * @var array
80 */
81 protected array $ssl_options = [];
82
83 /**
84 * Cache for uid of active folder.
85 *
86 * @var array
87 */
88 protected array $uid_cache = [];
89
90 /**
91 * Get an available cryptographic method
92 *
93 * @return int
94 */
95 public function getCryptoMethod(): int {
96 // Allow the best TLS version(s) we can
97 $cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT;
98
99 // PHP 5.6.7 dropped inclusion of TLS 1.1 and 1.2 in STREAM_CRYPTO_METHOD_TLS_CLIENT
100 // so add them back in manually if we can
101 if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
102 $cryptoMethod = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
103 }elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) {
104 $cryptoMethod = STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
105 }
106
107 return $cryptoMethod;
108 }
109
110 /**
111 * Enable SSL certificate validation
112 *
113 * @return Protocol
114 */
115 public function enableCertValidation(): Protocol {
116 $this->cert_validation = true;
117 return $this;
118 }
119
120 /**
121 * Disable SSL certificate validation
122 * @return Protocol
123 */
124 public function disableCertValidation(): Protocol {
125 $this->cert_validation = false;
126 return $this;
127 }
128
129 /**
130 * Set SSL certificate validation
131 * @var int $cert_validation
132 *
133 * @return Protocol
134 */
135 public function setCertValidation(int $cert_validation): Protocol {
136 $this->cert_validation = $cert_validation;
137 return $this;
138 }
139
140 /**
141 * Should we validate SSL certificate?
142 *
143 * @return bool
144 */
145 public function getCertValidation(): bool {
146 return $this->cert_validation;
147 }
148
149 /**
150 * Set connection proxy settings
151 * @var array $options
152 *
153 * @return Protocol
154 */
155 public function setProxy(array $options): Protocol {
156 foreach ($this->proxy as $key => $val) {
157 if (isset($options[$key])) {
158 $this->proxy[$key] = $options[$key];
159 }
160 }
161
162 return $this;
163 }
164
165 /**
166 * Get the current proxy settings
167 *
168 * @return array
169 */
170 public function getProxy(): array {
171 return $this->proxy;
172 }
173
174 /**
175 * Set SSL context options settings
176 * @var array $options
177 *
178 * @return Protocol
179 */
180 public function setSslOptions(array $options): Protocol
181 {
182 $this->ssl_options = $options;
183
184 return $this;
185 }
186
187 /**
188 * Get the current SSL context options settings
189 *
190 * @return array
191 */
192 public function getSslOptions(): array {
193 return $this->ssl_options;
194 }
195
196 /**
197 * Prepare socket options
198 * @return array
199 *@var string $transport
200 *
201 */
202 private function defaultSocketOptions(string $transport): array {
203 $options = [];
204 if ($this->encryption) {
205 $options["ssl"] = [
206 'verify_peer_name' => $this->getCertValidation(),
207 'verify_peer' => $this->getCertValidation(),
208 ];
209
210 if (count($this->ssl_options)) {
211 /* Get the ssl context options from the config, but prioritize the 'validate_cert' config over the ssl context options */
212 $options["ssl"] = array_replace($this->ssl_options, $options["ssl"]);
213 }
214 }
215
216 if ($this->proxy["socket"] != null) {
217 $options[$transport]["proxy"] = $this->proxy["socket"];
218 $options[$transport]["request_fulluri"] = $this->proxy["request_fulluri"];
219
220 if ($this->proxy["username"] != null) {
221 $auth = base64_encode($this->proxy["username"].':'.$this->proxy["password"]);
222
223 $options[$transport]["header"] = [
224 "Proxy-Authorization: Basic $auth"
225 ];
226 }
227 }
228
229 return $options;
230 }
231
232 /**
233 * Create a new resource stream
234 * @param $transport
235 * @param string $host hostname or IP address of IMAP server
236 * @param int $port of IMAP server, default is 143 (993 for ssl)
237 * @param int $timeout timeout in seconds for initiating session
238 *
239 * @return resource The socket created.
240 * @throws ConnectionFailedException
241 */
242 public function createStream($transport, string $host, int $port, int $timeout) {
243 $socket = "$transport://$host:$port";
244 $stream = stream_socket_client($socket, $errno, $errstr, $timeout,
245 STREAM_CLIENT_CONNECT,
246 stream_context_create($this->defaultSocketOptions($transport))
247 );
248
249 if (!$stream) {
250 throw new ConnectionFailedException($errstr, $errno);
251 }
252
253 if (false === stream_set_timeout($stream, $timeout)) {
254 throw new ConnectionFailedException('Failed to set stream timeout');
255 }
256
257 return $stream;
258 }
259
260 /**
261 * Get the current connection timeout
262 *
263 * @return int
264 */
265 public function getConnectionTimeout(): int {
266 return $this->connection_timeout;
267 }
268
269 /**
270 * Set the connection timeout
271 * @param int $connection_timeout
272 *
273 * @return Protocol
274 */
275 public function setConnectionTimeout(int $connection_timeout): Protocol {
276 $this->connection_timeout = $connection_timeout;
277 return $this;
278 }
279
280 /**
281 * Get the UID key string
282 * @param int|string $uid
283 *
284 * @return string
285 */
286 public function getUIDKey(int|string $uid): string {
287 if ($uid == IMAP::ST_UID || $uid == IMAP::FT_UID) {
288 return "UID";
289 }
290 if (strlen($uid) > 0 && !is_numeric($uid)) {
291 return (string)$uid;
292 }
293
294 return "";
295 }
296
297 /**
298 * Build a UID / MSGN command
299 * @param string $command
300 * @param int|string $uid
301 *
302 * @return string
303 */
304 public function buildUIDCommand(string $command, int|string $uid): string {
305 return trim($this->getUIDKey($uid)." ".$command);
306 }
307
308 /**
309 * Set the uid cache of current active folder
310 *
311 * @param array|null $uids
312 */
313 public function setUidCache(?array $uids): void {
314 if (is_null($uids)) {
315 $this->uid_cache = [];
316 return;
317 }
318
319 $messageNumber = 1;
320
321 $uid_cache = [];
322 foreach ($uids as $uid) {
323 $uid_cache[$messageNumber++] = (int)$uid;
324 }
325
326 $this->uid_cache = $uid_cache;
327 }
328
329 /**
330 * Enable the uid cache
331 *
332 * @return void
333 */
334 public function enableUidCache(): void {
335 $this->enable_uid_cache = true;
336 }
337
338 /**
339 * Disable the uid cache
340 *
341 * @return void
342 */
343 public function disableUidCache(): void {
344 $this->enable_uid_cache = false;
345 }
346
347 /**
348 * Set the encryption method
349 * @param string $encryption
350 *
351 * @return void
352 */
353 public function setEncryption(string $encryption): void {
354 $this->encryption = $encryption;
355 }
356
357 /**
358 * Get the encryption method
359 * @return string
360 */
361 public function getEncryption(): string {
362 return $this->encryption;
363 }
364
365 /**
366 * Check if the current session is connected
367 *
368 * @return bool
369 */
370 public function connected(): bool {
371 return (bool)$this->stream;
372 }
373
374 /**
375 * Retrieves header/metadata from the resource stream
376 *
377 * @return array
378 */
379 public function meta(): array {
380 if (!$this->stream) {
381 return [
382 "crypto" => [
383 "protocol" => "",
384 "cipher_name" => "",
385 "cipher_bits" => 0,
386 "cipher_version" => "",
387 ],
388 "timed_out" => true,
389 "blocked" => true,
390 "eof" => true,
391 "stream_type" => "tcp_socket/unknown",
392 "mode" => "c",
393 "unread_bytes" => 0,
394 "seekable" => false,
395 ];
396 }
397 return stream_get_meta_data($this->stream);
398 }
399
400 /**
401 * Get the resource stream
402 *
403 * @return mixed
404 */
405 public function getStream(): mixed {
406 return $this->stream;
407 }
408
409 /**
410 * Set the Config instance
411 *
412 * @return Config
413 */
414 public function getConfig(): Config {
415 return $this->config;
416 }
417 }