ScanController.php 4.8 KB
<?php

namespace app\commands;

use yii;
use yii\console\Controller;
use yii\db\Expression;
use yii\helpers\Console;
use app\components\Collection;
use app\models\Host;

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

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

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

    public function optionAliases()
    {
        return ['f' => 'file'];
    }

    /**
     * Парсинг запрещенных ресурсов в РФ.
     */
    public function actionIndex()
    {
        $rows = 0;
        $csv_file = Collection::download_file(Yii::$app->params['csv_url']);
        if ($csv_file!=='')
            {
                $csv_handle = fopen($csv_file ,'r');
                if ($csv_handle !== false)
                    {
                        $csv_row = 0;
                        $csv_datatime = '';
                        while (($csv_data = fgetcsv($csv_handle, 1024, ';')) !== false)
                            {
                                $csv_row++;
                                if ($csv_row===1)
                                    $csv_datatime = date(
                                        "Y-m-d H:i:s",
                                        strtotime(
                                            str_replace('Updated: ', '', $csv_data[0])
                                        )
                                    );
                                elseif (isset($csv_data[1])===true)
                                    {
                                        $host = str_replace(
                                            '*.',
                                            '',
                                            utf8_encode($csv_data[1])
                                        );
                                        $host_level = count( explode('.', $host) );
                                        if ($host_level === 2)
                                            {
                                                $db_request = Yii::$app->db->createCommand();
                                                if ($db_request)
                                                    {
                                                        if ( Host::find()->where(['domain' =>$host])->count()>0 )
                                                            $db_request->update(
                                                                '{{%host}}',
                                                                [
                                                                    'csv_date' => $csv_datatime
                                                                ],
                                                                [
                                                                    'domain' => $host
                                                                ]
                                                            );
                                                                else
                                                            $db_request->insert(
                                                                '{{%host}}',
                                                                [
                                                                    'domain' =>$host,
                                                                    'created_at' => new Expression('NOW()'),
                                                                    'csv_date' => $csv_datatime,
                                                                ]
                                                            );
                                                        $db_request->execute();
                                                        $rows++;
                                                    }
                                                unset($db_request);
                                            }

                                        unset($host_level);
                                        unset($host);
                                    }
                            }
                        unset($csv_data);
                        unset($csv_datatime);
                        unset($csv_row);
                        fclose($csv_handle);
                        unlink($csv_file);
                    }
                unset($csv_handle);
            }
        unset($csv_file);
        echo "\n".$this->ansiFormat('Count of hosts processed: ', Console::FG_CYAN) . ' => ' . $this->ansiFormat($rows, Console::BOLD, Console::FG_GREEN) . "\n";
        unset($rows);
    }

}