Collection.php 3.34 KB
<?php

namespace app\components;

use Yii;
use app\models\Filter;
use yii\base\Component;
use yii\db\Query;

class Collection extends Component
{

    //Фильтрация фомена
    public static function domain_filter($domain, $filters)
    {
        $out = false;
        $host = str_replace(
            '*.',
            '',
            utf8_encode($domain)
        );
        if (count( explode('.', $host) ) === 2)
            $out = Collection::apply_filter($host, $filters);
        unset($host);
        return $out;
    }

    //Скачиваине URL файла и передача пути к нему
    public static function get_filter($type)
    {
        $out = array();
        $filtes_bd = (new Query())
            ->select('`pattern`, `include`')
            ->from('{{%filters}}')
            ->where('type=:type AND status=:status', array(':type'=>$type, ':status'=>Filter::STATUS_ON ))
            ->all();
        if ($filtes_bd !== false)
            $out = $filtes_bd;
        unset($filtes_bd);
        return $out;
    }

    //Скачиваине URL файла и передача пути к нему
    public static function apply_filter($domain, $filters)
    {
        $out = false;
        $fiter_check = true;
        foreach($filters as $filter)
            if ($fiter_check === true)
                {
                    $find = preg_match($filter['pattern'], $domain);
                    if (
                        ($find === 1 && (int)$filter['include']===Filter::INCLUDE_OFF) ||
                        ($find === 0 && (int)$filter['include']===Filter::INCLUDE_ON)
                    )
                        {
                            $fiter_check = false;
                            break;
                        }
                    unset($find);
                }
        unset($filter);
        if ($fiter_check===true)
            $out = (string)$domain;
        unset($fiter_check);
        return $out;
    }




    //Скачиваине URL файла и передача пути к нему
    public static function download_file($csvFile)
    {
        $out = '';
        $temp_csv_name = tempnam(sys_get_temp_dir(), 'rkn_');
        if ($temp_csv_name !== false)
            {
                $file_handle = fopen((string)$csvFile, 'r');
                if ($file_handle!==false)
                    {
                        $file_write = file_put_contents($temp_csv_name, $file_handle);
                        if ($file_write!==false)
                            $out = $temp_csv_name;
                        unset($file_write);
                        fclose($file_handle);
                    }
                unset($file_handle);
            }
        unset($temp_csv_name);
        return $out;
    }


    public static function getRawData($url, $domain)
    {
        $post = [
            'doms' => implode("\r\n",$domain),
        ];
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        $response = curl_exec($ch);
        $errorNum = curl_errno($ch);
        $errorMsg = curl_error($ch);

        curl_close($ch);

        return ['data' => $response, 'errorno' => $errorNum, 'errormessage' => $errorMsg];
    }

}