Folder.php
5.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
<?php
namespace Lib\Imap\Request;
use Lib\Imap\Imap;
/**
* 登录
* @author:dc
* @time 2024/9/13 17:09
* Class Login
* @package Lib\Imap\Request
*/
class Folder extends Request{
protected string $folder;
/**
* 未读数量 有的邮箱是不会返回未读数量的
* @var int
*/
protected int $seen = 0;
/**
* 总数
* @var int
*/
protected int $total = 0;
/**
* 最近邮件的数量
* @var int
*/
protected int $recent = 0;
/**
* 标签
* @var array
*/
protected array $flags = [];
/**
* 是否初始了,就是是否select 文件夹
* @var bool
*/
private bool $isInit = false;
/**
* Folder constructor.
* @param Imap $imap
* @param string $folder
*/
public function __construct(Imap $imap, string $folder)
{
parent::__construct($imap);
$this->folder = $folder;
}
public function exec(): static
{
if(!$this->imap->checkedFolder($this->folder)){
$this->cmd('SELECT "%s"',$this->folder);
// ["* 128 EXISTS\r\n","* 2 RECENT\r\n","* OK [UIDVALIDITY 1] UIDs valid\r\n","* FLAGS (\\Answered \\Seen \\Deleted \\Draft \\Flagged)\r\n","* OK [PERMANENTFLAGS (\\Answered \\Seen \\Deleted \\Draft \\Flagged)] Limited\r\n","TAG3 OK [READ-WRITE] SELECT completed\r\n"]
if($this->isOk()){
$this->isInit = true;
foreach ($this->result as $item){
$item = trim($item);
// 总数量
if(preg_match("/^\* (\d+) EXISTS$/i",$item,$m)){
$this->total = (int) $m[1];
}
// 最近的
elseif (preg_match("/^\* (\d+) RECENT$/i",$item,$m)){
$this->total = (int) $m[1];
}
// 未读
elseif (preg_match("/^\*.*\[UNSEEN (\d+)\]/i",$item,$m)){
$this->seen = (int) $m[1];
}
// tag
elseif (preg_match("/^\* FLAGS \((.*)\)$/i",$item,$m)){
$this->flags = explode(' ',$m[1]);
}
}
// 设置
$this->imap->setCheckedFolder($this->folder);
}
}
return $this;
}
/**
* 邮件列表
* @return Msg
* @author:dc
* @time 2024/9/14 11:36
*/
public function msg():Msg {
return new Msg($this->imap,$this);
}
/**
* @return array
*/
public function getFlags(): array
{
if(!$this->isInit){
$this->exec();
}
return $this->flags;
}
/**
* @return int
*/
public function getRecent(): int
{
if(!$this->isInit){
$this->exec();
}
return $this->recent;
}
/**
* @return int
*/
public function getSeen(): int
{
if(!$this->isInit){
$this->exec();
}
return $this->seen;
}
/**
* @return int
*/
public function getTotal(): int
{
if(!$this->isInit){
$this->exec();
}
return $this->total;
}
/**
* 关闭当前目录 一般情况用不到
* @return bool
* @author:dc
* @time 2024/9/19 14:04
*/
public function close():bool {
// 必须初始了 并且在当前文件夹下面
if($this->isInit && $this->imap->checkedFolder($this->folder)){
$this->cmd('CLOSE');
return $this->isOk();
}
return true;
}
/**
* 创建目录
* @return bool
* @author:dc
* @time 2024/9/20 13:43
*/
public function create():bool {
$this->cmd('CREATE "%s"',$this->folder);
return $this->isOk();
}
/**
* 修改目录
* @param string $newFolder 新的文件夹名称
* @param bool $is_utf7 是否是utf7的编码
* @return bool
* @author:dc
* @time 2024/9/20 13:47
*/
public function rename(string $newFolder, bool $is_utf7 = true):bool {
// 需要转码
if(!$is_utf7) $newFolder = mb_convert_encoding($newFolder,'UTF7-IMAP','UTF-8');
// RENAME oldfolder newfolder
$this->cmd('RENAME "%s" "%s"',$this->folder,$newFolder);
return $this->isOk();
}
/**
* 删除 目录
* @return bool
* @author:dc
* @time 2024/9/20 13:50
*/
public function delete():bool {
$this->cmd('DELETE "%s"',$this->folder);
return $this->isOk();
}
/**
* 读取body体
* @param int $num
* @param false $is_uid
* @return \Lib\Imap\Parse\Body
* @author:dc
* @time 2024/9/20 15:37
*/
public function getBody(int $num, bool $is_uid = false):\Lib\Imap\Parse\Body{
$body = (new Body($this->imap,$this));
return $body->get($num, $is_uid = false);
}
}