Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Part.php

raw symbols SCIP
1 <?php
2 /*
3 * File: Part.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 17.09.20 20:38
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP;
14
15
16 use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
17
18 /**
19 * Class Part
20 *
21 * @package Webklex\PHPIMAP
22 */
23 class Part {
24
25 /**
26 * Raw part
27 *
28 * @var string $raw
29 */
30 public string $raw = "";
31
32 /**
33 * Part type
34 *
35 * @var int $type
36 */
37 public int $type = IMAP::MESSAGE_TYPE_TEXT;
38
39 /**
40 * Part content
41 *
42 * @var string $content
43 */
44 public string $content = "";
45
46 /**
47 * Part subtype
48 *
49 * @var ?string $subtype
50 */
51 public ?string $subtype = null;
52
53 /**
54 * Part charset - if available
55 *
56 * @var string $charset
57 */
58 public string $charset = "utf-8";
59
60 /**
61 * Part encoding method
62 *
63 * @var int $encoding
64 */
65 public int $encoding = IMAP::MESSAGE_ENC_OTHER;
66
67 /**
68 * Alias to check if the part is an attachment
69 *
70 * @var boolean $ifdisposition
71 */
72 public bool $ifdisposition = false;
73
74 /**
75 * Indicates if the part is an attachment
76 *
77 * @var ?string $disposition
78 */
79 public ?string $disposition = null;
80
81 /**
82 * Alias to check if the part has a description
83 *
84 * @var boolean $ifdescription
85 */
86 public bool $ifdescription = false;
87
88 /**
89 * Part description if available
90 *
91 * @var ?string $description
92 */
93 public ?string $description = null;
94
95 /**
96 * Part filename if available
97 *
98 * @var ?string $filename
99 */
100 public ?string $filename = null;
101
102 /**
103 * Part name if available
104 *
105 * @var ?string $name
106 */
107 public ?string $name = null;
108
109 /**
110 * Part id if available
111 *
112 * @var ?string $id
113 */
114 public ?string $id = null;
115
116 /**
117 * The part number of the current part
118 *
119 * @var integer $part_number
120 */
121 public int $part_number = 0;
122
123 /**
124 * Part length in bytes
125 *
126 * @var integer $bytes
127 */
128 public int $bytes;
129
130 /**
131 * Part content type
132 *
133 * @var string|null $content_type
134 */
135 public ?string $content_type = null;
136
137 /**
138 * @var ?Header $header
139 */
140 private ?Header $header;
141
142 /**
143 * @var Config $config
144 */
145 protected Config $config;
146
147 /**
148 * Part constructor.
149 * @param string $raw_part
150 * @param Config $config
151 * @param Header|null $header
152 * @param integer $part_number
153 *
154 * @throws InvalidMessageDateException
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 * Parse the raw parts
166 *
167 * @throws InvalidMessageDateException
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 * Find all available headers and return the leftover body segment
209 *
210 * @return string
211 * @throws InvalidMessageDateException
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 * Try to parse the subtype if any is present
228 * @param $content_type
229 *
230 * @return ?string
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 * Try to parse the disposition if any is present
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 * Try to parse the description if any is present
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 * Try to parse the encoding if any is present
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 * Check if the current part represents an attachment
288 *
289 * @return bool
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 * Get the part header
308 *
309 * @return Header|null
310 */
311 public function getHeader(): ?Header {
312 return $this->header;
313 }
314
315 /**
316 * Get the Config instance
317 *
318 * @return Config
319 */
320 public function getConfig(): Config {
321 return $this->config;
322 }
323
324 }