Skip to content

Commit

Permalink
Upload: Add tracking of updates and IP addresses (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
binarymaster committed Nov 22, 2020
1 parent 0e0dff9 commit 610589d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
10 changes: 8 additions & 2 deletions 3wifi.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,10 +895,11 @@ function randhex($length)
$useapi = false;
if (!is_null($key))
$useapi = $UserManager->AuthByApiKey($key, true);
$ip = _ip2long($_SERVER["REMOTE_ADDR"]);
$uid = $UserManager->uID;
if (is_null($uid) || $UserManager->Level < 1 || ($useapi && $UserManager->ApiAccess != 'write'))
$uid = 'NULL';
if ($db->query('INSERT INTO tasks (`tid`,`created`,`modified`,`ext`,`comment`,`checkexist`,`nowait`,`uid`) VALUES (\''.$tid.'\', now(), now(), \''.$ext.'\', \''.$comment.'\', '.$checkexist.', '.$nowait.', '.$uid.')'))
if ($db->query('INSERT INTO tasks (`tid`,`created`,`modified`,`ext`,`comment`,`checkexist`,`nowait`,`uid`,`ipaddr`) VALUES (\''.$tid.'\', now(), now(), \''.$ext.'\', \''.$comment.'\', '.$checkexist.', '.$nowait.', '.$uid.', '.$ip.')'))
{
$json['upload']['state'] = true;
$json['upload']['tid'] = $tid;
Expand Down Expand Up @@ -972,11 +973,13 @@ function randhex($length)
}
$id = (int)$id;
$sql = "SELECT
*
*, extinfo.data AS extinfo
FROM
`BASE_TABLE`
INNER JOIN `GEO_TABLE` USING(BSSID)
LEFT JOIN `comments` USING(cmtid)
LEFT JOIN extinfo USING (id)
LEFT JOIN logupload USING (id)
WHERE
id = $id";
$res = QuerySql($sql);
Expand All @@ -990,6 +993,8 @@ function randhex($length)
$json['data'] = array(
'id' => (int)$row['id'],
'time' => $row['time'],
'updated' => $row['updated'],
'last_uploader' => _long2ip($row['ipaddr']),
'comment' => $row['cmtval'],
'ip' => _long2ip($row['IP']),
'port' => (int)$row['Port'],
Expand All @@ -1010,6 +1015,7 @@ function randhex($length)
'dns1' => _long2ip($row['DNS1']),
'dns2' => _long2ip($row['DNS2']),
'dns3' => _long2ip($row['DNS3']),
'extinfo' => $row['extinfo'],
'lat' => 'none',
'lon' => 'none',
'quadkey' => $row['quadkey'],
Expand Down
15 changes: 14 additions & 1 deletion 3wifi.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ CREATE TABLE `comments` (
CREATE TABLE `tasks` (
`tid` CHAR(32) NOT NULL,
`uid` INT(11) UNSIGNED NULL DEFAULT NULL,
`ipaddr` INT(11) NOT NULL,
`tstate` TINYINT(3) UNSIGNED NOT NULL DEFAULT 0,
`created` TIMESTAMP NOT NULL DEFAULT current_timestamp(),
`modified` TIMESTAMP NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
Expand Down Expand Up @@ -178,14 +179,26 @@ CREATE TABLE `users` (
CREATE TABLE `logauth` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`IP` INT(15) UNSIGNED NOT NULL,
`IP` INT(15) NOT NULL,
`uid` INT(11) UNSIGNED NULL DEFAULT NULL,
`action` TINYINT(3) UNSIGNED NOT NULL,
`data` CHAR(64) NOT NULL DEFAULT '',
`status` BIT(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Дамп структуры для таблицы 3wifi.logupload
CREATE TABLE `logupload` (
`id` INT(10) UNSIGNED NOT NULL,
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ipaddr` INT(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `FK1_logupload_base` (`id`),
INDEX `updated` (`updated`),
INDEX `ipaddr` (`ipaddr`),
CONSTRAINT `FK1_logupload_base` FOREIGN KEY (`id`) REFERENCES `base` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Дамп структуры для таблицы 3wifi.uploads
CREATE TABLE `uploads` (
`uid` INT(10) UNSIGNED NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions 3wifid.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
{
$cntp++;
if ($cntp == 1) continue; // Пропуск заголовка CSV
$res = db_add_ap($data, $cmtid, $uid);
$res = db_add_ap($data, $cmtid, $uid, $task['ipaddr']);
($res == 0 ? $cnta++ : $warn[$cntp - 1] = $res);
if (microtime(true) - $time > $hangcheck)
{
Expand All @@ -86,7 +86,7 @@
{
$data = explode("\t", $str);
$cntp++;
$res = db_add_ap($data, $cmtid, $uid);
$res = db_add_ap($data, $cmtid, $uid, $task['ipaddr']);
($res == 0 ? $cnta++ : $warn[$cntp] = $res);
if (microtime(true) - $time > $hangcheck)
{
Expand Down
23 changes: 12 additions & 11 deletions db.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ function getTask($tid)
$result = array();
$result['id'] = $row['tid'];
$result['uid'] = $row['uid'];
$result['ipaddr'] = (int)$row['ipaddr'];
$result['state'] = (int)$row['tstate'];
$result['created'] = $row['created'];
$result['modified'] = $row['modified'];
Expand Down Expand Up @@ -356,7 +357,7 @@ function CheckRelevanceOfMemoryTables($UseFix)
return $Result;
}

function db_add_ap($row, $cmtid, $uid)
function db_add_ap($row, $cmtid, $uid, $ipaddr)
{
global $checkexist;
global $db;
Expand Down Expand Up @@ -463,13 +464,18 @@ function db_add_ap($row, $cmtid, $uid)
VALUES ($cmtid, $addr, $port, $auth, $name, $radio, $hide, $NoBSSID, $bssid, $essid, $sec, $key, $wps, $lanip, $lanmsk, $wanip, $wanmsk, $gate, $DNS[0], $DNS[1], $DNS[2])
ON DUPLICATE KEY UPDATE
`cmtid`=$cmtid,`IP`=$addr,`Port`=$port,`Authorization`=$auth,`name`=$name,`RadioOff`=$radio,`Hidden`=$hide,`NoBSSID`=$NoBSSID,`BSSID`=$bssid,`ESSID`=$essid,`Security`=$sec,`WiFiKey`=$key,`WPSPIN`=$wps,`LANIP`=$lanip,`LANMask`=$lanmsk,`WANIP`=$wanip,`WANMask`=$wanmsk,`WANGateway`=$gate,`DNS1`=$DNS[0],`DNS2`=$DNS[1],`DNS3`=$DNS[2];");
// Берём id точки из таблицы base в любом случае (могут быть расхождения с mem_base)
$res = $db->query("SELECT id FROM ".BASE_TABLE." WHERE NoBSSID=$NoBSSID AND BSSID=$bssid AND BINARY ESSID=$essid AND BINARY WiFiKey=$key AND WPSPIN=$wps");
$row = $res->fetch_row();
$res->close();
$id = (int)$row[0];
if ($ipaddr != 0)
{
// Регистрируем кто и когда загрузил / последний раз обновил эту точку
$db->query("INSERT INTO logupload (`id`, `ipaddr`) VALUES ($id, $ipaddr) ON DUPLICATE KEY UPDATE `ipaddr` = $ipaddr, `updated` = NOW()");
}
if (!is_null($uid))
{
// Берём id точки из таблицы base в любом случае (могут быть расхождения с mem_base)
$res = $db->query("SELECT id FROM ".BASE_TABLE." WHERE NoBSSID=$NoBSSID AND BSSID=$bssid AND BINARY ESSID=$essid AND BINARY WiFiKey=$key AND WPSPIN=$wps");
$row = $res->fetch_row();
$res->close();
$id = (int)$row[0];
// Выясняем, если кто-то уже загрузил такую точку
$res = $db->query("SELECT COUNT(uid) FROM uploads WHERE id=$id");
$row = $res->fetch_row();
Expand All @@ -481,11 +487,6 @@ function db_add_ap($row, $cmtid, $uid)
if (!empty($data))
{
// Собираем доп. информацию о точке (может быть серийник и что-либо ещё)
$res = $db->query("SELECT id FROM ".BASE_TABLE." WHERE NoBSSID=$NoBSSID AND BSSID=$bssid AND BINARY ESSID=$essid AND BINARY WiFiKey=$key AND WPSPIN=$wps");
$row = $res->fetch_row();
$res->close();
$id = (int)$row[0];
// Добавляем информацию
$dt = parseDelimStr($data);
$data = '\''.$db->real_escape_string($data).'\'';
$db->query("INSERT INTO extinfo (`id`, `data`) VALUES ($id, $data) ON DUPLICATE KEY UPDATE `data` = $data");
Expand Down

0 comments on commit 610589d

Please sign in to comment.