NoticeController.php
12.9 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2023/09/02
* Time: 10:36
*/
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use ZipArchive;
/**
* Class NoticeController
* @package App\Http\Controllers\Api
*/
class NoticeController extends Controller
{
const SUCCESS = 200;
const ERROR = 400;
const SERVERERROR = 500;
protected $header = [];//设置请求头参数
/**
* @param array $data
* @param string $message
* @param int $status
* @return string
*/
protected function success($data = [], $message = 'success', $status = 200)
{
$array = compact('status', 'message', 'data');
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
/**
* @param int $status
* @param string $message
* @param array $data
* @return string
*/
protected function error($message = 'error', $status = 400, $data = [])
{
$array = compact('status', 'message', 'data');
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
/**
* 响应response
* @param array $data
* @param int $code
* @param string $msg
* @param int $result_code
* @param string $type
*/
public function responseA($data = [], $code = 200, $msg = 'success', $result_code = 200, $type = 'application/json')
{
$result = [
'msg' => $msg,
'code' => $code,
'data' => $data,
];
$header = [
'Content-Type' => $type,
];
$response = response($result, $result_code, $header);
throw new HttpResponseException($response);
}
/**
* 响应
* @param null $msg
* @param int $code
* @param array $data
* @param int $result_code
* @param string $type
* @return void
*/
public function response($msg = null,$code = self::SUCCESS,$data = [],$result_code = 200,$type = 'application/json')
{
$result = [
'msg' => $msg,
'code' => $code,
'data' => $data,
];
$this->header['Content-Type'] = $type;
$response = response($result,$result_code,$this->header);
throw new HttpResponseException($response);
}
/**
* GET请求
* @param $url
* @return mixed
*/
public function curlGet($url){
$ch1 = curl_init();
$timeout = 0;
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_ENCODING, '');
curl_setopt($ch1, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array());
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$access_txt = curl_exec($ch1);
curl_close($ch1);
return json_decode($access_txt, true);
}
/**
* A端上传验证代码
* @param Request $request
* @return string
*/
public function uploadVerifyFile(Request $request)
{
$domain = $request->getHost();
$files = $request->allFiles();
foreach ($files as $file) {
$destinationPath = public_path($domain);
$file->move($destinationPath, $file->getClientOriginalName());
$filePath = $destinationPath . '/' . $file->getClientOriginalName();
@file_put_contents(storage_path('logs/upload_file.log'), date('Y-m-d H:i:s') . " ".$filePath . PHP_EOL, FILE_APPEND);
}
return $this->success();
}
/**
* A端上传amp站验证代码
* @param Request $request
* @return string
*/
public function uploadAmpVerifyFile(Request $request)
{
$domain = $request->getHost();
$domain_array = parse_url($domain);
$host = $domain_array['host'] ?? $domain_array['path'];
$host_array = explode('.',$host);
if(count($host_array) <= 2){
array_unshift($host_array,'m');
}else{
$host_array[0] = 'm';
}
$amp_domain = implode('.',$host_array);
$files = $request->allFiles();
foreach ($files as $file) {
$destinationPath = public_path($amp_domain);
$file->move($destinationPath, $file->getClientOriginalName());
$filePath = $destinationPath . '/' . $file->getClientOriginalName();
@file_put_contents(storage_path('logs/upload_file.log'), date('Y-m-d H:i:s') . " ".$filePath . PHP_EOL, FILE_APPEND);
}
return $this->success();
}
/**
* 获取需要下载的文件url
* @param Request $request
* @return string
*/
public function websiteHtml(Request $request){
// $domain = $request->getHost();
//临时测试域名
$domain = "demo.globalso.site";
$token = env("WEB_SITE_TOKEN");
$apiUrl = env("API_URL");
$requestUrl = $apiUrl."?domain=".$domain."&token=".$token;
@file_put_contents(storage_path('logs/notify_get_url.log'), date('Y-m-d H:i:s') . "接收到通知:". $requestUrl . PHP_EOL, FILE_APPEND);
try {
$res = $this->curlGet($requestUrl);
$url = isset($res["data"]["url"]) && !empty($res["data"]["url"]) ? urldecode($res["data"]["url"]) : "";
if ($res["status"] != self::SUCCESS || $url == ""){
$msg = isset($res["message"]) && !empty($res["message"]) ? $res["message"] : "请求失败!";
return $this->error($msg);
}
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $this->websiteHtmlHandle($url,$domain);
}
/**
* 网站html解压
* @param $url
* @param $domain
* @return string
*/
public function websiteHtmlHandle($url,$domain)
{
$pathInfo = pathinfo($url);
$extension = $pathInfo['extension'];
//只允许解压zip格式文件
if (in_array($extension, ["zip"])) {
try {
$targetFile = $this->downLoadFile($url);
$zip = new ZipArchive();
if ($zip->open($targetFile) === TRUE) {
$outputFolder = public_path($domain);
if (!is_dir($outputFolder)) {
mkdir($outputFolder, 0777, true);
}
// 解压缩文件,保留原文件结构
$zip->extractTo($outputFolder);
$zip->close();
$this->deleteDirectory($targetFile);
} else {
return $this->error('解压失败!');
// 处理打开压缩文件失败的情况
}
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}else{
return $this->error('不允许解压改格式压缩包!');
}
return $this->success();
}
/**
* 下载文件(下载到public目录下)
* @param $url
* @return string
*/
public function downLoadFile($url){
$savePath = public_path();
if (!file_exists($savePath)) {
mkdir($savePath, 0777, true);
}
$targetFile = $savePath . '/' . basename($url);
$context = stream_context_create([
'http' => [
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
],
]);
$fileContent = @file_get_contents($url, false, $context);
if ($fileContent !== false) {
file_put_contents($targetFile, $fileContent);
return $targetFile;
} else {
return "";
}
}
/**
* 删除文件
* @param $path
* @return bool
*/
public function deleteDirectory($path)
{
if (!is_dir($path)) {
try {
unlink($path);
} catch (\Exception $e) {
return true;
}
return true;
}
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file) {
$filePath = $path . '/' . $file;
if (is_dir($filePath)) {
$this->deleteDirectory($filePath);
} else {
try {
unlink($filePath);
} catch (\Exception $e) {
return true;
}
}
}
rmdir($path);
return true;
}
/**
* 客户访问埋点接口
* @param Request $request
* @return JsonResponse
*/
public function customerVisit(Request $request)
{
$data = $request->all();
$data["referrer_url"] = !empty($data["referrer_url"]) ? $data["referrer_url"] : "";
$data["url"] = !empty($data["url"]) ? $data["url"] : "";
$data["domain"] = !empty($data["domain"]) ? $data["domain"] : "";
$data["ip"] = $request->getClientIp();
$data["user_agent"] = $request->userAgent();
if (empty($data["referrer_url"]) || empty($data["url"]) || empty($data["domain"])){
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
}
//转发data
//埋点成功
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
}
/**
* 接口
* @param Request $request
* @return string
*/
public function trafficVisit(Request $request)
{
//获取参数
$data = $request->all();
$data["id"] = $request->input('ip');
$data["url"] = $request->input('url');
$data["device_port"] = intval($request->input('device_port'));
$data["referrer_url"] = $request->input('referrer_url');
$data["user_agent"] = $request->input('user_agent');
if (empty($data["id"]) || empty($data["url"]) || empty($data["referrer_url"]) || empty($data["user_agent"])){
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
}
//转发data
//成功
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
}
/**
* C端表单提交 询盘信息
* @param Request $request
* @return mixed
*/
public function inquiry(Request $request)
{
$data = $request->all();
$black_ips = ['203.86.233.27', '37.19.221.202'];
if(in_array(request()->getClientIp(), $black_ips)){
$this->success();
}
//同一ip 5秒内超过5次 限制1小时
$ip = $request->ip();
$lock_key = 'lock_ip_' . $ip;
$rate_key = 'rate_limit:' . $ip;
if(Cache::has($lock_key)){
$this->success();
}
if (Cache::has($rate_key)) {
$count = Cache::get($rate_key);
if ($count >= 5) {
Cache::put($lock_key, 1, 3600);
$this->success();
}
Cache::increment($rate_key);
} else {
Cache::put($rate_key, 1, 5);
}
$data["files"] = $request->allFiles();
//转发data
//返回数据
$res = "";
return $this->responseA($res);
}
/**
* 收集邮箱或者手机号等其他信息
* @param Request $request
* @return mixed
*/
public function inquiryOtherInfo(Request $request)
{
return $this->inquiry($request);
}
/**
* 起点表单询盘
* @param Request $request
* @return void
*/
public function inquiryQd(Request $request){
$data = $request->post();
$data['submit_ip'] = $data['submit_ip'] ? $data['submit_ip'] : '';
$data['submit_time'] = $data['submit_time'] ? $data['submit_time'] : '';
$data['refer'] = $data['refer'] ? $data['refer'] : '';
//转发data
//返回数据
$res = "";
return $this->responseA($res);
}
/**
* 搜索
* @param Request $request
*/
public function search(Request $request)
{
$data = $request->all();
//获取搜索参数
$data["search_content"] = '';
if (isset($data['s'])) {
$data["search_content"] = $searchContent = $data['s'];
}
if (isset($data['search'])) {
$data["search_content"] = $data['search'];
}
$data["page"] = isset($data['page']) && (int)$data['page'] > 1 ? (int)$data['page'] : 1;
//转发data
}
}