Folder.php
3.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
<?php
namespace Controller;
use Lib\Err;
use Lib\Mail\Mail;
use Lib\Mail\MailFun;
use Model\folderSql;
/**
* 文件夹管理
* @author:dc
* @time 2023/2/18 17:58
* Class Folder
* @package Controller
*/
class Folder extends Base {
/**
* 邮箱文件夹
* @author:dc
* @time 2023/2/18 10:58
*/
public function lists(){
try {
$emails = $this->getEmails();
}catch (Err $e){
$emails = [$this->getEmail()];
}
$emails = array_column($emails,'email','id');
// 查询
$folders = db()->all(
\Model\folderSql::all(
array_keys($emails),
'`id`,`folder`,`pid`,`exsts`,`unseen`,`email_id`'
)
);
$data = [];
foreach ($folders as $k=>$folder){
if (empty($data[$emails[$folder['email_id']]])){
$data[$emails[$folder['email_id']]] = [];
}
$data[$emails[$folder['email_id']]][$k] = $folder;
unset($data[$emails[$folder['email_id']]][$k]['email_id']);
}
$folders = $data;$data = null;
foreach ($folders as $f=>$folder){
// 转tree
$folders[$f] = list_to_tree($folder);
}
return $folders;
}
/**
* 创建 目录
* @author:dc
* @time 2023/2/18 17:56
*/
public function create(){
$email = $this->getEmail();
$formData = app()->request(['pid','folder']);
// 验证目录 可以输入中文英文数字
if(empty($formData['folder'])){
app()->e('folder_create_name_error');
}
// 加密后的文件夹
$origin_folder = MailFun::folderEncoding($formData['folder']);
// 不为空上级
if(!empty($formData['pid'])){
$parent = db()->first(folderSql::first(['id'=>$formData['pid']]));
if(!$parent || $parent['email_id'] != $email['id']){
app()->e('folder_parent_not_fount');
}
if($parent['pid']){
app()->e('folder_tree_max_two');
}
// 加密后的文件夹
$origin_folder = $parent['origin_folder'].'/'.$origin_folder;
}
$formData['folder'] = str_replace(["'",'"','/','\\','&','*','(',')'],'',$formData['folder']);
// 判断文件夹是否存在
$has = db()->count(folderSql::has(
[
'email_id' => $email['id'],
'origin_folder' => $origin_folder
]
));
if($has){
app()->e('folder_is_exist');
}
// 远程创建
$mail = new Mail(
$email['email'],
$email['password'],
$email['imap']
);
if(!$mail->login()){
app()->e('login_error_imap');
}
// 远程创建
$ret = $mail->client->folderCreate($origin_folder);
if($ret[0] === false){
app()->e($ret[1]);
}
// 插入数据
$id = db()->insert(folderSql::$table,[
'pid' => $parent['id']??0,
'email_id' => $email['id'],
'folder' => $formData['folder'],
'origin_folder' => $origin_folder,
'uuid' => md5($email['id'].$origin_folder),
]);
if(!$id){
app()->e('folder_create_save_error');
}
// 返回数据
app()->_json(db()->first(folderSql::first($id)));
}
/**
* 改名
* @author:dc
* @time 2023/3/3 17:40
*/
public function rename(){
}
/**
* 删除
* @author:dc
* @time 2023/3/3 17:38
*/
public function delete(){
}
}