diff --git a/front/network.php b/front/network.php index 253b9252..86b6f41b 100644 --- a/front/network.php +++ b/front/network.php @@ -8,8 +8,13 @@ require 'php/templates/header.php'; require 'php/server/db.php'; + require 'php/server/util.php'; + + global $pia_lang; + $DBFILE = '../db/pialert.db'; + $NETWORKTYPES = getNetworkTypes(); OpenDB(); // ##################################### @@ -32,224 +37,236 @@ - +
'.$name.' / '.$type; - if ($port != "") { - echo ' ('.$port.')'; + // prepare string with port number in brackets if available + $str_port = ""; + if ($node_ports_count != "") { + $str_port = ' ('.$node_ports_count.')'; } - echo ''; + + + + $str_tab_header = '
  • + ' + .$node_name.' / '.$node_type. ' ' .$str_port. + ' +
  • '; + + echo $str_tab_header; + } - function createTabContent($mac, $name, $type, $port, $activetab) { - global $pia_lang; - echo '
    -

    '.$name.' (ID: '.str_replace(":", "_", $mac).')


    '; - echo '
    - - - - - - - '; + function createPane($node_mac, $node_name, $node_type, $node_ports_count, $activetab){ + $str_tab_pane = '
    +

    '.$node_name.' (ID: '.str_replace(":", "_", $node_mac).')

    +
    +
    '; + + + + + $str_table = ' +
    Port'.$pia_lang['Network_Table_State'].''.$pia_lang['Network_Table_Hostname'].''.$pia_lang['Network_Table_IP'].'
    + + + + + + + '; + // Prepare Array for Devices with Port value - // If no Port is set, the Port number is set to 1 - if ($port == "") {$port = 1;} - // Create Array with specific length - $network_device_portname = array(); - $network_device_portmac = array(); - $network_device_portip = array(); - $network_device_portstate = array(); - // make sql query for Network Hardware ID + // If no Port is set, the Port number is set to 0 + if ($node_ports_count == "") { + $node_ports_count = 0; + } + + // Get all leafs connected to a node based on the node_mac + $func_sql = 'SELECT dev_Network_Node_port as port, + dev_MAC as mac, + dev_PresentLastScan as online, + dev_Name as name, + dev_DeviceType as type, + dev_LastIP as last_ip + FROM "Devices" WHERE "dev_Network_Node_MAC" = "'.$node_mac.'"'; + global $db; - $func_sql = 'SELECT * FROM "Devices" WHERE "dev_Network_Node_MAC" = "'.$mac.'"'; - $func_result = $db->query($func_sql); - while($func_res = $func_result->fetchArray(SQLITE3_ASSOC)) { - // Online / Offline state of port - if ($func_res['dev_PresentLastScan'] == 1) { + $func_result = $db->query($func_sql); + + // array + $tableData = array(); + while ($row = $func_result -> fetchArray (SQLITE3_ASSOC)) { + // Push row data + $tableData[] = array( 'port' => $row['port'], + 'mac' => $row['mac'], + 'online' => $row['online'], + 'name' => $row['name'], + 'type' => $row['type'], + 'last_ip' => $row['last_ip']); + } + + // Control no rows + if (empty($tableData)) { + $tableData = []; + } + + $str_table_rows = ""; + + foreach ($tableData as $row) { + + if ($row['online'] == 1) { $port_state = '
    Online
    '; } else { $port_state = '
    Offline
    '; } - // Prepare Table with Port > push values in array - if ($port > 1) - { - if (stristr($func_res['dev_Network_Node_port'], ',') == '') { - if ($network_device_portname[$func_res['dev_Network_Node_port']] != '') { - $network_device_portname[$func_res['dev_Network_Node_port']] = $network_device_portname[$func_res['dev_Network_Node_port']].','.$func_res['dev_Name']; - } else { - $network_device_portname[$func_res['dev_Network_Node_port']] = $func_res['dev_Name']; - } - if ($network_device_portmac[$func_res['dev_Network_Node_port']] != '') { - $network_device_portmac[$func_res['dev_Network_Node_port']] = $network_device_portmac[$func_res['dev_Network_Node_port']].','.$func_res['dev_MAC']; - } else { - $network_device_portmac[$func_res['dev_Network_Node_port']] = $func_res['dev_MAC']; - } - if ($network_device_portip[$func_res['dev_Network_Node_port']] != '') { - $network_device_portip[$func_res['dev_Network_Node_port']] = $network_device_portip[$func_res['dev_Network_Node_port']].','.$func_res['dev_LastIP']; - } else { - $network_device_portip[$func_res['dev_Network_Node_port']] = $func_res['dev_LastIP']; - } - if (isset($network_device_portstate[$func_res['dev_Network_Node_port']])) { - $network_device_portstate[$func_res['dev_Network_Node_port']] = $network_device_portstate[$func_res['dev_Network_Node_port']].','.$func_res['dev_PresentLastScan']; - } else { - $network_device_portstate[$func_res['dev_Network_Node_port']] = $func_res['dev_PresentLastScan']; - } - } else { - $multiport = array(); - $multiport = explode(',',$func_res['dev_Network_Node_port']); - foreach($multiport as $row) { - $network_device_portname[trim($row)] = $func_res['dev_Name']; - $network_device_portmac[trim($row)] = $func_res['dev_MAC']; - $network_device_portip[trim($row)] = $func_res['dev_LastIP']; - $network_device_portstate[trim($row)] = $func_res['dev_PresentLastScan']; - } - unset($multiport); - } - } else { - // Table without Port > echo values - // Specific icon for devicetype - if ($type == "WLAN") {$dev_port_icon = 'fa-wifi';} - if ($type == "Powerline") {$dev_port_icon = 'fa-flash';} - echo ''; - } - } - // Create table with Port - if ($port > 1) + + // BUG: TODO fix icons - I'll need to fix the SQL query to add the type of the node on line 95 + // prepare HTML for the port table column cell + $port_content = "N/A"; + + if ($row['type'] == "WLAN" || $row['type'] == "AP" ) { + $port_content = ''; + } elseif ($row['type'] == "Powerline") { - for ($x=1; $x<=$port; $x++) - { - // Prepare online/offline badge for later functions - $online_badge = '
    Online
    '; - $offline_badge = '
    Offline
    '; - // Set online/offline badge - echo ''; - echo ''; - // Set online/offline badge - // Check if multiple badges necessary - if (stristr($network_device_portstate[$x],',') == '') { - // Set single online/offline badge - if ($network_device_portstate[$x] == 1) {$port_state = $online_badge;} else {$port_state = $offline_badge;} - echo ''; - } else { - // Set multiple online/offline badges - $multistate = array(); - $multistate = explode(',',$network_device_portstate[$x]); - echo ''; - unset($multistate); - } - // Check if multiple Hostnames are set - // print single hostname - if (stristr($network_device_portmac[$x],',') == '') { - echo ''; - } else { - // print multiple hostnames with separate links - $multimac = array(); - $multimac = explode(',',$network_device_portmac[$x]); - $multiname = array(); - $multiname = explode(',',$network_device_portname[$x]); - echo ''; - unset($multiname, $multimac); - } - // Check if multiple IP are set - // print single IP - if (stristr($network_device_portip[$x],',') == '') { - echo ''; - } else { - // print multiple IPs - $multiip = array(); - $multiip = explode(',',$network_device_portip[$x]); - echo ''; - unset($multiip); - } - echo ''; - } + $port_content = ''; + } elseif ($row['port'] != NULL && $row['port'] != "") + { + $port_content = $row['port']; + } + + $str_table_rows = $str_table_rows. + ' + + + + + '; + + } + + $str_table_close = ' +
    Port'.$pia_lang['Network_Table_State'].''.$pia_lang['Network_Table_Hostname'].''.$pia_lang['Network_Table_IP'].'
    '.$port_state.''.$func_res['dev_Name'].''.$func_res['dev_LastIP'].'
    '.$x.''.$port_state.''; - foreach($multistate as $key => $value) { - if ($value == 1) {$port_state = $online_badge;} else {$port_state = $offline_badge;} - echo $port_state.'
    '; - } - echo '
    '.$network_device_portname[$x].''; - foreach($multiname as $key => $value) { - echo ''.$value.'
    '; - } - echo '
    '.$network_device_portip[$x].''; - foreach($multiip as $key => $value) { - echo $value.'
    '; - } - echo '
    + '.$port_content.' + ' + .$port_state. + ' + + '.$row['name'].' + + ' + .$row['last_ip']. + '
    '; + + // no connected device - don't render table + if($str_table_rows == "") + { + $str_table = ""; + $str_table_close = ""; } - echo ' - -
    '; - echo '
    '; - } + + $str_close_pane = ' + +
    '; + + // write the HTML + echo ''.$str_tab_header. + $str_tab_pane. + $str_table. + $str_table_rows. + $str_table_close. + $str_close_pane; + } - // Create Tabs + // Create Top level tabs (List of network devices), explanation of the terminology below: + // + // Switch 1 (node) + // /(p1) \ (p2) <----- port numbers + // / \ + // Smart TV (leaf) Switch 2 (node (for the PC) and leaf (for Switch 1)) + // \ + // PC (leaf) + + $sql = "SELECT node_name, node_mac, node_type, node_ports_count + FROM + ( + SELECT a.dev_Name as node_name, + a.dev_MAC as node_mac, + a.dev_DeviceType as node_type + FROM Devices a + WHERE a.dev_DeviceType in ('AP', 'Gateway', 'Powerline', 'Switch', 'WLAN', 'PLC', 'Router','USB LAN Adapter', 'USB WIFI Adapter', 'Internet') + ) t1 + LEFT JOIN + ( + SELECT b.dev_Network_Node_MAC as node_mac_2, + count() as node_ports_count + FROM Devices b + WHERE b.dev_Network_Node_MAC NOT NULL group by b.dev_Network_Node_MAC + ) t2 + ON (t1.node_mac = t2.node_mac_2); + "; - $sql = 'select dev_MAC, dev_Name, dev_DeviceType, dev_Network_Node_port from Devices where dev_DeviceType in ("AP", "Gateway", "Powerline", "Switch", "WLAN", "PLC", "Router","USB LAN Adapter", "USB WIFI Adapter")'; - $result = $db->query($sql); + $result = $db->query($sql); - // array - $tableData = array(); - while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { - // Push row data - $tableData[] = array('dev_MAC' => $row['dev_MAC'], - 'dev_Name' => $row['dev_Name'], - 'dev_DeviceType' => $row['dev_DeviceType'], - 'dev_Network_Node_port' => $row['dev_Network_Node_port'] ); - } + // array + $tableData = array(); + while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { + // Push row data + $tableData[] = array( 'node_mac' => $row['node_mac'], + 'node_name' => $row['node_name'], + 'node_type' => $row['node_type'], + 'node_ports_count' => $row['node_ports_count']); + } - // Control no rows - if (empty($tableData)) { - $tableData = []; - } + // Control no rows + if (empty($tableData)) { + $tableData = []; + } + echo '