This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 182
/
functions.php
executable file
·100 lines (96 loc) · 3.3 KB
/
functions.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
<?php
function pre_var_dump($var)
{
echo "<pre>";
var_dump($var);
echo "</pre>";
}
function browse($filter, $export = false)
{
$records_per_page = (int)$filter['rec_per_page'];
if (isset($filter['page']) && $filter['page'] > 1):
$page = (int)$filter['page'];
else:
$page = 1;
endif;
$from = ($page - 1) * $records_per_page;
$q1 = "SELECT ip AS ipaddress, port_id, protocol, state, reason, service, banner, title";
$q2 = "SELECT COUNT(*) as total_records";
$q = " FROM data WHERE 1 = 1";
if (!empty($filter['ip'])):
list($start_ip, $end_ip) = getStartAndEndIps($filter['ip']);
$q .= " AND (ip >= $start_ip AND ip <= $end_ip)";
endif;
if (isset($filter['port']) && (int) $filter['port'] > 0 && (int) $filter['port'] <= 65535):
$q .= " AND port_id = " . (int) $filter['port'];
endif;
if (!empty($filter['protocol'])):
$q .= " AND protocol = '" . DB::escape($filter['protocol']) . "'";
endif;
if (!empty($filter['state'])):
$q .= " AND state = '" . DB::escape($filter['state']) . "'";
endif;
if (!empty($filter['service'])):
$q .= " AND service = '" . DB::escape($filter['service']) . "'";
endif;
if (!empty($filter['banner'])):
if ((int)$filter['exact-match'] === 1):
$q .= " AND (banner LIKE BINARY \"%" . $filter['banner'] . "%\" OR title LIKE BINARY \"%" . $filter['banner'] . "%\")";
else:
$q .= " AND match(title, banner) AGAINST (\"" . DB::escape($filter['banner']) . "\" IN NATURAL LANGUAGE MODE)";
endif;
endif;
if (!empty($filter['text'])):
$q .= " AND (match(title, banner) AGAINST (\"" . DB::escape($filter['text']) . "\" IN NATURAL LANGUAGE MODE)
OR service = \"" . DB::escape($filter['text']) . "%\"
OR protocol = \"" . DB::escape($filter['text']) . "%\"
OR port_id = \"" . (int)$filter['text'] . "%\")";
endif;
if (isset($start_ip)):
$q3 = " ORDER BY ip ASC";
else:
$q3 = " ORDER BY scanned_ts DESC";
endif;
if (!$export):
$q4 = " LIMIT $from, $records_per_page";
else:
$q4 = "";
endif;
$data = DB::fetchAll($q1 . $q . $q3 . $q4);
if ($export) {
return $data;
}
$total = DB::fetch($q2 . $q);
$to = $from + $records_per_page < $total['total_records'] ? $from + $records_per_page : $total['total_records'];
$pages = $total ['total_records'] > 1 ? ceil($total ['total_records'] / $records_per_page) : 0;
return array(
'data' => $data,
'pagination' => array(
'page' => $page,
'pages' => $pages,
'records' => $total ['total_records'],
'from' => ++$from,
'to' => $to)
);
}
function getStartAndEndIps($ip)
{
$start_ip = '';
$end_ip = '';
$ip = trim($ip, '.');
$p = explode('.', trim($ip));
for ($i = 0; $i < 4; $i++):
if ($i > 0):
$start_ip .= '.';
$end_ip .= '.';
endif;
if (isset($p[$i])):
$start_ip .= $p[$i];
$end_ip .= $p[$i];
else:
$start_ip .= "0";
$end_ip .= "255";
endif;
endfor;
return array(ip2long($start_ip), ip2long($end_ip));
}