-
Notifications
You must be signed in to change notification settings - Fork 89
/
devmac.php
127 lines (126 loc) · 2.85 KB
/
devmac.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
119
120
121
122
123
124
125
126
127
<?php
require_once 'utils.php';
/**
* Получает список соседних устройств по MAC-адресу
* @param string $bssid MAC-адрес устройства
* @param bool $nocli Не учитывать Wi-Fi устройства в режиме клиента
* @return array $json
*/
function API_device_mac($bssid, $nocli)
{
$result = array();
$res = QuerySql(
"CREATE TEMPORARY TABLE tmp_dev_names
(
`name` TINYTEXT NOT NULL,
`BSSID` BIGINT(15) UNSIGNED NOT NULL,
INDEX name (name(512)),
INDEX BSSID (BSSID),
UNIQUE INDEX uni (BSSID)
)
");
$result['result'] = $res !== false;
if (!$result['result'])
{
$result['error'] = 'database';
return $result;
}
$mac = mac2dec($bssid);
$x = 1;
for ($i = 1; $i <= 24; $i++)
{
$m2 = $mac | $x;
$m1 = $m2 - $x;
$m2 = base_convert($m2, 10, 16);
$m1 = base_convert($m1, 10, 16);
if ($result['result'])
{
$sql = "INSERT IGNORE tmp_dev_names
SELECT name, BSSID
FROM `BASE_TABLE`
WHERE
BSSID BETWEEN 0x{$m1} AND 0x{$m2}
AND NoBSSID = 0
AND name != ''
";
if ($nocli)
$sql .= "AND name NOT LIKE 'FOSCAM%'
AND name NOT LIKE 'IPCAM%'
AND name NOT LIKE '%D-Link DCS%'
AND name NOT LIKE '%IP Camera%'
AND name NOT LIKE '%Network Camera%'
";
$res = QuerySql($sql);
$result['result'] = $res !== false;
}
if ($result['result'])
{
$res = QuerySql("SELECT COUNT(*) FROM tmp_dev_names");
$result['result'] = $res !== false;
if ($result['result'])
{
$row = $res->fetch_row();
$res->close();
if ((int)$row[0] >= 20000)
break;
}
}
if (!$result['result'])
break;
$x |= $x << 1;
}
if ($result['result'])
{
$res = QuerySql("CREATE TEMPORARY TABLE tmp_device_names LIKE tmp_dev_names");
$result['result'] = $res !== false;
}
$mac = base_convert($mac, 10, 16);
if ($result['result'])
{
$res = QuerySql(
"INSERT tmp_device_names
SELECT name, BSSID
FROM tmp_dev_names
ORDER BY ABS(BSSID - 0x$mac)
");
$result['result'] = $res !== false;
}
if ($result['result'])
{
$res = QuerySql(
"SELECT name, COUNT(name) cnt, ABS(BSSID - 0x$mac) diff
FROM tmp_device_names
GROUP BY name HAVING(cnt > 1)
ORDER BY ABS(BSSID - 0x$mac)
LIMIT 10
");
$result['result'] = $res !== false;
}
if ($result['result'])
{
$devs = array();
while ($row = $res->fetch_assoc())
{
$devs[] = $row;
}
$res->close();
}
if (!$result['result'])
{
$result['error'] = 'database';
}
else
{
$result['scores'] = array();
foreach($devs as $dev)
{
$entry = array();
$entry['name'] = $dev['name'];
$entry['score'] = 1 - pow((int)$dev['diff'] / 0xFFFFFF, 1 / 8);
$entry['count'] = (int)$dev['cnt'];
$result['scores'][] = $entry;
}
}
QuerySql("DROP TABLE IF EXISTS tmp_dev_names, tmp_device_names");
return $result;
}