WhoisController.php 3.39 KB
<?php

namespace app\commands;

use PHPHtmlParser\Dom;
use yii\db\Query;
use yii;
use yii\console\Controller;
use yii\db\Expression;
use yii\helpers\Console;
use app\components\Collection;
use app\models\Host;
use app\models\Filter;

/**
 * Парсинг запрещенных ресурсов в РФ.
 */
class WhoisController extends Controller
{
    public $count = 30;

    public function beforeAction($action)
    {
        if (!parent::beforeAction($action)) {
            return false;
        }
        return true;
    }

    public function options($actionID)
    {
        return ['count'];
    }

    public function optionAliases()
    {
        return ['c' => 'count'];
    }

    /**
     * Парсинг запрещенных ресурсов в РФ.
     */
    public function actionIndex()
    {
        $query = (new Query());
        $hosts = $query
            ->select('id, domain')
            ->from('{{%host}}')
            ->andWhere(
                [
                    'or',
                    ['wis_status'=>Host::STATUS_WIS_NONE]
                ]
            )->orWhere(
                [
                    'and',
                    ['wis_status'=>Host::STATUS_WIS_BUSY],
                    ['<=', 'domain_expire', new Expression('NOW()')]
                ]
            )
            ->orderBy(['status'=>SORT_DESC, 'rand()'=>SORT_ASC])
            ->limit($this->count)
            ->all();

        $domainsArr = [];
        foreach ($hosts as $h) {
            $domainsArr[] = $h['domain'];
        }
        if ($domainsArr) {
            $result = Collection::getRawData(Yii::$app->params['registrators_urls'][0], $domainsArr);
            if (!$result['errorno']) {//без ошибок
                $dom = new Dom;
                $dom->loadStr($result['data']);
                $trArr = $dom->find('.tablesorter tbody tr');
                if (!count($trArr)) {
                    foreach ($domainsArr as $d) {
                        $this->upWhois($d);
                    }
                }
                $hasAnswer = [];
                foreach ($trArr as $tr) {
                    $domain = $tr->find('td')[0]->text;
                    $status = $tr->find('td')[1]->text;
                    $expired = $tr->find('td')[6]->text;
                    $hasAnswer[] = $domain;
                    $this->upWhois($domain, $status, $expired);
                }
                foreach (array_diff($domainsArr, $hasAnswer) as $domainDisable) {
                    $this->upWhois($domainDisable);
                }
            } else {
                echo $result['errorno'] . ' ' . $result['errormessage'];
            }
        }
    }

    private function upWhois($domain, $statusStr = 'Занят', $expired = '')
    {
        echo $domain . ' => ' . $statusStr . "\n";


        switch ($statusStr) {
            case 'Свободен':
                $status = Host::STATUS_WIS_FREE;
                break;
            default:
                $status = Host::STATUS_WIS_BUSY;
        }
        $update = ['wis_status' => $status, 'wis_date' => new Expression('NOW()')];
        if ($expired) {
            $expired = date("Y-m-d H:i:s", strtotime($expired));
            if ($expired) {
                $update['domain_expire'] = $expired;
            }
        }
        Yii::$app->db->createCommand()
            ->update('{{%host}}', $update, ['domain' => $domain])
            ->execute();
    }

}