From 7d0161f6e229b2b2bd6d02e299108ebf2440a6f8 Mon Sep 17 00:00:00 2001 From: sandb Date: Sun, 28 Feb 2010 19:15:10 +0100 Subject: [PATCH] sqlite backend for macs and known_macs --- db/pamela.sql | Bin 0 -> 8192 bytes lib/macs.php | 3 +-- lib/trans.php | 65 ++++++++++++++++++++++++++++++++++++++++++++++---- macs.php | 15 ++++++------ 4 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 db/pamela.sql diff --git a/db/pamela.sql b/db/pamela.sql new file mode 100644 index 0000000000000000000000000000000000000000..a76a9df196255ae7ff4a508d926af31efcb27630 GIT binary patch literal 8192 zcmeI0J#W)M7{{MBR>+V621++hs8nebRrbX`+s|iU0Kvd|K3}NS#Ay>peUXql!p6wV zS3p910EV(NAvP91LPze-zEoAarYh)8j%A;J_q_a_dvv~jpS;>D=44ciB@xrI;zc

7jMKv||B-jDn14BJXH4iq`IoIRX+T8~m zo4KkVV_8Fn3_;8!SLMY#AjY1yN-B9=kwsY?E+mDzLB#X?XsyuzV_+0yIZ@y-^99KPLE3C;7Y*DZit%kI`>mOzSZt-kSC{g%$MBd z6}uY!@&V~B=5ki#HnM?E%%n6RC07ya>;~juR!sQpgzU=`qLUtwdf1N6cr@*Z$(1FDG^URr; z$=-TMu2+G8vUr&INyy;b)q-1nN6wj3V08*~;KI%4)yeBDI|c5v0&f1_>DS|ea|*0b zfe!pp`+Ry2O@9aI^?!@k>wn5HGg|;O!!!rA{%2{FFts62^Lv&*Z1@N>fhorzLcuMc z_?G`@`L_)pAj?n)j2T9u4yUt%&{(a2{0%?Sak1<6`8$R{b z_ls^G^iQ4vvZs>PiP64OD|E<7Zw_xS< literal 0 HcmV?d00001 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();