Header.php
5.7 KB
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
<?php
namespace Lib\Imap\Parse;
/**
* 解析邮件
* @author:dc
* @time 2024/9/10 16:54
* Class Header
*/
class Header{
/**
* @var array
*/
private array $attributes = [];
/**
* 原始头信息
* @var string
*/
protected string $raw_header;
/**
* Header constructor.
* @param string $raw_header
*/
public function __construct(string $raw_header)
{
$this->raw_header = $raw_header;
$this->rfc822_parse_headers();
}
/**
* @param string $name
* @param string $value
* @param bool $append
* @author:dc
* @time 2024/9/11 16:24
*/
public function setAttribute(string $name, string $value, bool $append = true){
$name = strtolower($name);
if(!$append){
$this->attributes[$name] = $value;
}else{
if(isset($this->attributes[$name]))
$this->attributes[$name] .= "\r\n".$value;
else
$this->attributes[$name] = $value;
}
}
/**
* 解析邮件头部
* @author:dc
* @time 2024/9/10 16:29
*/
private function rfc822_parse_headers(){
$header = explode("\r\n",$this->raw_header);
$name = '';
foreach ($header as $str){
// 判断是否是上一行的
if(str_starts_with($str,' ') || str_starts_with($str,"\t")){
$this->setAttribute($name,$str);
}else{
$str = explode(":",$str);
$name = $str[0];
unset($str[0]);
$this->setAttribute($name,implode(':',$str));
}
}
foreach ($this->attributes as $name => $attribute){
$attribute = trim($attribute);
$this->attributes[$name] = $attribute;
}
}
public function __get(string $name)
{
return $this->get($name);
}
/**
* 读取字段
* @param string $name
* @return mixed
* @author:dc
* @time 2024/9/11 15:06
*/
public function get(string $name):mixed {
$name = strtolower($name);
$m = 'get'.str_replace(' ','',ucwords(str_replace('-',' ',$name)));
if(method_exists($this,$m)){
return $this->{$m}();
}
return $this->attributes[$name]??'';
}
/**
* 获取收件地址 可能是假地址
* @param bool $is_obj 是否解析成对象 Address
* @return Address[]
* @author:dc
* @time 2024/9/11 15:03
*/
public function getTo() {
// 如果有这个字段,就用这个字段 to字段的邮箱可能被伪造
if(!empty($this->attributes['delivered-to'])){
$to = $this->attributes['delivered-to'];
}else{
$to = $this->attributes['to']??'';
}
return $this->parseAddress($to);
}
/**
* 解析地址
* @param string $address
* @return array
* @author:dc
* @time 2024/9/11 15:53
*/
private function parseAddress(string $address){
$arr = [];
foreach (explode(',',$address) as $str){
$arr[] = Address::make($str);
}
return $arr;
}
/**
* 抄送人
* @return array
* @author:dc
* @time 2024/9/11 15:53
*/
public function getCc()
{
return $this->parseAddress($this->attributes['cc']??'');
}
/**
* 密送人
* @return array
* @author:dc
* @time 2024/9/11 15:54
*/
public function getBcc()
{
return $this->parseAddress($this->attributes['bcc']??'');
}
/**
* 发件人 可能是欺炸 发件人
* @return Address
* @author:dc
* @time 2024/9/11 15:19
*/
public function getFrom():Address {
return Address::make($this->attributes['from']??'');
}
/**
* 获取解码后的主题
* @return string
* @author:dc
* @time 2024/9/11 15:22
*/
public function getSubject():string {
$subject = explode("\n",$this->attributes['subject']??'');
foreach ($subject as $ak=>$str){
$subject[$ak] = DeCode::decode($str);
}
return implode("\n",$subject);
}
/**
* Boundary
* @param string $str
* @return string
* @author:dc
* @time 2024/9/11 16:05
*/
public function getBoundary(string $str = ''): string
{
$pre = "/boundary=(.*?(?=;)|(.*))/i";
// 在内容类型中读取 大多少情况都在这里面
$contentType = $str ? $str: ($this->attributes['content-type']??'');
if($contentType){
preg_match($pre,$contentType,$b);
if(!empty($b[1])){
return trim(str_replace(['"',';'],'',$b[1]));
}
}
if($this->raw_header && !$str){
return $this->getBoundary($this->raw_header);
}
return '';
}
/**
* 这个是是否加急 1加急 3普通 5不急
* @return string
*/
public function getPriority(): string
{
if(!empty($this->attributes['priority'])){
return $this->attributes['priority'];
}
if(!empty($this->attributes['x-priority'])){
return $this->attributes['x-priority'];
}
return '3';
}
/**
* @return array
*/
public function getAttributes(): array
{
return $this->attributes;
}
/**
* @return array
* @author:dc
* @time 2024/9/11 16:15
*/
public function toArray():array {
$arr = [];
foreach ($this->attributes as $key=>$attr){
$arr[$key] = $this->get($key);
}
return $arr;
}
}