NoticeController.php 17.6 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2023/09/02
 * Time: 10:36
 */
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\SyncSubmitTask\SyncSubmitTask;
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;
    const  TYPEVISIT = "visit";
    const TYPEINQUIRY = "inquiry";
    const TRAFFICZERO = 0;
    const TRAFFICONE = 1;
    const DEVICE_PORT_ONE = 1;
    const DEVICE_PORT_TWO = 2;
    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);
    }

    /**
     * 响应
     * @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);
    }

    /**
     * 发送http post请求
     * @param $url
     * @param $data
     * @param array $header
     * @param bool $is_json
     * @return mixed|string
     */
    function httpPost($url, $data, $header = [],$is_json = true)
    {
        if (empty($header)) {
            $header = array(
                "Accept: application/json",
                "Content-Type:application/json;charset=utf-8",
                "token:".env("SECRET_TOKEN"),
                "pid:".env("MERCHANT_NUMBER")
            );
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $res = curl_exec($ch);
        if (curl_errno($ch)) {
            $error_message = curl_error($ch);
            @file_put_contents(storage_path('logs/error.log'), var_export($error_message, true) . PHP_EOL, FILE_APPEND);
        }
        curl_close($ch);
        if($is_json){
            return json_decode($res, true);
        }
        return trim($res);
    }

    /**
     * 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();
        $token = env("SECRET_TOKEN");
        $pid = env("MERCHANT_NUMBER");
        $apiUrl = env("API_URL");
        $requestUrl = $apiUrl."api/get_url_verify_token/?pid=".$pid."&token=".$token."&domain=".$domain;
        @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);

                $transmitUrl = env("TRANSMIT_URL");
                $this->httpPost($transmitUrl."api/selfSiteNotify/",json_encode(['domain'=>$domain]));
            } 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
     * @param string $type
     * @param int $traffic
     * @return string
     */
    public function transmit($request, $type = self::TYPEVISIT, $traffic = self::TRAFFICZERO){
        if($request->getClientIp() == "127.0.0.1"){
            return $this->success();
        }
        //判断是否是爬虫
        $isReptile = $this->isReptile($request);
        if($isReptile){
            return $this->success();
        }

        $data = $request->all();
        if (empty($data)){
            return $this->success();
        }
        $data["device_port"] = $this->userAgentHandle( $request->userAgent(),$data["device_port"] ?? self::DEVICE_PORT_ONE);
        $req["data"] = $data;
        $req["referer"] = $this->visitInfoHandle($data["referrer_url"] ?? $request->header('Referer'));
        $req["domain"] = $request->getHost();
        $req["ip"] = $request->getClientIp();
        $req["user_agent"] = $request->userAgent();
        $req["files"] = isset($data["files"]) ? $data["files"] : null;
        $req["type"] = $type;
        $req["traffic"] = $traffic;
        $req["timestamp"] = time();

        //转发接口
        $transmitUrl = env("TRANSMIT_URL");
        $resp = $this->httpPost($transmitUrl."api/selfSiteApi/",json_encode($req));
        if ($resp["status"] != self::SUCCESS){
            $dataJson = json_encode($req);
            if (!file_exists(storage_path('logs/fail_req'))){
                mkdir(storage_path('logs/fail_req',0777,true));
            }
            @file_put_contents(storage_path('logs/fail_req/'.date('Y-m-d').'.log'), var_export($dataJson, true) . PHP_EOL, FILE_APPEND);
        }
        return $this->success();
    }

    /**
     * 处理user_agent
     * @param $user_agent
     * @param $device_port
     * @return int
     */
    public function userAgentHandle($user_agent,$device_port){
        if (!in_array($device_port,[self::DEVICE_PORT_ONE,self::DEVICE_PORT_TWO])){
            $device_port = self::DEVICE_PORT_ONE;
        }
        // 头信息中带有这些信息, 代表是手机端, 重置设备类型
        if (preg_match('/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|wap|windowsce|ucweb/', $user_agent)) {
            $device_port = self::DEVICE_PORT_TWO;
        }
        return $device_port;
    }

    /**
     * 客户访问埋点接口
     * @param Request $request
     * @return string
     */
    public function customerVisit(Request $request)
    {
        return $this->transmit($request);
    }

    /**
     * 埋点信息处理
     * @param $referrerUrl
     * @return string
     */
    public function visitInfoHandle($referrerUrl)
    {
        if(preg_match('/google|facebook|bing|yahoo|youtobe|linkedin|messefrankfurt|yandex|tiktok|twitter|instagram|reddit|telegram|pinterest|tumblr/',$referrerUrl)){
        }else if($referrerUrl == null){
            //直访用户
            $referrerUrl = "";
        }else{
            $referrerUrl = "https://www.google.com/";
        }
        return $referrerUrl;
    }

    /**
     * 是否是爬虫访问
     * @param $request
     * @return bool
     */
    public function isReptile($request)
    {
        $agent = $request->header('User-Agent');
        if (!empty($agent)) {
            $spiderSite= array(
                "TencentTraveler",
                "Baiduspider+",
                "BaiduGame",
                "Googlebot",
                "msnbot",
                "Sosospider+",
                "Sogou web spider",
                "ia_archiver",
                "Yahoo! Slurp",
                "YoudaoBot",
                "Yahoo Slurp",
                "MSNBot",
                "Java (Often spam bot)",
                "BaiDuSpider",
                "Voila",
                "Yandex bot",
                "BSpider",
                "twiceler",
                "Sogou Spider",
                "Speedy Spider",
                "Google AdSense",
                "Heritrix",
                "Python-urllib",
                "Alexa (IA Archiver)",
                "Ask",
                "Exabot",
                "Custo",
                "OutfoxBot/YodaoBot",
                "yacy",
                "SurveyBot",
                "legs",
                "lwp-trivial",
                "Nutch",
                "StackRambler",
                "The web archive (IA Archiver)",
                "Perl tool",
                "MJ12bot",
                "Netcraft",
                "MSIECrawler",
                "WGet tools",
                "larbin",
                "Fish search",
                "yandex.com/bots",
                "google.com/bot",
                "bingbot",
                "YandexMobileBot",
                "BingPreview",
                "AhrefsBot",
                "bot"
            );
            $flag = 0;
            foreach($spiderSite as $val) {
                $str = strtolower($val);
                if (strpos($agent, $str) !== false) {
                    $flag = 1;
                }
            }
            if($flag == 1){
                return true;
            }else{
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 接口
     * @param Request $request
     * @return string
     */
    public function trafficVisit(Request $request)
    {
        return $this->transmit($request,self::TYPEVISIT,self::TRAFFICONE);
    }

    /**
     * C端表单提交 询盘信息
     * @param Request $request
     * @return string
     */
    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();
        return $this->transmit($request,self::TYPEINQUIRY,self::TRAFFICZERO);
    }

    /**
     * 收集邮箱或者手机号等其他信息
     * @param Request $request
     * @return string
     */
    public function inquiryOtherInfo(Request $request)
    {
        return $this->transmit($request,self::TYPEINQUIRY);
    }

    /**
     * 起点表单询盘
     * @param Request $request
     * @return string
     */
    public function inquiryQd(Request $request){
        return $this->transmit($request,self::TYPEINQUIRY);
    }

    /**
     * 搜索
     * @param Request $request
     * @return mixed|string
     */
    public function search(Request $request)
    {
        $data = $request->all();

        //获取搜索参数
        $data["search_content"] = '';
        if (isset($data['s'])) {
            $req["search_content"]  = $data['s'];
        }
        if (isset($data['search'])) {
            $req["search_content"] = $data['search'];
        }

        //分页
        $req["page"] = isset($data['page']) && (int)$data['page'] > 1 ? (int)$data['page'] : 1;
        $req["domain"] = $request->getHost();

        //转发data
        $transmitUrl = env("API_URL");
        $resp = $this->httpPost($transmitUrl."api/get_search_content/",json_encode($req));
        return $resp["data"]["html"];
    }
}