Skip to content

Commit 0492379

Browse files
committed
Attachment name and filename parsing fixed and improved to support more formats
1 parent 8e0bec8 commit 0492379

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

src/Attachment.php

+47-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
* @property string content_type
3030
* @property string id
3131
* @property string name
32-
* @property string disposition
32+
* @property string description
33+
* @property string filename
34+
* @property ?string disposition
3335
* @property string img_src
3436
*
3537
* @method integer getPartNumber()
@@ -78,6 +80,8 @@ class Attachment {
7880
'content_type' => null,
7981
'id' => null,
8082
'name' => null,
83+
'filename' => null,
84+
'description' => null,
8185
'disposition' => null,
8286
'img_src' => null,
8387
'size' => null,
@@ -209,21 +213,31 @@ protected function fetch(): void {
209213
$this->disposition = $this->part->disposition;
210214

211215
if (($filename = $this->part->filename) !== null) {
212-
$this->setName($filename);
213-
} elseif (($name = $this->part->name) !== null) {
214-
$this->setName($name);
215-
}else {
216-
$this->setName("undefined");
216+
$this->filename = $this->decodeName($filename);
217+
}
218+
219+
if (($name = $this->part->name) !== null) {
220+
$this->name = $this->decodeName($name);
221+
}
222+
if (!$this->name && $this->filename != "") {
223+
$this->name = $this->filename;
217224
}
218225

219226
if (IMAP::ATTACHMENT_TYPE_MESSAGE == $this->part->type) {
220227
if ($this->part->ifdescription) {
221-
$this->setName($this->part->description);
222-
} else {
223-
$this->setName($this->part->subtype);
228+
if (!$this->name) {
229+
$this->name = $this->part->description;
230+
}
231+
$this->description = $this->part->description;
232+
} else if (!$this->name) {
233+
$this->name = $this->part->subtype;
224234
}
225235
}
226236

237+
if (!$this->filename) {
238+
$this->filename = $this->name;
239+
}
240+
227241
$this->attributes = array_merge($this->part->getHeader()->getAttributes(), $this->attributes);
228242
}
229243

@@ -241,18 +255,36 @@ public function save(string $path, string $filename = null): bool {
241255
}
242256

243257
/**
244-
* Set the attachment name and try to decode it
258+
* Decode a given name
245259
* @param $name
260+
*
261+
* @return string
246262
*/
247-
public function setName($name): void {
248-
$decoder = $this->config['decoder']['attachment'];
263+
public function decodeName($name): string {
249264
if ($name !== null) {
265+
if (str_contains($name, "''")) {
266+
$parts = explode("''", $name);
267+
if (EncodingAliases::has($parts[0])) {
268+
$name = implode("''", array_slice($parts, 1));
269+
}
270+
}
271+
272+
$decoder = $this->config['decoder']['message'];
250273
if($decoder === 'utf-8' && extension_loaded('imap')) {
251-
$this->name = \imap_utf8($name);
252-
}else{
253-
$this->name = mb_decode_mimeheader($name);
274+
$name = \imap_utf8($name);
275+
}
276+
277+
if (preg_match('/=\?([^?]+)\?(Q|B)\?(.+)\?=/i', $name, $matches)) {
278+
$name = $this->part->getHeader()->decode($name);
279+
}
280+
281+
// check if $name is url encoded
282+
if (preg_match('/%[0-9A-F]{2}/i', $name)) {
283+
$name = urldecode($name);
254284
}
285+
return $name;
255286
}
287+
return "";
256288
}
257289

258290
/**

0 commit comments

Comments
 (0)