| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
abstract class Protocol implements ProtocolInterface { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
protected int $connection_timeout = 30; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
protected bool $debug = false; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected bool $enable_uid_cache = true; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public $stream = false; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected Config $config; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected string $encryption = ""; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected bool $cert_validation = true; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
protected array $proxy = [ |
| 68 |
'socket' => null, |
| 69 |
'request_fulluri' => false, |
| 70 |
'username' => null, |
| 71 |
'password' => null, |
| 72 |
]; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
protected array $ssl_options = []; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected array $uid_cache = []; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getCryptoMethod(): int { |
| 96 |
|
| 97 |
$cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT; |
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function enableCertValidation(): Protocol { |
| 116 |
$this->cert_validation = true; |
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function disableCertValidation(): Protocol { |
| 125 |
$this->cert_validation = false; |
| 126 |
return $this; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function setCertValidation(int $cert_validation): Protocol { |
| 136 |
$this->cert_validation = $cert_validation; |
| 137 |
return $this; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function getCertValidation(): bool { |
| 146 |
return $this->cert_validation; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function getProxy(): array { |
| 171 |
return $this->proxy; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function setSslOptions(array $options): Protocol |
| 181 |
{ |
| 182 |
$this->ssl_options = $options; |
| 183 |
|
| 184 |
return $this; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function getSslOptions(): array { |
| 193 |
return $this->ssl_options; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 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 |
|
| 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 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 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 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public function getConnectionTimeout(): int { |
| 266 |
return $this->connection_timeout; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function setConnectionTimeout(int $connection_timeout): Protocol { |
| 276 |
$this->connection_timeout = $connection_timeout; |
| 277 |
return $this; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 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 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public function buildUIDCommand(string $command, int|string $uid): string { |
| 305 |
return trim($this->getUIDKey($uid)." ".$command); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 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 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function enableUidCache(): void { |
| 335 |
$this->enable_uid_cache = true; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public function disableUidCache(): void { |
| 344 |
$this->enable_uid_cache = false; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function setEncryption(string $encryption): void { |
| 354 |
$this->encryption = $encryption; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function getEncryption(): string { |
| 362 |
return $this->encryption; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public function connected(): bool { |
| 371 |
return (bool)$this->stream; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 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 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public function getStream(): mixed { |
| 406 |
return $this->stream; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public function getConfig(): Config { |
| 415 |
return $this->config; |
| 416 |
} |
| 417 |
} |