1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rdx\imap; |
4
|
|
|
|
5
|
|
|
class IMAPMessagePart implements IMAPMessagePartInterface { |
6
|
|
|
|
7
|
|
|
public $section = []; |
8
|
|
|
public $subtype = ''; |
9
|
|
|
|
10
|
|
|
public $parts = array(); |
11
|
|
|
public $parameters = array(); |
12
|
|
|
|
13
|
|
|
public $message; // typeof IMAPMessage |
14
|
|
|
public $structure; // typeof stdClass |
15
|
|
|
public $skippedParts = []; // Array<stdClass> |
16
|
|
|
|
17
|
|
|
public function __construct( IMAPMessage $message, $structure, array $section ) { |
18
|
|
|
$this->message = $message; |
19
|
|
|
$this->structure = $structure; |
20
|
|
|
$this->section = $section; |
21
|
|
|
$this->subtype = strtoupper($structure->subtype); |
22
|
|
|
|
23
|
|
|
if ( !empty($structure->parts) ) { |
24
|
|
|
$parts = $structure->parts; |
25
|
|
|
|
26
|
|
|
while ( count($parts) == 1 && empty($parts[0]->bytes) && !empty($parts[0]->parts) ) { |
27
|
|
|
$this->skippedParts[] = $parts[0]; |
28
|
|
|
$parts = $parts[0]->parts; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
foreach ( $parts as $n => $part ) { |
32
|
|
|
$this->parts[] = new IMAPMessagePart( |
33
|
|
|
$this->message, |
34
|
|
|
$part, |
35
|
|
|
array_merge($this->section, [$n+1]) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function parts() { |
42
|
|
|
return $this->parts; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function allParts( $withContainers = false ) { |
|
|
|
|
46
|
|
|
$parts = []; |
47
|
|
|
$iterate = function($message) use (&$iterate, &$parts, $withContainers) { |
48
|
|
|
foreach ( $message->parts() as $part ) { |
49
|
|
|
if ( $part->parts() ) { |
50
|
|
|
if ( $withContainers ) { |
51
|
|
|
$parts[] = $part; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$iterate($part); |
55
|
|
|
} |
56
|
|
|
else { |
57
|
|
|
$parts[] = $part; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
$iterate($this); |
63
|
|
|
return $parts; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function part( $index ) { |
67
|
|
|
$parts = $this->parts(); |
68
|
|
|
return @$parts[$index]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function structure() { |
72
|
|
|
return $this->structure; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function parameters() { |
|
|
|
|
76
|
|
|
if ( !$this->parameters ) { |
|
|
|
|
77
|
|
|
$structure = $this->structure(); |
78
|
|
|
|
79
|
|
|
$this->parameters['bytes'] = @$structure->bytes; |
80
|
|
|
|
81
|
|
|
foreach ((array) @$structure->parameters as $param) { |
82
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
83
|
|
|
} |
84
|
|
|
foreach ((array) @$structure->dparameters as $param) { |
85
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->parameters; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function parameter( $name ) { |
93
|
|
|
$parameters = $this->parameters(); |
94
|
|
|
return @$parameters[ strtolower($name) ]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function content() { |
98
|
|
|
$body = imap_fetchbody( |
99
|
|
|
$this->message->mailbox->imap(), |
100
|
|
|
$this->message->msgNumber, |
101
|
|
|
implode('.', $this->section), |
102
|
|
|
FT_PEEK |
103
|
|
|
); |
104
|
|
|
return $this->decode($body); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function decode( $content ) { |
108
|
|
|
return quoted_printable_decode($content); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.