|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rdx\imap; |
|
4
|
|
|
|
|
5
|
|
|
abstract class IMAPMessageContent implements IMAPMessagePartInterface { |
|
6
|
|
|
|
|
7
|
|
|
/** @var object */ |
|
8
|
|
|
protected $structure; |
|
9
|
|
|
|
|
10
|
|
|
/** @var IMAPMessagePart[] */ |
|
11
|
|
|
protected $parts = []; |
|
12
|
|
|
|
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
protected $parameters = []; |
|
15
|
|
|
|
|
16
|
|
|
/** @return IMAPMessagePartInterface[] */ |
|
17
|
|
|
abstract public function parts(); |
|
18
|
|
|
|
|
19
|
|
|
/** @return IMAPMessagePartInterface[] */ |
|
20
|
|
|
public function allParts() { |
|
21
|
|
|
$parts = []; |
|
22
|
|
|
$iterate = function( IMAPMessagePartInterface $message ) use (&$iterate, &$parts) { |
|
23
|
|
|
foreach ( $message->parts() as $part ) { |
|
24
|
|
|
$parts[] = $part; |
|
25
|
|
|
|
|
26
|
|
|
if ( count($part->parts()) ) { |
|
27
|
|
|
$iterate($part); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
}; |
|
31
|
|
|
|
|
32
|
|
|
$iterate($this); |
|
33
|
|
|
return $parts; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** @return IMAPMessagePartInterface */ |
|
37
|
|
|
public function part( $index ) { |
|
38
|
|
|
$parts = $this->parts(); |
|
39
|
|
|
return @$parts[$index]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @return array */ |
|
43
|
|
|
public function parameters() { |
|
44
|
|
|
if ( empty($this->parameters) ) { |
|
45
|
|
|
$structure = $this->structure(); |
|
46
|
|
|
|
|
47
|
|
|
$this->parameters['bytes'] = @$structure->bytes; |
|
48
|
|
|
|
|
49
|
|
|
foreach ( (array) @$structure->parameters as $param ) { |
|
50
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
|
51
|
|
|
} |
|
52
|
|
|
foreach ( (array) @$structure->dparameters as $param ) { |
|
53
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->parameters; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @return mixed */ |
|
61
|
|
|
public function parameter( $name ) { |
|
62
|
|
|
$parameters = $this->parameters(); |
|
63
|
|
|
$structure = $this->structure(); |
|
64
|
|
|
return @$parameters[ strtolower($name) ] ?: @$structure->$name; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @return string[] */ |
|
68
|
|
|
public function headers() { |
|
69
|
|
|
if ( empty($this->headers) ) { |
|
70
|
|
|
$headers = preg_split('#[\r\n]+(?=\w)#', $this->headerString()); |
|
71
|
|
|
$headers = array_map('mb_decode_mimeheader', $headers); |
|
72
|
|
|
|
|
73
|
|
|
$this->headers = []; |
|
|
|
|
|
|
74
|
|
|
foreach ($headers as $header) { |
|
75
|
|
|
$x = explode(':', $header, 2); |
|
76
|
|
|
$this->headers[ trim(strtolower($x[0])) ][] = trim($x[1]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->headers; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @return string|string[] */ |
|
84
|
|
|
public function header( $name ) { |
|
85
|
|
|
$headers = $this->headers(); |
|
86
|
|
|
$header = $headers[strtolower($name)] ?? [null]; |
|
87
|
|
|
return count($header) == 1 ? $header[0] : $header; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** @return IMAPMessagePartInterface[] */ |
|
91
|
|
|
public function subtypeParts( $subtypes, $recursive ) { |
|
92
|
|
|
$subtypes = (array) $subtypes; |
|
93
|
|
|
|
|
94
|
|
|
$parts = $recursive ? $this->allParts() : $this->parts(); |
|
95
|
|
|
array_unshift($parts, $this); |
|
96
|
|
|
|
|
97
|
|
|
return array_values(array_filter($parts, function(IMAPMessagePartInterface $part) use ($subtypes) { |
|
98
|
|
|
return in_array($part->subtype(), $subtypes); |
|
99
|
|
|
})); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** @return IMAPMessagePartInterface */ |
|
103
|
|
|
public function subtypePart( $subtypes, $recursive ) { |
|
104
|
|
|
if ( count($parts = $this->subtypeParts($subtypes, $recursive)) ) { |
|
105
|
|
|
return $parts[0]; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** @return string */ |
|
110
|
|
|
public function subtypeContent( $subtypes, $recursive ) { |
|
111
|
|
|
if ( $part = $this->subtypePart($subtypes, $recursive) ) { |
|
112
|
|
|
return $part->content(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return ''; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** @return string */ |
|
119
|
|
|
public function text( $recursive = false ) { |
|
120
|
|
|
return $this->subtypeContent($this->mailbox()->getTextSubtypes(), $recursive); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** @return string */ |
|
124
|
|
|
public function html( $recursive = false ) { |
|
125
|
|
|
return $this->subtypeContent($this->mailbox()->getHtmlSubtypes(), $recursive); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: