SelfSiteController.php
7.8 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
<?php
namespace App\Http\Controllers\Api;
use App\Exceptions\InquiryFilterException;
use App\Models\Domain\DomainInfo;
use App\Models\Mail\Mail;
use App\Models\Project\DeployBuild;
use App\Models\Project\Project;
use App\Models\User\User;
use App\Models\Visit\SyncSubmitTask;
use App\Models\WebSetting\WebSettingFormBack;
use App\Services\CosService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class SelfSiteController extends BaseController
{
/**
* 自建站访问接口
* @param Request $request
* @return false|string
* @author Akun
* @date 2024/05/16 11:21
*/
public function selfSiteApi(Request $request)
{
$token = $request->header('token');//token
$pid = $request->header('pid');//项目id
$data = $request->input('data');//访问数据
$domain = $request->input('domain');//访问域名
$ip = $request->input('ip');//访问ip
$referer = $request->input('referer');//访问来源
$user_agent = $request->input('user_agent');//访问头信息
$type = $request->input('type');//类型:visit,inquiry
$traffic = $request->input('traffic', SyncSubmitTask::TRAFFIC_DEFAULT);//是否引流
$files = $request->input('files', []);//文件
if (empty($token) || empty($pid)) {
return $this->error('token无效', 401);
}
if (empty($data) || empty($domain)) {
return $this->error('参数错误');
}
if (!in_array($type, [SyncSubmitTask::TYPE_VISIT, SyncSubmitTask::TYPE_INQUIRY])) {
return $this->error('类型错误');
}
//判断token是否有效
$cache_key = 'self_site_api_' . $pid . '_' . $token;
$project_info = Redis::get($cache_key);
if ($project_info === null) {
$project_model = new Project();
$project_info = $project_model->read(['id' => $pid, 'site_token' => $token]);
Redis::set($cache_key, json_encode($project_info));
Redis::expire($cache_key, 3600);
}
if (!$project_info) {
return $this->error('token无效', 401);
}
if (!empty($files)) {
try {
foreach ($files as $key => $file) {
$cos = new CosService();
$fileName = uniqid() . rand(10000, 99999) . '.' . $file['ext'];
$file_data = base64_decode($file['data']);
$path = $cos->uploadFile($file_data, '/inquiry/' . date('Ymd'), $fileName, true);
$data[$key] = [
'path' => $path,
'original_name' => $file['name'],
];
}
} catch (InquiryFilterException $e) {
return $this->error($e->getMessage());
} catch (\Exception $e) {
return $this->error('File upload fail');
}
}
$array = [
'data' => $data,
'ip' => $ip,
'domain' => $domain,
'referer' => $referer,
'user_agent' => $user_agent
];
SyncSubmitTask::createTask($array, $type, $traffic);
$result = [];
if ($type == SyncSubmitTask::TYPE_INQUIRY) {
$result = $this->inquiryResult($pid, $domain);
}
return $this->success($result);
}
protected function inquiryResult($pid, $domain)
{
$cache_key = 'inquiry_form_back_' . $domain;
$result = Cache::get($cache_key);
if (!$result) {
$result = [
'message' => "",
'url' => "",
'other' => ""
];
$webFormBack = WebSettingFormBack::getBack($pid);
if (!empty($webFormBack)) {
$result["message"] = $webFormBack->message ?? "";
$result["url"] = $webFormBack->url ?? "";
$result["other"] = $webFormBack->other ?? "";
Cache::put($cache_key, $result, 3600);
}
}
return $result;
}
/**
* 自建站页面更新通知接口
* @param Request $request
* @return false|string
* @author Akun
* @date 2024/05/22 15:24
*/
public function selfSiteNotify(Request $request)
{
$token = $request->header('token');//token
$pid = $request->header('pid');//项目id
$domain = $request->input('domain');//域名
if (empty($token) || empty($pid)) {
return $this->error('token无效', 401);
}
//判断token是否有效
$project_model = new Project();
$project_info = $project_model->read(['id' => $pid, 'site_token' => $token]);
if (!$project_info) {
return $this->error('token无效', 401);
}
$user = new User();
$userInfo = $user->list(['project_id' => $pid], 'id', ['id']);
$user_list = '';
if ($userInfo) {
$user_list = implode(',', array_column($userInfo, 'id'));
}
$mail = new Mail();
$data["title"] = "页面更新通知";
$data["user_list"] = $mail->setUserList($user_list);
$data["content"] = "该项目于 " . date('Y-m-d H:i:s') . " 执行页面更新完成,更新站点域名:" . $domain;
$mail->add($data);
return $this->success([]);
}
/**
* 自建站上传验证文件接口
* @param Request $request
* @return false|string
* @author Akun
* @date 2024/05/28 11:47
*/
public function selfSiteVerify(Request $request)
{
$token = $request->header('token');//token
$pid = $request->header('pid');//项目id
$file_name = $request->input('file_name');//验证文件名称
$type = $request->input('type');//验证文件类型,1主站,2小语种站
if (empty($token) || empty($pid)) {
return $this->error('token无效', 401);
}
//判断token是否有效
$project_model = new Project();
$project_info = $project_model->read(['id' => $pid, 'site_token' => $token]);
if (!$project_info) {
return $this->error('token无效', 401);
}
$model = new DeployBuild();
$data = $type == 1 ? ['main_verify_file' => $file_name] : ['amp_verify_file' => $file_name];
$model->edit($data, ['project_id' => $pid]);
return $this->success([]);
}
/**
* 自建站获取通配符证书接口
* @param Request $request
* @return false|string
* @author Akun
* @date 2025/09/02 16:55
*/
public function selfSiteSsl(Request $request)
{
$token = $request->header('token');//token
$pid = $request->header('pid');//项目id
if (empty($token) || empty($pid)) {
return $this->error('token无效', 401);
}
//判断token是否有效
$project_model = new Project();
$project_info = $project_model->read(['id' => $pid, 'site_token' => $token]);
if (!$project_info) {
return $this->error('token无效', 401);
}
//获取域名信息
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['project_id' => $pid]);
if (!$domain_info) {
return $this->error('获取域名失败', 401);
}
//获取通配符证书
$top_domain = getTopDomain($domain_info['domain']);
$ssl_re = httpGetSsl($top_domain);
$return = [
'ssl_key' => '',
'ssl_cert' => ''
];
if (isset($ssl_re['status']) && $ssl_re['status'] == 2) {
//获取成功
$return['ssl_key'] = $ssl_re['ssl_key'];
$return['ssl_cert'] = $ssl_re['ssl_cert'];
}
return $this->success($return);
}
}