WhoisController.php
3.11 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
<?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()
{
$hosts = (new Query())
->select('id, domain')
->from('{{%host}}')
->where(['in', 'wis_status', [Host::STATUS_WIS_NONE, Host::STATUS_WIS_BUSY]])
->andWhere(['status'=> Filter::STATUS_ON])
->orderBy('rand()')
->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();
}
}