-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attachment.php
266 lines (228 loc) · 6.76 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
<?php
/*
* File: Attachment.php
* Category: -
* Author: M. Goldenbaum
* Created: 16.03.18 19:37
* Updated: -
*
* Description:
* -
*/
namespace Webklex\IMAP;
use Illuminate\Support\Facades\File;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
/**
* Class Attachment
*
* @package Webklex\IMAP
*/
class Attachment {
/** @var Message $oMessage */
protected $oMessage;
/** @var object $structure */
protected $structure;
/** @var int $part_number */
protected $part_number = 1;
/** @var null|string $content */
public $content = null;
/** @var null|string $type */
public $type = null;
/** @var null|string $content_type */
public $content_type = null;
/** @var null|string $id */
public $id = null;
/** @var null|string $name */
public $name = null;
/** @var null|string $disposition */
public $disposition = null;
/** @var null|string $img_src */
public $img_src = null;
/**
* Attachment const
*
* @const integer TYPE_TEXT
* @const integer TYPE_MULTIPART
* @const integer TYPE_MESSAGE
* @const integer TYPE_APPLICATION
* @const integer TYPE_AUDIO
* @const integer TYPE_IMAGE
* @const integer TYPE_VIDEO
* @const integer TYPE_MODEL
* @const integer TYPE_OTHER
*/
const TYPE_TEXT = 0;
const TYPE_MULTIPART = 1;
const TYPE_MESSAGE = 2;
const TYPE_APPLICATION = 3;
const TYPE_AUDIO = 4;
const TYPE_IMAGE = 5;
const TYPE_VIDEO = 6;
const TYPE_MODEL = 7;
const TYPE_OTHER = 8;
/**
* Attachment constructor.
*
* @param Message $oMessage
* @param object $structure
* @param integer $part_number
*
* @throws Exceptions\ConnectionFailedException
*/
public function __construct(Message $oMessage, $structure, $part_number = 1) {
$this->oMessage = $oMessage;
$this->structure = $structure;
$this->part_number = ($part_number) ? $part_number : $this->part_number;
$this->findType();
$this->fetch();
}
/**
* Determine the structure type
*/
protected function findType() {
switch ($this->structure->type) {
case self::TYPE_MESSAGE:
$this->type = 'message';
break;
case self::TYPE_APPLICATION:
$this->type = 'application';
break;
case self::TYPE_AUDIO:
$this->type = 'audio';
break;
case self::TYPE_IMAGE:
$this->type = 'image';
break;
case self::TYPE_VIDEO:
$this->type = 'video';
break;
case self::TYPE_MODEL:
$this->type = 'model';
break;
case self::TYPE_TEXT:
$this->type = 'text';
break;
case self::TYPE_MULTIPART:
$this->type = 'multipart';
break;
default:
$this->type = 'other';
break;
}
}
/**
* Fetch the given attachment
*
* @throws Exceptions\ConnectionFailedException
*/
protected function fetch() {
$content = imap_fetchbody($this->oMessage->getClient()->getConnection(), $this->oMessage->getUid(), $this->part_number, $this->oMessage->getFetchOptions() | FT_UID);
$this->content_type = $this->type.'/'.strtolower($this->structure->subtype);
$this->content = $this->oMessage->decodeString($content, $this->structure->encoding);
if (property_exists($this->structure, 'id')) {
$this->id = str_replace(['<', '>'], '', $this->structure->id);
}
if (property_exists($this->structure, 'dparameters')) {
foreach ($this->structure->dparameters as $parameter) {
if (strtolower($parameter->attribute) == "filename") {
$this->setName($parameter->value);
$this->disposition = property_exists($this->structure, 'disposition') ? $this->structure->disposition : null;
break;
}
}
}
if (self::TYPE_MESSAGE == $this->structure->type) {
if ($this->structure->ifdescription) {
$this->setName($this->structure->description);
} else {
$this->setName($this->structure->subtype);
}
}
if (!$this->name && property_exists($this->structure, 'parameters')) {
foreach ($this->structure->parameters as $parameter) {
if (strtolower($parameter->attribute) == "name") {
$this->setName($parameter->value);
$this->disposition = property_exists($this->structure, 'disposition') ? $this->structure->disposition : null;
break;
}
}
}
if ($this->type == 'image') {
$this->img_src = 'data:'.$this->content_type.';base64,'.base64_encode($this->content);
}
}
/**
* Save the attachment content to your filesystem
*
* @param string|null $path
* @param string|null $filename
*
* @return boolean
*/
public function save($path = null, $filename = null) {
$path = $path ?: storage_path();
$filename = $filename ?: $this->getName();
$path = substr($path, -1) == DIRECTORY_SEPARATOR ? $path : $path.DIRECTORY_SEPARATOR;
return File::put($path.$filename, $this->getContent()) !== false;
}
/**
* @return null|string
*/
public function getContent() {
return $this->content;
}
/**
* @return null|string
*/
public function getType() {
return $this->type;
}
/**
* @return null|string
*/
public function getContentType() {
return $this->content_type;
}
/**
* @return null|string
*/
public function getId() {
return $this->id;
}
/**
* @param $name
*/
public function setName($name) {
$this->name = mb_decode_mimeheader($name);
}
/**
* @return null|string
*/
public function getName() {
return $this->name;
}
/**
* @return null|string
*/
public function getDisposition() {
return $this->disposition;
}
/**
* @return null|string
*/
public function getImgSrc() {
return $this->img_src;
}
/**
* @return string|null
*/
public function getMimeType(){
return (new \finfo())->buffer($this->getContent(), FILEINFO_MIME_TYPE);
}
/**
* @return string|null
*/
public function getExtension(){
return ExtensionGuesser::getInstance()->guess($this->getMimeType());
}
}