forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttachment.php
executable file
·344 lines (302 loc) · 8.53 KB
/
Attachment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/*
* File: Attachment.php
* Category: -
* Author: M. Goldenbaum
* Created: 16.03.18 19:37
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP;
use Illuminate\Support\Str;
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
use Webklex\PHPIMAP\Support\Masks\AttachmentMask;
/**
* Class Attachment
*
* @package Webklex\PHPIMAP
*
* @property integer part_number
* @property integer size
* @property string content
* @property string type
* @property string content_type
* @property string id
* @property string name
* @property string disposition
* @property string img_src
*
* @method integer getPartNumber()
* @method integer setPartNumber(integer $part_number)
* @method string getContent()
* @method string setContent(string $content)
* @method string getType()
* @method string setType(string $type)
* @method string getContentType()
* @method string setContentType(string $content_type)
* @method string getId()
* @method string setId(string $id)
* @method string getSize()
* @method string setSize(integer $size)
* @method string getName()
* @method string getDisposition()
* @method string setDisposition(string $disposition)
* @method string setImgSrc(string $img_src)
*/
class Attachment {
/**
* @var Message $oMessage
*/
protected $oMessage;
/**
* Used config
*
* @var array $config
*/
protected $config = [];
/** @var Part $part */
protected $part;
/**
* Attribute holder
*
* @var array $attributes
*/
protected $attributes = [
'content' => null,
'type' => null,
'part_number' => 0,
'content_type' => null,
'id' => null,
'name' => null,
'disposition' => null,
'img_src' => null,
'size' => null,
];
/**
* Default mask
*
* @var string $mask
*/
protected $mask = AttachmentMask::class;
/**
* Attachment constructor.
* @param Message $oMessage
* @param Part $part
*/
public function __construct(Message $oMessage, Part $part) {
$this->config = ClientManager::get('options');
$this->oMessage = $oMessage;
$this->part = $part;
$this->part_number = $part->part_number;
$default_mask = $this->oMessage->getClient()->getDefaultAttachmentMask();
if($default_mask != null) {
$this->mask = $default_mask;
}
$this->findType();
$this->fetch();
}
/**
* Call dynamic attribute setter and getter methods
* @param string $method
* @param array $arguments
*
* @return mixed
* @throws MethodNotFoundException
*/
public function __call($method, $arguments) {
if(strtolower(substr($method, 0, 3)) === 'get') {
$name = Str::snake(substr($method, 3));
if(isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}elseif (strtolower(substr($method, 0, 3)) === 'set') {
$name = Str::snake(substr($method, 3));
$this->attributes[$name] = array_pop($arguments);
return $this->attributes[$name];
}
throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
}
/**
* Magic setter
* @param $name
* @param $value
*
* @return mixed
*/
public function __set($name, $value) {
$this->attributes[$name] = $value;
return $this->attributes[$name];
}
/**
* magic getter
* @param $name
*
* @return mixed|null
*/
public function __get($name) {
if(isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
/**
* Determine the structure type
*/
protected function findType() {
switch ($this->part->type) {
case IMAP::ATTACHMENT_TYPE_MESSAGE:
$this->type = 'message';
break;
case IMAP::ATTACHMENT_TYPE_APPLICATION:
$this->type = 'application';
break;
case IMAP::ATTACHMENT_TYPE_AUDIO:
$this->type = 'audio';
break;
case IMAP::ATTACHMENT_TYPE_IMAGE:
$this->type = 'image';
break;
case IMAP::ATTACHMENT_TYPE_VIDEO:
$this->type = 'video';
break;
case IMAP::ATTACHMENT_TYPE_MODEL:
$this->type = 'model';
break;
case IMAP::ATTACHMENT_TYPE_TEXT:
$this->type = 'text';
break;
case IMAP::ATTACHMENT_TYPE_MULTIPART:
$this->type = 'multipart';
break;
default:
$this->type = 'other';
break;
}
}
/**
* Fetch the given attachment
*/
protected function fetch() {
$content = $this->part->content;
$this->content_type = $this->part->content_type;
$this->content = $this->oMessage->decodeString($content, $this->part->encoding);
if (($id = $this->part->id) !== null) {
$this->id = str_replace(['<', '>'], '', $id);
}
$this->size = $this->part->bytes;
$this->disposition = $this->part->disposition;
if (($filename = $this->part->filename) !== null) {
$this->setName($filename);
} elseif (($name = $this->part->name) !== null) {
$this->setName($name);
}else {
$this->setName("undefined");
}
if (IMAP::ATTACHMENT_TYPE_MESSAGE == $this->part->type) {
if ($this->part->ifdescription) {
$this->setName($this->part->description);
} else {
$this->setName($this->part->subtype);
}
}
}
/**
* Save the attachment content to your filesystem
* @param string $path
* @param string|null $filename
*
* @return boolean
*/
public function save($path, $filename = null) {
$filename = $filename ? $filename : $this->getName();
return file_put_contents($path.$filename, $this->getContent()) !== false;
}
/**
* Set the attachment name and try to decode it
* @param $name
*/
public function setName($name) {
$decoder = $this->config['decoder']['attachment'];
if ($name !== null) {
if($decoder === 'utf-8' && extension_loaded('imap')) {
$this->name = \imap_utf8($name);
}else{
$this->name = mb_decode_mimeheader($name);
}
}
}
/**
* Get the attachment mime type
*
* @return string|null
*/
public function getMimeType(){
return (new \finfo())->buffer($this->getContent(), FILEINFO_MIME_TYPE);
}
/**
* Try to guess the attachment file extension
*
* @return string|null
*/
public function getExtension(){
$deprecated_guesser = "\Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser";
if (class_exists($deprecated_guesser) !== false){
return $deprecated_guesser::getInstance()->guess($this->getMimeType());
}
$guesser = "\Symfony\Component\Mime\MimeTypes";
$extensions = $guesser::getDefault()->getExtensions($this->getMimeType());
return isset($extensions[0]) ? $extensions[0] : null;
}
/**
* Get all attributes
*
* @return array
*/
public function getAttributes(){
return $this->attributes;
}
/**
* @return Message
*/
public function getMessage(){
return $this->oMessage;
}
/**
* Set the default mask
* @param $mask
*
* @return $this
*/
public function setMask($mask){
if(class_exists($mask)){
$this->mask = $mask;
}
return $this;
}
/**
* Get the used default mask
*
* @return string
*/
public function getMask(){
return $this->mask;
}
/**
* Get a masked instance by providing a mask name
* @param string|null $mask
*
* @return mixed
* @throws MaskNotFoundException
*/
public function mask($mask = null){
$mask = $mask !== null ? $mask : $this->mask;
if(class_exists($mask)){
return new $mask($this);
}
throw new MaskNotFoundException("Unknown mask provided: ".$mask);
}
}