29
29
* @property string content_type
30
30
* @property string id
31
31
* @property string name
32
- * @property string disposition
32
+ * @property string description
33
+ * @property string filename
34
+ * @property ?string disposition
33
35
* @property string img_src
34
36
*
35
37
* @method integer getPartNumber()
@@ -78,6 +80,8 @@ class Attachment {
78
80
'content_type ' => null ,
79
81
'id ' => null ,
80
82
'name ' => null ,
83
+ 'filename ' => null ,
84
+ 'description ' => null ,
81
85
'disposition ' => null ,
82
86
'img_src ' => null ,
83
87
'size ' => null ,
@@ -209,21 +213,31 @@ protected function fetch(): void {
209
213
$ this ->disposition = $ this ->part ->disposition ;
210
214
211
215
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 ;
217
224
}
218
225
219
226
if (IMAP ::ATTACHMENT_TYPE_MESSAGE == $ this ->part ->type ) {
220
227
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 ;
224
234
}
225
235
}
226
236
237
+ if (!$ this ->filename ) {
238
+ $ this ->filename = $ this ->name ;
239
+ }
240
+
227
241
$ this ->attributes = array_merge ($ this ->part ->getHeader ()->getAttributes (), $ this ->attributes );
228
242
}
229
243
@@ -241,18 +255,36 @@ public function save(string $path, string $filename = null): bool {
241
255
}
242
256
243
257
/**
244
- * Set the attachment name and try to decode it
258
+ * Decode a given name
245
259
* @param $name
260
+ *
261
+ * @return string
246
262
*/
247
- public function setName ($ name ): void {
248
- $ decoder = $ this ->config ['decoder ' ]['attachment ' ];
263
+ public function decodeName ($ name ): string {
249
264
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 ' ];
250
273
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 );
254
284
}
285
+ return $ name ;
255
286
}
287
+ return "" ;
256
288
}
257
289
258
290
/**
0 commit comments