Skip to content

Commit

Permalink
RaspAP#602: Adds rest endpoints for adblock config and wifistations
Browse files Browse the repository at this point in the history
  • Loading branch information
fjhunter committed Jun 9, 2020
1 parent 1a59c06 commit ce1ef90
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 65 deletions.
13 changes: 1 addition & 12 deletions ajax/networking/wifi_stations.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
<?php

require '../../includes/csrf.php';
require_once '../../includes/config.php';
require_once '../../includes/defaults.php';
require_once '../../includes/functions.php';
require_once '../../includes/wifi_functions.php';

$networks = [];
$network = null;
$ssid = null;

knownWifiStations($networks);
nearbyWifiStations($networks, !isset($_REQUEST["refresh"]));
connectedWifiStations($networks);
sortNetworksByRSSI($networks);
$networks = getWifiStations();

echo renderTemplate('wifi_stations', compact('networks'));
55 changes: 3 additions & 52 deletions includes/adblock.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,11 @@
<?php

require_once 'includes/status_messages.php';
require_once 'config.php';
require_once 'functions.php';

/**
* Manages ad blocking (dnsmasq) configuration
*
*/
function DisplayAdBlockConfig()
{
$status = new StatusMessages();
$enabled = false;

if (!RASPI_MONITOR_ENABLED) {
if (isset($_POST['saveadblocksettings'])) {
if ($_POST['adblock-enable'] == "1") {
$config = 'conf-file=' .RASPI_ADBLOCK_LISTPATH .'domains.txt'.PHP_EOL;
$config.= 'addn-hosts=' .RASPI_ADBLOCK_LISTPATH .'hostnames.txt'.PHP_EOL;
} elseif ($_POST['adblock-enable'] == "0") {
$config = null;
}
file_put_contents("/tmp/dnsmasqdata", $config);
system('sudo cp /tmp/dnsmasqdata '.RASPI_ADBLOCK_CONFIG, $return);

if ($return == 0) {
$status->addMessage('Adblock configuration updated successfully', 'success');
} else {
$status->addMessage('Adblock configuration failed to be updated.', 'danger');
}
} elseif (isset($_POST['restartadblock']) || isset($_POST['startadblock'])) {
exec('sudo /bin/systemctl restart dnsmasq.service', $dnsmasq, $return);
if ($return == 0) {
$status->addMessage('Adblock restart successful', 'success');
} else {
$status->addMessage('Adblock failed to restart.', 'danger');
}
}
}

exec('cat '. RASPI_ADBLOCK_CONFIG, $return);
$arrConf = ParseConfig($return);
if (sizeof($arrConf) > 0) {
$enabled = true;
}

exec('pidof dnsmasq | wc -l', $dnsmasq);
$dnsmasq_state = ($dnsmasq[0] > 0);
$serviceStatus = $dnsmasq_state && $enabled ? "up" : "down";

echo renderTemplate(
"adblock", compact(
"status",
"serviceStatus",
"dnsmasq_state",
"enabled"
)
);
{ $config = getAdBlockConfig();
echo renderTemplate("adblock", $config);
}

51 changes: 51 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/* Functions for Networking */
require 'status_messages.php'; //for getAdblockConfig

function mask2cidr($mask)
{
Expand Down Expand Up @@ -417,3 +418,53 @@ function formatDateAgo($datetime, $full = false)
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}

function getAdBlockConfig()
{
$status = new StatusMessages();
$enabled = false;

if (!RASPI_MONITOR_ENABLED) {
if (isset($_POST['saveadblocksettings'])) {
if ($_POST['adblock-enable'] == "1") {
$config = 'conf-file=' .RASPI_ADBLOCK_LISTPATH .'domains.txt'.PHP_EOL;
$config.= 'addn-hosts=' .RASPI_ADBLOCK_LISTPATH .'hostnames.txt'.PHP_EOL;
} elseif ($_POST['adblock-enable'] == "0") {
$config = null;
}
file_put_contents("/tmp/dnsmasqdata", $config);
system('sudo cp /tmp/dnsmasqdata '.RASPI_ADBLOCK_CONFIG, $return);

if ($return == 0) {
$status->addMessage('Adblock configuration updated successfully', 'success');
} else {
$status->addMessage('Adblock configuration failed to be updated.', 'danger');
}
} elseif (isset($_POST['restartadblock']) || isset($_POST['startadblock'])) {
exec('sudo /bin/systemctl restart dnsmasq.service', $dnsmasq, $return);
if ($return == 0) {
$status->addMessage('Adblock restart successful', 'success');
} else {
$status->addMessage('Adblock failed to restart.', 'danger');
}
}
}

exec('cat '. RASPI_ADBLOCK_CONFIG, $return);
$arrConf = ParseConfig($return);
if (sizeof($arrConf) > 0) {
$enabled = true;
}

exec('pidof dnsmasq | wc -l', $dnsmasq);
$dnsmasq_state = ($dnsmasq[0] > 0);
$serviceStatus = $dnsmasq_state && $enabled ? "up" : "down";

return compact(
"status",
"serviceStatus",
"dnsmasq_state",
"enabled"
);
}

16 changes: 15 additions & 1 deletion includes/wifi_functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once 'functions.php';
require_once 'defaults.php';

function knownWifiStations(&$networks)
{
Expand Down Expand Up @@ -77,7 +78,7 @@ function () {

$ssid = trim($arrNetwork[4]);
// filter SSID string: anything invisible in 7bit ASCII or quotes -> ignore network
if (preg_match('[\x00-\x1f\x7f-\xff\'\`\´\"]', $ssid)) {
if (preg_match('[\x00-\x1f\x7f-\xff\'\`\\"]', $ssid)) {
continue;
}

Expand Down Expand Up @@ -133,3 +134,16 @@ function sortNetworksByRSSI(&$networks)
$networks[$SSID]['RSSI'] = $RSSI;
}
}

function getWifiStations() {
$networks = [];
$network = null;
$ssid = null;

knownWifiStations($networks);
nearbyWifiStations($networks, !isset($_REQUEST["refresh"]));
connectedWifiStations($networks);
sortNetworksByRSSI($networks);

return $networks;
}
4 changes: 4 additions & 0 deletions rest/get_adblock_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
require_once '../includes/functions.php';

echo json_encode(getAdBlockConfig());
5 changes: 5 additions & 0 deletions rest/get_wifi_stations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require_once '../includes/wifi_functions.php';

echo json_encode(getWifiStations());

0 comments on commit ce1ef90

Please sign in to comment.