diff --git a/db/pamela.sql b/db/pamela.sql new file mode 100644 index 0000000..a76a9df Binary files /dev/null and b/db/pamela.sql differ diff --git a/lib/macs.php b/lib/macs.php index bdebd9e..ef72247 100644 --- a/lib/macs.php +++ b/lib/macs.php @@ -16,8 +16,7 @@ function macs_get() { function macs_add($mac) { $db = get_db(); $mac = sqlite_escape_string($mac); - $q = sqlite_exec($db, "insert into macs values (\"$mac\", strftime('%s','now'))"); - if (!$q) return $results; + return sqlite_exec($db, "insert or replace into macs values (\"$mac\", strftime('%s','now'))"); } function macs_purge() { diff --git a/lib/trans.php b/lib/trans.php index 8c08404..54d66f0 100644 --- a/lib/trans.php +++ b/lib/trans.php @@ -1,6 +1,61 @@ 'appelblauwzeegroen', //192.168.42.11 appelblauwzeegroen FreeBSD 8.0 00:30:05:25:B2:F5 man-ip: 172.16.1.11 - '00:08:02:c8:56:1f' => 'gitorious', //192.168.42.44 gitorious Ubuntu 8.04 00:08:02:C8:56:1F man-ip: 172.16.1.44 - '00:0f:66:c8:ac:db' => 'openwrt' //192.168.42.13 openwrt Openwrt 00:0F:66:C8:AC:DB man-ip: 172.16.1.1 -); +require_once("lib/db.php"); + +function known_macs_get() { + $results = array(); + $db = get_db(); + $q = sqlite_query($db, "select * from knownmacs"); + if (!$q) return $results; + while(sqlite_has_more($q)) { + $row = sqlite_fetch_array($q, SQLITE_ASSOC); + $results[$row['mac']] = $row['name']; + } + return $results; +} + +function known_macs_get_by_user($userid) { + $results = array(); + $db = get_db(); + $q = sqlite_query($db, "select * from knownmacs where userid = \"userid\""); + if (!$q) return $results; + while(sqlite_has_more($q)) { + $row = sqlite_fetch_array($q, SQLITE_ASSOC); + $results[$row['mac']] = $row['name']; + } + return $results; +} + +function known_macs_upsert($mac, $name, $show) { + $db = get_db(); + $mac = sqlite_escape_string($mac); + $name = sqlite_escape_string($name); + $show = sqlite_escape_string($show); + return sqlite_exec($db, "insert into knownmacs or replace (mac, name, show) values (\"$mac\", \"$name\", \"$show\")"); +} + +function known_macs_translate($macs) { + $known_macs = known_macs_get(); + $results = array(); + foreach($macs as $mac) { + + // no translation info? return as-is + if (!array_key_exists($mac, $known_macs)) { + $results[] = $mac; + continue; + } + + // private mac? skip + if ($known_macs[$mac] == NULL) continue; + + // return translation + $results[] = $known_macs[$mac]; + + } + return $results; +} + +//$mac_translation_table=array( +// '00:30:05:25:b2:f5' => 'appelblauwzeegroen', //192.168.42.11 appelblauwzeegroen FreeBSD 8.0 00:30:05:25:B2:F5 man-ip: 172.16.1.11 +// '00:08:02:c8:56:1f' => 'gitorious', //192.168.42.44 gitorious Ubuntu 8.04 00:08:02:C8:56:1F man-ip: 172.16.1.44 +// '00:0f:66:c8:ac:db' => 'openwrt' //192.168.42.13 openwrt Openwrt 00:0F:66:C8:AC:DB man-ip: 172.16.1.1 +//); diff --git a/macs.php b/macs.php index 91f50a5..0753632 100644 --- a/macs.php +++ b/macs.php @@ -27,14 +27,15 @@ require_once("config.php"); require_once("lib/trans.php"); require_once("lib/macs.php"); -function translator($mac) { - global $mac_translation_table; - if (array_key_exists($mac, $mac_translation_table)) - return $mac_translation_table[$mac]; - return $mac; -} +//function translator($mac) { +// global $mac_translation_table; +// if (array_key_exists($mac, $mac_translation_table)) +// return $mac_translation_table[$mac]; +// return $mac; +//} $macs = macs_get(); -$macs = array_map("translator", $macs); +$macs = known_macs_translate($macs); +//$macs = array_map("translator", $macs); echo '["'.implode('", "', $macs).'"]'; macs_purge();