forked from mikelynn2/blacklistmonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
118 lines (105 loc) · 3.17 KB
/
api.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
class_exists('Setup', false) or include('classes/Setup.class.php');
class_exists('Utilities', false) or include('classes/Utilities.class.php');
class_exists('_MySQL', false) or include('classes/_MySQL.class.php');
class_exists('_FileCache', false) or include('classes/_FileCache.class.php');
$username = array_key_exists('username', $_POST) ? trim($_POST['username']) : '';
$passwd = array_key_exists('passwd', $_POST) ? trim($_POST['passwd']) : '';
$apiKey = array_key_exists('apiKey', $_POST) ? trim($_POST['apiKey']) : '';
$type = array_key_exists('type', $_POST) ? trim($_POST['type']) : '';
$data = array_key_exists('data', $_POST) ? trim($_POST['data']) : '';
$groupName = array_key_exists('groupName', $_POST) ? trim($_POST['groupName']) : '';
$result = array(
'status'=>'',
'result'=>array(),
);
$id = Utilities::validateLogin($username, $passwd, true, $apiKey);
if($id == 0) {
$result['status'] = 'invalid login';
output();
}
switch($type){
case 'updateDomains':
if($groupName=='') {
$result['status'] = 'groupName is required';
break;
}
$id = Utilities::ensureGroupExists($groupName);
Utilities::updateDomains($data, $id);
$result['status'] = 'success';
break;
case 'updateIPs':
if($groupName=='') {
$result['status'] = 'groupName is required';
break;
}
$id = Utilities::ensureGroupExists($groupName);
Utilities::updateIPs($data, $id);
$result['status'] = 'success';
break;
case 'checkHostStatus':
$result['status'] = 'success';
Utilities::setBlockLists();
$result['result'] = Utilities::checkBlacklists($data);
break;
case 'clearAllHostAndGroupData':
$mysql = new _MySQL();
$mysql->connect(Setup::$connectionArray);
$mysql->runQuery("truncate table monitors");
$mysql->runQuery("truncate table monitorGroup");
$result['status'] = 'success';
break;
case 'blacklistStatus':
$localCache = new _FileCache('blacklistmonitor-api', 90);
$cacheKey = md5("$username|$passwd|$apiKey|$type|$data");
$cacheData = $localCache->get($cacheKey);
if ($cacheData !== false) {
output($cacheData);
}
$mysql = new _MySQL();
$mysql->connect(Setup::$connectionArray);
$searchSQL = '';
switch($data){
case 'changed':
$searchSQL .= " and lastStatusChanged = 1 ";
break;
case 'blocked':
$searchSQL .= " and isBlocked = 1 ";
break;
case 'clean': $searchSQL .= " and isBlocked = 0 ";
break;
case 'all':
default:
}
$rs = $mysql->runQuery("
select ipDomain,isBlocked,rDNS,status,lastStatusChangeTime,lastUpdate
from monitors
where 1=1 $searchSQL");
$result['status'] = 'success';
$result['result'] = array();
while($row = mysqli_fetch_array($rs, MYSQL_ASSOC)){
$result['result'][] = array(
'host'=>$row['ipDomain'],
'isBlocked'=>$row['isBlocked'],
'dns'=>$row['rDNS'],
'status'=>unserialize($row['status']),
'lastChanged'=>$row['lastStatusChangeTime'],
'lastChecked'=>$row['lastUpdate'],
);
}
$mysql->close();
$localCache->set($cacheKey, $result);
break;
default:
$result['status'] = 'no such method';
}
output();
function output($data = false){
global $result;
if($data!==false){
echo(json_encode($data));
}else{
echo(json_encode($result));
}
exit();
}