Header.php
7.0 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
namespace Lib\Imap\Parse;
use Lib\Imap\Fun;
/**
* 解析邮件
* @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);
}elseif(str_contains($str,':')){
list($name, $str) = explode(":",$str,2);
$this->setAttribute($name,$str);
}
}
// 头信息content-type
$contentType = $this->get('content-type');
if($contentType){
preg_match("/charset=\"?([a-z0-9-._]{2,})\"?/",$contentType,$charset);
if(!empty($charset[1])){
// 编码
$this->setAttribute('charset',$charset[1]);
}
}
// 编码
$charset = $this->get('charset');
foreach ($this->attributes as $name => $attribute){
if($charset){
$attribute = Fun::mb_convert_encoding($attribute,'UTF-8',$charset);
}
$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(bool $isArray = false):array {
// 如果有这个字段,就用这个字段 to字段的邮箱可能被伪造
if(!empty($this->attributes['delivered-to'])){
$to = $this->attributes['delivered-to'];
}else{
$to = $this->attributes['to']??'';
}
return $this->parseAddress($to,$isArray);
}
/**
* 解析地址
* @param string $address
* @param false $isArray 是否返回数组
* @return array
* @author:dc
* @time 2024/9/29 14:54
*/
private function parseAddress(string $address, $isArray = false){
$arr = [];
if($address){
foreach (explode(',',$address) as $k=>$str){
$arr[$k] = Address::make($str);
if($isArray) $arr[$k] = $arr[$k]->toArray();
}
}
return $arr;
}
/**
* 抄送人
* @return array
* @author:dc
* @time 2024/9/11 15:53
*/
public function getCc(bool $isArray = false):array
{
return $this->parseAddress($this->attributes['cc']??'',$isArray);
}
/**
* 密送人
* @return array
* @author:dc
* @time 2024/9/11 15:54
*/
public function getBcc(bool $isArray = false):array
{
return $this->parseAddress($this->attributes['bcc']??'',$isArray);
}
/**
* 发件人 可能是欺炸 发件人
* @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 {
return static::mime_decode($this->attributes['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;
}
/**
* @return string
*/
public function getRaw(): string
{
return $this->raw_header;
}
/**
* 解析加密字符
* @param string $str
* @return string
* @author:dc
* @time 2024/9/29 14:21
*/
public static function mime_decode(string $str):string {
$str = trim($str);
if(preg_match("/^=\?([a-z0-9-]{3,})\?[bq]\?/i",$str,$code)){
// 解码 这个函数好像已经转码了,
$str = mb_decode_mimeheader($str);
return $str;
// 转字符编码
// return mb_convert_encoding($str,'utf-8',$code[1]);
}
return $str;
}
}