-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
103 lines (83 loc) · 2.68 KB
/
update.php
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
<?php
require_once('config/config.php');
// Report ALL
error_reporting(E_ALL);
// API authorization
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode(API_USER . ':' . API_PASS)
)
));
// START load data from Adminus CRM
$json = file_get_contents(API_HOST . '/contract-type-state/all', false, $context);
$states = json_decode($json);
$json = file_get_contents(API_HOST . '/contract/all', false, $context);
$contracts = json_decode($json);
$json = file_get_contents(API_HOST . '/ip-address/all', false, $context);
$ips = json_decode($json);
$blockStates = array();
if (is_array($states->data)) foreach($states->data as $state)
{
if (in_array($state->name, BLOCKED_STATES)) $blockStates[] = $state->id;
}
else die('Failure when loading contract states from Adminus CRM' . "\n");
$blockContracts = array();
if (is_array($contracts->data)) foreach($contracts->data as $contract)
{
if (in_array($contract->state, $blockStates)) $blockContracts[] = $contract->id;
}
else die('Failure when loading contracts from Adminus CRM' . "\n");
$blockIPs = array();
if (is_array($ips->data)) foreach($ips->data as $ip)
{
if (in_array($ip->contractId, $blockContracts)) $blockIPs[] = $ip->ip;
}
else die('Failure when loading IPs from Adminus CRM' . "\n");
// END load data from Adminus CRM
//var_dump($blockIPs);
// START update RouterOS address-list
require_once('class/routeros-api.class.php');
$API = new RouterosAPI();
$API->debug = false;
$routers = MT_API_HOSTS;
$table = MT_API_ADDRESS_LIST;
foreach ($routers as $routernumber => $router)
{
echo 'connecting to ' . $router . '...';
if ($API->connect($router, MT_API_USER, MT_API_PASS))
{
echo 'OK' . "\n";
$API->write('/ip/firewall/address-list/print', false);
$API->write('?list=' . $table, false);
$API->write('=.proplist=.id');
$ARRAY = $API->read();
//print_r($ARRAY);
foreach ($ARRAY as $ITEM)
{
$API->write('/ip/firewall/address-list/remove', false);
$API->write('=.id=' . $ITEM['.id']);
$ARRAY = $API->read();
//print_r($ARRAY);
}
if (is_array($blockIPs)) foreach ($blockIPs as $blockIP)
{
$API->write('/ip/firewall/address-list/add', false);
$API->write('=address=' . $blockIP, false);
$API->write('=list=' . $table);
$ARRAY = $API->read();
//print_r($ARRAY);
};
$API->write('/ip/firewall/address-list/print', false);
$API->write('?list=' . $table, false);
$API->write('=.proplist=address');
$ARRAY = $API->read();
//print_r($ARRAY);
$API->disconnect();
echo 'Blocked IPs:' . "\n";
foreach ($ARRAY as $ip) echo $ip['address'] . "\n";
}
else echo 'FAULT' . "\n";
echo "\n";
}
// END update RouterOS address-list
?>