From b21ea0d28d86b0867f232151cf98561ef32b123f Mon Sep 17 00:00:00 2001 From: billz Date: Tue, 2 Feb 2021 12:26:14 +0000 Subject: [PATCH] Transfer template logic to includes, see #749 thx @mp035 --- ajax/networking/wifi_stations.php | 2 +- includes/about.php | 9 ++- includes/adblock.php | 20 ++++++- includes/authenticate.php | 7 ++- includes/configure_client.php | 10 +++- includes/dashboard.php | 24 +++++++- includes/internetRoute.php | 70 ++++++++++++++---------- includes/networking.php | 14 +++-- includes/system.php | 76 +++++++++++++++++++++++++- includes/wifi_functions.php | 2 +- templates/about/sponsors.php | 6 +- templates/adblock/custom.php | 8 +-- templates/adblock/logging.php | 17 +----- templates/configure_client.php | 12 +--- templates/dashboard.php | 32 +++-------- templates/networking.php | 91 +++---------------------------- templates/system/basic.php | 3 +- templates/system/language.php | 2 +- templates/themes.php | 4 +- 19 files changed, 216 insertions(+), 193 deletions(-) diff --git a/ajax/networking/wifi_stations.php b/ajax/networking/wifi_stations.php index 7d9bb82e..37d662ff 100644 --- a/ajax/networking/wifi_stations.php +++ b/ajax/networking/wifi_stations.php @@ -15,4 +15,4 @@ nearbyWifiStations($networks, !isset($_REQUEST["refresh"])); connectedWifiStations($networks); sortNetworksByRSSI($networks); -echo renderTemplate('wifi_stations', compact('networks')); +echo renderTemplate('wifi_stations', compact('networks'), true); diff --git a/includes/about.php b/includes/about.php index 67c87bee..1ddcb8c2 100755 --- a/includes/about.php +++ b/includes/about.php @@ -1,9 +1,16 @@ text($strContent); + + echo renderTemplate("about", compact('sponsorsHtml')); } + diff --git a/includes/adblock.php b/includes/adblock.php index 74f94e51..034971b7 100755 --- a/includes/adblock.php +++ b/includes/adblock.php @@ -75,13 +75,31 @@ function DisplayAdBlockConfig() $dnsmasq_state = ($dnsmasq[0] > 0); $serviceStatus = $dnsmasq_state && $enabled ? "up" : "down"; + $adblock_custom_content = file_get_contents(RASPI_ADBLOCK_LISTPATH .'custom.txt'); + + $adblock_log = ''; + exec('sudo chmod o+r /tmp/dnsmasq.log'); + $handle = fopen("/tmp/dnsmasq.log", "r"); + if ($handle) { + while (($line = fgets($handle)) !== false) { + if (preg_match('/(0.0.0.0)/', $line)) { + $adblock_log .= $line; + } + } + fclose($handle); + } else { + $adblock_log = "Unable to open log file"; + } + echo renderTemplate( "adblock", compact( "status", "serviceStatus", "dnsmasq_state", "enabled", - "custom_enabled" + "custom_enabled", + "adblock_custom_content", + "adblock_log" ) ); } diff --git a/includes/authenticate.php b/includes/authenticate.php index 4c21279e..af2eb307 100755 --- a/includes/authenticate.php +++ b/includes/authenticate.php @@ -1,6 +1,9 @@ = 0) { $strLinkQuality = 100; } else { - $strLinkQuality = 100 + $signalLevel; + $strLinkQuality = 100 + intval($signalLevel); } } @@ -177,9 +178,30 @@ function DisplayDashboard(&$extraFooterScripts) $status->addMessage(sprintf(_('Interface is %s.'), strtolower($interfaceState)), $classMsgDevicestatus); } } + // brought in from template + $arrHostapdConf = parse_ini_file(RASPI_CONFIG.'/hostapd.ini'); + $bridgedEnable = $arrHostapdConf['BridgedEnable']; + $clientInterface = $_SESSION['wifi_client_interface']; + $apInterface = $_SESSION['ap_interface']; + $MACPattern = '"([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}"'; + + if (getBridgedState()) { + $moreLink = "hostapd_conf"; + exec('iw dev ' . $apInterface . ' station dump | grep -oE ' . $MACPattern, $clients); + } else { + $moreLink = "dhcpd_conf"; + exec('cat ' . RASPI_DNSMASQ_LEASES . '| grep -E $(iw dev ' . $apInterface . ' station dump | grep -oE ' . $MACPattern . ' | paste -sd "|")', $clients); + } + $ifaceStatus = $wlan0up ? "up" : "down"; echo renderTemplate( "dashboard", compact( + "clients", + "moreLink", + "apInterface", + "clientInterface", + "ifaceStatus", + "bridgedEnable", "status", "ipv4Addrs", "ipv4Netmasks", diff --git a/includes/internetRoute.php b/includes/internetRoute.php index d51dd9e8..0143a099 100755 --- a/includes/internetRoute.php +++ b/includes/internetRoute.php @@ -1,35 +1,45 @@ $route) { - $prop=explode(' ', $route); - $rInfo[$i]["interface"]=$prop[0]; - $rInfo[$i]["ip-address"]=$prop[1]; - $rInfo[$i]["gateway"]=$prop[2]; - // resolve the name of the gateway (if possible) - unset($host); - exec('host '.$prop[2].' | sed -rn "s/.*domain name pointer (.*)\./\1/p" | head -n 1', $host); - $rInfo[$i]["gw-name"] = empty($host) ? "*" : $host[0]; - if (isset($checkAccess) && $checkAccess) { - // check internet connectivity w/ and w/o DNS resolution - unset($okip); - exec('ping -W1 -c 1 -I '.$prop[0].' '.RASPI_ACCESS_CHECK_IP.' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"',$okip); - $rInfo[$i]["access-ip"] = empty($okip) ? false : true; - unset($okdns); - exec('ping -W1 -c 1 -I '.$prop[0].' '.RASPI_ACCESS_CHECK_DNS.' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"',$okdns); - $rInfo[$i]["access-dns"] = empty($okdns) ? false : true; - } +/* + * Fetches details of the kernel routing table + * + * @param boolean $checkAccesss Perform connectivity test + * @return string + */ +function getRouteInfo($checkAccess) +{ + $rInfo = array(); + // get all default routes + exec('ip route list | sed -rn "s/default via (([0-9]{1,3}\.){3}[0-9]{1,3}).*dev (\w*).*src (([0-9]{1,3}\.){3}[0-9]{1,3}).*/\3 \4 \1/p"', $routes); + exec('ip route list | sed -rn "s/default dev (\w*) scope link/\1/p"', $devs); + if (!empty($devs)) { + foreach ($devs as $dev) + exec('ip route list | sed -rn "s/(([0-9]{1,3}\.){3}[0-9]{1,3}).*dev.*("' . $dev . '").*scope link src (([0-9]{1,3}\.){3}[0-9]{1,3}).*/\3 \4 \1/p"', $routes); } -} else { - $rInfo = array("error"=>"No route to the internet found"); + if (!empty($routes)) { + foreach ($routes as $i => $route) { + $prop = explode(' ', $route); + $rInfo[$i]["interface"] = $prop[0]; + $rInfo[$i]["ip-address"] = $prop[1]; + $rInfo[$i]["gateway"] = $prop[2]; + // resolve the name of the gateway (if possible) + unset($host); + exec('host ' . $prop[2] . ' | sed -rn "s/.*domain name pointer (.*)\./\1/p" | head -n 1', $host); + $rInfo[$i]["gw-name"] = empty($host) ? "*" : $host[0]; + if (isset($checkAccess) && $checkAccess) { + // check internet connectivity w/ and w/o DNS resolution + unset($okip); + exec('ping -W1 -c 1 -I ' . $prop[0] . ' ' . RASPI_ACCESS_CHECK_IP . ' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"', $okip); + $rInfo[$i]["access-ip"] = empty($okip) ? false : true; + unset($okdns); + exec('ping -W1 -c 1 -I ' . $prop[0] . ' ' . RASPI_ACCESS_CHECK_DNS . ' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"', $okdns); + $rInfo[$i]["access-dns"] = empty($okdns) ? false : true; + } + } + } else { + $rInfo = array("error" => "No route to the internet found"); + } + return $rInfo; } -$rInfo_json = json_encode($rInfo); ?> + diff --git a/includes/networking.php b/includes/networking.php index a4ec0e8d..ac5d39bc 100755 --- a/includes/networking.php +++ b/includes/networking.php @@ -1,6 +1,7 @@ 'Tiếng Việt (Vietnamese)' ); - echo renderTemplate("system", compact("arrLocales", "status", "serverPort", "serverBind")); + #fetch system status variables. + $system = new System(); + + $hostname = $system->hostname(); + $uptime = $system->uptime(); + $cores = $system->processorCount(); + + // mem used + $memused = $system->usedMemory(); + $memused_status = "primary"; + if ($memused > 90) { + $memused_status = "danger"; + $memused_led = "service-status-down"; + } elseif ($memused > 75) { + $memused_status = "warning"; + $memused_led = "service-status-warn"; + } elseif ($memused > 0) { + $memused_status = "success"; + $memused_led = "service-status-up"; + } + + // cpu load + $cpuload = $system->systemLoadPercentage(); + if ($cpuload > 90) { + $cpuload_status = "danger"; + } elseif ($cpuload > 75) { + $cpuload_status = "warning"; + } elseif ($cpuload >= 0) { + $cpuload_status = "success"; + } + + // cpu temp + $cputemp = $system->systemTemperature(); + if ($cputemp > 70) { + $cputemp_status = "danger"; + $cputemp_led = "service-status-down"; + } elseif ($cputemp > 50) { + $cputemp_status = "warning"; + $cputemp_led = "service-status-warn"; + } else { + $cputemp_status = "success"; + $cputemp_led = "service-status-up"; + } + + // hostapd status + $hostapd = $system->hostapdStatus(); + if ($hostapd[0] == 1) { + $hostapd_status = "active"; + $hostapd_led = "service-status-up"; + } else { + $hostapd_status = "inactive"; + $hostapd_led = "service-status-down"; + } + + echo renderTemplate("system", compact( + "arrLocales", + "status", + "serverPort", + "serverBind", + "hostname", + "uptime", + "cores", + "memused", + "memused_status", + "memused_led", + "cpuload", + "cpuload_status", + "cputemp", + "cputemp_status", + "cputemp_led", + "hostapd", + "hostapd_status", + "hostapd_led" + )); } diff --git a/includes/wifi_functions.php b/includes/wifi_functions.php index 5b994dae..8296c57c 100755 --- a/includes/wifi_functions.php +++ b/includes/wifi_functions.php @@ -9,7 +9,7 @@ function knownWifiStations(&$networks) foreach ($known_return as $line) { if (preg_match('/network\s*=/', $line)) { $network = array('visible' => false, 'configured' => true, 'connected' => false); - } elseif ($network !== null) { + } elseif (isset($network) && $network !== null) { if (preg_match('/^\s*}\s*$/', $line)) { $networks[$ssid] = $network; $network = null; diff --git a/templates/about/sponsors.php b/templates/about/sponsors.php index bddbe646..2bd7bb40 100644 --- a/templates/about/sponsors.php +++ b/templates/about/sponsors.php @@ -2,11 +2,7 @@
- text($strContent); - ?> +
diff --git a/templates/adblock/custom.php b/templates/adblock/custom.php index 8701cdb0..5a6e930c 100644 --- a/templates/adblock/custom.php +++ b/templates/adblock/custom.php @@ -21,13 +21,7 @@
- '.htmlspecialchars($adblock_custom, ENT_QUOTES).''; - ?> + '.htmlspecialchars($adblock_custom_content, ENT_QUOTES).''; ?>
diff --git a/templates/adblock/logging.php b/templates/adblock/logging.php index 66ef94fc..8378e68d 100644 --- a/templates/adblock/logging.php +++ b/templates/adblock/logging.php @@ -3,22 +3,7 @@

- '.htmlspecialchars($log, ENT_QUOTES).''; - ?> + '.htmlspecialchars($adblock_log, ENT_QUOTES).''; ?>
diff --git a/templates/configure_client.php b/templates/configure_client.php index 22302905..90b0a071 100755 --- a/templates/configure_client.php +++ b/templates/configure_client.php @@ -1,13 +1,3 @@ -
@@ -19,7 +9,7 @@ $ifaceStatus = strtolower($matchesState[1]) ? "up" : "down";
diff --git a/templates/dashboard.php b/templates/dashboard.php index e48b625d..ab1d3d8f 100755 --- a/templates/dashboard.php +++ b/templates/dashboard.php @@ -1,19 +1,3 @@ -
@@ -25,7 +9,7 @@ $ifaceStatus = $wlan0up ? "up" : "down";
@@ -38,7 +22,7 @@ $ifaceStatus = $wlan0up ? "up" : "down";

-
+
@@ -53,7 +37,7 @@ $ifaceStatus = $wlan0up ? "up" : "down";
-
+
@@ -78,7 +62,7 @@ $ifaceStatus = $wlan0up ? "up" : "down"; - + @@ -88,7 +72,7 @@ $ifaceStatus = $wlan0up ? "up" : "down"; - + @@ -126,12 +110,12 @@ $ifaceStatus = $wlan0up ? "up" : "down"; - " name="ifup_wlan0" /> + " name="ifup_wlan0" /> - " name="ifdown_wlan0" /> + " name="ifdown_wlan0" /> - + "; } else { - foreach($rInfo as $route) { + foreach($routeInfo as $route) { echo ""; echo ""; echo ""; @@ -65,6 +57,7 @@

+
@@ -76,84 +69,18 @@
+
- -
-
- - - - -
-
-
- -
- -
-

-
- - -
-

-
- - -
-
- -
- -

-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- - - - - - +
-
+
- - - + - + + diff --git a/templates/system/basic.php b/templates/system/basic.php index b5e4c124..1079c2b4 100644 --- a/templates/system/basic.php +++ b/templates/system/basic.php @@ -35,12 +35,11 @@ include('includes/sysstats.php'); - - " /> " /> +
No route to the internet found
".$route["interface"]."".$route["ip-address"]."