-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
255 lines (252 loc) · 10 KB
/
helper.php
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
<?php
/**
* Get Mail Inbox List
* @author ken <[email protected]>
* @license Apache-2.0
*/
use Ddeboer\Imap\Server;
use helper_v3\Image;
class imap_inbox
{
protected $mailboxes;
/**
* 绝对路径,根目录
*/
public $base_path;
protected $save_path;
/**
* 相对路径
*/
public $save_url;
/**
* 是否加载所有邮件
* 默认加载最近30天的
*/
public $load_all_date = false;
public static $data;
public static $key;
public $extract_url;
/**
* 附件支持的后缀
*/
public $allow_ext = [
'zip','ofd','xml','md','txt','pdf','jpg','jpeg','png','webp','webm','mp3','mp4','gz','7z','doc','docx','xlsx','xlsx','ppt','pptx',
'dpg','csv',
];
/**
* 只保留有附件且后缀在以下范围内的邮件
*/
public $only_ext = [
];
/**
* 默认取30天数据
*/
public $days = 30;
public function __construct($base_path = '', $save_url = '', $extract_url = '')
{
$imap_server = get_config('imap_server');
$imap_address = get_config('imap_address');
$imap_password = get_config('imap_password');
if(!$imap_server || !$imap_address || !$imap_password) {
throw new \Exception("请配置 imap_server 、imap_address、 imap_password参数");
}
$server = new Server($imap_server);
$connection = $server->authenticate($imap_address, $imap_password);
$this->mailboxes = $connection->getMailboxes();
$this->base_path = $base_path ?: PATH;
$this->save_url = $save_url ?: '/data/mail_inbox/';
$this->extract_url = $this->base_path.($extract_url ?: '/data/mail_inbox_tmp_extract/');
$this->save_path = $this->base_path.$this->save_url;
self::$key = "mail_inbox".$imap_server.$imap_address;
return $this;
}
/**
* 获取收件箱
* @param $allow_box ['inbox','sent_messages','drafts','deleted_messages']
*/
public function get($allow_box = 'inbox', $days = '')
{
if(is_numeric($allow_box)) {
$this->days = $allow_box;
$allow_box = 'inbox';
}
if($days > 0) {
$this->days = $days;
}
if(is_string($allow_box)) {
$array_in[] = $allow_box;
} else {
$array_in = $allow_box;
}
$cache_key = self::$key.md5(json_encode($array_in));
$full = [];
if(!self::$data[$cache_key]) {
$only_ext = false;
if($this->only_ext) {
$only_ext = true;
}
foreach ($this->mailboxes as $mailbox) {
// Skip container-only mailboxes
// @see https://www.php.net/manual/en/function.imap-getmailboxes.php
if ($mailbox->getAttributes() & \LATT_NOSELECT) {
continue;
}
//INBOX
$name = $mailbox->getName();
$name = str_replace(" ", "_", $name);
$name = trim(strtolower($name));
if(!in_array($name, $array_in)) {
continue;
}
$top = ['name' => $name,'count' => $mailbox->count()];
if($this->load_all_date) {
$messages = $mailbox->getMessages();
} else {
$today = new DateTimeImmutable();
$thirtyDaysAgo = $today->sub(new DateInterval('P'.$this->days.'D'));
$messages = $mailbox->getMessages(
new Ddeboer\Imap\Search\Date\Since($thirtyDaysAgo),
\SORTDATE,
true
);
}
$items = [];
foreach ($messages as $message) {
$item = [];
$item['subject'] = $message->getSubject();
$item['from'] = $message->getFrom()->getAddress(); // Message\EmailAddress
$to = $message->getTo();
$to_list = [];
foreach($to as $_to) {
$to_list[] = $_to->getAddress();
}
$item['to'] = $to_list;
$date = $message->getDate();
$item['number'] = $message->getNumber();
$id = $message->getId();
$item['date'] = $date->format('Y-m-d H:i:s');
$item['time_zone'] = $date->getTimezone()->getName();
$item['is_answered'] = $message->isAnswered();
$item['is_deleted'] = $message->isDeleted();
$item['is_draft'] = $message->isDraft();
$item['is_seen'] = $message->isSeen();
$body = $message->getBodyHtml();
if ($body === null) {
$body = $message->getBodyText();
}
if(!$id) {
$id = "m".$item['date'].$item['subject'].$item['from'].json_encode($item['to']);
}
$id = str_replace("<", "[", $id);
$id = str_replace(">", "]", $id);
if(substr($id, 0, 1) == '[') {
$id = substr($id, 1);
}
if(substr($id, -1) == ']') {
$id = substr($id, 0, -1);
}
$item['id'] = $id;
$for_action = $item;
if(function_exists('do_action')) {
$for_action['body'] = $body;
$for_action['countinue'] = false;
do_action("imap_inbox", $for_action);
if($for_action['countinue'] === true) {
continue;
}
}
$attachments = $message->getAttachments();
$file = [];
$load_attachment = false;
foreach ($attachments as $attachment) {
$name = $attachment->getFilename();
$content = $attachment->getDecodedContent();
$ext = get_ext($name) ?? get_mime_content($content, true);
$new_name = md5($content).".".$ext;
if(!in_array($ext, $this->allow_ext)) {
continue;
}
if($this->only_ext) {
if(!in_array($ext, $this->only_ext)) {
continue;
}
}
$load_attachment = true;
}
if($load_attachment) {
foreach ($attachments as $attachment) {
$name = $attachment->getFilename();
$content = $attachment->getDecodedContent();
$ext = get_ext($name) ?? get_mime_content($content, true);
$new_name = md5($content).".".$ext;
if(!in_array($ext, $this->allow_ext)) {
continue;
}
$save_file = $this->save_path.$new_name;
if(!file_exists($save_file)) {
$dir = get_dir($save_file);
create_dir_if_not_exists([$dir]);
file_put_contents(
$save_file,
$content
);
}
$file[] = [
'name' => $name,
'url' => $this->save_url.$new_name
];
}
}
if($only_ext) {
if(!$file || count($file) == 0) {
continue;
}
}
$arr = Image::get_img_tag($body);
$files = $file;
if($arr) {
foreach($arr as $i => $v) {
if(strpos($v, '://') !== false) {
$content = file_get_contents($v);
$ext = get_mime_content($content, true);
$new_name = md5($content).".".$ext;
$save_file = $this->save_path.$new_name;
if(!file_exists($save_file)) {
$dir = get_dir($save_file);
create_dir_if_not_exists([$dir]);
file_put_contents(
$save_file,
$content
);
}
$body = str_replace($v, $this->save_url.$new_name, $body);
} elseif($file[$i]['url']) {
$body = str_replace($v, $file[$i]['url'], $body);
unset($file[$i]);
}
}
}
$item['body'] = $body;
if($file) {
$file = array_values($file);
}
$item['file'] = $file;
$items[] = $item;
}
$full[$top['name']] = $items;
}
} else {
$full = self::$data[$cache_key];
}
$new_list = [];
foreach($full as $k => $v) {
if(in_array($k, $array_in)) {
$new_list[$k] = $v;
}
}
if(count($array_in) == 1) {
return $new_list[$array_in[0]];
}
return $new_list;
}
}