| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP; |
| 14 |
|
| 15 |
|
| 16 |
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Part { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public string $raw = ""; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public int $type = IMAP::MESSAGE_TYPE_TEXT; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public string $content = ""; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public ?string $subtype = null; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public string $charset = "utf-8"; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public int $encoding = IMAP::MESSAGE_ENC_OTHER; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public bool $ifdisposition = false; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public ?string $disposition = null; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public bool $ifdescription = false; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public ?string $description = null; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public ?string $filename = null; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public ?string $name = null; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public ?string $id = null; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public int $part_number = 0; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public int $bytes; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public ?string $content_type = null; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private ?Header $header; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
protected Config $config; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function __construct(string $raw_part, Config $config, ?Header $header = null, int $part_number = 0) { |
| 157 |
$this->raw = $raw_part; |
| 158 |
$this->config = $config; |
| 159 |
$this->header = $header; |
| 160 |
$this->part_number = $part_number; |
| 161 |
$this->parse(); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
protected function parse(): void { |
| 170 |
if ($this->header === null) { |
| 171 |
$body = $this->findHeaders(); |
| 172 |
}else{ |
| 173 |
$body = $this->raw; |
| 174 |
} |
| 175 |
|
| 176 |
$this->parseDisposition(); |
| 177 |
$this->parseDescription(); |
| 178 |
$this->parseEncoding(); |
| 179 |
|
| 180 |
$this->charset = $this->header->get("charset")->first(); |
| 181 |
$this->name = $this->header->get("name"); |
| 182 |
$this->filename = $this->header->get("filename"); |
| 183 |
|
| 184 |
if($this->header->get("id")->exist()) { |
| 185 |
$this->id = $this->header->get("id"); |
| 186 |
} else if($this->header->get("x_attachment_id")->exist()){ |
| 187 |
$this->id = $this->header->get("x_attachment_id"); |
| 188 |
} else if($this->header->get("content_id")->exist()){ |
| 189 |
$this->id = strtr($this->header->get("content_id"), [ |
| 190 |
'<' => '', |
| 191 |
'>' => '' |
| 192 |
]); |
| 193 |
} |
| 194 |
|
| 195 |
$content_types = $this->header->get("content_type")->all(); |
| 196 |
if(!empty($content_types)){ |
| 197 |
$this->subtype = $this->parseSubtype($content_types); |
| 198 |
$content_type = $content_types[0]; |
| 199 |
$parts = explode(';', $content_type); |
| 200 |
$this->content_type = trim($parts[0]); |
| 201 |
} |
| 202 |
|
| 203 |
$this->content = trim(rtrim($body)); |
| 204 |
$this->bytes = strlen($this->content); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
private function findHeaders(): string { |
| 214 |
$body = $this->raw; |
| 215 |
while (($pos = strpos($body, "\r\n")) > 0) { |
| 216 |
$body = substr($body, $pos + 2); |
| 217 |
} |
| 218 |
$headers = substr($this->raw, 0, strlen($body) * -1); |
| 219 |
$body = substr($body, 0, -2); |
| 220 |
|
| 221 |
$this->header = new Header($headers, $this->config); |
| 222 |
|
| 223 |
return $body; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private function parseSubtype($content_type): ?string { |
| 233 |
if (is_array($content_type)) { |
| 234 |
foreach ($content_type as $part){ |
| 235 |
if ((strpos($part, "/")) !== false){ |
| 236 |
return $this->parseSubtype($part); |
| 237 |
} |
| 238 |
} |
| 239 |
return null; |
| 240 |
} |
| 241 |
if (($pos = strpos($content_type, "/")) !== false){ |
| 242 |
return substr(explode(";", $content_type)[0], $pos + 1); |
| 243 |
} |
| 244 |
return null; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
private function parseDisposition(): void { |
| 251 |
$content_disposition = $this->header->get("content_disposition")->first(); |
| 252 |
if($content_disposition) { |
| 253 |
$this->ifdisposition = true; |
| 254 |
$this->disposition = (is_array($content_disposition)) ? implode(' ', $content_disposition) : explode(";", $content_disposition)[0]; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
private function parseDescription(): void { |
| 262 |
$content_description = $this->header->get("content_description")->first(); |
| 263 |
if($content_description) { |
| 264 |
$this->ifdescription = true; |
| 265 |
$this->description = $content_description; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
private function parseEncoding(): void { |
| 273 |
$encoding = $this->header->get("content_transfer_encoding")->first(); |
| 274 |
if($encoding) { |
| 275 |
$this->encoding = match (strtolower($encoding)) { |
| 276 |
"quoted-printable" => IMAP::MESSAGE_ENC_QUOTED_PRINTABLE, |
| 277 |
"base64" => IMAP::MESSAGE_ENC_BASE64, |
| 278 |
"7bit" => IMAP::MESSAGE_ENC_7BIT, |
| 279 |
"8bit" => IMAP::MESSAGE_ENC_8BIT, |
| 280 |
"binary" => IMAP::MESSAGE_ENC_BINARY, |
| 281 |
default => IMAP::MESSAGE_ENC_OTHER, |
| 282 |
}; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public function isAttachment(): bool { |
| 292 |
$valid_disposition = in_array(strtolower($this->disposition ?? ''), $this->config->get('options.dispositions')); |
| 293 |
|
| 294 |
if ($this->type == IMAP::MESSAGE_TYPE_TEXT && ($this->ifdisposition == 0 || empty($this->disposition) || !$valid_disposition)) { |
| 295 |
if (($this->subtype == null || in_array((strtolower($this->subtype)), ["plain", "html"])) && $this->filename == null && $this->name == null) { |
| 296 |
return false; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if ($this->disposition === "inline" && $this->filename == null && $this->name == null && !$this->header->has("content_id")) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
return true; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public function getHeader(): ?Header { |
| 312 |
return $this->header; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public function getConfig(): Config { |
| 321 |
return $this->config; |
| 322 |
} |
| 323 |
|
| 324 |
} |