<?php

namespace App\Console\Commands\Test;

use App\Helper\Arr;
use App\Models\Collect\CollectTask;
use App\Models\Com\Notify;
use App\Models\Com\UpdateLog;
use App\Models\Com\UpdateVisit;
use App\Models\Devops\ServerConfig;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;

class Temp extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test_temp';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '临时脚本';

    public function handle(){
        ProjectServer::useProject(1515);

        $products = Product::select(['id','content'])->get();

        foreach ($products as $product){
            $content = $product->content;
            $content = str_replace('<h1','<h2', $content);
            $content = str_replace('</h1','</h2', $content);

            $product->content = $content;
            $product->save();

            $this->output('productID:'.$product->id.',success');
        }

        $this->output('end');
    }

    public function check_cname($domain, $server_info)
    {
        $checkA = false;
        $checkCname = false;

        $process = new Process(['nslookup', '-qt=a', $domain]);
        $process->run();
        $output = explode(PHP_EOL, $process->getOutput());
        foreach ($output as $line) {
            if ($line) {
                $checkA = strpos($line, $server_info['ip']) !== false;
                if ($checkA) {
                    return $domain;
                }
            }
        }

        //是否cname
        $process = new Process(['nslookup', '-qt=cname', $domain]);
        $process->run();
        $output = explode(PHP_EOL, $process->getOutput());
        foreach ($output as $line) {
            if ($line) {
                $checkCname = (strpos($line, $server_info['domain']) !== false);
                if ($checkCname) {
                    return $domain;
                }
            }
        }
        return false;
    }

    public function output($msg)
    {
        echo $msg . PHP_EOL;
    }
}