Login.php
4.3 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
<?php
namespace Controller;
use Lib\Mail\Mail;
use Lib\Mail\MailFun;
use Lib\Verify;
use Model\emailSql;
/**
* 登录
* @author:dc
* @time 2023/2/18 16:49
* Class Login
* @package Controller
*/
class Login {
/**
* 登录操作
* @return int[]
* @author:dc
* @time 2023/2/13 11:50
*/
public function login(){
// $mail,$password,$imap,$smtp
$formData = app()->request(['email','password','imap','smtp']);
if(empty($formData['email']) || !Verify::sEmail($formData['email'])){
app()->e('email_verify_error');
}
if(empty($formData['password'])){
app()->e('password_verify_error');
}
if(empty($formData['imap'])){
app()->e('imap_verify_error');
}
if(empty($formData['smtp'])){
app()->e('smtp_verify_error');
}
// 是否是shopk那边已添加的邮箱,进行代理
$status = file_get_contents('https://oa.shopk.com/fob-check-email/'.$formData['email']);
// 存在 就把服务器替换为代理服务器
if($status=='exist'){
/**
* 代理功能 去shopk检查邮件是否被添加到shopk了。
* 如果添加到shopk了就把请求地址替换成shopk的服务器地址
* 由shopk代理
* 避免出现换ip登录的情况而被封号
*/
$callHost = function (string $host,int $prot){
$host = parse_url($host);
if(empty($host['host'])){
$host = $host['path'].':'.$prot;
}else{
$host = $host['host'].':'.(empty($host['port'])?$prot:$host['port']);
}
return $host;
};
$formData['imap'] = $callHost($formData['imap'],993);
$formData['smtp'] = $callHost($formData['smtp'],465);
// 服务器代理到shopk服务器上面
$proxy = [
'imap.gmail.com:993' => '43.154.117.107:10086',
'smtp.gmail.com:465' => '43.154.117.107:10087'
];
// 是否存在代理
if(empty($proxy[$formData['imap']]) || empty($proxy[$formData['smtp']]) ){
app()->e('请联系站点管理员添加代理');
}
// 替换为代理服务器
$formData['imap'] = $proxy[$formData['imap']];
$formData['smtp'] = $proxy[$formData['smtp']];
}
// 进行远程登录,验证
$mailClient = (new Mail($formData['email'],$formData['password'],$formData['imap']));
$mailClient->client->debug(true,'echo');
$login = $mailClient->login(false);
if( $login === 0){
app()->e('login_error_imap');
}elseif ($login === -1){
app()->e('email_server_error');
}
// 验证smtp登录
if(!MailFun::smtpLoginTest($formData['smtp'],$formData['email'],$formData['password'])){
app()->e('email_smtp_server_error');
}
// 是否存在
$id = db()->value(emailSql::hasEmail($formData['email']));
$data = [
'password' => base64_encode($formData['password']),
'imap' => $formData['imap'],
'smtp' => $formData['smtp'],
'status' => 1,
'pwd_error' => 0,
];
if($id){
// 修改
$ret = db()->update(emailSql::$table,$data,dbWhere(['id'=>$id]));
}else{
// 新增
$data['email'] = $formData['email'];
$ret = db()->insert(emailSql::$table,$data);
if($ret){
// // 增加邮件数量
// redis()->incr('email_total');
// 立即处理 同步
redis()->lPush('sync_email_lists', $ret);
}
}
if($ret){
$mailClient->syncFolder(db()->value(emailSql::hasEmail($formData['email'])));
app()->_json(
db()->first(
emailSql::first(
$formData['email'],
'`id`,`email`,`imap`,`smtp`,`status`,`created_at`'
)
)
);
}
app()->e('login_error');
}
}