| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP; |
| 14 |
|
| 15 |
use Illuminate\Support\Str; |
| 16 |
use Webklex\PHPIMAP\Decoder\DecoderInterface; |
| 17 |
use Webklex\PHPIMAP\Exceptions\DecoderNotFoundException; |
| 18 |
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException; |
| 19 |
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException; |
| 20 |
use Webklex\PHPIMAP\Support\Masks\AttachmentMask; |
| 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 |
class Attachment { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
protected Message $message; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected Config $config; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
protected array $options = []; |
| 79 |
|
| 80 |
|
| 81 |
protected Part $part; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected DecoderInterface $decoder; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
protected array $attributes = [ |
| 96 |
'content' => null, |
| 97 |
'hash' => null, |
| 98 |
'type' => null, |
| 99 |
'part_number' => 0, |
| 100 |
'content_type' => null, |
| 101 |
'id' => null, |
| 102 |
'name' => null, |
| 103 |
'filename' => null, |
| 104 |
'description' => null, |
| 105 |
'disposition' => null, |
| 106 |
'img_src' => null, |
| 107 |
'size' => null, |
| 108 |
]; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected string $mask = AttachmentMask::class; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function __construct(Message $message, Part $part) { |
| 124 |
$this->message = $message; |
| 125 |
$this->config = $this->message->getConfig(); |
| 126 |
$this->options = $this->config->get('options'); |
| 127 |
$this->decoder = $this->config->getDecoder("attachment"); |
| 128 |
|
| 129 |
$this->part = $part; |
| 130 |
$this->part_number = $part->part_number; |
| 131 |
|
| 132 |
if ($this->message->getClient()) { |
| 133 |
$default_mask = $this->message->getClient()?->getDefaultAttachmentMask(); |
| 134 |
if ($default_mask != null) { |
| 135 |
$this->mask = $default_mask; |
| 136 |
} |
| 137 |
} else { |
| 138 |
$default_mask = $this->config->getMask("attachment"); |
| 139 |
if ($default_mask != "") { |
| 140 |
$this->mask = $default_mask; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
$this->findType(); |
| 145 |
$this->fetch(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function __call(string $method, array $arguments) { |
| 157 |
if (strtolower(substr($method, 0, 3)) === 'get') { |
| 158 |
$name = Str::snake(substr($method, 3)); |
| 159 |
|
| 160 |
if (isset($this->attributes[$name])) { |
| 161 |
return $this->attributes[$name]; |
| 162 |
} |
| 163 |
|
| 164 |
return null; |
| 165 |
} elseif (strtolower(substr($method, 0, 3)) === 'set') { |
| 166 |
$name = Str::snake(substr($method, 3)); |
| 167 |
|
| 168 |
$this->attributes[$name] = array_pop($arguments); |
| 169 |
|
| 170 |
return $this->attributes[$name]; |
| 171 |
} |
| 172 |
|
| 173 |
throw new MethodNotFoundException("Method " . self::class . '::' . $method . '() is not supported'); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function __set($name, $value) { |
| 184 |
$this->attributes[$name] = $value; |
| 185 |
|
| 186 |
return $this->attributes[$name]; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function __get($name) { |
| 196 |
if (isset($this->attributes[$name])) { |
| 197 |
return $this->attributes[$name]; |
| 198 |
} |
| 199 |
|
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
protected function findType(): void { |
| 207 |
$this->type = match ($this->part->type) { |
| 208 |
IMAP::ATTACHMENT_TYPE_MESSAGE => 'message', |
| 209 |
IMAP::ATTACHMENT_TYPE_APPLICATION => 'application', |
| 210 |
IMAP::ATTACHMENT_TYPE_AUDIO => 'audio', |
| 211 |
IMAP::ATTACHMENT_TYPE_IMAGE => 'image', |
| 212 |
IMAP::ATTACHMENT_TYPE_VIDEO => 'video', |
| 213 |
IMAP::ATTACHMENT_TYPE_MODEL => 'model', |
| 214 |
IMAP::ATTACHMENT_TYPE_TEXT => 'text', |
| 215 |
IMAP::ATTACHMENT_TYPE_MULTIPART => 'multipart', |
| 216 |
default => 'other', |
| 217 |
}; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
protected function fetch(): void { |
| 224 |
$content = $this->part->content; |
| 225 |
|
| 226 |
$this->content_type = $this->part->content_type; |
| 227 |
$this->content = $this->decoder->decode($content, $this->part->encoding); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
$this->hash = hash("crc32c", $this->part->getHeader()->raw."\r\n\r\n".$this->part->content); |
| 244 |
|
| 245 |
if (($id = $this->part->id) !== null) { |
| 246 |
$this->id = str_replace(['<', '>'], '', $id); |
| 247 |
}else { |
| 248 |
$this->id = $this->hash; |
| 249 |
} |
| 250 |
|
| 251 |
$this->size = $this->part->bytes; |
| 252 |
$this->disposition = $this->part->disposition; |
| 253 |
|
| 254 |
if (($filename = $this->part->filename) !== null) { |
| 255 |
$this->filename = $this->decodeName($filename); |
| 256 |
} |
| 257 |
|
| 258 |
if (($description = $this->part->description) !== null) { |
| 259 |
$this->description = $this->part->getHeader()->getDecoder()->decode($description); |
| 260 |
} |
| 261 |
|
| 262 |
if (($name = $this->part->name) !== null) { |
| 263 |
$this->name = $this->decodeName($name); |
| 264 |
} |
| 265 |
|
| 266 |
if (IMAP::ATTACHMENT_TYPE_MESSAGE == $this->part->type) { |
| 267 |
if ($this->part->ifdescription) { |
| 268 |
if (!$this->name) { |
| 269 |
$this->name = $this->part->description; |
| 270 |
} |
| 271 |
} else if (!$this->name) { |
| 272 |
$this->name = $this->part->subtype; |
| 273 |
} |
| 274 |
} |
| 275 |
$this->attributes = array_merge($this->part->getHeader()->getAttributes(), $this->attributes); |
| 276 |
|
| 277 |
if (!$this->filename) { |
| 278 |
$this->filename = $this->hash; |
| 279 |
} |
| 280 |
|
| 281 |
if (!$this->name && $this->filename != "") { |
| 282 |
$this->name = $this->filename; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public function save(string $path, ?string $filename = null): bool { |
| 294 |
$filename = $filename ? $this->decodeName($filename) : $this->filename; |
| 295 |
|
| 296 |
return file_put_contents($path . DIRECTORY_SEPARATOR . $filename, $this->getContent()) !== false; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public function decodeName(?string $name): string { |
| 306 |
if ($name !== null) { |
| 307 |
if (str_contains($name, "''")) { |
| 308 |
$parts = explode("''", $name); |
| 309 |
if (EncodingAliases::has($parts[0])) { |
| 310 |
$encoding = $parts[0]; |
| 311 |
$name = implode("''", array_slice($parts, 1)); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
$decoder = $this->decoder->getOptions()['message']; |
| 316 |
if (preg_match('/=\?([^?]+)\?(Q|B)\?(.+)\?=/i', $name, $matches)) { |
| 317 |
$name = $this->part->getHeader()->getDecoder()->decode($name); |
| 318 |
} elseif ($decoder === 'utf-8' && extension_loaded('imap')) { |
| 319 |
$name = \imap_utf8($name); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
if (preg_match('/%[0-9A-F]{2}/i', $name)) { |
| 324 |
$name = urldecode($name); |
| 325 |
} |
| 326 |
|
| 327 |
if (isset($encoding)) { |
| 328 |
$name = EncodingAliases::convert($name, $encoding); |
| 329 |
} |
| 330 |
|
| 331 |
if($this->config->get('security.sanitize_filenames', true)) { |
| 332 |
$name = $this->sanitizeName($name); |
| 333 |
} |
| 334 |
|
| 335 |
return $name; |
| 336 |
} |
| 337 |
return ""; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function getMimeType(): ?string { |
| 346 |
return (new \finfo())->buffer($this->getContent(), FILEINFO_MIME_TYPE); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public function getExtension(): ?string { |
| 355 |
$extension = null; |
| 356 |
$guesser = "\Symfony\Component\Mime\MimeTypes"; |
| 357 |
if (class_exists($guesser) !== false) { |
| 358 |
|
| 359 |
$extensions = $guesser::getDefault()->getExtensions($this->getMimeType()); |
| 360 |
$extension = $extensions[0] ?? null; |
| 361 |
} |
| 362 |
if ($extension === null) { |
| 363 |
$deprecated_guesser = "\Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser"; |
| 364 |
if (class_exists($deprecated_guesser) !== false) { |
| 365 |
|
| 366 |
$extension = $deprecated_guesser::getInstance()->guess($this->getMimeType()); |
| 367 |
} |
| 368 |
} |
| 369 |
if ($extension === null) { |
| 370 |
$parts = explode(".", $this->filename); |
| 371 |
$extension = count($parts) > 1 ? end($parts) : null; |
| 372 |
} |
| 373 |
if ($extension === null) { |
| 374 |
$parts = explode(".", $this->name); |
| 375 |
$extension = count($parts) > 1 ? end($parts) : null; |
| 376 |
} |
| 377 |
return $extension; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function getAttributes(): array { |
| 386 |
return $this->attributes; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public function getMessage(): Message { |
| 393 |
return $this->message; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public function setMask($mask): Attachment { |
| 403 |
if (class_exists($mask)) { |
| 404 |
$this->mask = $mask; |
| 405 |
} |
| 406 |
|
| 407 |
return $this; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public function getMask(): string { |
| 416 |
return $this->mask; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public function getOptions(): array { |
| 424 |
return $this->options; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public function setOptions(array $options): Attachment { |
| 434 |
$this->options = $options; |
| 435 |
return $this; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function getConfig(): Config { |
| 444 |
return $this->config; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public function setConfig(Config $config): Attachment { |
| 454 |
$this->config = $config; |
| 455 |
return $this; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public function mask(?string $mask = null): mixed { |
| 466 |
$mask = $mask !== null ? $mask : $this->mask; |
| 467 |
if (class_exists($mask)) { |
| 468 |
return new $mask($this); |
| 469 |
} |
| 470 |
|
| 471 |
throw new MaskNotFoundException("Unknown mask provided: " . $mask); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public function getDecoder(): DecoderInterface { |
| 480 |
return $this->decoder; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public function setDecoder(DecoderInterface $decoder): static { |
| 490 |
$this->decoder = $decoder; |
| 491 |
return $this; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
private function sanitizeName(string $name): string { |
| 503 |
$replaces = [ |
| 504 |
'/\\\\/' => '', |
| 505 |
'/[\/\0:]+/' => '', |
| 506 |
'/\.+/' => '.', |
| 507 |
]; |
| 508 |
$name_starts_with_dots = str_starts_with($name, '..'); |
| 509 |
$name = preg_replace(array_keys($replaces), array_values($replaces), $name); |
| 510 |
if($name_starts_with_dots) { |
| 511 |
return substr($name, 1); |
| 512 |
} |
| 513 |
return $name; |
| 514 |
} |
| 515 |
} |