Skip to content

Commit 45520dc

Browse files
committed
Attachments isolated & Collections moved to Support
1 parent f07c6cd commit 45520dc

File tree

7 files changed

+286
-88
lines changed

7 files changed

+286
-88
lines changed

src/IMAP/Attachment.php

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
/*
3+
* File: Attachment.php
4+
* Category: -
5+
* Author: M. Goldenbaum
6+
* Created: 16.03.18 19:37
7+
* Updated: -
8+
*
9+
* Description:
10+
* -
11+
*/
12+
13+
namespace Webklex\IMAP;
14+
15+
/**
16+
* Class Attachment
17+
*
18+
* @package Webklex\IMAP
19+
*/
20+
class Attachment {
21+
22+
/** @var Message $oMessage */
23+
protected $oMessage;
24+
25+
/** @var object $structure */
26+
protected $structure;
27+
28+
/** @var int $part_number */
29+
protected $part_number = 1;
30+
31+
/** @var null|string $content */
32+
public $content = null;
33+
34+
/** @var null|string $type */
35+
public $type = null;
36+
37+
/** @var null|string $content_type */
38+
public $content_type = null;
39+
40+
/** @var null|string $id */
41+
public $id = null;
42+
43+
/** @var null|string $name */
44+
public $name = null;
45+
46+
/** @var null|string $disposition */
47+
public $disposition = null;
48+
49+
/** @var null|string $img_src */
50+
public $img_src = null;
51+
52+
53+
/**
54+
* Attachment const
55+
*
56+
* @const integer TYPE_TEXT
57+
* @const integer TYPE_MULTIPART
58+
* @const integer TYPE_MESSAGE
59+
* @const integer TYPE_APPLICATION
60+
* @const integer TYPE_AUDIO
61+
* @const integer TYPE_IMAGE
62+
* @const integer TYPE_VIDEO
63+
* @const integer TYPE_MODEL
64+
* @const integer TYPE_OTHER
65+
*/
66+
const TYPE_TEXT = 0;
67+
const TYPE_MULTIPART = 1;
68+
const TYPE_MESSAGE = 2;
69+
const TYPE_APPLICATION = 3;
70+
const TYPE_AUDIO = 4;
71+
const TYPE_IMAGE = 5;
72+
const TYPE_VIDEO = 6;
73+
const TYPE_MODEL = 7;
74+
const TYPE_OTHER = 8;
75+
76+
/**
77+
* Attachment constructor.
78+
*
79+
* @param Message $oMessage
80+
* @param object $structure
81+
* @param integer $part_number
82+
*/
83+
public function __construct(Message $oMessage, $structure, $part_number = 1) {
84+
$this->oMessage = $oMessage;
85+
$this->structure = $structure;
86+
$this->part_number = ($part_number) ? $part_number : $this->part_number;
87+
88+
$this->findType();
89+
$this->fetch();
90+
}
91+
92+
/**
93+
* Determine the structure type
94+
*/
95+
protected function findType(){
96+
switch ($this->structure->type) {
97+
case self::TYPE_MESSAGE:
98+
$this->type = 'message';
99+
break;
100+
case self::TYPE_APPLICATION:
101+
$this->type = 'application';
102+
break;
103+
case self::TYPE_AUDIO:
104+
$this->type = 'audio';
105+
break;
106+
case self::TYPE_IMAGE:
107+
$this->type = 'image';
108+
break;
109+
case self::TYPE_VIDEO:
110+
$this->type = 'video';
111+
break;
112+
case self::TYPE_MODEL:
113+
$this->type = 'model';
114+
break;
115+
case self::TYPE_OTHER:
116+
$this->type = 'other';
117+
break;
118+
default:
119+
$this->type = 'other';
120+
break;
121+
}
122+
}
123+
124+
/**
125+
* Fetch the given attachment
126+
*/
127+
protected function fetch(){
128+
129+
$content = imap_fetchbody($this->oMessage->getClient()->getConnection(), $this->oMessage->getUid(), $this->part_number, $this->oMessage->getFetchOptions());
130+
131+
$this->content_type = $this->type.'/'.strtolower($this->structure->subtype);
132+
$this->content = $this->oMessage->decodeString($content, $this->structure->encoding);
133+
134+
if (property_exists($this->structure, 'id')) {
135+
$this->id = str_replace(['<', '>'], '', $this->structure->id);
136+
}
137+
138+
if (property_exists($this->structure, 'dparameters')) {
139+
foreach ($this->structure->dparameters as $parameter) {
140+
if (strtolower($parameter->attribute) == "filename") {
141+
$this->name = $parameter->value;
142+
$this->disposition = property_exists($this->structure, 'disposition') ? $this->structure->disposition : null;
143+
break;
144+
}
145+
}
146+
}
147+
if(self::TYPE_MESSAGE == $this->structure->type) {
148+
if($this->structure->ifdescription) {
149+
$this->name = $this->structure->description;
150+
} else {
151+
$this->name = $this->structure->subtype;
152+
}
153+
}
154+
155+
if (!$this->name && property_exists($this->structure, 'parameters')) {
156+
foreach ($this->structure->parameters as $parameter) {
157+
if (strtolower($parameter->attribute) == "name") {
158+
$this->name = $parameter->value;
159+
$this->disposition = property_exists($this->structure, 'disposition') ? $this->structure->disposition : null;
160+
break;
161+
}
162+
}
163+
}
164+
165+
if ($this->type == 'image') {
166+
$this->img_src = 'data:'.$this->content_type.';base64,'.base64_encode($this->content);
167+
}
168+
}
169+
170+
/**
171+
* @return null|string
172+
*/
173+
public function getContent(){
174+
return $this->content;
175+
}
176+
177+
/**
178+
* @return null|string
179+
*/
180+
public function getType(){
181+
return $this->type;
182+
}
183+
184+
/**
185+
* @return null|string
186+
*/
187+
public function getContentType(){
188+
return $this->content_type;
189+
}
190+
191+
/**
192+
* @return null|string
193+
*/
194+
public function getId(){
195+
return $this->id;
196+
}
197+
198+
/**
199+
* @return null|string
200+
*/
201+
public function getName(){
202+
return $this->name;
203+
}
204+
205+
/**
206+
* @return null|string
207+
*/
208+
public function getDisposition(){
209+
return $this->disposition;
210+
}
211+
212+
/**
213+
* @return null|string
214+
*/
215+
public function getImgSrc(){
216+
return $this->img_src;
217+
}
218+
}

src/IMAP/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Webklex\IMAP\Exceptions\ConnectionFailedException;
1616
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
1717
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
18+
use Webklex\IMAP\Support\MessageCollection;
1819

1920
/**
2021
* Class Client

src/IMAP/Folder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
*/
1212

1313
namespace Webklex\IMAP;
14+
1415
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
1516
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
17+
use Webklex\IMAP\Support\MessageCollection;
1618

1719
/**
1820
* Class Folder

src/IMAP/Message.php

Lines changed: 16 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Webklex\IMAP;
1414

1515
use Carbon\Carbon;
16+
use Webklex\IMAP\Support\AttachmentCollection;
1617

1718
/**
1819
* Class Message
@@ -91,7 +92,7 @@ class Message {
9192
* Message body components
9293
*
9394
* @var array $bodies
94-
* @var array $attachments
95+
* @var AttachmentCollection|array $attachments
9596
*/
9697
public $bodies = [];
9798
public $attachments = [];
@@ -144,6 +145,8 @@ class Message {
144145
*/
145146
public function __construct($uid, $msglist, Client $client, $fetch_options = null, $parse_body = false) {
146147
$this->setFetchOption($fetch_options);
148+
149+
$this->attachments = AttachmentCollection::make([]);
147150

148151
$this->msglist = $msglist;
149152
$this->client = $client;
@@ -225,11 +228,11 @@ public function getHTMLBody($replaceImages = false) {
225228

226229
$body = $this->bodies['html']->content;
227230
if ($replaceImages) {
228-
foreach ($this->attachments as $attachment) {
229-
if ($attachment->id && isset($attachment->img_src)){
230-
$body = str_replace('cid:'.$attachment->id, $attachment->img_src, $body);
231+
$this->attachments->each(function($oAttachment) use(&$body){
232+
if ($oAttachment->id && isset($oAttachment->img_src)){
233+
$body = str_replace('cid:'.$oAttachment->id, $oAttachment->img_src, $body);
231234
}
232-
}
235+
});
233236
}
234237

235238
return $body;
@@ -439,84 +442,14 @@ private function fetchStructure($structure, $partNumber = null) {
439442
* @param mixed $partNumber
440443
*/
441444
protected function fetchAttachment($structure, $partNumber){
442-
switch ($structure->type) {
443-
case self::TYPE_MESSAGE:
444-
$type = 'message';
445-
break;
446-
case self::TYPE_APPLICATION:
447-
$type = 'application';
448-
break;
449-
case self::TYPE_AUDIO:
450-
$type = 'audio';
451-
break;
452-
case self::TYPE_IMAGE:
453-
$type = 'image';
454-
break;
455-
case self::TYPE_VIDEO:
456-
$type = 'video';
457-
break;
458-
case self::TYPE_MODEL:
459-
$type = 'model';
460-
break;
461-
case self::TYPE_OTHER:
462-
$type = 'other';
463-
break;
464-
default:
465-
$type = 'other';
466-
break;
467-
}
468-
469-
$content = imap_fetchbody($this->client->getConnection(), $this->uid, ($partNumber) ? $partNumber : 1, $this->fetch_options);
470445

471-
$attachment = new \stdClass;
472-
$attachment->type = $type;
473-
$attachment->content_type = $type.'/'.strtolower($structure->subtype);
474-
$attachment->content = $this->decodeString($content, $structure->encoding);
446+
$oAttachment = new Attachment($this, $structure, $partNumber);
475447

476-
$attachment->id = false;
477-
if (property_exists($structure, 'id')) {
478-
$attachment->id = str_replace(['<', '>'], '', $structure->id);
479-
}
480-
481-
$attachment->name = false;
482-
if (property_exists($structure, 'dparameters')) {
483-
foreach ($structure->dparameters as $parameter) {
484-
if (strtolower($parameter->attribute) == "filename") {
485-
$attachment->name = $parameter->value;
486-
$attachment->disposition = property_exists($structure, 'disposition') ? $structure->disposition : null;
487-
break;
488-
}
489-
}
490-
}
491-
if(self::TYPE_MESSAGE == $structure->type) {
492-
if($structure->ifdescription) {
493-
$attachment->name = $structure->description;
448+
if($oAttachment->getName() != null){
449+
if ($oAttachment->getId() != null) {
450+
$this->attachments->put($oAttachment->getId(), $oAttachment);
494451
} else {
495-
$attachment->name = $structure->subtype;
496-
}
497-
}
498-
499-
if (!$attachment->name && property_exists($structure, 'parameters')) {
500-
foreach ($structure->parameters as $parameter) {
501-
if (strtolower($parameter->attribute) == "name") {
502-
$attachment->name = $parameter->value;
503-
$attachment->disposition = property_exists($structure, 'disposition') ? $structure->disposition : null;
504-
break;
505-
}
506-
}
507-
}
508-
509-
if ($attachment->type == 'image') {
510-
$attachment->img_src = 'data:'.$attachment->content_type.';base64,'.base64_encode($attachment->content);
511-
}
512-
513-
if(property_exists($attachment, 'name')){
514-
if($attachment->name != false){
515-
if ($attachment->id) {
516-
$this->attachments[$attachment->id] = $attachment;
517-
} else {
518-
$this->attachments[] = $attachment;
519-
}
452+
$this->attachments->push($oAttachment);
520453
}
521454
}
522455
}
@@ -547,7 +480,7 @@ public function setFetchOption($option){
547480
*
548481
* @return string
549482
*/
550-
private function decodeString($string, $encoding) {
483+
public function decodeString($string, $encoding) {
551484
switch ($encoding) {
552485
case self::ENC_7BIT:
553486
return $string;
@@ -644,10 +577,10 @@ public function restore(){
644577
/**
645578
* Get all message attachments.
646579
*
647-
* @return \Illuminate\Support\Collection
580+
* @return AttachmentCollection|array
648581
*/
649582
public function getAttachments(){
650-
return collect($this->attachments);
583+
return $this->attachments;
651584
}
652585

653586
/**

0 commit comments

Comments
 (0)