Collection.php
4.42 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
<?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 human_filesize($bytes, $decimals = 2)
{
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
//Фильтрация фомена
public static function domain_filter($domain)
{
$out = false;
$host = str_replace(
'*.',
'',
utf8_encode($domain)
);
if (filter_var($host, FILTER_VALIDATE_IP)===false)
{
$host_parts = explode('.', $host);
$host_parts = array_reverse($host_parts);
$host_parts = array_slice($host_parts, 0, 2);
$host_parts = array_reverse($host_parts);
if (count($host_parts) === 2)
$out = $host;
unset($host_parts);
}
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];
}
public static function getTixRawData($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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' => json_decode($response, true), 'errorno' => $errorNum, 'errormessage' => $errorMsg];
}
}